diff --git a/DungeonShooting_Godot/src/framework/generate/DoorDirection.cs b/DungeonShooting_Godot/src/framework/generate/DoorDirection.cs new file mode 100644 index 0000000..9f4858d --- /dev/null +++ b/DungeonShooting_Godot/src/framework/generate/DoorDirection.cs @@ -0,0 +1,23 @@ + +/// +/// 房间门的朝向 +/// +public enum DoorDirection +{ + /// + /// 东 + /// + E, + /// + /// 西 + /// + W, + /// + /// 南 + /// + S, + /// + /// 北 + /// + N, +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/generate/GenerateDungeon.cs b/DungeonShooting_Godot/src/framework/generate/GenerateDungeon.cs index 43527f4..cdfcf6a 100644 --- a/DungeonShooting_Godot/src/framework/generate/GenerateDungeon.cs +++ b/DungeonShooting_Godot/src/framework/generate/GenerateDungeon.cs @@ -34,18 +34,7 @@ room.Next.Add(nextRoom); //找门 - if (Mathf.Max(room.Position.x, nextRoom.Position.x) <= Mathf.Min(room.Position.x + room.Size.x, nextRoom.Position.x + nextRoom.Size.x)) //x轴 - { - GD.Print("----1: " + room.Id + ", " + nextRoom.Id + ", = " + (Mathf.Min(room.Position.x + room.Size.x, nextRoom.Position.x + nextRoom.Size.x) - Mathf.Max(room.Position.x, nextRoom.Position.x))); - } - else if (Mathf.Max(room.Position.y, nextRoom.Position.y) <= Mathf.Min(room.Position.y + room.Size.y, nextRoom.Position.y + nextRoom.Size.y)) //y轴 - { - GD.Print("----2: " + room.Id + ", " + nextRoom.Id + ", = " + (Mathf.Min(room.Position.y + room.Size.y, nextRoom.Position.y + nextRoom.Size.y) - Mathf.Max(room.Position.y, nextRoom.Position.y))); - } - else - { - GD.Print("----3: " + room.Id + ", " + nextRoom.Id); - } + FindDoor(room, nextRoom); } } @@ -72,7 +61,6 @@ var room = new RoomInfo(_count); room.Size = new Vector2(Utils.RandRangeInt(25, 60), Utils.RandRangeInt(25, 45)); room.Position = Vector2.Zero; - room.Direction = direction; if (prevRoomInfo != null) //表示这不是第一个房间, 就得判断当前位置下的房间是否被遮挡 { @@ -144,24 +132,97 @@ dirList.Remove(randDir); //找门 - if (Mathf.Max(room.Position.x, nextRoom.Position.x) <= Mathf.Min(room.Position.x + room.Size.x, nextRoom.Position.x + nextRoom.Size.x)) //x轴 - { - GD.Print("----1: " + room.Id + ", " + nextRoom.Id + ", = " + (Mathf.Min(room.Position.x + room.Size.x, nextRoom.Position.x + nextRoom.Size.x) - Mathf.Max(room.Position.x, nextRoom.Position.x))); - } - else if (Mathf.Max(room.Position.y, nextRoom.Position.y) <= Mathf.Min(room.Position.y + room.Size.y, nextRoom.Position.y + nextRoom.Size.y)) //y轴 - { - GD.Print("----2: " + room.Id + ", " + nextRoom.Id + ", = " + (Mathf.Min(room.Position.y + room.Size.y, nextRoom.Position.y + nextRoom.Size.y) - Mathf.Max(room.Position.y, nextRoom.Position.y))); - } - else - { - GD.Print("----3: " + room.Id + ", " + nextRoom.Id); - } + FindDoor(room, nextRoom); } } return room; } + /// + /// 找两个房间的门 + /// + private void FindDoor(RoomInfo room, RoomInfo nextRoom) + { + if (Mathf.Min(room.Position.x + room.Size.x, nextRoom.Position.x + nextRoom.Size.x) - + Mathf.Max(room.Position.x, nextRoom.Position.x) >= 6) //x轴 + { + GD.Print("----1: " + room.Id + ", " + nextRoom.Id); + + //找到重叠区域 + var range = CalcRange(room.Position.x, room.Position.x + room.Size.x, + nextRoom.Position.x, nextRoom.Position.x + nextRoom.Size.x); + var x = Utils.RandRangeInt((int)range.x, (int)range.y - 4); + + //门描述 + var roomDoor = new RoomDoor(); + var nextRoomDoor = new RoomDoor(); + roomDoor.ConnectRoom = nextRoom; + nextRoomDoor.ConnectRoom = room; + + if (room.Position.y < nextRoom.Position.y) //room在上, nextRoom在下 + { + roomDoor.Direction = DoorDirection.S; + nextRoomDoor.Direction = DoorDirection.N; + roomDoor.OriginPosition = new Vector2(x, room.Position.y + room.Size.y); + nextRoomDoor.OriginPosition = new Vector2(x, nextRoom.Position.y); + } + else //room在下, nextRoom在上 + { + roomDoor.Direction = DoorDirection.N; + nextRoomDoor.Direction = DoorDirection.S; + roomDoor.OriginPosition = new Vector2(x, room.Position.y); + nextRoomDoor.OriginPosition = new Vector2(x, nextRoom.Position.y + nextRoom.Size.y); + } + + room.Doors.Add(roomDoor); + nextRoom.Doors.Add(nextRoomDoor); + } + else if (Mathf.Min(room.Position.y + room.Size.y, nextRoom.Position.y + nextRoom.Size.y) - + Mathf.Max(room.Position.y, nextRoom.Position.y) >= 6) //y轴 + { + GD.Print("----2: " + room.Id + ", " + nextRoom.Id); + + //找到重叠区域 + var range = CalcRange(room.Position.y, room.Position.y + room.Size.y, + nextRoom.Position.y, nextRoom.Position.y + nextRoom.Size.y); + var y = Utils.RandRangeInt((int)range.x, (int)range.y - 4); + + //门描述 + var roomDoor = new RoomDoor(); + var nextRoomDoor = new RoomDoor(); + roomDoor.ConnectRoom = nextRoom; + nextRoomDoor.ConnectRoom = room; + + if (room.Position.x < nextRoom.Position.x) //room在左, nextRoom在右 + { + roomDoor.Direction = DoorDirection.E; + nextRoomDoor.Direction = DoorDirection.W; + roomDoor.OriginPosition = new Vector2(room.Position.x + room.Size.x, y); + nextRoomDoor.OriginPosition = new Vector2(nextRoom.Position.x, y); + } + else //room在右, nextRoom在左 + { + roomDoor.Direction = DoorDirection.W; + nextRoomDoor.Direction = DoorDirection.E; + roomDoor.OriginPosition = new Vector2(room.Position.x, y); + nextRoomDoor.OriginPosition = new Vector2(nextRoom.Position.x + nextRoom.Size.x, y); + } + + room.Doors.Add(roomDoor); + nextRoom.Doors.Add(nextRoomDoor); + } + else + { + GD.Print("----3: " + room.Id + ", " + nextRoom.Id); + } + } + + private Vector2 CalcRange(float start1, float end1, float start2, float end2) + { + return new Vector2(Mathf.Max(start1, start2), Mathf.Min(end1, end2)); + } + private int GetReverseDirection(int direction) { switch (direction) diff --git a/DungeonShooting_Godot/src/framework/generate/RoomDoor.cs b/DungeonShooting_Godot/src/framework/generate/RoomDoor.cs index 7b3280e..8657759 100644 --- a/DungeonShooting_Godot/src/framework/generate/RoomDoor.cs +++ b/DungeonShooting_Godot/src/framework/generate/RoomDoor.cs @@ -1,8 +1,23 @@  +using Godot; + /// /// 房间的门 /// public class RoomDoor { - //public RoomInfo Next + /// + /// 所在墙面方向 + /// + public DoorDirection Direction; + + /// + /// 连接的房间 + /// + public RoomInfo ConnectRoom; + + /// + /// 原点坐标 + /// + public Vector2 OriginPosition; } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/generate/RoomInfo.cs b/DungeonShooting_Godot/src/framework/generate/RoomInfo.cs index 17e5555..603e4bd 100644 --- a/DungeonShooting_Godot/src/framework/generate/RoomInfo.cs +++ b/DungeonShooting_Godot/src/framework/generate/RoomInfo.cs @@ -27,13 +27,8 @@ /// /// 门 /// - public List Doors; + public List Doors = new List(); - /// - /// 房间生成时所处方向: 0上, 1右, 2下, 3左 - /// - public int Direction; - public List Next = new List(); public RoomInfo Prev; diff --git a/DungeonShooting_Godot/src/test/TestGenerateDungeon.cs b/DungeonShooting_Godot/src/test/TestGenerateDungeon.cs index 527808b..22c3539 100644 --- a/DungeonShooting_Godot/src/test/TestGenerateDungeon.cs +++ b/DungeonShooting_Godot/src/test/TestGenerateDungeon.cs @@ -33,8 +33,8 @@ public override void _Process(float delta) { //移动相机位置 - var dir = Input.GetVector("move_left", "move_right", "move_up", "move_down"); - _camera.Position += dir * 500 * delta; + var dir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); + _camera.Position += dir * 1000 * delta; Update(); } @@ -46,15 +46,40 @@ } - private void DrawRoomInfo(RoomInfo roomInfo) + private void DrawRoomInfo(RoomInfo room) { - var pos1 = (roomInfo.Position + roomInfo.Size / 2) * _tileMap.CellSize; - foreach (var info in roomInfo.Next) + var cellSize = _tileMap.CellSize; + var pos1 = (room.Position + room.Size / 2) * cellSize; + foreach (var nextRoom in room.Next) { - var pos2 = (info.Position + info.Size / 2) * _tileMap.CellSize; + var pos2 = (nextRoom.Position + nextRoom.Size / 2) * cellSize; DrawLine(pos1, pos2, Colors.Red); - DrawRoomInfo(info); + DrawRoomInfo(nextRoom); } - DrawString(_font, pos1, roomInfo.Id.ToString(), Colors.Yellow); + DrawString(_font, pos1, room.Id.ToString(), Colors.Yellow); + + foreach (var roomDoor in room.Doors) + { + var originPos = roomDoor.OriginPosition * cellSize; + switch (roomDoor.Direction) + { + case DoorDirection.E: + DrawLine(originPos, originPos + new Vector2(3, 0) * cellSize, Colors.Yellow); + DrawLine(originPos + new Vector2(0, 4) * cellSize, originPos + new Vector2(3, 4) * cellSize, Colors.Yellow); + break; + case DoorDirection.W: + DrawLine(originPos, originPos - new Vector2(3, 0) * cellSize, Colors.Yellow); + DrawLine(originPos + new Vector2(0, 4) * cellSize, originPos - new Vector2(3, -4) * cellSize, Colors.Yellow); + break; + case DoorDirection.S: + DrawLine(originPos, originPos + new Vector2(0, 3) * cellSize, Colors.Yellow); + DrawLine(originPos + new Vector2(4, 0) * cellSize, originPos + new Vector2(4, 3) * cellSize, Colors.Yellow); + break; + case DoorDirection.N: + DrawLine(originPos, originPos - new Vector2(0, 3) * cellSize, Colors.Yellow); + DrawLine(originPos + new Vector2(4, 0) * cellSize, originPos - new Vector2(-4, 3) * cellSize, Colors.Yellow); + break; + } + } } } \ No newline at end of file