summary refs log tree commit diff stats
path: root/gamestate.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2009-06-19 10:57:53 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2009-06-19 10:57:53 -0400
commit684a27ce13a9a08f4f1d94621cdab1232d2d33cf (patch)
tree4b85d42802b588767b6ac7b6972f2093154c7196 /gamestate.cpp
parent1e3808b3338ff3b1499e000ce1f23e87d6050b8c (diff)
downloadmazeoflife-684a27ce13a9a08f4f1d94621cdab1232d2d33cf.tar.gz
mazeoflife-684a27ce13a9a08f4f1d94621cdab1232d2d33cf.tar.bz2
mazeoflife-684a27ce13a9a08f4f1d94621cdab1232d2d33cf.zip
Added GNU configure stuff
Diffstat (limited to 'gamestate.cpp')
-rw-r--r--gamestate.cpp273
1 files changed, 0 insertions, 273 deletions
diff --git a/gamestate.cpp b/gamestate.cpp deleted file mode 100644 index 39d589d..0000000 --- a/gamestate.cpp +++ /dev/null
@@ -1,273 +0,0 @@
1#include "includes.h"
2
3GameState::GameState()
4{
5 player_color = getColor(255, 255, 0);
6 event_color = getColor(0, 0, 255);
7
8 newGame = false;
9
10 info.playerx = 1;
11 info.playery = 1;
12 info.level = Level();
13 info.doneMaking = false;
14 board = Board(&info);
15
16 SDL_WM_SetCaption("Maze Of Life - Level 1", NULL);
17}
18
19void GameState::input(SDLKey key)
20{
21 if (info.doneMaking)
22 {
23 switch (key)
24 {
25 case SDLK_LEFT:
26 move(info.playerx-1, info.playery);
27
28 break;
29 case SDLK_RIGHT:
30 move(info.playerx+1, info.playery);
31
32 break;
33 case SDLK_UP:
34 move(info.playerx, info.playery-1);
35
36 break;
37 case SDLK_DOWN:
38 move(info.playerx, info.playery+1);
39
40 break;
41 case SDLK_ESCAPE:
42 newGame = false;
43
44 info.playerx = 1;
45 info.playery = 1;
46 info.level = Level();
47 info.doneMaking = false;
48 board = Board(&info);
49
50 SDL_WM_SetCaption("Maze Of Life - Level 1", NULL);
51
52 break;
53 }
54 }
55}
56
57void GameState::tick()
58{
59 if (newGame)
60 {
61 switch (rand()%4)
62 {
63 case 0: info.playerx = 1; info.playery = 1; break;
64 case 1: info.playerx = 1; info.playery = HEIGHT-2; break;
65 case 2: info.playerx = WIDTH-2; info.playery = HEIGHT-2; break;
66 case 3: info.playerx = WIDTH-2; info.playery = 1; break;
67 }
68
69 info.level.incrementLevel();
70 info.doneMaking = false;
71 board = Board(&info);
72 newGame = false;
73
74 char title[32];
75 sprintf(title, "Maze Of Life - Level %d", info.level.getLevel());
76 SDL_WM_SetCaption(title, NULL);
77 }
78
79 board.tick();
80}
81
82void GameState::move(int x, int y)
83{
84 wrap(&x, &y);
85
86 if (board.isObstructed(x,y)) return;
87 if ((x==15)&&(y==15)) newGame = true;
88
89 info.playerx = x;
90 info.playery = y;
91}
92
93void GameState::render(SDL_Surface* screen)
94{
95 // Paint maze
96 board.render(screen);
97
98 // Paint player
99 SDL_Rect block;
100 block.x = info.playerx*16;
101 block.y = info.playery*16;
102 block.w = 16;
103 block.h = 16;
104
105 SDL_FillRect(screen, &block, player_color);
106
107 // Paint event
108 block.x = 15*16;
109 block.y = 15*16;
110
111 SDL_FillRect(screen, &block, event_color);
112}
113
114GameState::Level::Level()
115{
116 level = 1;
117
118 alive[0] = getColor(0, 0, 0); // Black
119 alive[1] = getColor(255, 0, 0); // Red
120 alive[2] = getColor(0, 255, 0); // Green
121 alive[3] = getColor(85, 85, 85); // Dark Gray
122 alive[4] = getColor(255, 0, 255); // Magenta
123
124 dead[0] = getColor(255, 255, 255); // White
125 dead[1] = getColor(255, 192, 203); // Pink
126 dead[2] = getColor(0, 255, 255); // Cyan
127 dead[3] = getColor(170, 170, 170); // Light Gray
128 dead[4] = getColor(255, 128, 0); // Orange
129}
130
131int GameState::Level::getLevel()
132{
133 return level;
134}
135
136bool GameState::Level::checkSquare(int x, int y)
137{
138 switch (level/10+1)
139 {
140 case 1:
141 return ((x>13)&&(x<16)&&(y>13)&&(y<16));
142 case 2:
143 return ((x>13)&&(x<17)&&(y>13)&&(y<17));
144 case 3:
145 case 4:
146 return ((x>12)&&(x<18)&&(y>12)&&(y<18));
147 case 5:
148 case 6:
149 return ((x>11)&&(x<19)&&(y>11)&&(y<19));
150 default:
151 return true;
152 }
153}
154
155Uint32 GameState::Level::getAliveColor()
156{
157 return alive[(level/10)%5];
158}
159
160Uint32 GameState::Level::getDeadColor()
161{
162 return dead[(level/10)%5];
163}
164
165void GameState::Level::incrementLevel()
166{
167 level++;
168}
169
170GameState::Board::Board()
171{
172 GameState::Board::Board(new GameState::Info());
173}
174
175GameState::Board::Board(GameState::Info* info)
176{
177 this->info = info;
178
179 int x,y;
180 for (y=0;y<HEIGHT;y++)
181 {
182 for (x=0;x<WIDTH;x++)
183 {
184 if (info->level.checkSquare(x, y))
185 {
186 blocks[x][y] = rand() % 2;
187 } else {
188 blocks[x][y] = false;
189 }
190 }
191 }
192
193 blocks[15][15] = false;
194}
195
196bool GameState::Board::isObstructed(int x, int y)
197{
198 return blocks[x][y];
199}
200
201void GameState::Board::render(SDL_Surface* screen)
202{
203 SDL_Rect block;
204 block.w = 16;
205 block.h = 16;
206
207 int x,y;
208
209 for (y=0;y<HEIGHT;y++)
210 {
211 for (x=0;x<WIDTH;x++)
212 {
213 block.x = x*16;
214 block.y = y*16;
215
216 SDL_FillRect(screen, &block, (blocks[x][y] ? info->level.getAliveColor() : info->level.getDeadColor()));
217 }
218 }
219}
220
221void GameState::Board::tick()
222{
223 bool temp[WIDTH][HEIGHT];
224 int x,y;
225 for (x=0;x<WIDTH;x++)
226 {
227 for (y=0;y<HEIGHT;y++)
228 {
229 temp[x][y] = blocks[x][y];
230 }
231 }
232
233 for (x=0;x<WIDTH;x++)
234 {
235 for (y=0;y<HEIGHT;y++)
236 {
237 if ((x==15)&&(y==15))
238 {
239 continue;
240 }
241
242 int neighbors = 0;
243
244 if ((x>0)&&(y>0)) incrementIfNeighbor(x-1,y-1,temp,&neighbors);
245 if ((x>0)) incrementIfNeighbor(x-1,y,temp,&neighbors);
246 if ((x>0)&&(y<HEIGHT-1)) incrementIfNeighbor(x-1,y+1,temp,&neighbors);
247 if ((y>0)) incrementIfNeighbor(x,y-1,temp,&neighbors);
248 if ((y<HEIGHT-1)) incrementIfNeighbor(x,y+1,temp,&neighbors);
249 if ((x<WIDTH-1)&&(y>0)) incrementIfNeighbor(x+1,y-1,temp,&neighbors);
250 if ((x<WIDTH-1)) incrementIfNeighbor(x+1,y,temp,&neighbors);
251 if ((x<WIDTH-1)&&(y<HEIGHT-1)) incrementIfNeighbor(x+1,y+1,temp,&neighbors);
252
253 if (temp[x][y])
254 {
255 blocks[x][y] = ((neighbors >= 1) && (neighbors <= 4));
256 } else {
257 blocks[x][y] = (neighbors == 3);
258 }
259 }
260 }
261
262 if (!info->doneMaking && ++gens > 100) info->doneMaking = true;
263}
264
265void GameState::Board::incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick)
266{
267 wrap(&x, &y);
268
269 if ((temp[x][y])||((info->playerx==x)&&(info->playery==y))||((x==15)&&(y==15)))
270 {
271 ++*tick;
272 }
273}