Portal fix, mob ai adjustment
This commit is contained in:
+62
-26
@@ -508,8 +508,12 @@ public static class MobAI
|
||||
/// <summary>
|
||||
/// Wander around spawn area when no targets
|
||||
/// </summary>
|
||||
private static void UpdateIdleWander(MobInstance mob, DungeonSimulation sim, float deltaTime, float wanderRadius = 100f)
|
||||
private static void UpdateIdleWander(MobInstance mob, DungeonSimulation sim, float deltaTime, float wanderRadius = 0f)
|
||||
{
|
||||
// Use leash range if no wander radius specified
|
||||
if (wanderRadius <= 0)
|
||||
wanderRadius = mob.Stats.LeashRange * 0.8f;
|
||||
|
||||
float distFromSpawn = GetDistance(mob.X, mob.Y, mob.SpawnX, mob.SpawnY);
|
||||
|
||||
// Too far from spawn? Return first
|
||||
@@ -526,19 +530,55 @@ public static class MobAI
|
||||
}
|
||||
|
||||
// Check if we need a new wander target
|
||||
if (mob.State != MobState.Idle || mob.StateTime > 3f ||
|
||||
(mob.WanderTargetX == 0 && mob.WanderTargetY == 0))
|
||||
bool needNewTarget = false;
|
||||
|
||||
if (mob.WanderTargetX == 0 && mob.WanderTargetY == 0)
|
||||
{
|
||||
// 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
|
||||
float angle = (float)(_rng.NextDouble() * Math.PI * 2);
|
||||
float dist = (float)(_rng.NextDouble() * wanderRadius * 0.7f);
|
||||
// Try to find a valid floor tile
|
||||
for (int attempts = 0; attempts < 5; attempts++)
|
||||
{
|
||||
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))
|
||||
{
|
||||
mob.WanderTargetX = targetX;
|
||||
mob.WanderTargetY = targetY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mob.WanderTargetX = mob.SpawnX + MathF.Cos(angle) * dist;
|
||||
mob.WanderTargetY = mob.SpawnY + MathF.Sin(angle) * dist;
|
||||
mob.StateTime = 0;
|
||||
|
||||
// Sometimes just idle for a bit
|
||||
if (_rng.NextDouble() < 0.3f)
|
||||
// 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");
|
||||
@@ -546,24 +586,20 @@ public static class MobAI
|
||||
}
|
||||
}
|
||||
|
||||
// Move toward wander target
|
||||
float distToTarget = GetDistance(mob.X, mob.Y, mob.WanderTargetX, mob.WanderTargetY);
|
||||
// Move toward wander target if we have one and not idling
|
||||
if (mob.State == MobState.Idle && mob.StateTime < 2f)
|
||||
{
|
||||
// Still idling
|
||||
return;
|
||||
}
|
||||
|
||||
if (distToTarget < 10f)
|
||||
{
|
||||
// Reached target, idle for a bit
|
||||
mob.State = MobState.Idle;
|
||||
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
|
||||
mob.StateTime = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
mob.State = MobState.Idle; // Still "idle" but moving
|
||||
mob.UpdateFacingDirection(mob.WanderTargetX, mob.WanderTargetY);
|
||||
mob.CurrentAnimation = mob.GetDirectionalAnimation("walk");
|
||||
// Wander slowly
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user