Ability for enemies to shoot when controlled

This commit is contained in:
Marco 2025-02-25 17:29:24 +01:00
commit 2d6bcc5f00
10 changed files with 93 additions and 34 deletions

View file

@ -17,6 +17,8 @@ public partial class ActorFreeMovement : MovementHandler
set => _parent.MovementDirection = value;
}
[Export] public string StrafeAction { get; private set; } = "strafe";
public bool IsDestroyed => _parent.IsDestroyed;
public override void Init(Actor parent)
@ -39,7 +41,7 @@ public partial class ActorFreeMovement : MovementHandler
MovementDirection = AggregateInputProviders().Normalized();
var aimingDirection = GetAimingDirection().Normalized();
var isStrafing = GetStrafing();
var isStrafing = GetActionPressed(StrafeAction);
if (!isStrafing && aimingDirection.Length() > 0.1f)
{

View file

@ -12,7 +12,7 @@ public partial class DamageReceiverActorModule : ActorModule
[Export]
public bool Invulnerable { get; private set; } = false;
[Export] protected BulletOwner BulletGroup { get; set; } = BulletOwner.None;
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.None;
public override void Init(Actor actor)
{

View file

@ -1,3 +1,4 @@
using Cirno.Scripts.Components.Actors;
using Godot;
public partial class EnemyPossessionMovement : ActorFreeMovement
@ -11,6 +12,14 @@ public partial class EnemyPossessionMovement : ActorFreeMovement
[Export]
public AnimatedSprite2D PossessionSprite { get; private set; }
[Export] public string ControlEndAction { get; private set; } = "pause";
[Export] public string ShootAction { get; private set; } = "shoot";
[Export] public DamageReceiverActorModule DamageReceiver { get; private set; }
[Export] public Weapon EquippedWeapon;
public override void Init(Actor parent)
{
base.Init(parent);
@ -19,11 +28,26 @@ public partial class EnemyPossessionMovement : ActorFreeMovement
_parent.OnControlAssumed += AssumeControl;
_previousAiState = _actorAi.Ai;
parent.OnDeath += () =>
{
if (_actorAi.Ai is AiState.Controlled)
{
ResumeControl();
}
};
}
public override void Update(double deltaTime)
{
if (_actorAi.Ai is AiState.Controlled && Input.IsActionJustPressed("pause"))
if (_actorAi.Ai is not AiState.Controlled) return;
if (GetActionPressed(ShootAction))
{
Shoot();
}
if (GetActionJustPressed(ControlEndAction))
{
if (GameManager.Instance.ToggleControlMode() is GameState.Playing)
{
@ -31,6 +55,17 @@ public partial class EnemyPossessionMovement : ActorFreeMovement
}
}
}
private void Shoot()
{
if (EquippedWeapon == null) return;
var direction = FacingDirection.Normalized();
EquippedWeapon.ShootDirection = direction;
EquippedWeapon.Shoot(BulletOwner.Player);
}
public override void PhysicsUpdate(double delta)
{
@ -47,6 +82,8 @@ public partial class EnemyPossessionMovement : ActorFreeMovement
_previousAiState = _actorAi.Ai;
_actorAi.Ai = AiState.Controlled;
DamageReceiver.BulletGroup = BulletOwner.Player;
PossessionSprite?.Show();
}
@ -57,6 +94,8 @@ public partial class EnemyPossessionMovement : ActorFreeMovement
GameManager.Instance.CameraTargetPlayer();
GameManager.Instance.Player.RequestMovementDisable(false);
DamageReceiver.BulletGroup = BulletOwner.Enemy;
PossessionSprite?.Hide();
}

View file

@ -7,5 +7,6 @@ public abstract partial class InputProvider : Node2D
public abstract Vector2 GetMovementInput();
public abstract Vector2 GetAimInput();
public abstract bool GetStrafing();
public abstract bool GetActionJustPressed(string action);
public abstract bool GetActionPressed(string action);
}

View file

@ -11,9 +11,7 @@ public partial class KeyboardInputProvider : InputProvider
public override Vector2 GetAimInput()
{
var rightStickInput = GetRightStickInput();
return GetRightStickInput();
return GetRightStickInput();
}
private Vector2 GetRightStickInput()
@ -24,8 +22,13 @@ public partial class KeyboardInputProvider : InputProvider
);
}
public override bool GetStrafing()
public override bool GetActionJustPressed(string action)
{
return Input.IsActionPressed("strafe");
return Input.IsActionJustPressed(action);
}
public override bool GetActionPressed(string action)
{
return Input.IsActionPressed(action);
}
}

View file

@ -36,9 +36,14 @@ public abstract partial class MovementHandler : ActorModule
return _inputProviders.Aggregate(Vector2.Zero, (current, inputProvider) => current + inputProvider.GetAimInput());
}
public virtual bool GetStrafing()
public virtual bool GetActionJustPressed(string action)
{
return _inputProviders.Aggregate(false, (current, inputProvider) => current && inputProvider.GetStrafing());
return _inputProviders.Aggregate(false, (current, inputProvider) => current || inputProvider.GetActionJustPressed(action));
}
public virtual bool GetActionPressed(string action)
{
return _inputProviders.Aggregate(false, (current, inputProvider) => current || inputProvider.GetActionPressed(action));
}
}

View file

@ -0,0 +1 @@
uid://c5x8kd1ftr25s

View file

@ -67,7 +67,7 @@ public partial class Weapon : Node2D
}
}
public void Shoot()
public void Shoot(BulletOwner? ownerOverride = null)
{
// Waiting on reload or Rate of Fire cooldown?
if (!_cooldownTimer.IsStopped())
@ -106,8 +106,14 @@ public partial class Weapon : Node2D
GD.PrintErr("Bullet is null, not shooting");
return;
};
var bulletData = WeaponData.MakeBullet(_muzzle.GlobalPosition);
if (ownerOverride.HasValue)
{
bulletData.Owner = ownerOverride.Value;
}
bullet.Initialize(WeaponData.MakeBullet(_muzzle.GlobalPosition), _gameManager);
bullet.Initialize(bulletData, _gameManager);
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);