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,
Stats = stats,
State = MobState.Idle,
StateTime = (float)(rng.NextDouble() * 3f), // Randomize so mobs don't all start moving at once
SpriteConfigJson = mobDef.SpriteConfigJson,
TintColor = mobDef.TintColor,
Scale = mobDef.Scale,
+56 -64
View File
@@ -512,7 +512,7 @@ public static class MobAI
{
// Use leash range if no wander radius specified
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);
@@ -523,83 +523,72 @@ public static class MobAI
return;
}
// Heal when idle
// Heal when idle/wandering
if (mob.Health < mob.MaxHealth)
{
mob.Health = Math.Min(mob.MaxHealth, mob.Health + (int)(mob.MaxHealth * 0.02f * deltaTime));
}
// Check if we need a new wander target
bool needNewTarget = false;
// State machine for wandering
// 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
needNewTarget = true;
}
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++)
// Currently idling - wait a bit then pick a new target
if (mob.StateTime >= 1.5f + (_rng.NextDouble() * 2f)) // 1.5-3.5 seconds of idling
{
float angle = (float)(_rng.NextDouble() * Math.PI * 2);
float dist = (float)(_rng.NextDouble() * wanderRadius * 0.7f) + 30f;
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))
// Time to pick a new wander target
bool foundTarget = false;
for (int attempts = 0; attempts < 8; attempts++)
{
mob.WanderTargetX = targetX;
mob.WanderTargetY = targetY;
break;
float angle = (float)(_rng.NextDouble() * Math.PI * 2);
float dist = 40f + (float)(_rng.NextDouble() * wanderRadius * 0.5f);
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;
}
}
if (foundTarget)
{
// Start moving
mob.State = MobState.Chasing; // Reuse chasing state for wander movement
mob.StateTime = 0;
mob.UpdateFacingDirection(mob.WanderTargetX, mob.WanderTargetY);
mob.CurrentAnimation = mob.GetDirectionalAnimation("walk");
}
// else stay idle and try again next cycle
}
mob.StateTime = 0;
// 20% chance to just idle for a bit instead of moving
if (_rng.NextDouble() < 0.2f)
{
mob.State = MobState.Idle;
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
return;
}
// else keep idling, animation already set
}
// Move toward wander target if we have one and not idling
if (mob.State == MobState.Idle && mob.StateTime < 2f)
else
{
// Still idling
return;
// Currently moving toward wander target
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)
@@ -608,10 +597,13 @@ public static class MobAI
if (dist < 20f)
{
// Arrived at spawn - reset to idle so wandering can start fresh
mob.State = MobState.Idle;
mob.TargetId = null;
mob.CombatTime = 0;
mob.StateTime = 0;
mob.WanderTargetX = 0; // Clear so new target will be picked
mob.WanderTargetY = 0;
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
}
else