Connect room gaps using NavigationLink2D.

使用NavigationLink2D连接房间间隙。
This commit is contained in:
Cold-Mint 2024-07-19 22:25:55 +08:00
parent 9401913779
commit 36e656cf46
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
5 changed files with 92 additions and 21 deletions

View File

@ -317,7 +317,7 @@ public static class MapGenerator
//The input parameters are incomplete.
//输入参数不全。
if (_roomPlacementStrategy == null || _mapRoot == null || string.IsNullOrEmpty(roomNodeDataId) ||
roomPlacementData.Room == null)
roomPlacementData.NewRoom == null)
{
return false;
}
@ -330,11 +330,12 @@ public static class MapGenerator
if (!await _roomPlacementStrategy.PlaceRoom(_mapRoot, roomPlacementData))
{
LogCat.LogWarningWithFormat("room_placement_failed", LogCat.UploadFormat,LogCat.LogLabel.Default, roomNodeDataId);
LogCat.LogWarningWithFormat("room_placement_failed", LogCat.UploadFormat, LogCat.LogLabel.Default,
roomNodeDataId);
return false;
}
dictionary.Add(roomNodeDataId, roomPlacementData.Room);
dictionary.Add(roomNodeDataId, roomPlacementData.NewRoom);
LogCat.LogWithFormat("room_placement_information", LogCat.LogLabel.Default, LogCat.UploadFormat, roomNodeDataId,
roomPlacementData.Position.ToString());
return true;

View File

@ -14,11 +14,18 @@ public class RoomPlacementData
/// <para>放置的位置</para>
/// </summary>
public Vector2? Position { get; set; }
/// <summary>
/// <para>Place the room template</para>
/// <para>放置的房间模板</para>
/// </summary>
public Room? Room { get; set; }
public Room? NewRoom { get; set; }
/// <summary>
/// <para>Parent room</para>
/// <para>父级房间</para>
/// </summary>
public Room? ParentRoom { get; set; }
/// <summary>
/// <para>Parent room slot</para>

View File

@ -16,8 +16,34 @@ public class RoomSlot
/// </summary>
public bool Matched { get; set; }
/// <summary>
/// <para>The starting position of the room slot</para>
/// <para>房间插槽的开始位置</para>
/// </summary>
///<remarks>
///<para>As opposed to a tile map. Convert to local location please call <see cref="TileMap.MapToLocal"/></para>
///<para>相对于瓦片地图而言的。转换为本地位置请调用<see cref="TileMap.MapToLocal"/></para>
/// </remarks>
public Vector2I StartPosition { get; set; }
/// <summary>
/// <para>The midpoint of the slot</para>
/// <para>插槽的中点位置</para>
/// </summary>
///<remarks>
///<para>As opposed to a tile map. Convert to local location please call <see cref="TileMap.MapToLocal"/></para>
///<para>相对于瓦片地图而言的。转换为本地位置请调用<see cref="TileMap.MapToLocal"/></para>
/// </remarks>
public Vector2I MidpointPosition { get; set; }
/// <summary>
/// <para>The end position of the room slot</para>
/// <para>房间插槽的结束位置</para>
/// </summary>
///<remarks>
///<para>As opposed to a tile map. Convert to local location please call <see cref="TileMap.MapToLocal"/></para>
///<para>相对于瓦片地图而言的。转换为本地位置请调用<see cref="TileMap.MapToLocal"/></para>
/// </remarks>
public Vector2I EndPosition { get; set; }
/// <summary>

View File

@ -194,10 +194,12 @@ public class Room
//转为瓦片地图的坐标(中点)
var tileMapStartPosition = tileMap.LocalToMap(startPosition);
var tileMapEndPosition = tileMap.LocalToMap(endPosition);
var midpointPosition = tileMap.LocalToMap(midpointOfRoomSlots);
var roomSlot = new RoomSlot
{
EndPosition = tileMapEndPosition,
StartPosition = tileMapStartPosition,
MidpointPosition = midpointPosition,
//Calculate the orientation of the slot (the midpoint of the room is the origin, the vector pointing to the midpoint of the slot)
//计算槽位的方向(房间中点为原点,指向槽位中点的向量)
DistanceToMidpointOfRoom = CoordinateUtils.VectorToOrientationArray(midpoint, midpointOfRoomSlots)

View File

@ -98,19 +98,53 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy
public Task<bool> PlaceRoom(Node mapRoot, RoomPlacementData roomPlacementData)
{
if (roomPlacementData.Room == null || roomPlacementData.Position == null)
if (roomPlacementData.NewRoom == null || roomPlacementData.Position == null)
{
return Task.FromResult(false);
}
if (roomPlacementData.Room.RootNode == null)
if (roomPlacementData.NewRoom.RootNode == null)
{
return Task.FromResult(false);
}
var rootNode = roomPlacementData.Room.RootNode;
mapRoot.AddChild(rootNode);
rootNode.Position = roomPlacementData.Position.Value;
var newRootRootNode = roomPlacementData.NewRoom.RootNode;
mapRoot.AddChild(newRootRootNode);
newRootRootNode.Position = roomPlacementData.Position.Value;
//Place navigation Link
//放置导航Link
Vector2? navigationLink2DStartPosition = null;
if (roomPlacementData is { ParentRoom: not null, ParentRoomSlot: not null })
{
var parentRoomTileMap = roomPlacementData.ParentRoom.TileMap;
var parentRoomRootNode = roomPlacementData.ParentRoom.RootNode;
if (parentRoomTileMap != null && parentRoomRootNode != null)
{
navigationLink2DStartPosition = parentRoomRootNode.Position +
parentRoomTileMap.MapToLocal(roomPlacementData.ParentRoomSlot
.EndPosition);
}
}
Vector2? navigationLink2DEndPosition = null;
if (roomPlacementData.NewRoomSlot != null)
{
var newRoomTileMap = roomPlacementData.NewRoom.TileMap;
if (newRoomTileMap != null)
{
navigationLink2DEndPosition = newRootRootNode.Position +
newRoomTileMap.MapToLocal(roomPlacementData.NewRoomSlot.EndPosition);
}
}
if (navigationLink2DStartPosition != null && navigationLink2DEndPosition != null)
{
var navigationLink2D = new NavigationLink2D();
navigationLink2D.StartPosition = navigationLink2DStartPosition.Value;
navigationLink2D.EndPosition = navigationLink2DEndPosition.Value;
mapRoot.AddChild(navigationLink2D);
}
return Task.FromResult(true);
}
@ -142,7 +176,7 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy
//Saves all data in the room template that matches the parent room.
//保存房间模板内所有与父房间匹配的数据。
var useableRoomPlacementData = new List<RoomPlacementData>();
var usableRoomPlacementData = new List<RoomPlacementData>();
foreach (var roomRes in roomResArray)
{
var newRoom = RoomFactory.CreateRoom(roomRes, newRoomNodeData.EnterRoomEventHandlerId,
@ -168,22 +202,23 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy
if (position == null) continue;
var roomPlacementData = new RoomPlacementData
{
Room = newRoom,
NewRoom = newRoom,
ParentRoom = parentRoomNode,
Position = position,
ParentRoomSlot = mainRoomSlot,
NewRoomSlot = newRoomSlot
};
useableRoomPlacementData.Add(roomPlacementData);
usableRoomPlacementData.Add(roomPlacementData);
}
if (useableRoomPlacementData.Count == 0)
if (usableRoomPlacementData.Count == 0)
{
return null;
}
else
{
var index = randomNumberGenerator.Randi() % useableRoomPlacementData.Count;
var roomPlacementData = useableRoomPlacementData[(int)index];
var index = randomNumberGenerator.Randi() % usableRoomPlacementData.Count;
var roomPlacementData = usableRoomPlacementData[(int)index];
//Be sure to mark its slot as a match when you use it.
//一定要在使用时,将其插槽标记为匹配。
if (roomPlacementData.ParentRoomSlot != null)
@ -217,7 +252,7 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy
var index = randomNumberGenerator.Randi() % roomResArray.Length;
var roomPlacementData = new RoomPlacementData
{
Room = RoomFactory.CreateRoom(roomResArray[index], startRoomNodeData.EnterRoomEventHandlerId,
NewRoom = RoomFactory.CreateRoom(roomResArray[index], startRoomNodeData.EnterRoomEventHandlerId,
startRoomNodeData.ExitRoomEventHandlerId),
Position = Vector2.Zero
};