forked from sschellhoff/SFMLGameDevelopmentByExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityBase.cpp
More file actions
206 lines (182 loc) · 6.47 KB
/
Copy pathEntityBase.cpp
File metadata and controls
206 lines (182 loc) · 6.47 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
#include "EntityBase.h"
#include "EntityManager.h"
#include "SharedContext.h"
#include "Map.h"
#include <cmath>
bool SortCollisions(const CollisionElement& l_1, const CollisionElement& l_2)
{
return l_1.m_area > l_2.m_area;
}
EntityBase::EntityBase(EntityManager* l_entityMgr)
:m_entityManager(l_entityMgr), m_name("BaseEntity"),
m_type(EntityType::Base), m_id(0), m_referenceTile(nullptr),
m_state(EntityState::Idle), m_collidingOnX(false), m_collidingOnY(false){}
EntityBase::~EntityBase(){}
void EntityBase::SetPosition(float l_x, float l_y){
m_position = sf::Vector2f(l_x,l_y);
UpdateAABB();
}
void EntityBase::SetPosition(const sf::Vector2f& l_pos){
m_position = l_pos;
UpdateAABB();
}
void EntityBase::SetSize(float l_x, float l_y){
m_size = sf::Vector2f(l_x,l_y);
UpdateAABB();
}
void EntityBase::SetAcceleration(float l_x, float l_y){
m_acceleration = sf::Vector2f(l_x,l_y);
}
void EntityBase::SetState(const EntityState& l_state){
if (m_state == EntityState::Dying){ return; }
m_state = l_state;
}
const sf::Vector2f& EntityBase::GetSize()const{ return m_size; }
std::string EntityBase::GetName()const{ return m_name; }
EntityState EntityBase::GetState()const{ return m_state; }
unsigned int EntityBase::GetId()const{ return m_id; }
EntityType EntityBase::GetType()const{ return m_type; }
const sf::Vector2f& EntityBase::GetPosition()const{ return m_position; }
void EntityBase::Move(float l_x, float l_y){
m_positionOld = m_position;
m_position += sf::Vector2f(l_x,l_y);
sf::Vector2u mapSize = m_entityManager->GetContext()->m_gameMap->GetMapSize();
if(m_position.x < 0){
m_position.x = 0;
} else if(m_position.x > (mapSize.x + 1) * Sheet::Tile_Size){
m_position.x = (mapSize.x + 1) * Sheet::Tile_Size;
}
if(m_position.y < 0){
m_position.y = 0;
} else if(m_position.y > (mapSize.y + 1) * Sheet::Tile_Size){
m_position.y = (mapSize.y + 1) * Sheet::Tile_Size;
SetState(EntityState::Dying);
}
UpdateAABB();
}
void EntityBase::AddVelocity(float l_x, float l_y){
m_velocity += sf::Vector2f(l_x,l_y);
if(abs(m_velocity.x) > m_maxVelocity.x){
if(m_velocity.x < 0){ m_velocity.x = -m_maxVelocity.x; }
else { m_velocity.x = m_maxVelocity.x; }
}
if(abs(m_velocity.y) > m_maxVelocity.y){
if(m_velocity.y < 0){ m_velocity.y = -m_maxVelocity.y; }
else { m_velocity.y = m_maxVelocity.y; }
}
}
void EntityBase::Accelerate(float l_x, float l_y){
m_acceleration += sf::Vector2f(l_x,l_y);
}
void EntityBase::ApplyFriction(float l_x, float l_y){
if(m_velocity.x != 0){
if(abs(m_velocity.x) - abs(l_x) < 0){ m_velocity.x = 0; }
else {
if(m_velocity.x < 0){ m_velocity.x += l_x; }
else { m_velocity.x -= l_x; }
}
}
if(m_velocity.y != 0){
if (abs(m_velocity.y) - abs(l_y) < 0){ m_velocity.y = 0; }
else {
if(m_velocity.y < 0){ m_velocity.y += l_y; }
else { m_velocity.y -= l_y; }
}
}
}
void EntityBase::Update(float l_dT){
Map* map = m_entityManager->GetContext()->m_gameMap;
float gravity = map->GetGravity();
Accelerate(0,gravity);
AddVelocity(m_acceleration.x * l_dT, m_acceleration.y * l_dT);
SetAcceleration(0.0f, 0.0f);
sf::Vector2f frictionValue;
if(m_referenceTile){
frictionValue = m_referenceTile->m_friction;
if(m_referenceTile->m_deadly){ SetState(EntityState::Dying); }
} else if(map->GetDefaultTile()){
frictionValue = map->GetDefaultTile()->m_friction;
} else {
frictionValue = m_friction;
}
float friction_x = (m_speed.x * frictionValue.x) * l_dT;
float friction_y = (m_speed.y * frictionValue.y) * l_dT;
ApplyFriction(friction_x, friction_y);
sf::Vector2f deltaPos = m_velocity * l_dT;
Move(deltaPos.x, deltaPos.y);
m_collidingOnX = false;
m_collidingOnY = false;
CheckCollisions();
ResolveCollisions();
}
void EntityBase::UpdateAABB(){
m_AABB = sf::FloatRect(m_position.x - (m_size.x / 2),m_position.y - m_size.y, m_size.x,m_size.y);
}
void EntityBase::CheckCollisions(){
Map* gameMap = m_entityManager->GetContext()->m_gameMap;
unsigned int tileSize = gameMap->GetTileSize();
int fromX = floor(m_AABB.left / tileSize);
int toX = floor((m_AABB.left + m_AABB.width) / tileSize);
int fromY = floor(m_AABB.top / tileSize);
int toY = floor((m_AABB.top + m_AABB.height) / tileSize);
for(int x = fromX; x <= toX; ++x){
for(int y = fromY; y <= toY; ++y){
Tile* tile = gameMap->GetTile(x,y);
if (!tile){ continue; }
sf::FloatRect tileBounds(x * tileSize,y * tileSize,tileSize,tileSize);
sf::FloatRect intersection;
m_AABB.intersects(tileBounds,intersection);
float area = intersection.width * intersection.height;
CollisionElement e(area, tile->m_properties, tileBounds);
m_collisions.emplace_back(e);
if(tile->m_warp && m_type == EntityType::Player){
gameMap->LoadNext();
}
}
}
}
void EntityBase::ResolveCollisions(){
if(!m_collisions.empty()){
std::sort(m_collisions.begin(), m_collisions.end(), SortCollisions);
Map* gameMap = m_entityManager->GetContext()->m_gameMap;
unsigned int tileSize = gameMap->GetTileSize();
for (auto &itr : m_collisions){
if (!m_AABB.intersects(itr.m_tileBounds)){ continue; }
// Debug
if(m_entityManager->GetContext()->m_debugOverlay.Debug()){
sf::Vector2f tempPos(itr.m_tileBounds.left, itr.m_tileBounds.top);
sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(tileSize,tileSize));
rect->setPosition(tempPos);
rect->setFillColor(sf::Color(255,255,0,150));
m_entityManager->GetContext()->m_debugOverlay.Add(rect);
}
// End debug.
float xDiff = (m_AABB.left + (m_AABB.width / 2)) - (itr.m_tileBounds.left + (itr.m_tileBounds.width / 2));
float yDiff = (m_AABB.top + (m_AABB.height / 2)) - (itr.m_tileBounds.top + (itr.m_tileBounds.height / 2));
float resolve = 0;
if(abs(xDiff) > abs(yDiff)){
if(xDiff > 0){
resolve = (itr.m_tileBounds.left + tileSize) - m_AABB.left;
} else {
resolve = -((m_AABB.left + m_AABB.width) - itr.m_tileBounds.left);
}
Move(resolve, 0);
m_velocity.x = 0;
m_collidingOnX = true;
} else {
if(yDiff > 0){
resolve = (itr.m_tileBounds.top + tileSize) - m_AABB.top;
} else {
resolve = - ((m_AABB.top + m_AABB.height) - itr.m_tileBounds.top);
}
Move(0,resolve);
m_velocity.y = 0;
if (m_collidingOnY){ continue; }
m_referenceTile = itr.m_tile;
m_collidingOnY = true;
}
}
m_collisions.clear();
}
if(!m_collidingOnY){ m_referenceTile = nullptr; }
}