Altering Escape
This commit is contained in:
@@ -64,8 +64,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
DungeonOpCodes.DUNGEON_INPUT_ATTACK,
|
DungeonOpCodes.DUNGEON_INPUT_ATTACK,
|
||||||
DungeonOpCodes.DUNGEON_INPUT_PICKUP,
|
DungeonOpCodes.DUNGEON_INPUT_PICKUP,
|
||||||
DungeonOpCodes.DUNGEON_INPUT_CHEST,
|
DungeonOpCodes.DUNGEON_INPUT_CHEST,
|
||||||
DungeonOpCodes.ESCAPE_START,
|
DungeonOpCodes.DUNGEON_INPUT_PORTAL,
|
||||||
DungeonOpCodes.ESCAPE_CANCEL,
|
|
||||||
DungeonOpCodes.INVENTORY_REQUEST,
|
DungeonOpCodes.INVENTORY_REQUEST,
|
||||||
DungeonOpCodes.EQUIP_ITEM,
|
DungeonOpCodes.EQUIP_ITEM,
|
||||||
DungeonOpCodes.UNEQUIP_ITEM,
|
DungeonOpCodes.UNEQUIP_ITEM,
|
||||||
@@ -170,12 +169,8 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
HandleInputChest(session, payload);
|
HandleInputChest(session, payload);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.ESCAPE_START:
|
case DungeonOpCodes.DUNGEON_INPUT_PORTAL:
|
||||||
HandleEscapeStart(session);
|
await HandlePortalUse(connection, session, payload, ct);
|
||||||
break;
|
|
||||||
|
|
||||||
case DungeonOpCodes.ESCAPE_CANCEL:
|
|
||||||
HandleEscapeCancel(session);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.INVENTORY_REQUEST:
|
case DungeonOpCodes.INVENTORY_REQUEST:
|
||||||
@@ -419,6 +414,13 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
y = c.Y,
|
y = c.Y,
|
||||||
type = c.Type.ToString().ToLower(),
|
type = c.Type.ToString().ToLower(),
|
||||||
opened = c.IsOpened
|
opened = c.IsOpened
|
||||||
|
}),
|
||||||
|
portals = instance.Simulation.Portals.Values.Where(p => p.IsActive).Select(p => new
|
||||||
|
{
|
||||||
|
id = p.Id,
|
||||||
|
x = p.X,
|
||||||
|
y = p.Y,
|
||||||
|
type = p.Type.ToString().ToLower()
|
||||||
})
|
})
|
||||||
}, ct);
|
}, ct);
|
||||||
}
|
}
|
||||||
@@ -522,33 +524,61 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleEscapeStart(DungeonPlayerSession session)
|
/// <summary>
|
||||||
|
/// Handle portal use - instant extraction when player clicks/walks into portal
|
||||||
|
/// </summary>
|
||||||
|
private async Task HandlePortalUse(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
||||||
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
||||||
|
|
||||||
// Check if at exit
|
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
|
||||||
var layout = dungeon.Simulation.Layout;
|
var request = System.Text.Json.JsonSerializer.Deserialize<PortalUseRequest>(json);
|
||||||
if (layout.ExitPosition == null) return;
|
if (request == null) return;
|
||||||
|
|
||||||
|
// Find the portal
|
||||||
|
if (!dungeon.Simulation.Portals.TryGetValue(request.PortalId, out var portal))
|
||||||
|
{
|
||||||
|
_log.LogWarning("Player {Name} tried to use non-existent portal {Id}", session.Character.Name, request.PortalId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!portal.IsActive)
|
||||||
|
{
|
||||||
|
_log.LogWarning("Player {Name} tried to use inactive portal {Id}", session.Character.Name, request.PortalId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var player = session.PlayerInstance;
|
var player = session.PlayerInstance;
|
||||||
if (player == null) return;
|
if (player == null) return;
|
||||||
|
|
||||||
float exitX = layout.ExitPosition.Value.X * 32 + 16;
|
// Check distance to portal (must be within 64 pixels)
|
||||||
float exitY = layout.ExitPosition.Value.Y * 32 + 16;
|
float dx = player.X - portal.X;
|
||||||
float dist = MathF.Sqrt((player.X - exitX) * (player.X - exitX) + (player.Y - exitY) * (player.Y - exitY));
|
float dy = player.Y - portal.Y;
|
||||||
|
float dist = MathF.Sqrt(dx * dx + dy * dy);
|
||||||
|
|
||||||
if (dist < 48) // Within range
|
if (dist > 64)
|
||||||
{
|
{
|
||||||
session.IsEscaping = true;
|
_log.LogDebug("Player {Name} too far from portal ({Dist})", session.Character.Name, dist);
|
||||||
session.EscapeStartTick = _currentTick;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_log.LogInformation("Player {Name} using exit portal", session.Character.Name);
|
||||||
|
|
||||||
|
// Send portal used confirmation
|
||||||
|
await SendAsync(connection, DungeonOpCodes.PORTAL_USED, new
|
||||||
|
{
|
||||||
|
portalId = portal.Id,
|
||||||
|
type = portal.Type.ToString().ToLower()
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
// Extract player (keep loot, save progress)
|
||||||
|
await RemoveFromDungeonAsync(session, true, ct);
|
||||||
|
|
||||||
|
// Return to hub
|
||||||
|
await SendAsync(connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleEscapeCancel(DungeonPlayerSession session)
|
|
||||||
{
|
|
||||||
session.IsEscaping = false;
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handle hotbar slot assignment - links a hotbar slot to an inventory slot
|
/// Handle hotbar slot assignment - links a hotbar slot to an inventory slot
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1159,6 +1189,14 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
y = i.Y
|
y = i.Y
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
portals = sim.Portals.Values.Where(p => p.IsActive).Select(p => new
|
||||||
|
{
|
||||||
|
id = p.Id,
|
||||||
|
x = p.X,
|
||||||
|
y = p.Y,
|
||||||
|
type = p.Type.ToString().ToLower()
|
||||||
|
}),
|
||||||
|
|
||||||
events = tickResult == null ? null : new
|
events = tickResult == null ? null : new
|
||||||
{
|
{
|
||||||
damage = tickResult.DamageEvents.Select(e => new
|
damage = tickResult.DamageEvents.Select(e => new
|
||||||
@@ -1489,3 +1527,8 @@ public class ChatSendRequest
|
|||||||
{
|
{
|
||||||
public string Message { get; set; } = "";
|
public string Message { get; set; } = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class PortalUseRequest
|
||||||
|
{
|
||||||
|
public int PortalId { get; set; }
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ public class DungeonSimulation
|
|||||||
public ConcurrentDictionary<int, MobInstance> Mobs { get; } = new();
|
public ConcurrentDictionary<int, MobInstance> Mobs { get; } = new();
|
||||||
public ConcurrentDictionary<int, ChestInstance> Chests { get; } = new();
|
public ConcurrentDictionary<int, ChestInstance> Chests { get; } = new();
|
||||||
public ConcurrentDictionary<int, DroppedItem> DroppedItems { get; } = new();
|
public ConcurrentDictionary<int, DroppedItem> DroppedItems { get; } = new();
|
||||||
|
public ConcurrentDictionary<int, PortalInstance> Portals { get; } = new();
|
||||||
|
|
||||||
// Combat tracking
|
// Combat tracking
|
||||||
private readonly List<DamageEvent> _pendingDamageEvents = new();
|
private readonly List<DamageEvent> _pendingDamageEvents = new();
|
||||||
@@ -40,6 +41,54 @@ public class DungeonSimulation
|
|||||||
|
|
||||||
SpawnMobs();
|
SpawnMobs();
|
||||||
InitializeChests();
|
InitializeChests();
|
||||||
|
SpawnExitPortal();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SpawnExitPortal()
|
||||||
|
{
|
||||||
|
// Spawn exit portal near the entrance room (but offset so players don't spawn on it)
|
||||||
|
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) };
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
var portal = new PortalInstance
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
X = portalTileX * 32 + 16, // Center of tile in world coords
|
||||||
|
Y = portalTileY * 32 + 16,
|
||||||
|
Type = PortalType.Exit,
|
||||||
|
IsActive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
Portals[portal.Id] = portal;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: spawn at room center if no offset worked
|
||||||
|
var fallbackPortal = new PortalInstance
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
X = entranceRoom.CenterX * 32 + 16,
|
||||||
|
Y = entranceRoom.CenterY * 32 + 16,
|
||||||
|
Type = PortalType.Exit,
|
||||||
|
IsActive = true
|
||||||
|
};
|
||||||
|
Portals[fallbackPortal.Id] = fallbackPortal;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SpawnMobs()
|
private void SpawnMobs()
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
namespace Server.Game.Dungeon;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Runtime instance of a portal in a dungeon
|
||||||
|
/// </summary>
|
||||||
|
public class PortalInstance
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public float X { get; set; }
|
||||||
|
public float Y { get; set; }
|
||||||
|
public PortalType Type { get; set; }
|
||||||
|
public bool IsActive { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum PortalType
|
||||||
|
{
|
||||||
|
Exit, // Return to hub
|
||||||
|
NextFloor, // Go deeper (future)
|
||||||
|
Secret // Hidden area (future)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user