Add collision teleport portals and ledge-safe movement in VNext dungeons
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.Json;
|
||||
using DataServer.Data.Entities;
|
||||
using DataServer.Data.Enums;
|
||||
|
||||
@@ -60,6 +61,9 @@ public partial class DungeonSimulation
|
||||
private readonly Dictionary<int, DungeonNpcDefinition>? _npcDefinitions;
|
||||
private readonly List<(int MobDefinitionId, float X, float Y, int Tier)> _pendingSummons = new();
|
||||
private readonly Random _npcWanderRandom = new();
|
||||
private readonly Dictionary<ulong, long> _portalTeleportCooldownUntilTickByPlayer = new();
|
||||
private const float ActorCollisionRadiusPx = 10f;
|
||||
private const long PortalTeleportCooldownTicks = TicksPerSecond / 2;
|
||||
|
||||
public DungeonSimulation(
|
||||
DungeonLayout layout,
|
||||
@@ -84,6 +88,7 @@ public partial class DungeonSimulation
|
||||
SpawnNpcs();
|
||||
InitializeChests();
|
||||
SpawnExitPortal();
|
||||
SpawnTemplatePortals();
|
||||
}
|
||||
|
||||
private void SpawnNpcs()
|
||||
@@ -218,6 +223,114 @@ public partial class DungeonSimulation
|
||||
};
|
||||
Portals[fallbackPortal.Id] = fallbackPortal;
|
||||
}
|
||||
|
||||
private void SpawnTemplatePortals()
|
||||
{
|
||||
if (Layout is not DungeonLayoutV2 layoutV2 || layoutV2.Objects.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int nextPortalId = Portals.IsEmpty ? 100 : Portals.Keys.Max() + 1;
|
||||
foreach (var obj in layoutV2.Objects)
|
||||
{
|
||||
if (!obj.Type.Equals(ObjectTemplateType.Portal.ToString(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!TryParsePortalDestination(obj.MetadataJson, out var destX, out var destY))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var portal = new PortalInstance
|
||||
{
|
||||
Id = nextPortalId++,
|
||||
X = obj.X * 32 + 16,
|
||||
Y = obj.Y * 32 + 16,
|
||||
Type = PortalType.Secret,
|
||||
IsActive = true,
|
||||
TriggerOnCollision = true,
|
||||
TriggerRadius = 16f,
|
||||
DestinationX = destX,
|
||||
DestinationY = destY
|
||||
};
|
||||
|
||||
Portals[portal.Id] = portal;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryParsePortalDestination(string? metadataJson, out float destinationX, out float destinationY)
|
||||
{
|
||||
destinationX = 0f;
|
||||
destinationY = 0f;
|
||||
if (string.IsNullOrWhiteSpace(metadataJson))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(metadataJson);
|
||||
var root = doc.RootElement;
|
||||
if (root.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TryReadCoordinate(root, "targetX", "targetTileX", out var rawX) ||
|
||||
!TryReadCoordinate(root, "targetY", "targetTileY", out var rawY))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool tileCoordinates = root.TryGetProperty("useTileCoords", out var useTileCoords) &&
|
||||
useTileCoords.ValueKind == JsonValueKind.True;
|
||||
|
||||
destinationX = tileCoordinates ? rawX * 32f + 16f : rawX;
|
||||
destinationY = tileCoordinates ? rawY * 32f + 16f : rawY;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryReadCoordinate(JsonElement root, string pxName, string tileName, out float value)
|
||||
{
|
||||
value = 0f;
|
||||
if (root.TryGetProperty(pxName, out var px))
|
||||
{
|
||||
if (px.ValueKind == JsonValueKind.Number)
|
||||
{
|
||||
value = px.GetSingle();
|
||||
return true;
|
||||
}
|
||||
if (px.ValueKind == JsonValueKind.String && float.TryParse(px.GetString(), out var parsedPx))
|
||||
{
|
||||
value = parsedPx;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (root.TryGetProperty(tileName, out var tile))
|
||||
{
|
||||
if (tile.ValueKind == JsonValueKind.Number)
|
||||
{
|
||||
value = tile.GetSingle();
|
||||
return true;
|
||||
}
|
||||
if (tile.ValueKind == JsonValueKind.String && float.TryParse(tile.GetString(), out var parsedTile))
|
||||
{
|
||||
value = parsedTile;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SpawnMobs()
|
||||
{
|
||||
@@ -427,6 +540,7 @@ public partial class DungeonSimulation
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
UpdatePlayer(player);
|
||||
TryTriggerCollisionPortal(player);
|
||||
}
|
||||
|
||||
UpdateNpcs();
|
||||
@@ -497,9 +611,7 @@ public partial class DungeonSimulation
|
||||
if ((distX * distX) + (distY * distY) > maxDistSq)
|
||||
continue;
|
||||
|
||||
var tileX = (int)MathF.Floor(nx / 32f);
|
||||
var tileY = (int)MathF.Floor(ny / 32f);
|
||||
if (!Layout.IsWalkable(tileX, tileY))
|
||||
if (!CanActorMoveTo(nx, ny))
|
||||
continue;
|
||||
|
||||
npc.X = nx;
|
||||
@@ -725,7 +837,7 @@ public partial class DungeonSimulation
|
||||
float spawnX = mob.X + (float)Math.Cos(angle) * summonDistance;
|
||||
float spawnY = mob.Y + (float)Math.Sin(angle) * summonDistance;
|
||||
|
||||
if (!CanMoveTo(spawnX, spawnY))
|
||||
if (!CanActorMoveTo(spawnX, spawnY))
|
||||
{
|
||||
spawnX = mob.X + (float)Math.Cos(angle) * (summonDistance * 0.5f);
|
||||
spawnY = mob.Y + (float)Math.Sin(angle) * (summonDistance * 0.5f);
|
||||
@@ -1247,8 +1359,8 @@ public partial class DungeonSimulation
|
||||
float newX = target.X + (dx / dist) * ability.DisplacementDistance;
|
||||
float newY = target.Y + (dy / dist) * ability.DisplacementDistance;
|
||||
|
||||
if (CanMoveTo(newX, target.Y)) target.X = newX;
|
||||
if (CanMoveTo(target.X, newY)) target.Y = newY;
|
||||
if (CanActorMoveTo(newX, target.Y)) target.X = newX;
|
||||
if (CanActorMoveTo(target.X, newY)) target.Y = newY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1267,8 +1379,8 @@ public partial class DungeonSimulation
|
||||
float newX = target.X + (dx / dist) * pullDist;
|
||||
float newY = target.Y + (dy / dist) * pullDist;
|
||||
|
||||
if (CanMoveTo(newX, target.Y)) target.X = newX;
|
||||
if (CanMoveTo(target.X, newY)) target.Y = newY;
|
||||
if (CanActorMoveTo(newX, target.Y)) target.X = newX;
|
||||
if (CanActorMoveTo(target.X, newY)) target.Y = newY;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1316,7 +1428,7 @@ public partial class DungeonSimulation
|
||||
|
||||
private bool TryFindWalkableNear(float x, float y, out float walkableX, out float walkableY)
|
||||
{
|
||||
if (CanMoveTo(x, y))
|
||||
if (CanActorMoveTo(x, y))
|
||||
{
|
||||
walkableX = x;
|
||||
walkableY = y;
|
||||
@@ -1332,7 +1444,7 @@ public partial class DungeonSimulation
|
||||
float angle = (MathF.PI * 2f * i) / 8f;
|
||||
float testX = x + MathF.Cos(angle) * radius;
|
||||
float testY = y + MathF.Sin(angle) * radius;
|
||||
if (CanMoveTo(testX, testY))
|
||||
if (CanActorMoveTo(testX, testY))
|
||||
{
|
||||
walkableX = testX;
|
||||
walkableY = testY;
|
||||
@@ -1472,8 +1584,8 @@ public partial class DungeonSimulation
|
||||
float newX = player.X + player.InputX * speed;
|
||||
float newY = player.Y + player.InputY * speed;
|
||||
|
||||
if (CanMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanMoveTo(player.X, newY)) player.Y = newY;
|
||||
if (CanActorMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanActorMoveTo(player.X, newY)) player.Y = newY;
|
||||
|
||||
if (player.InputX != 0 || player.InputY != 0)
|
||||
{
|
||||
@@ -1500,8 +1612,8 @@ public partial class DungeonSimulation
|
||||
float speed = player.MoveSpeed * 1.2f * TickDelta; // Run faster when feared
|
||||
float newX = player.X + (dx / dist) * speed;
|
||||
float newY = player.Y + (dy / dist) * speed;
|
||||
if (CanMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanMoveTo(player.X, newY)) player.Y = newY;
|
||||
if (CanActorMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanActorMoveTo(player.X, newY)) player.Y = newY;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1674,11 +1786,69 @@ public partial class DungeonSimulation
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanMoveTo(float x, float y)
|
||||
private void TryTriggerCollisionPortal(PlayerInstance player)
|
||||
{
|
||||
int tileX = (int)(x / 32);
|
||||
int tileY = (int)(y / 32);
|
||||
|
||||
if (_portalTeleportCooldownUntilTickByPlayer.TryGetValue(player.CharacterId, out var cooldownUntilTick) &&
|
||||
CurrentTick < cooldownUntilTick)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var portal in Portals.Values)
|
||||
{
|
||||
if (!portal.IsActive || !portal.TriggerOnCollision || !portal.DestinationX.HasValue || !portal.DestinationY.HasValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
float radius = portal.TriggerRadius > 0f ? portal.TriggerRadius : 18f;
|
||||
if (Distance(player.X, player.Y, portal.X, portal.Y) > radius)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!CanActorMoveTo(portal.DestinationX.Value, portal.DestinationY.Value))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
player.X = portal.DestinationX.Value;
|
||||
player.Y = portal.DestinationY.Value;
|
||||
player.InputX = 0f;
|
||||
player.InputY = 0f;
|
||||
_portalTeleportCooldownUntilTickByPlayer[player.CharacterId] = CurrentTick + PortalTeleportCooldownTicks;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanMoveTo(float x, float y, float collisionRadiusPx = 0f)
|
||||
{
|
||||
if (!IsWalkableAtPoint(x, y))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (collisionRadiusPx <= 0f)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
float r = collisionRadiusPx;
|
||||
return IsWalkableAtPoint(x - r, y - r) &&
|
||||
IsWalkableAtPoint(x + r, y - r) &&
|
||||
IsWalkableAtPoint(x - r, y + r) &&
|
||||
IsWalkableAtPoint(x + r, y + r);
|
||||
}
|
||||
|
||||
public bool CanActorMoveTo(float x, float y)
|
||||
{
|
||||
return CanMoveTo(x, y, ActorCollisionRadiusPx);
|
||||
}
|
||||
|
||||
private bool IsWalkableAtPoint(float x, float y)
|
||||
{
|
||||
int tileX = (int)(x / 32f);
|
||||
int tileY = (int)(y / 32f);
|
||||
return Layout.IsWalkable(tileX, tileY);
|
||||
}
|
||||
|
||||
@@ -1696,8 +1866,8 @@ public partial class DungeonSimulation
|
||||
float newX = mob.X + (dx / dist) * moveAmount;
|
||||
float newY = mob.Y + (dy / dist) * moveAmount;
|
||||
|
||||
if (CanMoveTo(newX, mob.Y)) mob.X = newX;
|
||||
if (CanMoveTo(mob.X, newY)) mob.Y = newY;
|
||||
if (CanActorMoveTo(newX, mob.Y)) mob.X = newX;
|
||||
if (CanActorMoveTo(mob.X, newY)) mob.Y = newY;
|
||||
|
||||
mob.Rotation = MathF.Atan2(dy, dx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user