summary refs log tree commit diff stats
path: root/gamestate.cpp
blob: f0de67a61b7f3c6b768e899d038331d3ac47054f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "gamestate.h"
#include <SDL_ttf.h>
#include "util.h"
#include "mazeoflife.h"
#include "highscore.h"
#include "titlestate.h"
#include <fstream>
#include "hslist.h"

class GameBoard {
	public:
		GameBoard(int level);
		void tick(int playerx, int playery);
		void render(SDL_Renderer* renderer);
		bool isObstructed(int x, int y);
		
	private:
		int level;
		bool blocks[WIDTH][HEIGHT];
};

class LoadGameState : public State {
	public:
		LoadGameState(int level);
		State* operator() (SDL_Window* window, SDL_Renderer* renderer);
		
	private:
		int level;
};

class PlayGameState : public State {
	public:
		PlayGameState(int level, GameBoard* board, int playerx, int playery);
		State* operator() (SDL_Window* window, SDL_Renderer* renderer);
	
	private:
		bool move(int x, int y);
		int level;
		GameBoard* board;
		int playerx;
		int playery;
};

void setRendererAliveColor(SDL_Renderer* renderer, int level)
{
	switch ((level/10)%5)
	{
		case 0: SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); break; // Black
		case 1: SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); break; // Red
		case 2: SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); break; // Green
		case 3: SDL_SetRenderDrawColor(renderer, 85, 85, 85, 255); break; // Dark Gray
		case 4: SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); break; // Magenta
	}
}

void setRendererDeadColor(SDL_Renderer* renderer, int level)
{
	switch ((level/10)%5)
	{
		case 0: SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); break; // White
		case 1: SDL_SetRenderDrawColor(renderer, 255, 192, 203, 255); break; // Pink
		case 2: SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255); break; // Cyan
		case 3: SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); break; // Light Gray
		case 4: SDL_SetRenderDrawColor(renderer, 255, 128, 0, 255); break; // Orange
	}
}

void incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick, int playerx, int playery)
{
	int nx = x;
	int ny = y;

	wrap(&x, &y);

	if (!((nx!=x)&&(ny!=y)))
	{
		if ((temp[x][y])||((playerx==x)&&(playery==y))||((x==15)&&(y==15)))
		{
			++*tick;
		}
	}
}

State* GameState::operator() (SDL_Window* window, SDL_Renderer* renderer)
{
	return new LoadGameState(0);
}

LoadGameState::LoadGameState(int m_level)
{
	level = m_level;
}

State* LoadGameState::operator() (SDL_Window* window, SDL_Renderer* renderer)
{
	char* wintitle = new char[50];
	sprintf(wintitle, "Maze Of Life - Level %d", level);
	SDL_SetWindowTitle(window, wintitle);
		
	// Randomly place the player in a corner
	int playerx, playery;
	switch (rand()%4)
	{
		case 0: playerx = 1; playery = 1; break;
		case 1: playerx = 1; playery = HEIGHT-2; break;
		case 2: playerx = WIDTH-2; playery = HEIGHT-2; break;
		case 3: playerx = WIDTH-2; playery = 1; break;
	}
	
	// Display the level number
	setRendererDeadColor(renderer, level);
	SDL_RenderClear(renderer);
	
	TTF_Font* font = loadFont(100);
	SDL_Color fontColor = {0, 0, 0, 0};
	char levelnum[8];
	sprintf(levelnum, "%d", level);
	SDL_Surface* dispsurf = TTF_RenderText_Solid(font, levelnum, fontColor);
	SDL_Texture* disptext = SDL_CreateTextureFromSurface(renderer, dispsurf);
	SDL_FreeSurface(dispsurf);
	
	SDL_Rect pos;
	SDL_QueryTexture(disptext, NULL, NULL, &pos.w, &pos.h);
	pos.x = 240-(pos.w/2);
	pos.y = 240-(pos.h/2);
	
	SDL_RenderCopy(renderer, disptext, NULL, &pos);
	SDL_RenderPresent(renderer);
	
	// Do 50 gens of Conway
	GameBoard* board = new GameBoard(level);
	for (int i=0; i<50; i++)
	{
		board->tick(playerx, playery);
	}
	
	// Wait a bit
	SDL_Delay(500);
	
	// Start the level
	return new PlayGameState(level, board, playerx, playery);
}

PlayGameState::PlayGameState(int m_level, GameBoard* m_board, int m_playerx, int m_playery)
{
	level = m_level;
	board = m_board;
	playerx = m_playerx;
	playery = m_playery;
}

State* PlayGameState::operator() (SDL_Window* window, SDL_Renderer* renderer)
{
	SDL_Event e;
	
	for (;;)
	{
		// Tick board
		board->tick(playerx, playery);
		
		// Paint board
		board->render(renderer);
		
		// Paint event
		SDL_Rect block;
		block.w = 16;
		block.h = 16;
		block.x = 15*16;
		block.y = 15*16;
		SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
		SDL_RenderFillRect(renderer, &block);
		
		// Paint player
		block.x = playerx*16;
		block.y = playery*16;
		SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
		SDL_RenderFillRect(renderer, &block);
		
		SDL_RenderPresent(renderer);
		
		while (SDL_PollEvent(&e))
		{
			if (e.type == SDL_QUIT)
			{
				return NULL;
			} else if (e.type == SDL_KEYDOWN)
			{
				switch (e.key.keysym.sym)
				{
					case SDLK_LEFT:
						if (move(playerx-1, playery))
						{
							return new LoadGameState(level+1);
						} else {
							break;
						}
						
					case SDLK_RIGHT:
						if (move(playerx+1, playery))
						{
							return new LoadGameState(level+1);
						} else {
							break;
						}
						
					case SDLK_UP:
						if (move(playerx, playery-1))
						{
							return new LoadGameState(level+1);
						} else {
							break;
						}
					
					case SDLK_DOWN:
						if (move(playerx, playery+1))
						{
							return new LoadGameState(level+1);
						} else {
							break;
						}
					
					case SDLK_ESCAPE:
						SDL_SetWindowTitle(window, "");
						
						std::ifstream exists(getDataFile());
						if (exists)
						{
							FILE* hslist = fopen(getDataFile(), "r");
							int scores;
							Highscore* h;

							fscanf(hslist, "%d%*c", &scores);

							if (scores < 10)
							{
								fclose(hslist);

								return new EnterHighscoreState(level);
							} else {
								for (int i=0; i<scores; i++)
								{
									int namelen;
									char namelens[4];
									char* name = (char*) calloc(25, sizeof(char));
									int score;

									fscanf(hslist, "%d", &namelen);
									sprintf(namelens, "%%%dc", namelen);
									fscanf(hslist, namelens, name);
									fscanf(hslist, "%d%*c", &score);

									h = new Highscore(name, score);
								}

								fclose(hslist);

								if (h->getLevel() < level)
								{
									return new EnterHighscoreState(level);
								} else {
									return new DisplayAndReturnLocalHighscoreListState();
								}
							}
						} else {
							return new EnterHighscoreState(level);
						}
				}
			}
		}
		
		SDL_Delay(5);
	}
}

bool PlayGameState::move(int x, int y)
{
	wrap(&x, &y);
	
	// Are we at the event?
	if ((x==15)&&(y==15))
	{
		return true;
	}

	// Can we even go there?
	if (!board->isObstructed(x,y))
	{
		playerx = x;
		playery = y;
	}
	
	return false;
}

GameBoard::GameBoard(int m_level)
{
	level = m_level;
	
	for (int y=0; y<HEIGHT; y++)
	{
		for (int x=0; x<WIDTH; x++)
		{
			blocks[x][y] = false;
			
			switch (level/10+1)
			{
				case 1:
					if ((x>13)&&(x<17)&&(y>13)&&(y<17))
					{
						blocks[x][y] = rand() % 2;
					}
					break;
				case 2:
				case 3:
			        if ((x>12)&&(x<18)&&(y>12)&&(y<18))
					{
						blocks[x][y] = rand() % 2;
					}
					break;
				case 4:
				case 5:
					if ((x>11)&&(x<19)&&(y>11)&&(y<19))
					{
						blocks[x][y] = rand() % 2;
					}
					break;
				default:
					blocks[x][y] = rand() % 2;
			}
		}
	}
	
	blocks[15][15] = false;
}

void GameBoard::tick(int playerx, int playery)
{
	bool temp[WIDTH][HEIGHT];
	int x,y;
	for (x=0;x<WIDTH;x++)
	{
		for (y=0;y<HEIGHT;y++)
		{
			temp[x][y] = blocks[x][y];
		}
	}

	for (x=0;x<WIDTH;x++)
	{
		for (y=0;y<HEIGHT;y++)
		{
			if ((x==15)&&(y==15))
			{
				continue;
			}

			int neighbors = 0;

			incrementIfNeighbor(x-1,y-1,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x-1,y,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x-1,y+1,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x,y-1,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x,y+1,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x+1,y-1,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x+1,y,temp,&neighbors, playerx, playery);
			incrementIfNeighbor(x+1,y+1,temp,&neighbors, playerx, playery);

			if (temp[x][y])
			{
				blocks[x][y] = ((neighbors >= 1) && (neighbors <= 4));
			} else {
				blocks[x][y] = (neighbors == 3);
			}
		}
	}
}

void GameBoard::render(SDL_Renderer* renderer)
{
	SDL_Rect block;
	block.w = 16;
	block.h = 16;
	
	for (int y=0; y<HEIGHT; y++)
	{
		for (int x=0; x<WIDTH; x++)
		{
			block.x = x*16;
			block.y = y*16;
			
			if (blocks[x][y])
			{
				setRendererAliveColor(renderer, level);
			} else {
				setRendererDeadColor(renderer, level);
			}
			
			SDL_RenderFillRect(renderer, &block);
		}
	}
}

bool GameBoard::isObstructed(int x, int y)
{
	return blocks[x][y];
}