mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-05 16:55:54 +00:00
Navigation and shooting modules
This commit is contained in:
parent
ef6c240e8e
commit
5a6d31e2f0
8 changed files with 229 additions and 11 deletions
69
Scripts/Components/FSM/Enemy/NavigationMovementModule.cs
Normal file
69
Scripts/Components/FSM/Enemy/NavigationMovementModule.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy;
|
||||
|
||||
public partial class NavigationMovementModule : Node2D
|
||||
{
|
||||
|
||||
private Vector2? _lastTargetPosition;
|
||||
private CharacterBody2D _characterBody;
|
||||
private NavigationAgent2D _navigationAgent;
|
||||
|
||||
[Export]
|
||||
public EnemyStorageModule StorageModule { get; private set; }
|
||||
|
||||
public void Init(CharacterBody2D characterBody)
|
||||
{
|
||||
_characterBody = characterBody;
|
||||
|
||||
if (_navigationAgent is not null) return;
|
||||
_navigationAgent = this.GetNode<NavigationAgent2D>("NavigationAgent2D");
|
||||
}
|
||||
|
||||
public void SetTarget(Vector2? target)
|
||||
{
|
||||
_lastTargetPosition = target;
|
||||
}
|
||||
|
||||
public void Move()
|
||||
{
|
||||
if (!_lastTargetPosition.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_navigationAgent.SetTargetPosition(_lastTargetPosition.Value);
|
||||
|
||||
var currentAgentPosition = _characterBody.GlobalPosition;
|
||||
|
||||
if (_navigationAgent.IsNavigationFinished())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var nextPathPosition = _navigationAgent.GetNextPathPosition();
|
||||
|
||||
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * StorageModule.MovementSpeed;
|
||||
|
||||
if (_navigationAgent.AvoidanceEnabled)
|
||||
{
|
||||
_navigationAgent.SetVelocity(newVelocity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_on_navigation_agent_2d_velocity_computed(newVelocity);
|
||||
}
|
||||
|
||||
_characterBody.MoveAndSlide();
|
||||
}
|
||||
|
||||
public void _on_navigation_agent_2d_velocity_computed(Vector2 safeVelocity)
|
||||
{
|
||||
if (_characterBody is null) return;
|
||||
_characterBody.Velocity = safeVelocity;
|
||||
if (_characterBody.Velocity.Length() > 0)
|
||||
{
|
||||
StorageModule.FacingDirection = _characterBody.Velocity.Normalized();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue