source: 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/LevelGenerator.cs @ 3596

Revision 3596, 5.8 KB checked in by dezhidki, 11 years ago (diff)

Talletus.

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