summary refs log tree commit diff stats
path: root/gamestate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gamestate.cpp')
-rw-r--r--gamestate.cpp317
1 files changed, 0 insertions, 317 deletions
diff --git a/gamestate.cpp b/gamestate.cpp deleted file mode 100644 index fcf865f..0000000 --- a/gamestate.cpp +++ /dev/null
@@ -1,317 +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 = new Info();
11 info->playerx = 1;
12 info->playery = 1;
13 info->level = Level();
14 info->doneMaking = false;
15 board = Board(info);
16}
17
18void GameState::input(SDL_keysym key)
19{
20 if (info->doneMaking)
21 {
22 switch (key.sym)
23 {
24 case SDLK_LEFT:
25 move(info->playerx-1, info->playery);
26
27 break;
28 case SDLK_RIGHT:
29 move(info->playerx+1, info->playery);
30
31 break;
32 case SDLK_UP:
33 move(info->playerx, info->playery-1);
34
35 break;
36 case SDLK_DOWN:
37 move(info->playerx, info->playery+1);
38
39 break;
40 case SDLK_ESCAPE:
41 std::ifstream exists(getDataFile());
42 if (exists)
43 {
44 FILE* hslist = fopen(getDataFile(), "r");
45 int scores;
46 Highscore* h;
47
48 fscanf(hslist, "%d%*c", &scores);
49
50 if (scores < 10)
51 {
52 fclose(hslist);
53
54 changeState(new NewHighscoreState(info->level.getLevel()));
55 } else {
56 for (int i=0; i<scores; i++)
57 {
58 int namelen;
59 char namelens[4];
60 char* name = (char*) calloc(25, sizeof(char));
61 int score;
62
63 fscanf(hslist, "%d", &namelen);
64 sprintf(namelens, "%%%dc", namelen);
65 fscanf(hslist, namelens, name);
66 fscanf(hslist, "%d%*c", &score);
67
68 h = new Highscore(name, score);
69 }
70
71 fclose(hslist);
72
73 if (h->getLevel() < info->level.getLevel())
74 {
75 changeState(new NewHighscoreState(info->level.getLevel()));
76 } else {
77 changeState(new LocalHighscoreListState(true));
78 }
79 }
80 } else {
81 changeState(new NewHighscoreState(info->level.getLevel()));
82 }
83
84 break;
85 }
86 }
87}
88
89void GameState::tick()
90{
91 if (newGame)
92 {
93 switch (rand()%4)
94 {
95 case 0: info->playerx = 1; info->playery = 1; break;
96 case 1: info->playerx = 1; info->playery = HEIGHT-2; break;
97 case 2: info->playerx = WIDTH-2; info->playery = HEIGHT-2; break;
98 case 3: info->playerx = WIDTH-2; info->playery = 1; break;
99 }
100
101 info->level.incrementLevel();
102 info->doneMaking = false;
103 info->gens = 0;
104 board = Board(info);
105 newGame = false;
106 }
107
108 board.tick();
109}
110
111void GameState::move(int x, int y)
112{
113 wrap(&x, &y);
114
115 if (board.isObstructed(x,y)) return;
116 if ((x==15)&&(y==15)) newGame = true;
117
118 info->playerx = x;
119 info->playery = y;
120}
121
122void GameState::render(SDL_Surface* screen)
123{
124 // Paint maze
125 board.render(screen);
126
127 // Paint event
128 SDL_Rect block;
129 block.x = 15*16;
130 block.y = 15*16;
131 block.w = 16;
132 block.h = 16;
133
134 SDL_FillRect(screen, &block, event_color);
135
136 if (!info->doneMaking)
137 {
138 SDL_Color fontColor = {0, 0, 0, 0};
139 char levelnum[8];
140 sprintf(levelnum, "%d", info->level.getLevel());
141 SDL_Surface* title = TTF_RenderText_Solid(loadFont(100), levelnum, fontColor);
142 SDL_Rect tSpace = {240-(title->w/2), 240-(title->h/2), title->w, title->h};
143 SDL_FillRect(screen, NULL, info->level.getDeadColor());
144 SDL_BlitSurface(title, NULL, screen, &tSpace);
145 }
146
147 // Paint player
148 block.x = info->playerx*16;
149 block.y = info->playery*16;
150
151 SDL_FillRect(screen, &block, player_color);
152}
153
154GameState::Level::Level()
155{
156 level = 0;
157
158 alive[0] = getColor(0, 0, 0); // Black
159 alive[1] = getColor(255, 0, 0); // Red
160 alive[2] = getColor(0, 255, 0); // Green
161 alive[3] = getColor(85, 85, 85); // Dark Gray
162 alive[4] = getColor(255, 0, 255); // Magenta
163
164 dead[0] = getColor(255, 255, 255); // White
165 dead[1] = getColor(255, 192, 203); // Pink
166 dead[2] = getColor(0, 255, 255); // Cyan
167 dead[3] = getColor(170, 170, 170); // Light Gray
168 dead[4] = getColor(255, 128, 0); // Orange
169}
170
171int GameState::Level::getLevel()
172{
173 return level;
174}
175
176bool GameState::Level::checkSquare(int x, int y)
177{
178 switch (level/10+1)
179 {
180 case 1:
181 return ((x>13)&&(x<17)&&(y>13)&&(y<17));
182 case 2:
183 case 3:
184 return ((x>12)&&(x<18)&&(y>12)&&(y<18));
185 case 4:
186 case 5:
187 return ((x>11)&&(x<19)&&(y>11)&&(y<19));
188 default:
189 return true;
190 }
191}
192
193Uint32 GameState::Level::getAliveColor()
194{
195 return alive[(level/10)%5];
196}
197
198Uint32 GameState::Level::getDeadColor()
199{
200 return dead[(level/10)%5];
201}
202
203void GameState::Level::incrementLevel()
204{
205 level++;
206}
207
208GameState::Board::Board()
209{
210 GameState::Board::Board(new GameState::Info());
211}
212
213GameState::Board::Board(GameState::Info* info)
214{
215 this->info = info;
216
217 int x,y;
218 for (y=0;y<HEIGHT;y++)
219 {
220 for (x=0;x<WIDTH;x++)
221 {
222 if (info->level.checkSquare(x, y))
223 {
224 blocks[x][y] = rand() % 2;
225 } else {
226 blocks[x][y] = false;
227 }
228 }
229 }
230
231 blocks[15][15] = false;
232}
233
234bool GameState::Board::isObstructed(int x, int y)
235{
236 return blocks[x][y];
237}
238
239void GameState::Board::render(SDL_Surface* screen)
240{
241 SDL_Rect block;
242 block.w = 16;
243 block.h = 16;
244
245 int x,y;
246
247 for (y=0;y<HEIGHT;y++)
248 {
249 for (x=0;x<WIDTH;x++)
250 {
251 block.x = x*16;
252 block.y = y*16;
253
254 SDL_FillRect(screen, &block, (blocks[x][y] ? info->level.getAliveColor() : info->level.getDeadColor()));
255 }
256 }
257}
258
259void GameState::Board::tick()
260{
261 bool temp[WIDTH][HEIGHT];
262 int x,y;
263 for (x=0;x<WIDTH;x++)
264 {
265 for (y=0;y<HEIGHT;y++)
266 {
267 temp[x][y] = blocks[x][y];
268 }
269 }
270
271 for (x=0;x<WIDTH;x++)
272 {
273 for (y=0;y<HEIGHT;y++)
274 {
275 if ((x==15)&&(y==15))
276 {
277 continue;
278 }
279
280 int neighbors = 0;
281
282 incrementIfNeighbor(x-1,y-1,temp,&neighbors);
283 incrementIfNeighbor(x-1,y,temp,&neighbors);
284 incrementIfNeighbor(x-1,y+1,temp,&neighbors);
285 incrementIfNeighbor(x,y-1,temp,&neighbors);
286 incrementIfNeighbor(x,y+1,temp,&neighbors);
287 incrementIfNeighbor(x+1,y-1,temp,&neighbors);
288 incrementIfNeighbor(x+1,y,temp,&neighbors);
289 incrementIfNeighbor(x+1,y+1,temp,&neighbors);
290
291 if (temp[x][y])
292 {
293 blocks[x][y] = ((neighbors >= 1) && (neighbors <= 4));
294 } else {
295 blocks[x][y] = (neighbors == 3);
296 }
297 }
298 }
299
300 if (!info->doneMaking && ++info->gens > 50) info->doneMaking = true;
301}
302
303void GameState::Board::incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick)
304{
305 int nx = x;
306 int ny = y;
307
308 wrap(&x, &y);
309
310 if (!((nx!=x)&&(ny!=y)))
311 {
312 if ((temp[x][y])||((info->playerx==x)&&(info->playery==y))||((x==15)&&(y==15)))
313 {
314 ++*tick;
315 }
316 }
317}