Portal fix, mob ai adjustment
This commit is contained in:
@@ -62,29 +62,90 @@ public class DungeonSimulation
|
||||
|
||||
private void SpawnExitPortal()
|
||||
{
|
||||
// Spawn exit portal near the entrance room (but offset so players don't spawn on it)
|
||||
// Spawn exit portal in corner of entrance room (away from center and hallways)
|
||||
if (Layout.Rooms.Count == 0) return;
|
||||
|
||||
var entranceRoom = Layout.Rooms[0];
|
||||
|
||||
// Find a valid floor tile near the entrance that's not at the exact center
|
||||
// Try positions around the room center
|
||||
var offsets = new[] { (2, 0), (-2, 0), (0, 2), (0, -2), (2, 2), (-2, -2) };
|
||||
// Room uses X, Y as top-left corner
|
||||
int roomLeft = entranceRoom.X + 1;
|
||||
int roomRight = entranceRoom.X + entranceRoom.Width - 2;
|
||||
int roomTop = entranceRoom.Y + 1;
|
||||
int roomBottom = entranceRoom.Y + entranceRoom.Height - 2;
|
||||
|
||||
// Try corners first (away from center where players spawn)
|
||||
var cornerPositions = new[]
|
||||
{
|
||||
(roomLeft + 1, roomTop + 1), // Top-left
|
||||
(roomRight - 1, roomTop + 1), // Top-right
|
||||
(roomLeft + 1, roomBottom - 1), // Bottom-left
|
||||
(roomRight - 1, roomBottom - 1), // Bottom-right
|
||||
};
|
||||
|
||||
foreach (var (tileX, tileY) in cornerPositions)
|
||||
{
|
||||
// Check if this is a valid floor tile
|
||||
if (tileX >= 0 && tileX < Layout.Width &&
|
||||
tileY >= 0 && tileY < Layout.Height &&
|
||||
Layout.Tiles[tileX, tileY] == 1) // 1 = floor
|
||||
{
|
||||
// Check surrounding tiles to avoid placing near hallways
|
||||
bool nearHallway = false;
|
||||
for (int dx = -1; dx <= 1 && !nearHallway; dx++)
|
||||
{
|
||||
for (int dy = -1; dy <= 1 && !nearHallway; dy++)
|
||||
{
|
||||
int checkX = tileX + dx;
|
||||
int checkY = tileY + dy;
|
||||
if (checkX >= 0 && checkX < Layout.Width && checkY >= 0 && checkY < Layout.Height)
|
||||
{
|
||||
// Check if this position is outside the room bounds (potential hallway)
|
||||
bool outsideRoom = checkX < entranceRoom.X ||
|
||||
checkX >= entranceRoom.X + entranceRoom.Width ||
|
||||
checkY < entranceRoom.Y ||
|
||||
checkY >= entranceRoom.Y + entranceRoom.Height;
|
||||
|
||||
if (outsideRoom && Layout.Tiles[checkX, checkY] == 1) // Floor outside room = hallway
|
||||
{
|
||||
nearHallway = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!nearHallway)
|
||||
{
|
||||
var portal = new PortalInstance
|
||||
{
|
||||
Id = 1,
|
||||
X = tileX * 32 + 16, // Center of tile in world coords
|
||||
Y = tileY * 32 + 16,
|
||||
Type = PortalType.Exit,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
Portals[portal.Id] = portal;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: try any position in the room away from center
|
||||
var offsets = new[] { (-3, -3), (3, -3), (-3, 3), (3, 3), (-3, 0), (3, 0), (0, -3), (0, 3) };
|
||||
|
||||
foreach (var (dx, dy) in offsets)
|
||||
{
|
||||
int portalTileX = entranceRoom.CenterX + dx;
|
||||
int portalTileY = entranceRoom.CenterY + dy;
|
||||
|
||||
// Check if this is a valid floor tile
|
||||
if (portalTileX >= 0 && portalTileX < Layout.Width &&
|
||||
portalTileY >= 0 && portalTileY < Layout.Height &&
|
||||
Layout.Tiles[portalTileX, portalTileY] == 1) // 1 = floor
|
||||
Layout.Tiles[portalTileX, portalTileY] == 1)
|
||||
{
|
||||
var portal = new PortalInstance
|
||||
{
|
||||
Id = 1,
|
||||
X = portalTileX * 32 + 16, // Center of tile in world coords
|
||||
X = portalTileX * 32 + 16,
|
||||
Y = portalTileY * 32 + 16,
|
||||
Type = PortalType.Exit,
|
||||
IsActive = true
|
||||
@@ -95,7 +156,7 @@ public class DungeonSimulation
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: spawn at room center if no offset worked
|
||||
// Last fallback: spawn at room center
|
||||
var fallbackPortal = new PortalInstance
|
||||
{
|
||||
Id = 1,
|
||||
|
||||
+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