Altering Escape
This commit is contained in:
@@ -64,8 +64,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
DungeonOpCodes.DUNGEON_INPUT_ATTACK,
|
||||
DungeonOpCodes.DUNGEON_INPUT_PICKUP,
|
||||
DungeonOpCodes.DUNGEON_INPUT_CHEST,
|
||||
DungeonOpCodes.ESCAPE_START,
|
||||
DungeonOpCodes.ESCAPE_CANCEL,
|
||||
DungeonOpCodes.DUNGEON_INPUT_PORTAL,
|
||||
DungeonOpCodes.INVENTORY_REQUEST,
|
||||
DungeonOpCodes.EQUIP_ITEM,
|
||||
DungeonOpCodes.UNEQUIP_ITEM,
|
||||
@@ -170,12 +169,8 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
HandleInputChest(session, payload);
|
||||
break;
|
||||
|
||||
case DungeonOpCodes.ESCAPE_START:
|
||||
HandleEscapeStart(session);
|
||||
break;
|
||||
|
||||
case DungeonOpCodes.ESCAPE_CANCEL:
|
||||
HandleEscapeCancel(session);
|
||||
case DungeonOpCodes.DUNGEON_INPUT_PORTAL:
|
||||
await HandlePortalUse(connection, session, payload, ct);
|
||||
break;
|
||||
|
||||
case DungeonOpCodes.INVENTORY_REQUEST:
|
||||
@@ -419,6 +414,13 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
y = c.Y,
|
||||
type = c.Type.ToString().ToLower(),
|
||||
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);
|
||||
}
|
||||
@@ -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 (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
||||
|
||||
// Check if at exit
|
||||
var layout = dungeon.Simulation.Layout;
|
||||
if (layout.ExitPosition == null) return;
|
||||
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
|
||||
var request = System.Text.Json.JsonSerializer.Deserialize<PortalUseRequest>(json);
|
||||
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;
|
||||
if (player == null) return;
|
||||
|
||||
float exitX = layout.ExitPosition.Value.X * 32 + 16;
|
||||
float exitY = layout.ExitPosition.Value.Y * 32 + 16;
|
||||
float dist = MathF.Sqrt((player.X - exitX) * (player.X - exitX) + (player.Y - exitY) * (player.Y - exitY));
|
||||
// Check distance to portal (must be within 64 pixels)
|
||||
float dx = player.X - portal.X;
|
||||
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;
|
||||
session.EscapeStartTick = _currentTick;
|
||||
_log.LogDebug("Player {Name} too far from portal ({Dist})", session.Character.Name, dist);
|
||||
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>
|
||||
/// Handle hotbar slot assignment - links a hotbar slot to an inventory slot
|
||||
/// </summary>
|
||||
@@ -1159,6 +1189,14 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
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
|
||||
{
|
||||
damage = tickResult.DamageEvents.Select(e => new
|
||||
@@ -1489,3 +1527,8 @@ public class ChatSendRequest
|
||||
{
|
||||
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, ChestInstance> Chests { get; } = new();
|
||||
public ConcurrentDictionary<int, DroppedItem> DroppedItems { get; } = new();
|
||||
public ConcurrentDictionary<int, PortalInstance> Portals { get; } = new();
|
||||
|
||||
// Combat tracking
|
||||
private readonly List<DamageEvent> _pendingDamageEvents = new();
|
||||
@@ -40,6 +41,54 @@ public class DungeonSimulation
|
||||
|
||||
SpawnMobs();
|
||||
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()
|
||||
|
||||
@@ -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