mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 23:45:55 +00:00
Navigation and shooting modules
This commit is contained in:
parent
ef6c240e8e
commit
5a6d31e2f0
8 changed files with 229 additions and 11 deletions
|
|
@ -17,12 +17,16 @@ public partial class Alert : EnemyStateBase
|
|||
[Export]
|
||||
public GenericDamageReceiver DamageReceiver { get; private set; }
|
||||
|
||||
[Export]
|
||||
public NavigationMovementModule NavigationModule { get; private set; }
|
||||
|
||||
private bool _isPlayerInRange = false;
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
GD.Print("Entered Idle");
|
||||
GD.Print($"Entered {Name}");
|
||||
NavigationModule.Init(MainObject);
|
||||
PlayerDetection.SetRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
||||
|
||||
_isPlayerInRange = PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
||||
|
|
@ -52,14 +56,14 @@ public partial class Alert : EnemyStateBase
|
|||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
GD.Print("Exited Idle");
|
||||
GD.Print($"Exited {Name}");
|
||||
PlayerDetection.PlayerInRange -= PlayerDetectionOnPlayerInRange;
|
||||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
|
||||
DamageReceiver.ChangeState(false);
|
||||
|
||||
NavigationModule.SetTarget(null);
|
||||
}
|
||||
|
||||
private void PlayerDetectionOnPlayerInRange()
|
||||
|
|
@ -73,6 +77,7 @@ public partial class Alert : EnemyStateBase
|
|||
base.PhysicsProcessState(delta);
|
||||
if (_isPlayerInRange && PlayerDetection.IsPlayerInSight())
|
||||
{
|
||||
GD.Print("Player is in sight, shooting");
|
||||
StateMachine.SetState(EnemyState.Shooting);
|
||||
return;
|
||||
}
|
||||
|
|
@ -81,7 +86,9 @@ public partial class Alert : EnemyStateBase
|
|||
if (this.GlobalPosition.DistanceTo(GameManager.Instance.PlayerPosition.Value) >=
|
||||
StorageModule.Root.EnemyResource.PlayerDisengageRange)
|
||||
{
|
||||
GD.Print("Player is out of sight, idling");
|
||||
StateMachine.SetState(EnemyState.Idle);
|
||||
return;
|
||||
}
|
||||
|
||||
// Move towards last known position
|
||||
|
|
@ -90,12 +97,12 @@ public partial class Alert : EnemyStateBase
|
|||
MoveTowardsPosition(PlayerDetection.LastKnownPlayerPosition.Value);
|
||||
}
|
||||
|
||||
|
||||
NavigationModule.Move();
|
||||
}
|
||||
|
||||
private void MoveTowardsPosition(Vector2 position)
|
||||
{
|
||||
|
||||
NavigationModule.SetTarget(position);
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
|
|
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://ik7s65de723k
|
||||
|
|
@ -35,7 +35,9 @@ public partial class PlayerDetectionModule : Area2D
|
|||
|
||||
public bool IsPlayerInRange(float range)
|
||||
{
|
||||
if (!GameManager.Instance?.PlayerPosition.HasValue ?? false)
|
||||
if (GameManager.Instance is null) return false;
|
||||
|
||||
if (!GameManager.Instance.PlayerPosition.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -45,8 +47,9 @@ public partial class PlayerDetectionModule : Area2D
|
|||
|
||||
public bool IsPlayerInSight()
|
||||
{
|
||||
if (GameManager.Instance is null) return false;
|
||||
//if (_cachedPlayer == null) return false;
|
||||
if (!GameManager.Instance?.PlayerPosition.HasValue ?? false) return false;
|
||||
if (!GameManager.Instance.PlayerPosition.HasValue) return false;
|
||||
|
||||
var spaceState = GetWorld2D().DirectSpaceState;
|
||||
|
||||
|
|
|
|||
104
Scripts/Components/FSM/Enemy/Shooting.cs
Normal file
104
Scripts/Components/FSM/Enemy/Shooting.cs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy;
|
||||
|
||||
public partial class Shooting : EnemyStateBase
|
||||
{
|
||||
public override EnemyState StateId => EnemyState.Shooting;
|
||||
|
||||
[Export]
|
||||
public EnemyStorageModule StorageModule { get; private set; }
|
||||
|
||||
[Export]
|
||||
public PlayerDetectionModule PlayerDetection { get; private set; }
|
||||
|
||||
[Export]
|
||||
public GenericDamageReceiver DamageReceiver { get; private set; }
|
||||
|
||||
[Export] public Weapon EquippedWeapon;
|
||||
|
||||
private bool _isPlayerInRange = false;
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
GD.Print($"Entered {Name}");
|
||||
|
||||
_isPlayerInRange = PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
||||
|
||||
PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
DamageReceiver.ChangeState(true);
|
||||
|
||||
DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
||||
|
||||
// PlayerDetection.SetRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
||||
//
|
||||
// _isPlayerInRange = PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
||||
//
|
||||
// PlayerDetection.PlayerInRange += PlayerDetectionOnPlayerInRange;
|
||||
//
|
||||
// PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
|
||||
//
|
||||
// DamageReceiver.ChangeState(true);
|
||||
//
|
||||
// DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
||||
|
||||
}
|
||||
|
||||
private void HealthProviderOnResourceDepleted()
|
||||
{
|
||||
ChangeState(EnemyState.Dead);
|
||||
}
|
||||
|
||||
private void PlayerDetectionOnPlayerOutOfRange()
|
||||
{
|
||||
_isPlayerInRange = false;
|
||||
StateMachine.SetState(EnemyState.Alert);
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
GD.Print($"Exited {Name}");
|
||||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
|
||||
DamageReceiver.ChangeState(false);
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
base.PhysicsProcessState(delta);
|
||||
|
||||
if (_isPlayerInRange && PlayerDetection.IsPlayerInSight())
|
||||
{
|
||||
// SHOOT
|
||||
Shoot();
|
||||
}
|
||||
else
|
||||
{
|
||||
StateMachine.SetState(EnemyState.Alert);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
{
|
||||
if (EquippedWeapon == null) return;
|
||||
if (!PlayerDetection.LastKnownPlayerPosition.HasValue) return;
|
||||
|
||||
|
||||
var direction = ( PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).Normalized();
|
||||
|
||||
// Shoot at the player's last known position
|
||||
EquippedWeapon.ShootDirection = direction;
|
||||
StorageModule.FacingDirection = direction;
|
||||
|
||||
EquippedWeapon.Shoot();
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Enemy/Shooting.cs.uid
Normal file
1
Scripts/Components/FSM/Enemy/Shooting.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://7mig30eneu8x
|
||||
Loading…
Add table
Add a link
Reference in a new issue