Destroyable

This commit is contained in:
2020-08-21 21:40:15 +02:00
parent eaa463fb85
commit 618786f885
2 changed files with 94 additions and 6 deletions
+43 -6
View File
@@ -7,6 +7,8 @@
#define STONE_ID 0x00000002
#define DEATH 0x00000003
#define DESTROYABLE_DESTROY 0x00000001
bool pause = false;
class Player : public SDLPP::RectangleRender {
@@ -82,15 +84,51 @@ private:
bool jumping = false;
};
class Destroyable : public SDLPP::RectangleRender {
public:
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r) : SDLPP::RectangleRender(x,y,w,h,r) {}
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, int destruction_time) : SDLPP::RectangleRender(x,y,w,h,r) {
destruction_countdown = destruction_time;
}
virtual void specialAction(int code) {
if(code == DESTROYABLE_DESTROY)
startDestruction();
}
virtual void move(int ticks) {
/* copied from sdlpp*/
auto dimension = renderer->getSmallerSide();
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*(static_cast<double>(ticks)/1000);
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000);
x_ += addx;
y_ += addy;
rect.x = x_ * dimension;
rect.y = y_ * dimension;
for( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
}
if(destruction) {
destruction_countdown -= ticks;
if(destruction_countdown <= 0)
destroy();
}
}
private:
void startDestruction() {
destruction = true;
}
bool destruction = false;
int destruction_countdown = 3000;
};
std::shared_ptr<Player> player;
std::vector<std::shared_ptr<SDLPP::Texture>> bgtextures;
bool quit = false;
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
std::shared_ptr<SDLPP::RectangleRender> stone;
std::shared_ptr<Destroyable> stone;
double posx = 0;
while(posx < 3) {
stone = std::make_shared<SDLPP::RectangleRender>(posx,0.5,0.15,0.1,r);
stone = std::make_shared<Destroyable>(posx,0.5,0.15,0.1,r, 1000);
stone->addCollision(SDLPP::Rect(0,0,1,1));
stone->setTexture("stone.png");
stone->setId(STONE_ID);
@@ -146,7 +184,7 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
player->addMovement(1,0);
break;
case SDLK_w:
if(!player->isJumping()) {
if(!player->isJumping() && !pause) {
player->setLastStand();
player->jump();
}
@@ -176,7 +214,6 @@ void handleKeyUp(SDL_Keycode key) {
player->addMovement(-1,0);
break;
case SDLK_w:
break;
case SDLK_s:
default:
break;
@@ -228,7 +265,7 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
player->setPos(newPX, newPY);
player->setLastStand();
}
// x->setHidden(true);
x->specialAction(DESTROYABLE_DESTROY);
}
if( x->getId() == DEATH ) {
std::cout << "Oh no, you died!" << std::endl;
@@ -273,7 +310,7 @@ int main() {
SDL_setFramerate(&gFPS, 60);
std::thread inputThread(doInput, main_scene);
while( !quit ) {
SDL_framerateDelay(&gFPS);
// SDL_framerateDelay(&gFPS);
main_scene->renderScene();
main_scene->presentScene();
frames++;