Add hiding of stuff and scene pause

This commit is contained in:
zvon 2020-08-21 18:54:42 +02:00
parent 77d5f9a8cb
commit ea7dff32a2
2 changed files with 20 additions and 4 deletions

View File

@ -7,6 +7,8 @@
#define STONE_ID 0x00000002
#define DEATH 0x00000003
bool pause = false;
class Player : public SDLPP::RectangleRender {
public:
Player(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, double _max_gravity = 1.0, uint32_t _max_gravity_time = 1000) : SDLPP::RectangleRender(x,y,w,h,r) {
@ -139,6 +141,11 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
break;
case SDLK_SPACE:
if(pause)
scene.setPrevTicks(SDL_GetTicks());
pause = !pause;
break;
default:
scene.setBackground(bgtextures[4]);
break;
@ -191,6 +198,9 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
while(!quit) {
SDL_framerateDelay(&gFPS);
pollEvents(*scene);
if(pause)
continue;
std::cout << "NOOO" << std::endl;
scene->movement();
bool gravity = true;
for( auto &x : scene->getCollisions(*player) ) {
@ -204,6 +214,7 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
player->setPos(newPX, newPY);
if(player->isGravityEnabled())
player->setLastStand();
x->setHidden(true);
}
if( x->getId() == DEATH ) {
std::cout << "Oh no, you died!" << std::endl;

View File

@ -172,7 +172,7 @@ public:
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() = 0;
virtual void setPos(double x, double y) = 0;
bool colidesWith(const RenderObject &other) const {
if(!hasCollisions() || !other.hasCollisions()) {
if(!hasCollisions() || !other.hasCollisions() || getHidden() || other.getHidden()) {
return false;
}
for( const auto &x : collisions ) {
@ -226,7 +226,7 @@ public:
void setHidden(bool hid) {
hidden = hid;
}
bool getHidden() {
bool getHidden() const {
return hidden;
}
void setColiderColor(const std::string &color) {
@ -283,6 +283,8 @@ public:
prev_ticks = now_ticks;
}
std::vector<std::shared_ptr<RenderObject>> getCollisions(RenderObject &r) {
if(r.getHidden())
return {};
std::vector<std::shared_ptr<RenderObject>> ret{};
for(const auto &x : collisionObjects) {
if(x->colidesWith(r)) {
@ -336,6 +338,9 @@ public:
Renderer &getRenderer() {
return *renderer;
}
void setPrevTicks(int ticks) {
prev_ticks = ticks;
}
private:
std::vector<std::shared_ptr<RenderObject>> renderObjects;
std::vector<std::shared_ptr<RenderObject>> collisionObjects;
@ -369,9 +374,9 @@ public:
setTexture(texture);
}
virtual void render() {
if(texture != NULL)
if(texture != NULL && !getHidden())
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect);
if(hasCollisions() && renderer->getRenderColiders()) {
if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) {
for(const auto &col : getCollisions())
col->render(*renderer, colider_color);
}