mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Working rotating turrets
This commit is contained in:
parent
8889a2cdac
commit
1e5075eeb2
12 changed files with 278 additions and 66 deletions
|
|
@ -25,7 +25,6 @@ public partial class Alert : EnemyStateBase
|
|||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
GD.Print($"Entered {Name}");
|
||||
NavigationModule.Init(MainObject);
|
||||
|
||||
PlayerDetection.SetRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
||||
|
|
@ -50,13 +49,11 @@ public partial class Alert : EnemyStateBase
|
|||
private void PlayerDetectionOnPlayerOutOfRange()
|
||||
{
|
||||
_isPlayerInRange = false;
|
||||
GD.Print("Player out of range");
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
GD.Print($"Exited {Name}");
|
||||
PlayerDetection.PlayerInRange -= PlayerDetectionOnPlayerInRange;
|
||||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
|
|
@ -69,7 +66,6 @@ public partial class Alert : EnemyStateBase
|
|||
private void PlayerDetectionOnPlayerInRange()
|
||||
{
|
||||
//_isPlayerInRange = true;
|
||||
GD.Print("Player In Range");
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
|
|
@ -77,7 +73,6 @@ public partial class Alert : EnemyStateBase
|
|||
base.PhysicsProcessState(delta);
|
||||
if (PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.ViewRange) && PlayerDetection.IsPlayerInSight())
|
||||
{
|
||||
GD.Print("Player is in sight, shooting");
|
||||
StateMachine.SetState(EnemyState.Shooting);
|
||||
return;
|
||||
}
|
||||
|
|
@ -86,7 +81,6 @@ 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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -172,8 +172,6 @@ public partial class Shooting : EnemyStateBase
|
|||
if (!PlayerDetection.LastKnownPlayerPosition.HasValue) return;
|
||||
|
||||
var direction = GetShootDirection();
|
||||
|
||||
|
||||
|
||||
// Shoot at the player's last known position
|
||||
EquippedWeapon.ShootDirection = direction;
|
||||
|
|
|
|||
97
Scripts/Components/FSM/Enemy/Turret/TurretAlert.cs
Normal file
97
Scripts/Components/FSM/Enemy/Turret/TurretAlert.cs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy.Turret;
|
||||
|
||||
public partial class TurretAlert : EnemyStateBase
|
||||
{
|
||||
public override EnemyState StateId => EnemyState.Alert;
|
||||
|
||||
[Export]
|
||||
public EnemyStorageModule StorageModule { get; private set; }
|
||||
|
||||
[Export]
|
||||
public PlayerDetectionModule PlayerDetection { get; private set; }
|
||||
|
||||
[Export]
|
||||
public GenericDamageReceiver DamageReceiver { get; private set; }
|
||||
|
||||
|
||||
private bool _isPlayerInRange = false;
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
|
||||
PlayerDetection.SetRange(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;
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
PlayerDetection.PlayerInRange -= PlayerDetectionOnPlayerInRange;
|
||||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
|
||||
DamageReceiver.ChangeState(false);
|
||||
}
|
||||
|
||||
private void PlayerDetectionOnPlayerInRange()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
base.PhysicsProcessState(delta);
|
||||
if (PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.ViewRange) && PlayerDetection.IsPlayerInSight())
|
||||
{
|
||||
|
||||
StateMachine.SetState(EnemyState.Shooting);
|
||||
return;
|
||||
}
|
||||
|
||||
// if player is outside disengage range, change to idle (later on, search)
|
||||
if (this.GlobalPosition.DistanceTo(GameManager.Instance.PlayerPosition.Value) >=
|
||||
StorageModule.Root.EnemyResource.PlayerDisengageRange)
|
||||
{
|
||||
|
||||
StateMachine.SetState(EnemyState.Idle);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set facing direction towards player
|
||||
if (PlayerDetection.LastKnownPlayerPosition.HasValue)
|
||||
{
|
||||
StorageModule.FacingDirection =
|
||||
(PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).Normalized(); //MainObject.Velocity.SnapToCardinal().Normalized();
|
||||
StorageModule.AimingDirection = StorageModule.FacingDirection;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
base.ProcessState(delta);
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Enemy/Turret/TurretAlert.cs.uid
Normal file
1
Scripts/Components/FSM/Enemy/Turret/TurretAlert.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dn05b1fbc6ip5
|
||||
110
Scripts/Components/FSM/Enemy/Turret/TurretShooting.cs
Normal file
110
Scripts/Components/FSM/Enemy/Turret/TurretShooting.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy.Turret;
|
||||
|
||||
public partial class TurretShooting : 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;
|
||||
|
||||
private double _responseTimer = 0;
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
|
||||
PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
DamageReceiver.ChangeState(true);
|
||||
|
||||
DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
||||
|
||||
EquippedWeapon.WeaponData = StorageModule.Root.EnemyResource.Weapon;
|
||||
|
||||
_responseTimer = 0;
|
||||
}
|
||||
|
||||
private void HealthProviderOnResourceDepleted()
|
||||
{
|
||||
ChangeState(EnemyState.Dead);
|
||||
}
|
||||
|
||||
private void PlayerDetectionOnPlayerOutOfRange()
|
||||
{
|
||||
StateMachine.SetState(EnemyState.Alert);
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
|
||||
|
||||
DamageReceiver.ChangeState(false);
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
base.PhysicsProcessState(delta);
|
||||
|
||||
if (PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.ViewRange) && PlayerDetection.IsPlayerInSight())
|
||||
{
|
||||
// SHOOT
|
||||
Shoot();
|
||||
}
|
||||
else
|
||||
{
|
||||
StateMachine.SetState(EnemyState.Alert);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Vector2 GetShootDirection()
|
||||
{
|
||||
if (StorageModule.EnemyData.PredictPlayer && PlayerDetection.LastKnowPlayerVelocity.HasValue)
|
||||
{
|
||||
var predictedDirection = MathFunctions.PredictInterceptPosition(MainObject.GlobalPosition,
|
||||
PlayerDetection.LastKnownPlayerPosition.Value, PlayerDetection.LastKnowPlayerVelocity.Value,
|
||||
EquippedWeapon.WeaponData.BulletData.BulletSpeed);
|
||||
if (predictedDirection.HasValue) return (predictedDirection.Value - MainObject.GlobalPosition).Normalized();
|
||||
|
||||
}
|
||||
|
||||
return ( PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).Normalized();
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
{
|
||||
if (EquippedWeapon == null) return;
|
||||
if (!PlayerDetection.LastKnownPlayerPosition.HasValue) return;
|
||||
|
||||
var direction = GetShootDirection();
|
||||
|
||||
// Shoot at the player's last known position
|
||||
EquippedWeapon.ShootDirection = direction;
|
||||
//StorageModule.FacingDirection = direction;
|
||||
StorageModule.FacingDirection = direction;
|
||||
StorageModule.AimingDirection = direction;
|
||||
|
||||
EquippedWeapon.Shoot();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://biwpcnt2rhw23
|
||||
|
|
@ -8,24 +8,24 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
|
|||
{
|
||||
private IStateMachine<EnemyState, CharacterBody2D> _machine;
|
||||
[Export] public PlayerAnimationProvider AnimationProvider { get; set; }
|
||||
|
||||
|
||||
[Export] public EnemyStorageModule StorageModule { get; set; }
|
||||
[Export] public ActorResourceProvider HealthProvider { get; set; }
|
||||
|
||||
|
||||
|
||||
[Export] public Vector2 TurretFacingDirection { get; set; } = Vector2.Down;
|
||||
[Export] public float SweepAngle = 90f; // In degrees
|
||||
[Export] public float SweepSpeed = 1f; // Speed of sweeping
|
||||
[Export] public bool Debug = false; // Enable debug lines
|
||||
[Export] public float SweepSpeed = 1f; // Speed of sweeping
|
||||
[Export] public bool Debug = false; // Enable debug lines
|
||||
//[Export] public NodePath SpritePath;
|
||||
|
||||
|
||||
private float _currentAngle;
|
||||
private float _sweepDirection = 1f;
|
||||
private float _raycastLength;
|
||||
|
||||
|
||||
public override void EnterState(EnemyState state)
|
||||
{
|
||||
AnimationProvider.SetAnimation(StorageModule.AimingDirection);
|
||||
AnimationProvider.SetAnimation(Vector2.Zero);
|
||||
//AnimationProvider.SetAnimation(StorageModule.AimingDirection);
|
||||
//AnimationProvider.SetAnimation(Vector2.Zero);
|
||||
if (HealthProvider is not null)
|
||||
{
|
||||
HealthProvider.ResourceDecreased += HealthProviderOnResourceDecreased;
|
||||
|
|
@ -39,7 +39,7 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
|
|||
|
||||
public override void ExitState(EnemyState state)
|
||||
{
|
||||
AnimationProvider.SetAnimation(Vector2.Zero);
|
||||
//AnimationProvider.SetAnimation(Vector2.Zero);
|
||||
if (HealthProvider is not null)
|
||||
{
|
||||
HealthProvider.ResourceDecreased -= HealthProviderOnResourceDecreased;
|
||||
|
|
@ -53,17 +53,27 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
|
|||
|
||||
public override void Process(double delta)
|
||||
{
|
||||
AnimationProvider.SetAnimation(StorageModule.AimingDirection);
|
||||
if (StorageModule.AimingDirection == Vector2.Zero || TurretFacingDirection == Vector2.Zero)
|
||||
return;
|
||||
|
||||
if (_machine.MainObject.Velocity == Vector2.Zero)
|
||||
{
|
||||
AnimationProvider.SetAnimation(Vector2.Zero);
|
||||
}
|
||||
// Calculate the angle between the turret's facing direction and the aim direction
|
||||
float angleToTarget = TurretFacingDirection.AngleTo(StorageModule.AimingDirection); // radians
|
||||
|
||||
// Clamp the angle to within the sweep cone
|
||||
float halfSweepRad = Mathf.DegToRad(SweepAngle / 2f);
|
||||
float clampedAngle = Mathf.Clamp(angleToTarget, -halfSweepRad, halfSweepRad);
|
||||
|
||||
// Convert to degrees before passing to SweepSprite
|
||||
float clampedAngleDeg = Mathf.RadToDeg(clampedAngle);
|
||||
// Update the sprite based on this clamped angle
|
||||
AnimationProvider.SweepSprite(clampedAngleDeg, SweepAngle);
|
||||
|
||||
|
||||
//AnimationProvider.SetAnimation(StorageModule.AimingDirection);
|
||||
}
|
||||
|
||||
public override void PhysicsProcess(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Sweep(float delta)
|
||||
|
|
@ -78,7 +88,7 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
|
|||
_currentAngle = Mathf.Clamp(_currentAngle, -halfAngle, halfAngle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void DrawDebugLine(Vector2 endPoint)
|
||||
{
|
||||
// Request the node to redraw
|
||||
|
|
@ -90,7 +100,7 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
|
|||
{
|
||||
AnimationProvider.SweepSprite(_currentAngle, SweepAngle);
|
||||
}
|
||||
|
||||
|
||||
private Vector2 _debugLineEndPoint;
|
||||
|
||||
public override void _Draw()
|
||||
|
|
@ -100,5 +110,4 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
|
|||
DrawLine(Vector2.Zero, ToLocal(_debugLineEndPoint), Colors.Red, 2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue