-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
256 lines (222 loc) · 6.13 KB
/
Game.cpp
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
//
// Game.cpp
// TankGame
//
// Created by Jacob Gonzalez on 4/05/2015.
// Copyright (c) 2015 Jacob Gonzalez. All rights reserved.
//
#include "Point.h"
#include "ConsoleTools.h"
#include "Timer.h"
#include "MutableArray.h"
#include "View.h"
#include "IntroView.h"
#include "GameView.h"
#include "EndView.h"
#include "TextureTools.h"
#include "Sprite.h"
#include "DebugView.h"
#include <signal.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
// :: Simple Game class for memory managment and timer delegate ::
// :: Main body of game runs here ::
class Game : public ITimerDelegate
{
public:
// : constructor to take runtime args :
Game(bool skip, float game_time, int rain)
: _skip_intro(skip)
{
ConsoleTools::play_music("music.wav");
// clear computer.txt for writing
if (DebugView::shared_view()->show == true)
{
std::ofstream file("computer.txt");
file << "Computer 1" << std::endl << "Computer 2" << std::endl;
file.close();
}
// write the seed of this game for replay
if (ConsoleTools::replay == false)
{
std::ofstream file("replay.txt");
int t = time(0);
file << t << std::endl;
file.close();
srand(t);
}
else
{
printf("seed: ");
srand(ConsoleTools::get_input<int>("int: ", Point<int>(StringTools::string_width("seed: "), 0)));
}
// load texture map file
TextureTools::load_texture_file("textures.txt");
// setup intro view
intro_view = new IntroView();
current_view = intro_view;
intro_view->tag = rain;
// switch to gameview straight away
if (_skip_intro == true)
{
game_view = new GameView("Default 1", "Default 2");
game_view->tag = rain;
current_view = game_view;
current_view->draw();
}
// : create timer and link to self
timer = Timer(game_time, true);
timer.set_delegate(this);
tick_time = 0;
}
~Game()
{
// cleanup
current_view = 0;
if (intro_view != 0)
{
delete intro_view;
}
if (game_view != 0)
{
delete game_view;
}
if (end_view != 0)
{
delete end_view;
}
ConsoleTools::end_color();
ConsoleTools::clear_screen();
ConsoleTools::stop_music();
}
// : enter main game loop :
void run()
{
timer.start();
}
// : main game loop :
void timer_did_tick(Timer *sender, int t)
{
tick_time++;
std::cin.clear();
// update
current_view->update(tick_time);
// clear and draw
ConsoleTools::end_color();
ConsoleTools::clear_screen();
current_view->draw();
// Exit introview and goto gameview
if (current_view == intro_view && current_view->should_remove_from_parent())
{
game_view = new GameView(intro_view->player1, intro_view->player2);
game_view->tag = intro_view->tag;
current_view = game_view;
delete intro_view;
intro_view = 0;
ConsoleTools::end_color();
ConsoleTools::clear_screen();
current_view->draw();
tick_time = 0;
}
// Exit gameview and goto endview
if (current_view == game_view && current_view->should_remove_from_parent())
{
end_view = new EndView(game_view->get_winner());
end_view->tag = game_view->tag;
current_view = end_view;
delete game_view;
game_view = 0;
ConsoleTools::end_color();
ConsoleTools::clear_screen();
current_view->draw();
tick_time = 0;
}
// Exit endview and exit game
if (current_view == end_view && current_view->should_remove_from_parent())
{
current_view = 0;
delete end_view;
end_view = 0;
//end game loop
timer.end();
}
}
private:
View *current_view;
IntroView *intro_view;
GameView *game_view;
EndView *end_view;
Timer timer;
int tick_time;
bool _skip_intro;
};
// : capture control-c to exit gracefully
void signal_handler(int signum)
{
ConsoleTools::stop_music();
ConsoleTools::end_color();
ConsoleTools::clear_screen();
std::exit(0);
}
// : entry point :
int main(int argc, char const *argv[])
{
signal(SIGINT, signal_handler);
// default args
float game_time = 0.05;
int rain = 1;
DebugView::shared_view()->show = false;
bool skip = false;
// handle args from terminal input
for (int i = 1; i < argc; ++i)
{
if (std::string(argv[i]) == "--skip")
{
skip = true;
}
else if (std::string(argv[i]) == "--noaudio")
{
ConsoleTools::audio = false;
}
else if (std::string(argv[i]) == "--debug")
{
DebugView::shared_view()->show = true;
}
else if (std::string(argv[i]) == "--norain")
{
rain = 0;
}
else if (std::string(argv[i]) == "--replay")
{
ConsoleTools::replay = true;
}
else if (std::string(argv[i]) == "--gametime")
{
if (++i > argc)
{
break;
}
std::stringstream ss;
ss << argv[i];
ss >> game_time;
}
else if (std::string(argv[i]) == "--help")
{
std::ifstream helpfile("help.txt");
ConsoleTools::set_fcolor(ConsoleTools::green);
printf("\n%s\n", std::string(std::istreambuf_iterator<char>(helpfile),
std::istreambuf_iterator<char>()).c_str());
ConsoleTools::end_color();
std::exit(0);
}
}
// start
Game game(skip, game_time, rain);
game.run();
return 0;
}