-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
209 lines (163 loc) · 8.2 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
#include "game.hpp"
#include "utils/loader.cpp"
#include "utils/comp_movement.cpp"
#include "utils/collisions.cpp"
#include "utils/animation.cpp"
/* Constructs the gamestate. */
GameState::GameState()
{
this->set_life(100);
this->set_tacos_eaten(0);
this->set_maximum_y(FALL_DEATH);
this->ptr = shared_ptr<Player>(new Player);
this->cptr = shared_ptr<Player>(new CompPlayer);
this->players = { this->ptr, this->cptr };
this->dim_area = shared_ptr<Backdrop>(new Backdrop);
this->backdrop = shared_ptr<Backdrop>(new Backdrop);
this->layer1 = Matrix<int> (MAP_ROWS, vector<int>(MAP_COLUMNS));
this->tile = Matrix<Block> (MAP_ROWS, vector<Block>(MAP_COLUMNS));
this->soiltile = Matrix<Soil> (MAP_ROWS, vector<Soil>(MAP_COLUMNS));
this->enemies = Matrix<Enemy> (MAP_ROWS, vector<Enemy>(MAP_COLUMNS));
this->spikes = Matrix<Spike> (MAP_ROWS, vector<Spike>(MAP_COLUMNS));
this->set_time(0);
this->set_scrollX(0);
this->set_scrollY(0);
}
void GameState::init(int level_choice)
{
this->level_transition();
this->set_generation(0);
this->set_level_choice(level_choice);
this->get_player()->set_x(210-PLAYER_WIDTH);
this->get_player()->set_y(0-PLAYER_HEIGHT);
this->get_player()->set_dx(0);
this->get_player()->set_dy(0);
this->get_comp_player()->set_x(190-PLAYER_WIDTH);
this->get_comp_player()->set_y(0-PLAYER_HEIGHT);
this->get_comp_player()->set_dx(0);
this->get_comp_player()->set_dy(0);
this->set_tacos_eaten(0);
this->loadImages();
this->init_blocks(this->get_generation());
this->render(this->get_renderer());
this->set_life(100);
this->set_time(0);
this->set_game_time(high_resolution_clock::now());
}
void GameState::render(SDL_Renderer *renderer)
{
// clear the screen (to blue)
SDL_RenderClear(renderer);
// Level color
switch (this->get_level_choice())
{
case 0:
SDL_SetRenderDrawColor(renderer, 50, 60, 57, 255);
break;
case 1:
SDL_SetRenderDrawColor(renderer, 50, 60, 57, 255);
break;
case 2:
SDL_SetRenderDrawColor(renderer, 73, 118, 201, 255);
break;
default:
std::cout << "render(): Level out of bounds\n";
exit(1);
}
// Background
SDL_Rect bgRect = { (int)((this->scrollX / 20) + this->get_backdrop()->get_x()),(int)((this->scrollY / 30) + this->get_backdrop()->get_y()), this->get_backdrop()->get_w(), this->get_backdrop()->get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_backdrop()->get_backdrop_texture(), NULL, &bgRect);
// Block Rendering
int x, y;
for (x = 0; x < MAP_ROWS; ++x)
{
for (y = 0; y < MAP_COLUMNS; ++y)
{
switch (layer1.at(x).at(y))
{
// Block
case world_map::BLOCK_COLLISION : {
SDL_Rect blockRect = { static_cast<int>(this->get_scrollX() + tile.at(x).at(y).get_x()), static_cast<int>(this->get_scrollY() + tile.at(x).at(y).get_y()), tile.at(x).at(y).get_w(), tile.at(x).at(y).get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_block(), NULL , &blockRect);
} break;
// Taco
case world_map::TACO_COLLISION : {
SDL_Rect tacoRect = { static_cast<int>(this->get_scrollX() + tile.at(x).at(y).get_x()), static_cast<int>(this->get_scrollY() + tile.at(x).at(y).get_y()), tile.at(x).at(y).get_w(), tile.at(x).at(y).get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_taco(), NULL , &tacoRect);
} break;
case world_map::EMEMY_COLLISION : {
SDL_Rect enemyRect = { static_cast<int>(this->get_scrollX() + enemies.at(x).at(y).get_x()), static_cast<int>(this->get_scrollY() + enemies.at(x).at(y).get_y()), enemies.at(x).at(y).get_w(), enemies.at(x).at(y).get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_enemy(), NULL , &enemyRect);
} break;
case world_map::SOIL_COLLISION : {
SDL_Rect soilRect = { static_cast<int>(this->get_scrollX() + soiltile.at(x).at(y).get_x()), static_cast<int>(this->get_scrollY() + soiltile.at(x).at(y).get_y()), soiltile.at(x).at(y).get_w(), soiltile.at(x).at(y).get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_soil(), NULL , &soilRect);
} break;
case world_map::SPIKE_COLLISION : {
SDL_Rect spikeRect = { static_cast<int>(this->get_scrollX() + spikes.at(x).at(y).get_x()), static_cast<int>(this->get_scrollY() + spikes.at(x).at(y).get_y()), spikes.at(x).at(y).get_w(), spikes.at(x).at(y).get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_spike(), NULL , &spikeRect);
} break;
case world_map::TACO_SOIL_COLLISION : {
SDL_Rect tacoSoilRect = { static_cast<int>(this->get_scrollX() + tile.at(x).at(y).get_x()), static_cast<int>(this->get_scrollY() + tile.at(x).at(y).get_y()), tile.at(x).at(y).get_w(), tile.at(x).at(y).get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_taco_soil(), NULL , &tacoSoilRect);
}
}
}
}
// Players
// draw a rectangle at he player's position.
for (auto &p : *this->get_players())
{
SDL_Rect prect = { static_cast<int> (this->get_scrollX() + p->get_x()), static_cast<int>(this->get_scrollY() + p->get_y()), PLAYER_WIDTH, PLAYER_HEIGHT };
SDL_RenderCopyEx(this->get_renderer(), p->get_player_frame(p->get_animFrame()), NULL, &prect, 0, NULL, (SDL_RendererFlip)(p->get_facingLeft() == 0));
// intializing landing animations
init_landing_rect(*p);
// initializing running animations
init_running_rect(*p);
}
if (this->get_level_choice() < 1)
{
SDL_Rect dimRect = { static_cast<int>((this->get_scrollX() + this->get_player()->get_x()) - 490), static_cast<int>((this->get_scrollY() + this->get_player()->get_y()) - 450), this->get_dim_area()->get_w(), this->get_dim_area()->get_h() };
SDL_RenderCopy(this->get_renderer(), this->get_dim_area()->get_backdrop_texture(), NULL, &dimRect);
}
// draw text rectangle.
SDL_Rect textRect = { 0, 0, static_cast<int> (this->life_label.get_w() / 4), static_cast<int> (this->life_label.get_h() / 3.75) };
SDL_RenderCopy(this->get_renderer(), this->get_life_label_texture(), NULL, &textRect);
SDL_RenderPresent(this->get_renderer());
}
// Initializes the SDL_Rect for the landing
// animation texture.
void GameState::init_landing_rect(Player &plyr)
{
SDL_Rect landRect = { static_cast<int>((this->get_scrollX() + plyr.get_x()) - 1), static_cast<int>(this->get_scrollY() + plyr.get_y()), PLAYER_WIDTH, PLAYER_HEIGHT };
SDL_RenderCopyEx(this->get_renderer(), plyr.get_landing_frame(plyr.get_landFrame()), NULL, &landRect, 0, NULL, (SDL_RendererFlip)(plyr.get_facingLeft() == 0));
}
// Initializes the SDL_Rect for the running
// animation texture.
void GameState::init_running_rect(Player &plyr)
{
int distance = -13;
if (plyr.get_facingLeft() == 0)
{
distance *= -1;
}
SDL_Rect runRect = { static_cast<int>((this->get_scrollX() + plyr.get_x()) - distance), static_cast<int>(this->get_scrollY() + plyr.get_y()), PLAYER_WIDTH, PLAYER_HEIGHT };
SDL_RenderCopyEx(this->get_renderer(), plyr.get_running_frame(plyr.get_runFrame()), NULL, &runRect, 0, NULL, (SDL_RendererFlip)(plyr.get_facingLeft() == 0));
}
GameState::~GameState()
{
// Shutdown game and upload all memory
SDL_DestroyTexture(this->get_taco());
SDL_DestroyTexture(this->get_taco_soil());
SDL_DestroyTexture(this->get_spike());
SDL_DestroyTexture(this->get_soil());
SDL_DestroyTexture(this->get_enemy());
SDL_DestroyTexture(this->get_player()->get_player_frame(0));
SDL_DestroyTexture(this->get_player()->get_player_frame(1));
SDL_DestroyTexture(this->get_block());
Mix_FreeChunk(this->get_jump_music());
if (this->get_life_label_texture() != NULL)
{
SDL_DestroyTexture(this->get_life_label_texture());
}
}