Update colliders when updating render objects

This commit is contained in:
zvon 2020-08-21 19:49:42 +02:00
parent 1b0a35cf5a
commit 12c6c9049f
2 changed files with 17 additions and 11 deletions

View File

@ -99,7 +99,10 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
posx += 0.45; posx += 0.45;
} }
auto x = std::make_shared<Player>(0,0,0.2,0.2, r); auto x = std::make_shared<Player>(0,0,0.2,0.2, r);
x->addCollision(SDLPP::Rect(0,0,1,1)); x->addCollision(SDLPP::Rect(0.3,0.7,0.05,0.31));
x->addCollision(SDLPP::Rect(0.65,0.7,0.05,0.31));
x->addCollision(SDLPP::Rect(0.2,0.3,0.6,0.45));
x->addCollision(SDLPP::Rect(0.35,0,0.3,0.3));
x->setTexture("5.png"); x->setTexture("5.png");
x->setId(PLAYER_ID); x->setId(PLAYER_ID);
x->setColiderColor("00FF00"); x->setColiderColor("00FF00");
@ -217,15 +220,15 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
for( auto &x : scene->getCollisions(*player) ) { for( auto &x : scene->getCollisions(*player) ) {
if( x->getId() == STONE_ID ) { if( x->getId() == STONE_ID ) {
gravity = false; gravity = false;
auto stoneRect = x->getDoubleRect(); if(player->isGravityEnabled()) {
auto playerPos = player->getDoubleRect(); auto stoneRect = x->getDoubleRect();
auto newPX = playerPos.first.first; auto playerPos = player->getDoubleRect();
auto newPY = playerPos.first.second; auto newPX = playerPos.first.first;
newPY = stoneRect.first.second - playerPos.second.second; auto newPY = stoneRect.first.second - playerPos.second.second;
player->setPos(newPX, newPY); player->setPos(newPX, newPY);
if(player->isGravityEnabled())
player->setLastStand(); player->setLastStand();
x->setHidden(true); }
// x->setHidden(true);
} }
if( x->getId() == DEATH ) { if( x->getId() == DEATH ) {
std::cout << "Oh no, you died!" << std::endl; std::cout << "Oh no, you died!" << std::endl;

View File

@ -312,6 +312,9 @@ public:
void updateSizeAndPosition() { void updateSizeAndPosition() {
for( auto &x : renderObjects ) { for( auto &x : renderObjects ) {
x->updateSizeAndPosition(); x->updateSizeAndPosition();
for( auto &col : x->getCollisions() ) {
col->updateCollision(x->collisionPushX(), x->collisionPushY(), x->collisionWidth(), x->collisionHeight());
}
} }
} }
void moveEverything(double x, double y) { void moveEverything(double x, double y) {
@ -482,7 +485,7 @@ private:
class Rect : public CollisionPolygon { class Rect : public CollisionPolygon {
public: public:
Rect(int x, int y, int w, int h) : CollisionPolygon(x, y) { Rect(double x, double y, double w, double h) : CollisionPolygon(x, y) {
w_ = w; w_ = w;
h_ = h; h_ = h;
} }
@ -545,7 +548,7 @@ private:
class Circle : public CollisionPolygon { class Circle : public CollisionPolygon {
public: public:
Circle(int x, int y, int rad) : CollisionPolygon(x, y) { Circle(double x, double y, double rad) : CollisionPolygon(x, y) {
rad_ = rad; rad_ = rad;
} }
virtual ~Circle() {} virtual ~Circle() {}