-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionSystem.cpp
More file actions
56 lines (42 loc) · 1.94 KB
/
CollisionSystem.cpp
File metadata and controls
56 lines (42 loc) · 1.94 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
//
// Created by Fabien on 24/01/2016.
//
#include "CollisionSystem.hpp"
CollisionSystem::CollisionSystem(sf::RenderTarget& ptarget, tmx::MapLoader& pLoader) : mLoader(pLoader), mTarget(ptarget), mFarCollisionLastTimeDetected(false)
{
}
void CollisionSystem::update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt)
{
const sf::IntRect &viewport = mTarget.getViewport(mTarget.getDefaultView());
mLoader.UpdateQuadTree(sf::FloatRect(viewport));
es.each<Drawable,Playable,Movable>([this,&events](entityx::Entity entity, Drawable& drawable, Playable& playable, Movable& movable) {
sf::Vector2f &pos = drawable.vectorPos;
sf::Vector2f& oldMov = movable.lastVectorMov;
sf::FloatRect playerRec = {pos.x, pos.y, TILE_SIZE, TILE_SIZE};
sf::FloatRect farPlayerRec = {playerRec.left+oldMov.x*20, playerRec.top+oldMov.y*20, TILE_SIZE, TILE_SIZE};
sf::FloatRect intersecRec;
bool farCollisionDetected{false};
for(auto& object : mLoader.QueryQuadTree(playerRec))
{
if(object->GetName()!="playerStart")
{
const sf::FloatRect &objRec = object->GetAABB();
if(objRec.intersects(playerRec, intersecRec))
{
events.emit<Collision>(Collision::CollisionType::DIRECT, object, playerRec, objRec, intersecRec,movable.vectorMov);
}
if(objRec.intersects(farPlayerRec,intersecRec))
{
farCollisionDetected = true;
mFarCollisionLastTimeDetected = true;
events.emit<Collision>(Collision::CollisionType::FAR, object, farPlayerRec, objRec, intersecRec,movable.vectorMov);
}
}
}
if(!farCollisionDetected&&mFarCollisionLastTimeDetected)
{
mFarCollisionLastTimeDetected = false;
events.emit<EndCollision>();
}
});
}