-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
200 lines (176 loc) · 5.37 KB
/
Game.cpp
File metadata and controls
200 lines (176 loc) · 5.37 KB
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
//Game.cpp -- implementation file for Sprite class
//updated 2016/05/16
//Good Day Fishing
//a game written by Jean Park
//created April 2016
#include <chrono>
#include <iostream>
#include <thread>
#include "Windows.h"
#include "Game.hpp"
using namespace std;
using std::chrono::milliseconds;
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::high_resolution_clock::time_point Time_point;
typedef std::chrono::duration<double, std::chrono::seconds::period> TimeInSeconds;
const double TIME_PER_UPDATE(.25); //time in seconds
const int NUM_PLAYER_SPRITES = 4;
const std::string PLAYER_FILENAMES[] = { "boating_left.txt"
, "boating_right.txt"
, "fishing_left.txt"
, "fishing_right.txt" };
Game::Game()
: world_(new World)
, player_sprites_(new Sprite[NUM_PLAYER_SPRITES])
, game_object_manager_(new GameObjectManager(*world_))
, state_(GameState::running)
, poop_message_ticks_(0)
, fish_message_ticks_(0) {
for (int i = 0; i < NUM_PLAYER_SPRITES; i++) {
player_sprites_[i].load(PLAYER_FILENAMES[i]);
}
player_ = make_unique<Player>(player_sprites_[0], player_sprites_[1], player_sprites_[2], player_sprites_[3]);
}
//default ~Game(): unique pointers will destroy objects
void Game::init() {
world_->update();
draw();
world_->swap();
}
void Game::process() {
if (GetAsyncKeyState(VK_ESCAPE)) {
state_ = GameState::exit;
}
player_->process(*game_object_manager_);
}
void Game::update() {
//regenerate world
game_object_manager_->regenerate(*world_);
//update world
world_->update();
game_object_manager_->update();
player_->update(*game_object_manager_);
handleFishToHookCollision();
}
void Game::draw() {
world_->draw();
game_object_manager_->draw(*world_);
player_->draw(*world_);
}
void Game::lateUpdate() {
game_object_manager_->lateUpdate(*world_);
handlePoopToHeadCollision();
if (player_->health() <= 0) {
state_ = GameState::game_over;
}
}
void Game::handleFishToHookCollision() {
--fish_message_ticks_;
if (game_object_manager_->isFishingCollision()) {
player_->score() += game_object_manager_->points();
fish_name_ = game_object_manager_->currentCatch();
fish_message_ticks_ = GameConstant::FISH_MESSAGE_TICKS;
}
if (fish_message_ticks_ <= 0) {
fish_message_ticks_ = 0;
fish_name_ = " ";
}
}
void Game::handlePoopToHeadCollision() {
if (isPoopToHeadCollision()) {
player_->damage();
poop_message_ticks_ = GameConstant::POOP_MESSAGE_TICKS;
}
}
bool Game::isPoopToHeadCollision() {
return game_object_manager_->isPoopCollision(player_->headX(), player_->headY());
}
void Game::lateDraw() {
game_object_manager_->lateDraw(*world_);
if (poop_message_ticks_ > 0) {
--poop_message_ticks_;
player_->drawPoopMessage(*world_);
}
world_->swap();
}
void Game::render() {
world_->render();
}
void Game::centeredPrint(std::string s, int y) {
int x = (WorldConstant::STAGE_WIDTH - s.size()) >> 1;
for (int i = 0; i < s.size(); i++) {
world_->drawTile(s[i], x++, y);
}
}
void Game::titleScreen() {
world_->drawBorders();
world_->clearStage();
centeredPrint(GameConstant::TITLE_MESSAGE, GameConstant::CENTER_Y - 1);
centeredPrint(GameConstant::AUTHOR_MESSAGE, GameConstant::CENTER_Y);
world_->swap();
render();
}
void Game::gameOverScreen() {
string input;
system("cls");
world_->drawBorders();
world_->clearStage();
if (GameState::game_over == state_) {
centeredPrint(GameConstant::GAME_OVER_MESSAGE, GameConstant::CENTER_Y - 1);
centeredPrint(GameConstant::CONTINUE_MESSAGE, GameConstant::CENTER_Y);
world_->swap();
render();
while (true) {
if (GetAsyncKeyState(0x59)) {
state_ = GameState::running;
player_->reset();
break;
}
if (GetAsyncKeyState(0x4E)) {
state_ = GameState::exit;
break;
}
this_thread::sleep_for(chrono::milliseconds(200));
}
}
}
void Game::endScreen() {
string input;
system("cls");
world_->drawBorders();
world_->clearStage();
centeredPrint(GameConstant::GOODBYE_MESSAGE, GameConstant::CENTER_Y);
world_->swap();
render();
}
void Game::run() {
titleScreen();
cin.ignore();
auto currentTime = Clock::now();
double accumulatedTime = 0.0;
double gameTime = 0.0;
do {
init();
while (state_ == GameState::running) {
auto newTime = Clock::now();
double elapsedTime = TimeInSeconds(newTime - currentTime).count();
currentTime = newTime;
accumulatedTime += elapsedTime;
process();
while (accumulatedTime >= TIME_PER_UPDATE) {
update();
accumulatedTime -= TIME_PER_UPDATE;
gameTime += TIME_PER_UPDATE;
draw();
lateUpdate();
lateDraw();
}
render();
message_system_.displayHealthMeter(player_->health(), player_->score(), fish_name_);
this_thread::sleep_for(chrono::milliseconds(200));
}
gameOverScreen();
} while (GameState::exit != state_);
endScreen();
this_thread::sleep_for(chrono::milliseconds(200));
}