-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivingentity.cpp
55 lines (46 loc) · 1.49 KB
/
livingentity.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
#include "livingentity.h"
void LivingEntity::position(Vector position) {
// check if we can move to new position
bool moveTo = true;
AABB aabbTo = AABB(position, Vector(position.x() + aabb().deltaX(), position.y() + aabb().deltaY()));
for (Entity *ent : m_collisions) {
if (ent->aabb().collides(aabbTo, -1)) {
moveTo = false;
}
}
if (moveTo) {
m_pos = position;
}
}
void LivingEntity::tick(double dt) {
// isOnGround
m_isOnGround = false;
AABB translated = aabb().translate(Vector(0, 1));
for (Entity *ent : m_collisions) {
if (ent->aabb().collides(translated)) {
m_isOnGround = true;
break;
}
}
// gravity
if (!m_isOnGround) {
Vector velo = velocity();
velo.add(Vector(0, dt * acceleration().y()));
velocity(velo);
} else {
if (m_vel.y() < 0) {
m_vel = Vector(m_vel.x(), 0);
}
}
// velocity
Vector to = Entity::position();
to.subtract(Vector(dt * velocity().x(), dt * velocity().y()));
position(to);
// acceleration
// velocity(Vector(velocity().x() + (dt * acceleration().x()), velocity().y() + (dt * acceleration().y())));
std::cout << "m_isOnGround: " << m_isOnGround << " " << "x:" << velocity().x() << " y:" << velocity().y() << std::endl;
}
AABB LivingEntity::aabb() {
Vector pos = Entity::position();
return {pos,Vector(pos.x() + m_aabb.deltaX(), pos.y() + m_aabb.deltaY())};
}