Add death drop

This commit is contained in:
zvon 2020-07-28 18:13:38 +02:00
parent 9c1fef7a86
commit 847b5cbd8e
2 changed files with 22 additions and 11 deletions

View File

@ -5,6 +5,7 @@
#define PLAYER_ID 0x00000001
#define STONE_ID 0x00000002
#define DEATH 0x00000003
class Player : public SDLPP::RectangleRender {
public:
@ -74,13 +75,13 @@ bool quit = false;
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
std::shared_ptr<SDLPP::RectangleRender> stone;
double posx = 0;
while(posx < 1) {
while(posx < 3) {
stone = std::make_shared<SDLPP::RectangleRender>(posx,0.5,0.15,0.1,r);
stone->addCollision(SDLPP::Rect(0,0,1,1));
stone->setTexture("stone.png");
stone->setId(STONE_ID);
scene.addObject(stone);
posx += 0.15;
posx += 0.45;
}
auto x = std::make_shared<Player>(0,0,0.2,0.2, r);
x->addCollision(SDLPP::Rect(0,0,1,1));
@ -88,6 +89,10 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
x->setId(PLAYER_ID);
scene.addObject(x);
player = x;
auto z = std::make_shared<SDLPP::RectangleRender>(0,2.5,100,100,r);
z->addCollision(SDLPP::Rect(0,0,1,1));
z->setId(DEATH);
scene.addObject(z);
}
void quitGame() {
@ -175,8 +180,6 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
int base = SDL_GetTicks();
int frames = 0;
while(!quit) {
SDL_framerateDelay(&gFPS);
pollEvents(*scene);
@ -194,14 +197,12 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
if(player->isGravityEnabled())
player->setLastStand();
}
if( x->getId() == DEATH ) {
std::cout << "Oh no, you died!" << std::endl;
quitGame();
}
}
player->setGravity(gravity);
frames++;
if(SDL_GetTicks() - base >= 1000) {
base = SDL_GetTicks();
printf("FPS: %d\n", frames);
frames = 0;
}
}
}
@ -220,6 +221,9 @@ int main() {
player->setMovementSpeed(0.3);
player->enableGravity();
int base = SDL_GetTicks();
int frames = 0;
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
@ -228,6 +232,12 @@ int main() {
SDL_framerateDelay(&gFPS);
main_scene->renderScene();
main_scene->presentScene();
frames++;
if(SDL_GetTicks() - base >= 1000) {
base = SDL_GetTicks();
printf("FPS: %d\n", frames);
frames = 0;
}
}
inputThread.join();
}

View File

@ -334,7 +334,8 @@ public:
setTexture(texture);
}
virtual void render() {
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect);
if(texture != NULL)
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect);
}
virtual void move(int ticks) {
auto dimension = renderer->getSmallerSide();