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

Revision 3660, 6.2 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); // arpotaan huoneiden määrä
80            Room lastRoom = null; // tarvitaan, jotta voidaan laittaa huoneita vierekkäin
81
82            for (int room = 0; room < roomAmount; room++)
83            {
84                if (room == 0) // eka huone on aina aloitushuone
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; // huoneiden tarkistettujen sivujen määrä
93                bool[] sideChecked = new bool[4]; // jokaiselle sivulle boolean, joka kertoo, onko sivu tarkistettu
94
95                while (checkedSides < 4)
96                {
97                    int dir = RandomGen.NextInt(4);
98                    if (sideChecked[dir])
99                    {
100                        checkedSides++; 
101                        continue; // skippaa sivun
102                    }
103                    else
104                    {
105                    //    checkedSides++; // en tiedä, auttaako tämä paljon
106                        sideChecked[dir] = true;
107                    }
108
109                    // me yritämmä luoda huoneen, koska se ei aina onnistu
110                    if (tryCreateRoom(dir, VecMath.add(lastRoom.PosOnGrid, RoomDirection.getOffsetFromDir(dir)), lastRoom)) 
111                    {
112                        lastRoom = currentRoom; //currentRoom löytyy tryCreateRoom:n yläpuolella
113                        roomsPlaced++;
114                        break;
115                    }
116                }
117
118            }
119        }
120
121        private Room currentRoom;
122        private bool tryCreateRoom(int dir, Vector pos, Room lastRoom)
123        {
124            if (getRoomFromDirection(lastRoom, dir) != null) return false;
125
126            if (roomsPlaced == roomAmount-5)
127            {
128                currentRoom = RoomCreator.createRoom(game, VecMath.mul(pos, new Vector(TheDungeonGame.ROOMWIDTH + TheDungeonGame.ROOMTHICKNESS, -TheDungeonGame.ROOMHEIGHT - TheDungeonGame.ROOMTHICKNESS)), TheDungeonGame.roomSize, TheDungeonGame.ROOMTHICKNESS, 7);
129                return placeRoomAt(currentRoom, (int)pos.X, (int)pos.Y);
130            }
131
132            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);
133
134            return placeRoomAt(currentRoom, (int)pos.X, (int)pos.Y);
135        }
136
137        public bool placeRoomAt(Room room, int x, int y)
138        {
139            if (!isValidPos(x, y) || getRoomAt(x, y) != null) return false;
140            room.PosOnGrid = new Vector(x, y);
141            rooms[x, y] = room;
142            roomList.Add(room);
143            return true;
144        }
145
146        public Room getRoomAt(int x, int y)
147        {
148            if (!isValidPos(x, y)) return null;
149            return rooms[x, y];
150        }
151
152        public Room getRoomAt(Vector pos)
153        {
154            return getRoomAt((int)pos.X, (int)pos.Y);
155        }
156
157        public Room getRoomFromDirection(Room source, int dir)
158        {
159            int roomX = (int)source.PosOnGrid.X;
160            int roomY = (int)source.PosOnGrid.Y;
161
162            Vector roomPos = VecMath.add(source.PosOnGrid, RoomDirection.getOffsetFromDir(dir));
163            return getRoomAt((int)roomPos.X, (int)roomPos.Y);
164        }
165
166        public Room getRandomRoom()
167        {
168            if (roomList.Count == 0) throw new InvalidOperationException("The List is empty!");
169            return roomList[RandomGen.NextInt(roomList.Count)];
170        }
171
172        private bool isValidPos(int x, int y)
173        {
174            return !(x >= roomAmountHor || x < 0 || y >= roomAmountVert || y < 0);
175        }
176
177
178    }
179}
Note: See TracBrowser for help on using the repository browser.