mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-15 06:53:47 +00:00
Navigation and shooting modules
This commit is contained in:
parent
ef6c240e8e
commit
5a6d31e2f0
8 changed files with 229 additions and 11 deletions
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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue