4 void GravityManager::subscribe(Thing
* thing
) {
8 void GravityManager::addPlatform(Platform
* linha
) {
9 platforms
.insert(linha
);
12 void GravityManager::deleteThing(Thing
* thing
) {
13 things
.erase(things
.find(thing
));
16 void GravityManager::update() {
17 std::set
<Thing
*>::iterator it
;
18 std::set
<Platform
*>::iterator plat
;
19 for (it
= things
.begin(); it
!= things
.end(); it
++) {
20 (*it
)->addSpeed(0,(*it
)->gravityRate
);
21 double timeToCollide
= 9999;
22 Platform
* collidedWith
= NULL
;
23 (*it
)->onGround
= false;
24 for (plat
= platforms
.begin(); plat
!= platforms
.end(); plat
++) {
26 if (checkGround(*it
, *plat
, t
)) {
29 double angle
= collidedWith
->angle();
30 double slope
= angle
/ (PI
/2);
31 if (slope
!= 1 && (*it
)->getSpeedY() >= 0)
32 (*it
)->onGround
= true;
33 if (slope
== 1) { //colisao com parede
34 double dir
= sign((*it
)->getSpeedX());
35 (*it
)->setSpeed(dir
*-3,(*it
)->getSpeedY());
37 else if ((*it
)->getSpeedY() >= (*it
)->gravityRate
) {
38 (*it
)->setSpeed((*it
)->getSpeedX(),(*it
)->getSpeedY()*timeToCollide
);
40 if ((*it
)->getSpeedY() < 0) {
47 void GravityManager::removePlatforms() {
51 //caso ocorra colisao, t retorna em quanto tempo ela ocorrera
52 bool GravityManager::checkGround(Thing
* thing
, Platform
*platform
, double &t
) {
53 if (platform
->isPassable() && thing
->bypass
)
55 Linha l1
= thing
->getBaseLine();
56 l1
.translate(thing
->getPosition());
58 l2
.translate(thing
->getSpeed());
59 Linha
l3(l1
.vertices
[0],l2
.vertices
[0]);
60 Linha
l4(l1
.vertices
[1],l2
.vertices
[1]);
61 Linha plat
= platform
->getLine();
63 if (linesIntersect(l1
,plat
))
66 if (linesIntersect(l3
,plat
))
67 t
= std::min(t
,timeToIntersection(l3
,plat
));
68 if (linesIntersect(l4
,plat
))
69 t
= std::min(t
,timeToIntersection(l4
,plat
));
70 if (linesIntersect(l2
,plat
))
73 return (0<= t
&& t
<= 1.0);