Not perfect but enemies move/attack with animation

This commit is contained in:
2024-09-15 23:56:05 -07:00
parent 4a1fdd94f8
commit 8143da44db
36 changed files with 891 additions and 507 deletions

View File

@@ -89,16 +89,22 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
EnemyBinding
.Handle((in EnemyLogic.Output.MovementComputed output) =>
{
if (AnimationPlayer.CurrentAnimation != "hit" && AnimationPlayer.CurrentAnimation != "attack")
RotateEnemy(-GameRepo.PlayerGlobalTransform.Value.Basis.Z);
MoveAndSlide();
})
.Handle((in EnemyLogic.Output.HitByPlayer output) =>
{
if (CurrentHP.Value > 0)
AnimationPlayer.Play("hit");
AnimationPlayer.Stop();
AnimationPlayer.Play("hit");
// TODO: Make this an event to notify game that player hit someone
if (GameRepo.PlayerData.Inventory.EquippedWeapon.Value.WeaponStats.WeaponTags.Contains(WeaponTag.SelfDamage))
GameRepo.PlayerData.SetCurrentHP(GameRepo.PlayerData.CurrentHP.Value - 5);
})
.Handle((in EnemyLogic.Output.Attack _) =>
{
AnimationPlayer.Play("attack");
})
.Handle((in EnemyLogic.Output.Defeated output) =>
{
AnimationPlayer.Play("defeated");
@@ -130,13 +136,12 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
rng.Randomize();
var randomizedSpot = new Vector3(rng.RandfRange(-7.0f, 7.0f), 0, rng.RandfRange(-7.0f, 7.0f));
EnemyLogic.Input(new EnemyLogic.Input.PatrolToRandomSpot(GlobalPosition + randomizedSpot));
PatrolTimer.WaitTime = rng.RandfRange(7.0f, 15.0f);
PatrolTimer.WaitTime = rng.RandfRange(1.0f, 3.0f);
}
public void OnPhysicsProcess(double delta)
{
EnemyLogic.Input(new EnemyLogic.Input.PhysicsTick(delta));
RotateEnemy(-GameRepo.PlayerGlobalTransform.Value.Basis.Z);
}
public void OnPlayerHitboxEntered(Area3D body)
@@ -201,17 +206,17 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
// Check if forward facing. If the dot product is -1, the enemy is facing the camera.
if (forwardDotProduct < -rotateUpperThreshold)
AnimatedSprite.Play("idle_front_walk");
AnimationPlayer.Play("idle_front_walk");
// Check if backward facing. If the dot product is 1, the enemy is facing the same direction as the camera.
else if (forwardDotProduct > rotateUpperThreshold)
AnimatedSprite.Play("idle_back_walk");
AnimationPlayer.Play("idle_back_walk");
else
{
// If the dot product of the perpendicular dot product is positive (up to 1), the enemy is facing to the left (since it's mirrored).
AnimatedSprite.FlipH = leftDotProduct > 0;
// Check is side facing. If the dot product is close to zero in the positive or negative direction, its close to the threshold for turning.
if (Mathf.Abs(forwardDotProduct) < rotateLowerThreshold)
AnimatedSprite.Play("idle_left_walk");
AnimationPlayer.Play("idle_left_walk");
}
}