MobAiv2p5

This commit is contained in:
dingo
2025-12-27 13:12:54 -05:00
parent 8e02217e92
commit 99472f226a
2 changed files with 57 additions and 64 deletions
+1
View File
@@ -195,6 +195,7 @@ public class DungeonSimulation
MaxMana = mobDef.BaseMana, MaxMana = mobDef.BaseMana,
Stats = stats, Stats = stats,
State = MobState.Idle, State = MobState.Idle,
StateTime = (float)(rng.NextDouble() * 3f), // Randomize so mobs don't all start moving at once
SpriteConfigJson = mobDef.SpriteConfigJson, SpriteConfigJson = mobDef.SpriteConfigJson,
TintColor = mobDef.TintColor, TintColor = mobDef.TintColor,
Scale = mobDef.Scale, Scale = mobDef.Scale,
+55 -63
View File
@@ -512,7 +512,7 @@ public static class MobAI
{ {
// Use leash range if no wander radius specified // Use leash range if no wander radius specified
if (wanderRadius <= 0) if (wanderRadius <= 0)
wanderRadius = mob.Stats.LeashRange * 0.8f; wanderRadius = Math.Max(mob.Stats.LeashRange * 0.6f, 100f);
float distFromSpawn = GetDistance(mob.X, mob.Y, mob.SpawnX, mob.SpawnY); float distFromSpawn = GetDistance(mob.X, mob.Y, mob.SpawnX, mob.SpawnY);
@@ -523,83 +523,72 @@ public static class MobAI
return; return;
} }
// Heal when idle // Heal when idle/wandering
if (mob.Health < mob.MaxHealth) if (mob.Health < mob.MaxHealth)
{ {
mob.Health = Math.Min(mob.MaxHealth, mob.Health + (int)(mob.MaxHealth * 0.02f * deltaTime)); mob.Health = Math.Min(mob.MaxHealth, mob.Health + (int)(mob.MaxHealth * 0.02f * deltaTime));
} }
// Check if we need a new wander target // State machine for wandering
bool needNewTarget = false; // States: Idle (standing still) -> Moving to wander target -> Idle again
if (mob.WanderTargetX == 0 && mob.WanderTargetY == 0) if (mob.State == MobState.Idle || mob.State == MobState.Returning)
{ {
// Never had a target // Currently idling - wait a bit then pick a new target
needNewTarget = true; if (mob.StateTime >= 1.5f + (_rng.NextDouble() * 2f)) // 1.5-3.5 seconds of idling
}
else if (mob.State == MobState.Idle && mob.StateTime > 2f)
{
// Idled long enough, time to move again
needNewTarget = true;
}
// Check if we reached the wander target
float distToTarget = GetDistance(mob.X, mob.Y, mob.WanderTargetX, mob.WanderTargetY);
if (distToTarget < 16f && mob.State != MobState.Idle)
{
// Reached target, idle for a bit
mob.State = MobState.Idle;
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
mob.StateTime = 0;
return;
}
if (needNewTarget)
{
// Pick a new random wander point near spawn
// Try to find a valid floor tile
for (int attempts = 0; attempts < 5; attempts++)
{ {
float angle = (float)(_rng.NextDouble() * Math.PI * 2); // Time to pick a new wander target
float dist = (float)(_rng.NextDouble() * wanderRadius * 0.7f) + 30f; bool foundTarget = false;
for (int attempts = 0; attempts < 8; attempts++)
float targetX = mob.SpawnX + MathF.Cos(angle) * dist;
float targetY = mob.SpawnY + MathF.Sin(angle) * dist;
// Check if target is walkable
if (sim.CanMoveTo(targetX, targetY))
{ {
mob.WanderTargetX = targetX; float angle = (float)(_rng.NextDouble() * Math.PI * 2);
mob.WanderTargetY = targetY; float dist = 40f + (float)(_rng.NextDouble() * wanderRadius * 0.5f);
break;
float targetX = mob.SpawnX + MathF.Cos(angle) * dist;
float targetY = mob.SpawnY + MathF.Sin(angle) * dist;
// Check if target is walkable
if (sim.CanMoveTo(targetX, targetY))
{
mob.WanderTargetX = targetX;
mob.WanderTargetY = targetY;
foundTarget = true;
break;
}
} }
}
mob.StateTime = 0; if (foundTarget)
{
// 20% chance to just idle for a bit instead of moving // Start moving
if (_rng.NextDouble() < 0.2f) mob.State = MobState.Chasing; // Reuse chasing state for wander movement
{ mob.StateTime = 0;
mob.State = MobState.Idle; mob.UpdateFacingDirection(mob.WanderTargetX, mob.WanderTargetY);
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle"); mob.CurrentAnimation = mob.GetDirectionalAnimation("walk");
return; }
// else stay idle and try again next cycle
} }
// else keep idling, animation already set
} }
else
// Move toward wander target if we have one and not idling
if (mob.State == MobState.Idle && mob.StateTime < 2f)
{ {
// Still idling // Currently moving toward wander target
return; float distToTarget = GetDistance(mob.X, mob.Y, mob.WanderTargetX, mob.WanderTargetY);
if (distToTarget < 20f || mob.StateTime > 10f) // Reached target or timeout
{
// Stop and idle
mob.State = MobState.Idle;
mob.StateTime = 0;
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
}
else
{
// Keep moving toward target (30% speed for casual wandering)
mob.UpdateFacingDirection(mob.WanderTargetX, mob.WanderTargetY);
mob.CurrentAnimation = mob.GetDirectionalAnimation("walk");
MoveToward(mob, mob.WanderTargetX, mob.WanderTargetY, mob.Stats.MoveSpeed * 0.3f * deltaTime, sim);
}
} }
// Move!
mob.State = MobState.Chasing; // Use chasing for movement state
mob.UpdateFacingDirection(mob.WanderTargetX, mob.WanderTargetY);
mob.CurrentAnimation = mob.GetDirectionalAnimation("walk");
// Wander slowly (30% speed)
MoveToward(mob, mob.WanderTargetX, mob.WanderTargetY, mob.Stats.MoveSpeed * 0.3f * deltaTime, sim);
} }
private static void ReturnToSpawn(MobInstance mob, DungeonSimulation sim, float deltaTime) private static void ReturnToSpawn(MobInstance mob, DungeonSimulation sim, float deltaTime)
@@ -608,10 +597,13 @@ public static class MobAI
if (dist < 20f) if (dist < 20f)
{ {
// Arrived at spawn - reset to idle so wandering can start fresh
mob.State = MobState.Idle; mob.State = MobState.Idle;
mob.TargetId = null; mob.TargetId = null;
mob.CombatTime = 0; mob.CombatTime = 0;
mob.StateTime = 0; mob.StateTime = 0;
mob.WanderTargetX = 0; // Clear so new target will be picked
mob.WanderTargetY = 0;
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle"); mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
} }
else else