/
BoundarySystem.cpp
67 lines (57 loc) · 1.97 KB
/
BoundarySystem.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
/**
* The BoundarySystem ensures that both paddles and the game ball stay within the window.
*
* Author: Skylar Payne
* Date: 8/23/2013
* File: BoundarySystem.cpp
**/
#include "BoundarySystem.h"
#include "ColliderComponent.h"
#include "PositionComponent.h"
#include "Entity.h"
/**
* @brief BoundarySystem::Update maintains that all game objects stay within the game window
* @param dt the time splice between frames
*/
void BoundarySystem::Update(unsigned int dt)
{
std::set<unsigned int>::iterator it;
for(it = _EntitiesToUpdate.begin(); it != _EntitiesToUpdate.end(); ++it)
{
Entity* e = this->GetEntity(*it);
ColliderComponent* cc = e->GetComponent<ColliderComponent>("Collider");
PositionComponent* pc = e->GetComponent<PositionComponent>("Position");
sf::Vector2f pos;
pos.x = pc->GetPosition().x;
pos.y = pc->GetPosition().y;
if(pc->GetPosition().y + cc->GetOffset().y < 0.0f)
{
pos.y = -cc->GetOffset().y;
}
else if(pc->GetPosition().y + cc->GetOffset().y + cc->GetDimensions().y > this->GetWindow()->getSize().y)
{
pos.y = this->GetWindow()->getSize().y - (cc->GetOffset().y + cc->GetDimensions().y);
}
if(pos.y != pc->GetPosition().y)
{
MoveEntityMessage msg;
msg.ID = *it;
msg.newPosition = pos;
Emit<MoveEntityMessage>(msg);
CollisionMessage cmsg;
cmsg.ID1 = *it;
cmsg.ID2 = *it;
Emit<CollisionMessage>(cmsg);
}
}
}
/**
* @brief BoundarySystem::ValidateEntity validates entities that have Collider and Position components
* @param ID the entity to validate
* @return true if the entity has a Collider and Position component, false otherwise
*/
bool BoundarySystem::ValidateEntity(unsigned int ID)
{
Entity* e = this->GetEntity(ID);
return e->HasComponent("Collider") && e->HasComponent("Position");
}