From 9c8c5c3283d8af18556f3d3f86974b5424eb263d Mon Sep 17 00:00:00 2001 From: Claudio Hoffmann <hoffmanncl72341@th-nuernberg.de> Date: Sat, 16 Jan 2021 01:27:06 +0100 Subject: [PATCH] Don't redeclare any X/Y positions in child classes of Entity --- Bomber.cpp | 8 +++--- Enemy.cpp | 49 ++++++++++++++-------------------- Enemy.hpp | 4 --- Entity.cpp | 9 +++++++ Entity.hpp | 2 ++ Level.cpp | 76 ++++++++++++++++++----------------------------------- Level.hpp | 17 +++--------- Tank.cpp | 22 ++++++++-------- Trooper.cpp | 22 ++++++++-------- main.cpp | 2 +- 10 files changed, 87 insertions(+), 124 deletions(-) diff --git a/Bomber.cpp b/Bomber.cpp index 9bcb14d..3c05372 100644 --- a/Bomber.cpp +++ b/Bomber.cpp @@ -149,16 +149,16 @@ void Bomber::move(FloatSeconds const & Frametime, [[maybe_unused]] Level &level) /*replace with Pathfinding*/ switch ( this->direcion ) { case 1: //front - this->y -= 10*Frametime.count(); + this->topleft.Y -= 10*Frametime.count(); break; case 2: //left - this->x -= 10*Frametime.count(); + this->topleft.X -= 10*Frametime.count(); break; case 3: //right - this->x += 10*Frametime.count(); + this->topleft.X += 10*Frametime.count(); break; default: - this->y += 10*Frametime.count(); + this->topleft.Y += 10*Frametime.count(); break; //exception break; diff --git a/Enemy.cpp b/Enemy.cpp index 7ea77b2..c128a80 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -4,8 +4,7 @@ Enemy::Enemy(int health,float x, float y, int direction, int animationstep) { this->hitpoints = health; - this->x = x; - this->y = y; + this->topleft = { x, y }; this->direcion = direction; this->animationstep = animationstep; } @@ -18,17 +17,17 @@ void Enemy::shoot(Level &level) switch ( this->direcion ) { case 1: //Up { - level.addEnemyProjectile({this->x+sprite.w()/2-1,this->y-sprite.h()/2,direcion}); + level.addEnemyProjectile({this->topleft.X+sprite.w()/2-1,this->topleft.Y-sprite.h()/2,direcion}); break; } case 2: //left - level.addEnemyProjectile({this->x,this->y+sprite.h()/2-1,direcion}); + level.addEnemyProjectile({this->topleft.X,this->topleft.Y+sprite.h()/2-1,direcion}); break; case 3: //right - level.addEnemyProjectile({this->x,this->y+sprite.h()/2-1,direcion}); + level.addEnemyProjectile({this->topleft.X,this->topleft.Y+sprite.h()/2-1,direcion}); break; default: //Down - level.addEnemyProjectile({this->x+sprite.w()/2-1,this->y+(sprite.h()/2),direcion}); + level.addEnemyProjectile({this->topleft.X+sprite.w()/2-1,this->topleft.Y+(sprite.h()/2),direcion}); break; } timetillreload = 5; @@ -43,23 +42,13 @@ void Enemy::move( }; -float Enemy::getX() -{ - return this->x; -}; - -float Enemy::getY() -{ - return this->y; -}; - bool Enemy::lookup(float PlayerY, Level &level) { - if(this->y > PlayerY) + if(this->topleft.Y > PlayerY) { - int i = static_cast<int>(this->y-(this->getCurrentSprite().h()/2)); + int i = static_cast<int>(this->topleft.Y-(this->getCurrentSprite().h()/2)); i = i-static_cast<int>(PlayerY); - if(level.checkCollision(this->x, this->y+(this->getCurrentSprite().h()/2)+i, i, 1)) + if(level.checkCollision(this->topleft.X, this->topleft.Y+(this->getCurrentSprite().h()/2)+i, i, 1)) { return false; } @@ -75,11 +64,11 @@ bool Enemy::lookup(float PlayerY, Level &level) }; bool Enemy::lookdown(float PlayerY, Level &level) { - if(this->y < PlayerY) + if(this->topleft.Y < PlayerY) { - int i = static_cast<int>(this->y+(this->getCurrentSprite().h()/2)); + int i = static_cast<int>(this->topleft.Y+(this->getCurrentSprite().h()/2)); i = static_cast<int>(PlayerY)-i; - if(level.checkCollision(this->x, this->y+(this->getCurrentSprite().h()/2), i, 1)) + if(level.checkCollision(this->topleft.X, this->topleft.Y+(this->getCurrentSprite().h()/2), i, 1)) { return false; } @@ -95,11 +84,11 @@ bool Enemy::lookdown(float PlayerY, Level &level) }; bool Enemy::lookleft(float PlayerX, Level &level) { - if(this->x > PlayerX) + if(this->topleft.X > PlayerX) { - int i = static_cast<int>(this->x-(this->getCurrentSprite().w()/2)); + int i = static_cast<int>(this->topleft.X-(this->getCurrentSprite().w()/2)); i = i-static_cast<int>(PlayerX); - if(level.checkCollision(this->x-(this->getCurrentSprite().w()/2)-i, this->y, i, 1)) + if(level.checkCollision(this->topleft.X-(this->getCurrentSprite().w()/2)-i, this->topleft.Y, i, 1)) { return false; } @@ -115,11 +104,11 @@ bool Enemy::lookleft(float PlayerX, Level &level) }; bool Enemy::lookright(float PlayerX, Level &level) { - if(this->x < PlayerX) + if(this->topleft.X < PlayerX) { - int i = static_cast<int>(this->x+(this->getCurrentSprite().w()/2)); + int i = static_cast<int>(this->topleft.X+(this->getCurrentSprite().w()/2)); i = static_cast<int>(PlayerX)-i; - if(level.checkCollision(this->x-(this->getCurrentSprite().w()/2), this->y, i, 1)) + if(level.checkCollision(this->topleft.X-(this->getCurrentSprite().w()/2), this->topleft.Y, i, 1)) { return false; } @@ -155,7 +144,7 @@ bool Enemy::lookAtPlayer(float PlayerX, float PlayerY, Level &level) bool Enemy::playerdetected(float PlayerX, float PlayerY, Level &level) { - if(abs(this->x - PlayerX) < 20) //buffer of 5 + if(abs(this->topleft.X - PlayerX) < 20) //buffer of 5 { if(lookdown(PlayerY, level)) { @@ -172,7 +161,7 @@ bool Enemy::playerdetected(float PlayerX, float PlayerY, Level &level) return false; } } - else if(abs(this->y - PlayerY) < 20) //buffer of 5 + else if(abs(this->topleft.Y - PlayerY) < 20) //buffer of 5 { if(lookleft(PlayerX, level)) { diff --git a/Enemy.hpp b/Enemy.hpp index e83dcae..3a67144 100644 --- a/Enemy.hpp +++ b/Enemy.hpp @@ -16,15 +16,11 @@ class Enemy : public Entity { public: Enemy(int Health, float x, float y, int direction, int animationstep); - float getX(); - float getY(); virtual const PixelBuffer& getCurrentSprite() = 0; virtual int getScore() = 0; protected: int direcion; - float x; - float y; float timetillreload = 0; float animationtimer = 0; //Kann nicht copy diff --git a/Entity.cpp b/Entity.cpp index e47a48a..323f86b 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -1,5 +1,14 @@ #include "Entity.hpp" +Entity::Entity() +{ +} + +Entity::Entity(Position const &topleft, int hitpoints) + : topleft(topleft), hitpoints(hitpoints) +{ +} + Entity::~Entity() { } diff --git a/Entity.hpp b/Entity.hpp index b7ac150..f782414 100644 --- a/Entity.hpp +++ b/Entity.hpp @@ -25,6 +25,8 @@ public: /// Constructors and destructors /// ---------------------------- + Entity(); + Entity(Position const &topleft, int hitpoints = 1); virtual ~Entity(); /// ---------------------------- diff --git a/Level.cpp b/Level.cpp index e8807c9..4d99516 100644 --- a/Level.cpp +++ b/Level.cpp @@ -16,11 +16,10 @@ DynamicPixelBuffer Wall::constructWallBuffer(Orientation orientation, int length return { thickness, length }; } -Wall::Wall(int length, int thickness, float posX, float posY, Orientation orientation) : +Wall::Wall(int length, int thickness, Position const &pos, Orientation orientation) : + Entity(pos), wallLength(length), wallThickness(thickness), - wallPosX(posX), - wallPosY(posY), wallOrientation(orientation), wallColor(_1), wallBuffer(constructWallBuffer(orientation, length, thickness)) @@ -28,16 +27,6 @@ Wall::Wall(int length, int thickness, float posX, float posY, Orientation orient this->wallBuffer.clear(this->wallColor); } -float Wall::getPosX() -{ - return this->wallPosX; -} - -float Wall::getPosY() -{ - return this->wallPosY; -} - DynamicPixelBuffer const& Wall::getWallBuffer() { return this->wallBuffer; @@ -50,19 +39,9 @@ void Wall::setWallColor(Color color) // Scenery stuff -------------------------------------------------------------------------- -Scenery::Scenery(float posX, float posY, SceneryType type) : - sceneryPosX(posX), - sceneryPosY(posY), - sType(type) {} - -float Scenery::getPosX() -{ - return this->sceneryPosX; -} - -float Scenery::getPosY() +Scenery::Scenery(Position const &pos, SceneryType type) + : Entity(pos), sType(type) { - return this->sceneryPosY; } SceneryType Scenery::getSceneryType() @@ -94,22 +73,20 @@ Level::Level(int width, int heigth, Player &player) : player(player) { int thickness = 6; - borders.push_back(Wall(width, thickness, 0.0f, 0.0f, Orientation::Horizontal)); - borders.push_back(Wall(width, thickness, 0.0f, static_cast<float>(heigth - thickness), Orientation::Horizontal)); - borders.push_back(Wall(heigth, thickness, 0.0f, 0.0f, Orientation::Vertical)); - borders.push_back(Wall(heigth, thickness, static_cast<float>(width - thickness), 0.0f, Orientation::Vertical)); + borders.push_back(Wall(width, thickness, { 0.0f, 0.0f }, Orientation::Horizontal)); + borders.push_back(Wall(width, thickness, { 0.0f, static_cast<float>(heigth - thickness )}, Orientation::Horizontal)); + borders.push_back(Wall(heigth, thickness, { 0.0f, 0.0f }, Orientation::Vertical)); + borders.push_back(Wall(heigth, thickness, { static_cast<float>(width - thickness), 0.0f }, Orientation::Vertical)); } -void Level::addWall(int length, int thickness, int posX, int posY, Orientation orientation) +void Level::addWall(int length, int thickness, float posX, float posY, Orientation orientation) { - Wall newWall(length, thickness, static_cast<float>(posX), static_cast<float>(posY), orientation); - wallVector.push_back(newWall); + wallVector.emplace_back(Wall{ length, thickness, { posX, posY }, orientation }); } void Level::addScenery(float posX, float posY, SceneryType type) { - Scenery newScenery(posX, posY, type); - sceneryVector.push_back(newScenery); + sceneryVector.emplace_back(Scenery{ { posX, posY }, type }); } void Level::addEnemy(std::unique_ptr<Enemy> enemy_) @@ -213,10 +190,10 @@ void Level::doDoSteps(FloatSeconds const &frame_time) } } -bool Level::collHelper(float vecPosX, float vecPosY, int vecWidth, int vecHeigth, float buffPosX, float buffPosY, int buffWidth, int buffHeigth) +bool Level::collHelper(Position const& vecPos, int vecWidth, int vecHeigth, float buffPosX, float buffPosY, int buffWidth, int buffHeigth) { - for(int x = static_cast<int>(vecPosX); x <= static_cast<int>(vecPosX) + vecWidth; ++x){ - for(int y = static_cast<int>(vecPosY); y <= static_cast<int>(vecPosY) + vecHeigth; ++y){ + for(int x = static_cast<int>(vecPos.X); x <= static_cast<int>(vecPos.X) + vecWidth; ++x){ + for(int y = static_cast<int>(vecPos.Y); y <= static_cast<int>(vecPos.Y) + vecHeigth; ++y){ if((x >= buffPosX && x <= buffPosX + buffWidth)&&(y >= buffPosY && y <= buffPosY + buffHeigth)){ return true; } @@ -228,13 +205,13 @@ bool Level::collHelper(float vecPosX, float vecPosY, int vecWidth, int vecHeigth bool Level::checkCollision(float posX, float posY, int bufferWidth, int bufferHeigth) { for(auto i = this->wallVector.begin(); i != this->wallVector.end(); i++ ){ - if(collHelper(i->getPosX(), i->getPosY(), i->getWallBuffer().w(), i->getWallBuffer().h(), posX, posY, bufferWidth, bufferHeigth)) return true; + if(collHelper(i->getTopLeft(), i->getWallBuffer().w(), i->getWallBuffer().h(), posX, posY, bufferWidth, bufferHeigth)) return true; } for(auto i = this->borders.begin(); i != this->borders.end(); i++ ){ - if(collHelper(i->getPosX(), i->getPosY(), i->getWallBuffer().w(), i->getWallBuffer().h(), posX, posY, bufferWidth, bufferHeigth)) return true; + if(collHelper(i->getTopLeft(), i->getWallBuffer().w(), i->getWallBuffer().h(), posX, posY, bufferWidth, bufferHeigth)) return true; } for(auto i = this->sceneryVector.begin(); i != this->sceneryVector.end(); i++ ){ - if(collHelper(i->getPosX(), i->getPosY(), i->getSceneryBuffer(i->getSceneryType()).w(), i->getSceneryBuffer(i->getSceneryType()).h(), posX, posY, bufferWidth, bufferHeigth)) return true; + if(collHelper(i->getTopLeft(), i->getSceneryBuffer(i->getSceneryType()).w(), i->getSceneryBuffer(i->getSceneryType()).h(), posX, posY, bufferWidth, bufferHeigth)) return true; } return false; } @@ -242,8 +219,7 @@ bool Level::checkCollision(float posX, float posY, int bufferWidth, int bufferHe /*Hauerch71498 Hitdetection*/ bool Level::HitEnemy(float posX, float posY, int bufferWidth, int bufferHeigth) { - auto playerPos=player.getTopLeft(); - if(collHelper(playerPos.X, playerPos.Y,player.getBuffer().w(),player.getBuffer().h(),posX,posY,bufferWidth,bufferHeigth)) + if(collHelper(player.getTopLeft(),player.getBuffer().w(),player.getBuffer().h(),posX,posY,bufferWidth,bufferHeigth)) { player.takeDamage(); return true; @@ -254,7 +230,7 @@ bool Level::HitEnemy(float posX, float posY, int bufferWidth, int bufferHeigth) bool Level::HitPlayer(float posX, float posY, int bufferWidth, int bufferHeigth) { for(auto &i : this->enemyVector){ - if(i != nullptr && collHelper(i->getX(), i->getY(), i->getCurrentSprite().w(), i->getCurrentSprite().h(), posX, posY, bufferWidth, bufferHeigth)) + if(i != nullptr && collHelper(i->getTopLeft(), i->getCurrentSprite().w(), i->getCurrentSprite().h(), posX, posY, bufferWidth, bufferHeigth)) { //HauerCh71498 //{ @@ -274,14 +250,14 @@ bool Level::HitPlayer(float posX, float posY, int bufferWidth, int bufferHeigth) bool Level::HitObstacle(float posX, float posY, int bufferWidth, int bufferHeigth) { for(auto &i : wallVector){ - if(collHelper(i.getPosX(), i.getPosY(), i.getWallBuffer().w(), i.getWallBuffer().h(), posX, posY, bufferWidth, bufferHeigth)) + if(collHelper(i.getTopLeft(), i.getWallBuffer().w(), i.getWallBuffer().h(), posX, posY, bufferWidth, bufferHeigth)) { i.takeDamage(); return true; } } for(auto &i : sceneryVector) { - if(collHelper(i.getPosX(), i.getPosY(), i.getSceneryBuffer(i.getSceneryType()).w(), i.getSceneryBuffer(i.getSceneryType()).h(), posX, posY, bufferWidth, bufferHeigth)) + if(collHelper(i.getTopLeft(), i.getSceneryBuffer(i.getSceneryType()).w(), i.getSceneryBuffer(i.getSceneryType()).h(), posX, posY, bufferWidth, bufferHeigth)) { i.takeDamage(); return true; @@ -295,13 +271,13 @@ DynamicPixelBuffer const& Level::draw() this->levelBuffer.clear(_t); for(auto i : this->wallVector){ - this->levelBuffer.blit_topleft({ static_cast<int>(i.getPosX()), static_cast<int>(i.getPosY()) }, i.getWallBuffer()); + this->levelBuffer.blit_topleft(i.getTopLeft(), i.getWallBuffer()); } for(auto i : this->borders){ - this->levelBuffer.blit_topleft({ static_cast<int>(i.getPosX()), static_cast<int>(i.getPosY()) }, i.getWallBuffer()); + this->levelBuffer.blit_topleft(i.getTopLeft(), i.getWallBuffer()); } for(auto i : this->sceneryVector){ - this->levelBuffer.blit_topleft({ static_cast<int>(i.getPosX()), static_cast<int>(i.getPosY()) }, i.getSceneryBuffer(i.getSceneryType())); + this->levelBuffer.blit_topleft(i.getTopLeft(), i.getSceneryBuffer(i.getSceneryType())); } this->levelBuffer.blit_topleft(this->player.getTopLeft(), this->player.getBuffer()); for(auto & i : this->enemyVector){ @@ -309,9 +285,9 @@ DynamicPixelBuffer const& Level::draw() auto hitTime = i->getHitTime(); if(hitTime > 0.0f) { HitShader hs = { i->getCurrentSprite(), hitTime, HIT_TIME }; - this->levelBuffer.blit_topleft({ static_cast<int>(i->getX()), static_cast<int>(i->getY()) }, hs); + this->levelBuffer.blit_topleft(i->getTopLeft(), hs); } else { - this->levelBuffer.blit_topleft({ static_cast<int>(i->getX()), static_cast<int>(i->getY()) }, i->getCurrentSprite()); + this->levelBuffer.blit_topleft(i->getTopLeft(), i->getCurrentSprite()); } } } diff --git a/Level.hpp b/Level.hpp index 6767b5a..26c30aa 100644 --- a/Level.hpp +++ b/Level.hpp @@ -27,18 +27,14 @@ enum class SceneryType class Wall : public Entity { public: - Wall(int length, int thickness, float posX, float posY, Orientation orientation); + Wall(int length, int thickness, Position const &pos, Orientation orientation); DynamicPixelBuffer constructWallBuffer(Orientation orientation, int length, int thickness); - float getPosX(); - float getPosY(); DynamicPixelBuffer const& getWallBuffer(); void setWallColor(Color color); private: int wallLength; int wallThickness; - float wallPosX; - float wallPosY; Orientation wallOrientation; Color wallColor; @@ -49,16 +45,11 @@ private: class Scenery : public Entity { public: - Scenery(float posX, float posY, SceneryType type); + Scenery(Position const &pos, SceneryType type); PixelBuffer const &getSceneryBuffer(SceneryType type); - float getPosX(); - float getPosY(); SceneryType getSceneryType(); private: - float sceneryPosX; - float sceneryPosY; - SceneryType sType; static StaticPixelBuffer<32, 16> constexpr DEAD_TREE = {{{ @@ -137,7 +128,7 @@ public: Level(int width, int heigth, Player &player); void addEnemyProjectile(const EnemyProjectile & projectile); void addPlayerProjectile(const PlayerProjectile& projectile); - void addWall(int length, int thickness, int posX, int posY, Orientation orientation); + void addWall(int length, int thickness, float posX, float posY, Orientation orientation); void addScenery(float posX, float posY, SceneryType type); void addEnemy(std::unique_ptr<Enemy> enemy_); void fillTrooperSpawner(float posX, float posY, unsigned int nmbrEnemies); @@ -167,7 +158,7 @@ private: std::vector<std::unique_ptr<Enemy>> enemyVector; std::vector<std::unique_ptr<Projectile>> projectileVector; std::vector<std::unique_ptr<Enemy>> trooperSpawner; - bool collHelper(float vecPosX, float vecPosY, int vecWidth, int vecHeigth, float buffPosX, float buffPosY, int buffWidth, int buffHeigth); + bool collHelper(Position const& vecPos, int vecWidth, int vecHeigth, float buffPosX, float buffPosY, int buffWidth, int buffHeigth); int score = 0; //hauerch71498 }; diff --git a/Tank.cpp b/Tank.cpp index a5fc356..50b8201 100644 --- a/Tank.cpp +++ b/Tank.cpp @@ -157,23 +157,23 @@ bool Tank::checkmove(FloatSeconds const & Frametime, Level &level) switch (this->direcion) { case 1: //down - if(!level.checkCollision(this->x, this->y - 3*Frametime.count(), this->getCurrentSprite().w(), this->getCurrentSprite().h())) - {this->y -= 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X, this->topleft.Y - 3*Frametime.count(), this->getCurrentSprite().w(), this->getCurrentSprite().h())) + {this->topleft.Y -= 3*Frametime.count(); return false;} else return true; break; case 2: - if(!level.checkCollision(this->x - 3*Frametime.count(), this->y, this->getCurrentSprite().w(), this->getCurrentSprite().h())) - {this->x -= 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X - 3*Frametime.count(), this->topleft.Y, this->getCurrentSprite().w(), this->getCurrentSprite().h())) + {this->topleft.X -= 3*Frametime.count(); return false;} else return true; break; case 3: - if(!level.checkCollision(this->x + 3*Frametime.count(),this->y, this->getCurrentSprite().w(), this->getCurrentSprite().h())) - {this->x += 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X + 3*Frametime.count(),this->topleft.Y, this->getCurrentSprite().w(), this->getCurrentSprite().h())) + {this->topleft.X += 3*Frametime.count(); return false;} else return true; break; default: - if(!level.checkCollision(this->x, this->y + 3*Frametime.count(), this->getCurrentSprite().w(), this->getCurrentSprite().h())) - {this->y += 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X, this->topleft.Y + 3*Frametime.count(), this->getCurrentSprite().w(), this->getCurrentSprite().h())) + {this->topleft.Y += 3*Frametime.count(); return false;} else return true; break; } @@ -198,9 +198,9 @@ void Tank::move(FloatSeconds const & Frametime, Level &level) void Tank::turnToPlayer(float PlayerX, float PlayerY) { - if(abs(this->x - PlayerX) < 5) //buffer of 5 + if(abs(this->topleft.X - PlayerX) < 5) //buffer of 5 { - if(this->y > PlayerY) + if(this->topleft.Y > PlayerY) { direcion = 1; //up } @@ -211,7 +211,7 @@ void Tank::turnToPlayer(float PlayerX, float PlayerY) } else { - if(this->x > PlayerX) + if(this->topleft.X > PlayerX) { direcion = 2; } diff --git a/Trooper.cpp b/Trooper.cpp index 6480b7b..5db3592 100644 --- a/Trooper.cpp +++ b/Trooper.cpp @@ -143,23 +143,23 @@ bool Trooper::checkmove(FloatSeconds const & Frametime, Level &level) switch (this->direcion) { case 1: //down - if(!level.checkCollision(this->x, this->y - 3*Frametime.count(), sprite.w(), sprite.h())) - {this->y -= 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X, this->topleft.Y - 3*Frametime.count(), sprite.w(), sprite.h())) + {this->topleft.Y -= 3*Frametime.count(); return false;} else return true; break; case 2: - if(!level.checkCollision(this->x - 3*Frametime.count(), this->y, sprite.w(), sprite.h())) - {this->x -= 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X - 3*Frametime.count(), this->topleft.Y, sprite.w(), sprite.h())) + {this->topleft.X -= 3*Frametime.count(); return false;} else return true; break; case 3: - if(!level.checkCollision(this->x + 3*Frametime.count(), this->y, sprite.w(), sprite.h())) - {this->x += 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X + 3*Frametime.count(), this->topleft.Y, sprite.w(), sprite.h())) + {this->topleft.X += 3*Frametime.count(); return false;} else return true; break; default: - if(!level.checkCollision(this->x, this->y + 3*Frametime.count(), sprite.w(), sprite.h())) - {this->y += 3*Frametime.count(); return false;} + if(!level.checkCollision(this->topleft.X, this->topleft.Y + 3*Frametime.count(), sprite.w(), sprite.h())) + {this->topleft.Y += 3*Frametime.count(); return false;} else return true; break; } @@ -184,9 +184,9 @@ void Trooper::move(FloatSeconds const & Frametime, Level &level) void Trooper::turnToPlayer(float PlayerX, float PlayerY) { - if(abs(this->x - PlayerX) < 5) //buffer of 5 + if(abs(this->topleft.X - PlayerX) < 5) //buffer of 5 { - if(this->y > PlayerY) + if(this->topleft.Y > PlayerY) { direcion = 1; //up } @@ -197,7 +197,7 @@ void Trooper::turnToPlayer(float PlayerX, float PlayerY) } else { - if(this->x > PlayerX) + if(this->topleft.X > PlayerX) { direcion = 2; } diff --git a/main.cpp b/main.cpp index 8125011..dd52c95 100644 --- a/main.cpp +++ b/main.cpp @@ -20,7 +20,7 @@ int screenWidth = 320; int screenHeigth = 320; // global helper funktion to add "buildings" -void addBuilding(int x, int y, int size, Level &level_ptr) +void addBuilding(float x, float y, int size, Level &level_ptr) { int thickness = 3; // adds a Wall to the level (length, thickness, position X, position Y, orientation) -- GitLab