-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cpp
261 lines (220 loc) · 8.64 KB
/
Entity.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
257
258
259
260
261
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
#define GL_GLEXT_PROTOTYPES 1
#define LOG(argument) std::cout << argument << '\n'
#include <SDL.h>
#include <SDL_opengl.h>
#include "glm/mat4x4.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "ShaderProgram.h"
#include "Entity.h"
Entity::Entity()
{
// ––––– PHYSICS ––––– //
m_position = glm::vec3(0.0f);
m_velocity = glm::vec3(0.0f);
m_acceleration = glm::vec3(0.0f);
// ––––– TRANSLATION ––––– //
m_movement = glm::vec3(0.0f);
m_speed = 0.0f;
m_model_matrix = glm::mat4(1.0f);
}
Entity::~Entity()
{
delete [] m_animation_up;
delete [] m_animation_down;
delete [] m_animation_left;
delete [] m_animation_right;
delete [] m_walking;
}
void Entity::draw_sprite_from_texture_atlas(ShaderProgram *program, GLuint texture_id, int index)
{
float u_coord = (float) (index % m_animation_cols) / (float) m_animation_cols;
float v_coord = (float) (index / m_animation_cols) / (float) m_animation_rows;
float width = 1.0f / (float) m_animation_cols;
float height = 1.0f / (float) m_animation_rows;
float tex_coords[] =
{
u_coord, v_coord + height, u_coord + width, v_coord + height, u_coord + width, v_coord,
u_coord, v_coord + height, u_coord + width, v_coord, u_coord, v_coord
};
float vertices[] =
{
-0.5, -0.5, 0.5, -0.5, 0.5, 0.5,
-0.5, -0.5, 0.5, 0.5, -0.5, 0.5
};
glBindTexture(GL_TEXTURE_2D, texture_id);
glVertexAttribPointer(program->positionAttribute, 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->positionAttribute);
glVertexAttribPointer(program->texCoordAttribute, 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->texCoordAttribute);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->positionAttribute);
glDisableVertexAttribArray(program->texCoordAttribute);
}
void Entity::update(float delta_time, Entity *collidable_entities,
int collidable_entity_count, bool& g_player_win, bool& g_player_lost)
{
if (!m_is_active) return;
m_collided_top = false;
m_collided_bottom = false;
m_collided_left = false;
m_collided_right = false;
// ––––– ANIMATION ––––– //
if (m_animation_indices != NULL)
{
if (glm::length(m_movement) != 0)
{
m_animation_time += delta_time;
float frames_per_second = (float) 1 / SECONDS_PER_FRAME;
if (m_animation_time >= frames_per_second)
{
m_animation_time = 0.0f;
m_animation_index++;
if (m_animation_index >= m_animation_frames)
{
m_animation_index = 0;
}
}
}
}
// ––––– GRAVITY ––––– //
// If no user input, decelerate until stop moving
// x-direction
if (m_movement.x == 0.0f) {
if (m_velocity.x == 0.0f) {
m_acceleration.x = 0.0f;
} else {
if (m_velocity.x > 0.0f) {
m_acceleration.x = -1.0f * m_speed;
} else {
m_acceleration.x = 1.0f * m_speed;
}
}
}
else {
m_acceleration.x = m_movement.x * m_speed;
}
// y-direction
if (m_movement.y == 0.0f) {
if (m_velocity.y == -0.25) {
m_acceleration.y = -0.25;
} else {
if (m_velocity.y > -0.25) {
m_acceleration.y = -1.0f * m_speed;
} else {
m_acceleration.y = 1.0f * m_speed;
}
}
} else {
m_acceleration.y = m_movement.y * m_speed;
}
m_movement = glm::vec3(0.0f, 0.0f, 0.0f);
m_velocity += m_acceleration * delta_time;
m_position.y += m_velocity.y * delta_time;
check_collision_y(collidable_entities, collidable_entity_count,
g_player_win, g_player_lost);
m_position.x += m_velocity.x * delta_time;
check_collision_x(collidable_entities, collidable_entity_count,
g_player_win, g_player_lost);
// ––––– TRANSFORMATIONS ––––– //
m_model_matrix = glm::mat4(1.0f);
m_model_matrix = glm::translate(m_model_matrix, m_position);
}
void const Entity::check_collision_y(Entity *collidable_entities, int collidable_entity_count,
bool& g_player_win, bool& g_player_lost)
{
for (int i = 0; i < collidable_entity_count; i++)
{
// STEP 1: For every entity that our player can collide with...
Entity *collidable_entity = &collidable_entities[i];
if (check_collision(collidable_entity))
{
if (collidable_entity->get_entity_type() == LOSE_PLATFORM)
{
g_player_lost = true;
}
else if (collidable_entity->get_entity_type() == WIN_PLATFORM)
{
g_player_win = true;
}
// STEP 2: Calculate the distance between its centre and our centre
// and use that to calculate the amount of overlap between
// both bodies.
float y_distance = fabs(m_position.y - collidable_entity->m_position.y);
float y_overlap = fabs(y_distance - (m_height / 2.0f) - (collidable_entity->m_height / 2.0f));
// STEP 3: "Unclip" ourselves from the other entity, and zero our
// vertical velocity.
if (m_velocity.y > 0) {
m_position.y -= y_overlap;
m_velocity.y = 0;
m_collided_top = true;
} else if (m_velocity.y < 0) {
m_position.y += y_overlap;
m_velocity.y = 0;
m_collided_bottom = true;
}
}
}
}
void const Entity::check_collision_x(Entity *collidable_entities, int collidable_entity_count,
bool& g_player_win, bool& g_player_lost)
{
for (int i = 0; i < collidable_entity_count; i++)
{
Entity *collidable_entity = &collidable_entities[i];
if (check_collision(collidable_entity))
{
if (collidable_entity->get_entity_type() == LOSE_PLATFORM)
{
g_player_lost = true;
}
else if (collidable_entity->get_entity_type() == WIN_PLATFORM)
{
g_player_win = true;
}
float x_distance = fabs(m_position.x - collidable_entity->m_position.x);
float x_overlap = fabs(x_distance - (m_width / 2.0f) - (collidable_entity->m_width / 2.0f));
if (m_velocity.x > 0) {
m_position.x -= x_overlap;
m_velocity.x = 0;
m_collided_right = true;
} else if (m_velocity.x < 0) {
m_position.x += x_overlap;
m_velocity.x = 0;
m_collided_left = true;
}
}
}
}
void Entity::render(ShaderProgram *program)
{
if (!m_is_active) return;
program->SetModelMatrix(m_model_matrix);
if (m_animation_indices != NULL)
{
draw_sprite_from_texture_atlas(program, m_texture_id, m_animation_indices[m_animation_index]);
return;
}
float vertices[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
float tex_coords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
glBindTexture(GL_TEXTURE_2D, m_texture_id);
glVertexAttribPointer(program->positionAttribute, 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->positionAttribute);
glVertexAttribPointer(program->texCoordAttribute, 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->texCoordAttribute);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->positionAttribute);
glDisableVertexAttribArray(program->texCoordAttribute);
}
bool const Entity::check_collision(Entity *other) const
{
// If either entity is inactive, there shouldn't be any collision
if (!m_is_active || !other->m_is_active) return false;
float x_distance = fabs(m_position.x - other->m_position.x) - ((m_width + other->m_width) / 2.0f);
float y_distance = fabs(m_position.y - other->m_position.y) - ((m_height + other->m_height) / 2.0f);
return x_distance < 0.0f && y_distance < 0.0f;
}