diff --git a/scenes/game.tscn b/scenes/game.tscn index 5a000bd..b4f4b30 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -72,3 +72,6 @@ script = ExtResource("5_dis4v") [node name="DamageNumberContainer" type="Node2D" parent="."] [node name="WeaponContainer" type="Node2D" parent="."] + +[node name="Camera2D" type="Camera2D" parent="."] +zoom = Vector2(0.5, 0.5) diff --git a/scripts/loader/sceneLoader/GameSceneLoader.cs b/scripts/loader/sceneLoader/GameSceneLoader.cs index 3d4969b..6179586 100644 --- a/scripts/loader/sceneLoader/GameSceneLoader.cs +++ b/scripts/loader/sceneLoader/GameSceneLoader.cs @@ -34,7 +34,10 @@ public partial class GameSceneLoader : SceneLoaderTemplate MapGenerator.LayoutStrategy = new TestLayoutStrategy(); MapGenerator.LayoutParsingStrategy = new SequenceLayoutParsingStrategy(); MapGenerator.RoomPlacementStrategy = new PatchworkRoomPlacementStrategy(); - MapGenerator.Seed = GuidUtils.GetGuid(); + //Test the seeds used + //2531276826 Right-Down和Left-Down匹配成功 + //4208831693 Left-Down和Right-Up匹配成功 + MapGenerator.Seed = "2531276826"; await MapGenerator.GenerateMap(); } } \ No newline at end of file diff --git a/scripts/map/interfaces/IRoomSlotsMatcher.cs b/scripts/map/interfaces/IRoomSlotsMatcher.cs deleted file mode 100644 index 29b0140..0000000 --- a/scripts/map/interfaces/IRoomSlotsMatcher.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Threading.Tasks; -using ColdMint.scripts.map.dateBean; -using ColdMint.scripts.map.room; - -namespace ColdMint.scripts.map.interfaces; - -/// -/// IRoomSlotsMatcher -/// 房间插槽匹配器 -/// -public interface IRoomSlotsMatcher -{ - /// - /// Whether the slots of the two rooms can be matched - /// 两个房间的插槽是否可匹配 - /// - /// - /// - /// - Task IsMatch(Room? mainRoom, Room newRoom); - - - /// - /// LastMatchedMainSlot - /// 最后匹配的主要插槽 - /// - RoomSlot? LastMatchedMainSlot { get; } - - /// - /// LastMatchedMinorSlot - /// 最后匹配的次要插槽 - /// - RoomSlot? LastMatchedMinorSlot { get; } -} \ No newline at end of file diff --git a/scripts/map/room/Room.cs b/scripts/map/room/Room.cs index 445d716..dd9c153 100644 --- a/scripts/map/room/Room.cs +++ b/scripts/map/room/Room.cs @@ -85,7 +85,7 @@ public class Room var midpoint = roomAreaCollisionShape2D.Position + roomAreaRect2.Position + roomAreaRect2.Size / 2; //endregion var roomSlots = new List(); - for (int i = 0; i < slotCount; i++) + for (var i = 0; i < slotCount; i++) { //拿到了房间卡槽对象 var area2D = slotList.GetChild(i); @@ -107,11 +107,13 @@ public class Room //转为瓦片地图的坐标(中点) var tileMapStartPosition = tileMap.LocalToMap(startPosition); var tileMapEndPosition = tileMap.LocalToMap(endPosition); - var roomSlot = new RoomSlot(); - roomSlot.EndPosition = tileMapEndPosition; - roomSlot.StartPosition = tileMapStartPosition; - //计算槽位的方向(房间中点为原点,指向槽位中点的向量) - roomSlot.DistanceToMidpointOfRoom = CoordinateUtils.VectorToOrientationArray(midpoint, midpointOfRoomSlots); + var roomSlot = new RoomSlot + { + EndPosition = tileMapEndPosition, + StartPosition = tileMapStartPosition, + //计算槽位的方向(房间中点为原点,指向槽位中点的向量) + DistanceToMidpointOfRoom = CoordinateUtils.VectorToOrientationArray(midpoint, midpointOfRoomSlots) + }; roomSlots.Add(roomSlot); } diff --git a/scripts/map/roomPlacer/PatchworkRoomPlacementStrategy.cs b/scripts/map/roomPlacer/PatchworkRoomPlacementStrategy.cs index 9b6e846..0f2e7de 100644 --- a/scripts/map/roomPlacer/PatchworkRoomPlacementStrategy.cs +++ b/scripts/map/roomPlacer/PatchworkRoomPlacementStrategy.cs @@ -1,8 +1,10 @@ using System.Threading.Tasks; +using ColdMint.scripts.debug; using ColdMint.scripts.levelGraphEditor; using ColdMint.scripts.map.dateBean; using ColdMint.scripts.map.interfaces; using ColdMint.scripts.map.room; +using ColdMint.scripts.utils; using Godot; namespace ColdMint.scripts.map.RoomPlacer; @@ -50,8 +52,48 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy { return Task.FromResult(null); } - // var roomResArray = RoomFactory.RoomTemplateSetToRoomRes(newRoomNodeData.RoomTemplateSet); - //TODO:在这里实现房间的放置策略。 + + var roomResArray = RoomFactory.RoomTemplateSetToRoomRes(newRoomNodeData.RoomTemplateSet); + if (roomResArray.Length == 0) + { + return Task.FromResult(null); + } + + var roomSlots = parentRoomNode.RoomSlots; + if (roomSlots == null || roomSlots.Length == 0) + { + return Task.FromResult(null); + } + + //Matches unmatched slots. + //对未匹配的插槽进行匹配。 + foreach (var roomRes in roomResArray) + { + var newRoom = RoomFactory.CreateRoom(roomRes); + if (newRoom == null) + { + continue; + } + + //Create a room, try to use the room slot to match the existing room slot. + //创建了一个房间,尝试使用房间的槽与现有的房间槽匹配。 + if (!IsMatch(parentRoomNode, newRoom, out var mainRoomSlot, out var newRoomSlot).Result) continue; + if (mainRoomSlot == null || newRoomSlot == null) + { + continue; + } + + var position = CalculatedPosition(parentRoomNode, newRoom, mainRoomSlot, newRoomSlot, true) + .Result; + if (position == null) continue; + var roomPlacementData = new RoomPlacementData + { + Room = newRoom, + Position = position + }; + return Task.FromResult(roomPlacementData); + } + return Task.FromResult(null); } @@ -79,32 +121,152 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy } + /// + /// if it matches + /// 是否匹配 + /// + /// + /// + /// + public Task IsMatch(Room? mainRoom, Room newRoom, out RoomSlot? outMainRoomSlot, out RoomSlot? outNewRoomSlot) + { + if (mainRoom == null) + { + outNewRoomSlot = null; + outMainRoomSlot = null; + return Task.FromResult(false); + } + + var roomSlots = mainRoom.RoomSlots; + if (roomSlots == null) + { + outNewRoomSlot = null; + outMainRoomSlot = null; + return Task.FromResult(false); + } + + var newRoomSlots = newRoom.RoomSlots; + if (newRoomSlots == null) + { + outNewRoomSlot = null; + outMainRoomSlot = null; + return Task.FromResult(false); + } + + foreach (var mainRoomSlot in roomSlots) + { + if (mainRoomSlot == null || mainRoomSlot.Matched) + { + //如果已经匹配过了,就不再匹配 + continue; + } + + foreach (var newRoomSlot in newRoomSlots) + { + if (newRoomSlot == null) + { + continue; + } + + if (newRoomSlot.Matched) + { + //如果已经匹配过了,就不再匹配 + continue; + } + + if (mainRoomSlot.IsHorizontal != newRoomSlot.IsHorizontal) + { + continue; + } + + if (mainRoomSlot.Length != newRoomSlot.Length) + { + continue; + } + + var distanceToMidpointOfRoom = mainRoomSlot.DistanceToMidpointOfRoom; + var newDistanceToMidpointOfRoom = newRoomSlot.DistanceToMidpointOfRoom; + if (distanceToMidpointOfRoom == null || newDistanceToMidpointOfRoom == null) + { + continue; + } + + if (distanceToMidpointOfRoom[0] == newDistanceToMidpointOfRoom[0] && + distanceToMidpointOfRoom[1] == newDistanceToMidpointOfRoom[1]) + { + continue; + } + + LogCat.Log(distanceToMidpointOfRoom[0] + "-" + distanceToMidpointOfRoom[1] + "和" + + newDistanceToMidpointOfRoom[0] + "-" + newDistanceToMidpointOfRoom[1] + "匹配成功"); + mainRoomSlot.Matched = true; + newRoomSlot.Matched = true; + outMainRoomSlot = mainRoomSlot; + outNewRoomSlot = newRoomSlot; + return Task.FromResult(true); + } + } + + outNewRoomSlot = null; + outMainRoomSlot = null; + return Task.FromResult(false); + } + private Task CalculatedPosition(Room mainRoom, Room newRoom, RoomSlot? mainRoomSlot, RoomSlot? newRoomSlot, bool roomSlotOverlap) { - if (mainRoom.RootNode == null || mainRoom.TileMap == null || newRoom.TileMap == null || mainRoomSlot == null || + if (mainRoom.RootNode == null || newRoom.RootNode == null || newRoom.TileMap == null || + mainRoom.TileMap == null || + newRoom.TileMap == null || mainRoomSlot == null || newRoomSlot == null) { return Task.FromResult(null); } - //计算主插槽中点在世界中的位置。 - //mainRoom.RootNode.Position意为房间所在的世界位置 - //mainRoom.TileMap.MapToLocal(mainRoomSlot.StartPosition)意为主插槽在房间中的位置 - var result = mainRoom.RootNode.Position + mainRoom.TileMap.MapToLocal(mainRoomSlot.StartPosition); - if (roomSlotOverlap) + //Main room slot location description + //主房间槽位置描述 + var mainOrientationDescribe = mainRoomSlot.DistanceToMidpointOfRoom; + //New room slot location description + //新房间槽位置描述 + var newOrientationDescribe = newRoomSlot.DistanceToMidpointOfRoom; + if (mainOrientationDescribe == null || newOrientationDescribe == null) { - //执行减法,从槽中点偏移到左上角 - result -= _halfCell; + //If the room slot is described as null, null is returned + //若房间槽描述为null,那么返回null + return Task.FromResult(null); + } + + Vector2 result; + if (mainOrientationDescribe[0] == CoordinateUtils.OrientationDescribe.Left && + newOrientationDescribe[0] == CoordinateUtils.OrientationDescribe.Right) + { + //Move left to new room. + //左移新房间。 + var mainSlotPosition = mainRoom.RootNode.Position + mainRoom.TileMap.MapToLocal(mainRoomSlot.StartPosition); + var newSlotPosition = newRoom.RootNode.Position + newRoom.TileMap.MapToLocal(newRoomSlot.StartPosition); + result = mainSlotPosition + + newRoom.TileMap.Position - newRoom.TileMap.MapToLocal(newRoomSlot.StartPosition); + //Modified y height + //修正y高度 + result.Y -= newSlotPosition.Y - mainSlotPosition.Y; + //If the room slots don't overlap + //如果房间槽不能重叠 + if (!roomSlotOverlap) + { + result.X -= Config.CellSize; + } } else { - //执行减法,从槽中点偏移到右下角 - result += _halfCell; + var mainSlotPosition = mainRoom.RootNode.Position + mainRoom.TileMap.MapToLocal(mainRoomSlot.StartPosition); + var newSlotPosition = newRoom.RootNode.Position + newRoom.TileMap.MapToLocal(newRoomSlot.StartPosition); + result = mainSlotPosition; + // result.Y += newSlotPosition.Y - mainSlotPosition.Y; } - //我们不能将新房间的原点设置在主房间槽的左上角或右下角,这会导致插槽不对应。 - //竖直槽,我们需要在同一水平上。 + + //We need to be on the same level. + //我们需要在同一水平上。 if (mainRoomSlot.IsHorizontal) { result += newRoom.TileMap.MapToLocal(new Vector2I(newRoomSlot.EndPosition.X, 0)) - _halfCell; diff --git a/scripts/map/slotsMatcher/RoomSlotsMatcher.cs b/scripts/map/slotsMatcher/RoomSlotsMatcher.cs deleted file mode 100644 index 910165d..0000000 --- a/scripts/map/slotsMatcher/RoomSlotsMatcher.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Threading.Tasks; -using ColdMint.scripts.map.dateBean; -using ColdMint.scripts.map.interfaces; -using ColdMint.scripts.map.room; - -namespace ColdMint.scripts.map.slotsMatcher; - -public class RoomSlotsMatcher : IRoomSlotsMatcher -{ - private RoomSlot? _lastMatchedMainSlot; - private RoomSlot? _lastMatchedMinorSlot; - - public Task IsMatch(Room? mainRoom, Room newRoom) - { - if (mainRoom == null) - { - return Task.FromResult(false); - } - - var roomSlots = mainRoom.RoomSlots; - if (roomSlots == null) - { - return Task.FromResult(false); - } - - var newRoomSlots = newRoom.RoomSlots; - if (newRoomSlots == null) - { - return Task.FromResult(false); - } - - foreach (var mainRoomSlot in roomSlots) - { - if (mainRoomSlot == null || mainRoomSlot.Matched) - { - //如果已经匹配过了,就不再匹配 - continue; - } - - foreach (var newRoomSlot in newRoomSlots) - { - if (newRoomSlot == null) - { - continue; - } - - if (newRoomSlot.Matched) - { - //如果已经匹配过了,就不再匹配 - continue; - } - - if (mainRoomSlot.IsHorizontal != newRoomSlot.IsHorizontal) - { - continue; - } - - if (mainRoomSlot.Length != newRoomSlot.Length) - { - continue; - } - - var distanceToMidpointOfRoom = mainRoomSlot.DistanceToMidpointOfRoom; - var newDistanceToMidpointOfRoom = newRoomSlot.DistanceToMidpointOfRoom; - if (distanceToMidpointOfRoom == null || newDistanceToMidpointOfRoom == null) - { - continue; - } - - if (distanceToMidpointOfRoom[0] == newDistanceToMidpointOfRoom[0] && - distanceToMidpointOfRoom[1] == newDistanceToMidpointOfRoom[1]) - { - continue; - } - - mainRoomSlot.Matched = true; - newRoomSlot.Matched = true; - _lastMatchedMainSlot = mainRoomSlot; - _lastMatchedMinorSlot = newRoomSlot; - return Task.FromResult(true); - } - } - - return Task.FromResult(false); - } - - public RoomSlot? LastMatchedMainSlot => _lastMatchedMainSlot; - public RoomSlot? LastMatchedMinorSlot => _lastMatchedMinorSlot; -} \ No newline at end of file