mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-07 10:25:55 +00:00
Simple knockback implementation
This commit is contained in:
parent
0146de8ab5
commit
78fe44e6ec
10 changed files with 96 additions and 8 deletions
|
|
@ -23,6 +23,8 @@ public partial class GenericDamageReceiver : Area2D, IHittable
|
|||
[Signal]
|
||||
public delegate void ShieldHitEventHandler();
|
||||
|
||||
[Signal] public delegate void BulletHitEventHandler(Bullet bullet, Vector2 position, Vector2 direction);
|
||||
|
||||
//[Signal] public delegate void DeathEventHandler();
|
||||
|
||||
private Node2D _parent;
|
||||
|
|
@ -54,6 +56,9 @@ public partial class GenericDamageReceiver : Area2D, IHittable
|
|||
if (BulletGroup is BulletOwner.None)
|
||||
{
|
||||
this.Hit(bullet.Damage, bullet.DamageType);
|
||||
|
||||
EmitSignalBulletHit(bullet, area.GlobalPosition, (this.GlobalPosition - area.GlobalPosition).Normalized());
|
||||
|
||||
bullet.RequestCollisionDestruction();
|
||||
return;
|
||||
}
|
||||
|
|
@ -61,6 +66,9 @@ public partial class GenericDamageReceiver : Area2D, IHittable
|
|||
if (bullet.BulletInfo.Owner == BulletGroup) return;
|
||||
|
||||
this.Hit(bullet.Damage, bullet.DamageType);
|
||||
|
||||
EmitSignalBulletHit(bullet, area.GlobalPosition, (this.GlobalPosition - area.GlobalPosition).Normalized());
|
||||
|
||||
bullet.RequestCollisionDestruction();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ public class BulletInfo
|
|||
public BulletOwner Owner { get; set; }
|
||||
public DamageType DamageType { get; set; }
|
||||
public float Damage { get; set; }
|
||||
public float Knockback { get; set; }
|
||||
public int BulletCount { get; set; }
|
||||
public float RotationSpeed { get; set; }
|
||||
public float RotationOffset { get; set; }
|
||||
|
|
|
|||
60
Scripts/Components/FSM/Enemy/EnemyKnockbackProvider.cs
Normal file
60
Scripts/Components/FSM/Enemy/EnemyKnockbackProvider.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy;
|
||||
|
||||
public partial class EnemyKnockbackProvider : ModuleBase<EnemyState, CharacterBody2D>
|
||||
{
|
||||
[Export] public GenericDamageReceiver DamageReceiver { get; private set; }
|
||||
[Export] public EnemyStorageModule StorageModule { get; private set; }
|
||||
|
||||
[Export] public float KnockbackDecayRate = 800f; // pixels per second^2, tweak this
|
||||
|
||||
private Vector2 KnockbackVelocity
|
||||
{
|
||||
get => StorageModule.KnockbackVelocity;
|
||||
set => StorageModule.KnockbackVelocity = value;
|
||||
}
|
||||
|
||||
private bool IsKnockbackActive => KnockbackVelocity.LengthSquared() > 1f;
|
||||
|
||||
private IStateMachine<EnemyState, CharacterBody2D> _machine;
|
||||
private bool _enabled = false;
|
||||
public override void EnterState(EnemyState state)
|
||||
{
|
||||
_enabled = true;
|
||||
DamageReceiver.BulletHit += DamageReceiverOnBulletHit;
|
||||
}
|
||||
|
||||
private void DamageReceiverOnBulletHit(Bullet bullet, Vector2 position, Vector2 direction)
|
||||
{
|
||||
var knockbackVelocity = direction * bullet.BulletInfo.Knockback;
|
||||
|
||||
KnockbackVelocity = knockbackVelocity;
|
||||
}
|
||||
|
||||
public override void ExitState(EnemyState state)
|
||||
{
|
||||
_enabled = false;
|
||||
DamageReceiver.BulletHit -= DamageReceiverOnBulletHit;
|
||||
}
|
||||
|
||||
public override void Init(IStateMachine<EnemyState, CharacterBody2D> machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
|
||||
public override void Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcess(double delta)
|
||||
{
|
||||
if (IsKnockbackActive)
|
||||
{
|
||||
KnockbackVelocity = KnockbackVelocity.MoveToward(Vector2.Zero, KnockbackDecayRate * (float)delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://by1w0oo6nguyv
|
||||
|
|
@ -22,6 +22,7 @@ public partial class EnemyStorageModule : Node2D, IFSMStorage
|
|||
public Vector2 FacingDirection { get; set; }
|
||||
|
||||
public Vector2 AimingDirection { get; set; }
|
||||
public Vector2 KnockbackVelocity { get; set; } = Vector2.Zero;
|
||||
|
||||
public float MovementSpeed => Root.EnemyResource.MovementSpeed;
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ public partial class NavigationMovementModule : Node2D
|
|||
var nextPathPosition = _navigationAgent.GetNextPathPosition();
|
||||
|
||||
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * movementSpeed;
|
||||
|
||||
newVelocity += StorageModule.KnockbackVelocity;
|
||||
|
||||
if (_navigationAgent.AvoidanceEnabled)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue