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;
}
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->setId(PLAYER_ID);
x->setColiderColor("00FF00");
@ -217,15 +220,15 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
for( auto &x : scene->getCollisions(*player) ) {
if( x->getId() == STONE_ID ) {
gravity = false;
auto stoneRect = x->getDoubleRect();
auto playerPos = player->getDoubleRect();
auto newPX = playerPos.first.first;
auto newPY = playerPos.first.second;
newPY = stoneRect.first.second - playerPos.second.second;
player->setPos(newPX, newPY);
if(player->isGravityEnabled())
if(player->isGravityEnabled()) {
auto stoneRect = x->getDoubleRect();
auto playerPos = player->getDoubleRect();
auto newPX = playerPos.first.first;
auto newPY = stoneRect.first.second - playerPos.second.second;
player->setPos(newPX, newPY);
player->setLastStand();
x->setHidden(true);
}
// x->setHidden(true);
}
if( x->getId() == DEATH ) {
std::cout << "Oh no, you died!" << std::endl;

View File

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