Minor fixes to navigation

This commit is contained in:
2025-02-25 02:04:15 -08:00
parent 22a930358c
commit 0d7242c96d
11 changed files with 194 additions and 115 deletions

View File

@@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace GameJamDungeon;
[Meta(typeof(IAutoNode))]
public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
{
#region Registration
public override void _Notification(int what) => this.Notify(what);
@@ -25,7 +25,7 @@ public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
[Dependency] IGameEventDepot GameEventDepot => this.DependOn<IGameEventDepot>();
[Dependency] protected IPlayer Player => this.DependOn<IPlayer>(() => GetParent().GetChildren().OfType<IPlayer>().Single());
[Dependency] protected IPlayer Player => this.DependOn(() => GetParent().GetChildren().OfType<IPlayer>().Single());
#endregion
#region Exports
@@ -67,12 +67,14 @@ public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
_enemyLogic.Set(this as IEnemy);
_enemyLogic.Set(Player);
_currentTarget = GlobalPosition;
NavAgent.VelocityComputed += NavAgent_VelocityComputed;
NavAgent.TargetReached += NavAgent_TargetReached;
_thinkTimer = new Timer
{
WaitTime = 1f
WaitTime = 0.4f
};
AddChild(_thinkTimer);
_thinkTimer.Timeout += NavAgent_TargetReached;
@@ -89,13 +91,9 @@ public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
if (CurrentHP <= 0)
return;
var lookDir = GlobalPosition - safeVelocity;
var leveledLookDir = new Vector3(lookDir.X, Position.Y, lookDir.Z);
if (leveledLookDir.DistanceTo(GlobalPosition) > 0.2f)
LookAt(new Vector3(lookDir.X, Position.Y, lookDir.Z), Vector3.Up);
_knockbackStrength = _knockbackStrength * 0.9f;
MoveAndCollide(safeVelocity + (_knockbackDirection * _knockbackStrength));
Velocity = safeVelocity + (_knockbackDirection * _knockbackStrength);
MoveAndSlide();
}
public void SetTarget(Vector3 target)
@@ -122,6 +120,7 @@ public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
EnemyModelView.PlayHitAnimation();
_enemyLogic.Input(new EnemyLogic.Input.Alerted());
NavAgent_TargetReached();
if (Player.EquippedWeapon.Value.WeaponTags.Contains(WeaponTag.SelfDamage))
Player.Stats.SetCurrentHP(Player.Stats.CurrentHP.Value - 5);
@@ -133,16 +132,21 @@ public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
if (CurrentHP <= 0)
return;
if (_enemyLogic.Value is EnemyLogic.State.Attacking)
SetTarget(GlobalPosition);
var nextPathPosition = NavAgent.GetNextPathPosition();
var movementDelta = 2f * (float)delta;
var newVelocity = GlobalPosition.DirectionTo(nextPathPosition) * movementDelta;
var newVelocity = GlobalPosition.DirectionTo(nextPathPosition) * 2f;
if (NavAgent.AvoidanceEnabled)
NavAgent.Velocity = newVelocity;
else
NavAgent_VelocityComputed(newVelocity);
var lookDir = GlobalPosition + Velocity;
if (_enemyLogic.Value is not EnemyLogic.State.Attacking && (!lookDir.IsEqualApprox(GlobalPosition) || !Velocity.IsZeroApprox()))
LookAt(lookDir, Vector3.Up, true);
var isWalking = _enemyLogic.Value is EnemyLogic.State.Patrolling or EnemyLogic.State.FollowPlayer;
EnemyModelView.RotateModel(GlobalTransform.Basis, -Player.CurrentBasis.Z, isWalking);
}
@@ -200,7 +204,7 @@ public partial class Enemy : RigidBody3D, IEnemy, IProvide<IEnemyLogic>
{
var rng = new RandomNumberGenerator();
rng.Randomize();
var randomizedSpot = new Vector3(rng.RandfRange(-7.0f, 7.0f), 0, rng.RandfRange(-7.0f, 7.0f));
var randomizedSpot = new Vector3(rng.RandfRange(-5.0f, 5.0f), 0, rng.RandfRange(-5.0f, 5.0f));
_enemyLogic.Input(new EnemyLogic.Input.PatrolToRandomSpot(GlobalPosition + randomizedSpot));
_enemyLogic.Input(new EnemyLogic.Input.StartPatrol());
PatrolTimer.WaitTime = rng.RandfRange(5.0f, 10.0f);