1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using MathHelper; |
---|
7 | |
---|
8 | namespace Rooms |
---|
9 | { |
---|
10 | public class LevelGenerator |
---|
11 | { |
---|
12 | private double width, height; |
---|
13 | private int roomAmountHor, roomAmountVert; |
---|
14 | private TheDungeonGame game; |
---|
15 | private Room[,] rooms; |
---|
16 | private List<Room> roomList = new List<Room>(); |
---|
17 | public int level = 1; |
---|
18 | |
---|
19 | public LevelGenerator(TheDungeonGame game, Vector size, Vector roomamount) |
---|
20 | { |
---|
21 | this.game = game; |
---|
22 | width = size.X; |
---|
23 | height = size.Y; |
---|
24 | roomAmountHor = (int)roomamount.X; |
---|
25 | roomAmountVert = (int)roomamount.Y; |
---|
26 | |
---|
27 | rooms = new Room[roomAmountHor, roomAmountVert]; |
---|
28 | } |
---|
29 | |
---|
30 | public Vector getGridPos(Vector pos) |
---|
31 | { |
---|
32 | double gridPosX = pos.X - width / 2; |
---|
33 | double gridPosY = pos.Y + height / 2; |
---|
34 | |
---|
35 | int x = (int)(gridPosX / TheDungeonGame.ROOMWIDTH); |
---|
36 | int y = (int)(gridPosY / TheDungeonGame.ROOMHEIGHT); |
---|
37 | |
---|
38 | return new Vector(x, y); |
---|
39 | } |
---|
40 | |
---|
41 | public Vector CenterRoom |
---|
42 | { |
---|
43 | get |
---|
44 | { |
---|
45 | return new Vector(roomAmountHor / 2, roomAmountVert / 2); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | public void buildDungeon() |
---|
50 | { |
---|
51 | foreach (Room room in roomList) |
---|
52 | { |
---|
53 | room.initRoom(); |
---|
54 | room.buildLevel(); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | public void destroyDungeon() |
---|
59 | { |
---|
60 | foreach (Room room in roomList) |
---|
61 | { |
---|
62 | room.destroyLevel(); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | public void generateRandomLevel(int minRooms, int maxRooms) |
---|
67 | { |
---|
68 | int roomAmount = RandomGen.NextInt(minRooms, maxRooms); |
---|
69 | Room lastRoom = null; |
---|
70 | |
---|
71 | for (int room = 0; room < roomAmount; room++) |
---|
72 | { |
---|
73 | if (room == 0) |
---|
74 | { |
---|
75 | lastRoom = new TestRoom(game, VecMath.mul(CenterRoom, new Vector(TheDungeonGame.ROOMWIDTH + TheDungeonGame.ROOMTHICKNESS, -TheDungeonGame.ROOMHEIGHT - TheDungeonGame.ROOMTHICKNESS)), TheDungeonGame.roomSize, TheDungeonGame.ROOMTHICKNESS); |
---|
76 | placeRoomAt(lastRoom, (int)CenterRoom.X, (int)CenterRoom.Y); |
---|
77 | continue; |
---|
78 | } |
---|
79 | |
---|
80 | Room randomRoom = getRandomRoom(); |
---|
81 | int checkedSides = 0; |
---|
82 | bool[] sideChecked = new bool[4]; |
---|
83 | |
---|
84 | while (checkedSides < 4) |
---|
85 | { |
---|
86 | int dir = RandomGen.NextInt(4); |
---|
87 | if (sideChecked[dir]) |
---|
88 | { |
---|
89 | checkedSides++; |
---|
90 | continue; |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | checkedSides++; |
---|
95 | sideChecked[dir] = true; |
---|
96 | } |
---|
97 | |
---|
98 | if (tryCreateRoom(dir, VecMath.add(lastRoom.PosOnGrid, RoomDirection.getOffsetFromDir(dir)), lastRoom)) |
---|
99 | { |
---|
100 | lastRoom = currentRoom; |
---|
101 | break; |
---|
102 | } |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | private Room currentRoom; |
---|
112 | private bool tryCreateRoom(int dir, Vector pos, Room lastRoom) |
---|
113 | { |
---|
114 | if (getRoomFromDirection(lastRoom, dir) != null) return false; |
---|
115 | currentRoom = new TestRoom(game, VecMath.mul(pos, new Vector(TheDungeonGame.ROOMWIDTH + TheDungeonGame.ROOMTHICKNESS, -TheDungeonGame.ROOMHEIGHT - TheDungeonGame.ROOMTHICKNESS)), TheDungeonGame.roomSize, TheDungeonGame.ROOMTHICKNESS); |
---|
116 | return placeRoomAt(currentRoom, (int)pos.X, (int)pos.Y); |
---|
117 | } |
---|
118 | |
---|
119 | public bool placeRoomAt(Room room, int x, int y) |
---|
120 | { |
---|
121 | if (!isValidPos(x, y) || getRoomAt(x, y) != null) return false; |
---|
122 | room.PosOnGrid = new Vector(x, y); |
---|
123 | rooms[x, y] = room; |
---|
124 | roomList.Add(room); |
---|
125 | return true; |
---|
126 | } |
---|
127 | |
---|
128 | public Room getRoomAt(int x, int y) |
---|
129 | { |
---|
130 | if (!isValidPos(x, y)) return null; |
---|
131 | return rooms[x, y]; |
---|
132 | } |
---|
133 | |
---|
134 | public Room getRoomAt(Vector pos) |
---|
135 | { |
---|
136 | return getRoomAt((int)pos.X, (int)pos.Y); |
---|
137 | } |
---|
138 | |
---|
139 | public Room getRoomFromDirection(Room source, int dir) |
---|
140 | { |
---|
141 | int roomX = (int)source.PosOnGrid.X; |
---|
142 | int roomY = (int)source.PosOnGrid.Y; |
---|
143 | |
---|
144 | Vector roomPos = VecMath.add(source.PosOnGrid, RoomDirection.getOffsetFromDir(dir)); |
---|
145 | return getRoomAt((int)roomPos.X, (int)roomPos.Y); |
---|
146 | } |
---|
147 | |
---|
148 | public Room getRandomRoom() |
---|
149 | { |
---|
150 | if (roomList.Count == 0) throw new InvalidOperationException("The List is empty!"); |
---|
151 | return roomList[RandomGen.NextInt(roomList.Count)]; |
---|
152 | } |
---|
153 | |
---|
154 | private bool isValidPos(int x, int y) |
---|
155 | { |
---|
156 | return !(x >= roomAmountHor || x < 0 || y >= roomAmountVert || y < 0); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | } |
---|
161 | } |
---|