-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.cpp
More file actions
226 lines (183 loc) · 7.41 KB
/
Entity.cpp
File metadata and controls
226 lines (183 loc) · 7.41 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
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
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
#define GL_GLEXT_PROTOTYPES 1
#include <SDL.h>
#include <SDL_opengl.h>
#include "include/glm/mat4x4.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "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;
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)
{
// Step 1: Calculate the UV location of the indexed frame
float u_coord = (float)(index % m_animation_cols) / (float)m_animation_cols;
float v_coord = (float)(index / m_animation_cols) / (float)m_animation_rows;
// Step 2: Calculate its UV size
float width = 1.0f / (float)m_animation_cols;
float height = 1.0f / (float)m_animation_rows;
// Step 3: Just as we have done before, match the texture coordinates to the vertices
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
};
// Step 4: And render
glBindTexture(GL_TEXTURE_2D, texture_id);
glVertexAttribPointer(program->get_position_attribute(), 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->get_position_attribute());
glVertexAttribPointer(program->get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->get_tex_coordinate_attribute());
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->get_position_attribute());
glDisableVertexAttribArray(program->get_tex_coordinate_attribute());
}
void Entity::update(float delta_time, Entity* collidable_entities, int collidable_entity_count)
{
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 ––––– //
// m_velocity.x = m_movement.x * m_speed;
m_velocity += m_acceleration * delta_time;
m_position.y += m_velocity.y * delta_time;
check_collision_y(collidable_entities, collidable_entity_count);
m_position.x += m_velocity.x * delta_time;
check_collision_x(collidable_entities, collidable_entity_count);
// ––––– JUMPING ––––– //
if (m_is_jumping)
{
// STEP 1: Immediately return the flag to its original false state
m_is_jumping = false;
// STEP 2: The player now acquires an upward velocity
m_velocity.y += m_jumping_power;
}
//---propelling---//
propel();
// ––––– 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)
{
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))
{
// 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)
{
for (int i = 0; i < collidable_entity_count; i++)
{
Entity* collidable_entity = &collidable_entities[i];
if (check_collision(collidable_entity))
{
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)
{
program->set_model_matrix(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->get_position_attribute(), 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->get_position_attribute());
glVertexAttribPointer(program->get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->get_tex_coordinate_attribute());
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->get_position_attribute());
glDisableVertexAttribArray(program->get_tex_coordinate_attribute());
}
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;
}