Inimigos e players afetados pelos tiros
[Projeto-PCG.git] / gravity.cpp
blob0d5fd3e0c38dfd6aed5a07d68fdea125a0cf9324
1 #include "gravity.h"
2 #include <math.h>
4 void GravityManager::subscribe(Thing* thing) {
5 things.insert(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++) {
25 double t;
26 if (checkGround(*it, *plat, t)) {
27 timeToCollide = t;
28 collidedWith = *plat;
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() {
48 platforms.clear();
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)
54 return false;
55 Linha l1 = thing->getBaseLine();
56 l1.translate(thing->getPosition());
57 Linha l2 = l1;
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();
62 t = 9999;
63 if (linesIntersect(l1,plat))
64 t = 0;
65 else {
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))
71 t = std::min(t,1.0);
73 return (0<= t && t <= 1.0);