Linhas mais espessas, anti-aliasing e pontos melhor definidos.
[Projeto-PCG.git] / gravity.cpp
blobb03b0d8ff8889d09a7d9304f60285e45eebec3da
1 #include "gravity.h"
2 #include <iostream>
4 void GravityManager::subscribe(Thing* thing) {
5 things.insert(thing);
8 void GravityManager::addPlataform(Linha* linha) {
9 plataforms.insert(linha);
12 void GravityManager::update() {
13 std::set<Thing*>::iterator it;
14 std::set<Linha*>::iterator plat;
15 for (it = things.begin(); it != things.end(); it++) {
16 if ((*it)->getSpeedY() > 0.6)
17 (*it)->onGround = false;
18 if ((*it)->getSpeedY() <= 0 && !(*it)->onGround) {
19 (*it)->addSpeed(0,0.1);
20 continue;
22 bool colisao = false;
23 for (plat = plataforms.begin(); !colisao && plat != plataforms.end(); plat++) {
24 colisao = checkGround(*it,*plat);
26 if (colisao) {
27 //std::cout<<"colisao"<<std::endl;
28 (*it)->onGround = true;
29 (*it)->setSpeed((*it)->getSpeedX(),-0.2);
31 else {
32 (*it)->addSpeed(0,0.1);
37 bool contido(double x1,double y1, double x2, double y2, Ponto &ponto) {
38 return x1 <= ponto.x && ponto.x <= x2 && y1 <= ponto.y && ponto.y <= y2;
42 //xy1 é o canto debaixo esq, xy2 decima direito
43 bool collisionRectLine(double x1,double y1, double x2, double y2, Linha &linha) {
44 if (contido(x1,y1,x2,y2,linha.vertices[0]) || contido(x1,y1,x2,y2,linha.vertices[1]))
45 return true;
46 double minxlinha = std::min(linha.vertices[0].x,linha.vertices[1].x);
47 double minylinha = std::min(linha.vertices[0].y,linha.vertices[1].y);
48 double maxxlinha = std::max(linha.vertices[0].x,linha.vertices[1].x);
49 double maxylinha = std::max(linha.vertices[0].y,linha.vertices[1].y);
50 //std::cout<<y1<<" "<<maxylinha<<" "<<y2<<" "<<minylinha<<" "<< x1<<" "<<maxxlinha<<" "<< x2 <<" "<<minylinha<<std::endl;
51 if (y1 > maxylinha || y2 < minylinha || x1 > maxxlinha || x2 < minxlinha)
52 return false;
53 return true;
57 bool GravityManager::checkGround(Thing* thing, Linha* plataform) {
58 Rect baseRect = thing->getBaseRect();
59 return collisionRectLine(baseRect.vertices[0].x,baseRect.vertices[0].y,
60 baseRect.vertices[1].x,baseRect.vertices[1].y,*plataform);