From e5ba46c81201a2310d4d16b6aac7b404d7a10680 Mon Sep 17 00:00:00 2001 From: Fernando Raviola Date: Sat, 23 May 2020 22:55:19 -0300 Subject: [PATCH] Timeout --- include/Components.h | 2 ++ include/Constants.h | 2 +- src/systems/MapSystem.cpp | 6 ++++-- src/systems/PlayerSystem.cpp | 15 +++++++++++++++ src/systems/ScoreSystem.cpp | 14 +++++++++++++- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/include/Components.h b/include/Components.h index eb9b0bd..96a7f97 100644 --- a/include/Components.h +++ b/include/Components.h @@ -114,6 +114,8 @@ struct FrozenComponent : public Component { struct DeadComponent : public Component { }; +struct GameOverComponent : public Component { +}; struct AnimationComponent : public Component { explicit AnimationComponent( diff --git a/include/Constants.h b/include/Constants.h index 1e23665..a077568 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -17,7 +17,7 @@ constexpr int SKY_BLUE = 254; constexpr int EDITOR_CAMERA_PAN_SPEED = 3; // Instantiate ${CAMERA_WORLD_OFFSET} tiles off camera from the left and right -constexpr int CAMERA_WORLD_OFFSET = 3 * TILE_SIZE; +constexpr int CAMERA_WORLD_OFFSET = 4 * TILE_SIZE; constexpr float GRAVITY = .040; constexpr float FRICTION = .94; diff --git a/src/systems/MapSystem.cpp b/src/systems/MapSystem.cpp index 2cfeb55..2b3fbb9 100644 --- a/src/systems/MapSystem.cpp +++ b/src/systems/MapSystem.cpp @@ -35,7 +35,9 @@ void MapSystem::tick(World* world) { ); } - if (!entity->has()) world->destroy(entity); + if (entity->has()) continue; + if (entity->has()) continue; + world->destroy(entity); } } @@ -46,7 +48,7 @@ void MapSystem::tick(World* world) { if (tile->x > camera->right() + CAMERA_WORLD_OFFSET) break; auto entity = world->create(); entity->assign(tile->x, tile->y, tile->w, tile->h); - if (tile->hasProperty(VISIBLE)) { + if (tile->hasProperty(VISIBLE)) { entity->assign(tile->textureId); } if (tile->hasProperty(MASS)) entity->assign(); diff --git a/src/systems/PlayerSystem.cpp b/src/systems/PlayerSystem.cpp index 1358219..cb6d448 100644 --- a/src/systems/PlayerSystem.cpp +++ b/src/systems/PlayerSystem.cpp @@ -70,6 +70,17 @@ void createDebris(World* world, TransformComponent* brickTransform) { } void onGameOver(World* world, Entity* player) { + if (player->has()) { + //player time ran out while being Super Mario + player->remove(); + auto texture = player->get(); + auto transform = player->get(); + transform->h = SMALL_MARIO_COLLIDER_HEIGHT; + transform->y += SUPER_MARIO_COLLIDER_HEIGHT - SMALL_MARIO_COLLIDER_HEIGHT; + texture->h = TILE_SIZE; + texture->offSetY = -1; + } + player->assign(); world->create()->assign(Sound::Id::DEATH); auto music = world->findFirst(); if (music) world->destroy(music); @@ -177,6 +188,10 @@ void PlayerSystem::setAnimation(ANIMATION_STATE state) { } void PlayerSystem::tick(World* world) { + if (world->findFirst()) { + world->findFirst()->remove(); + onGameOver(world, player); + } if (player->has()) return; auto transform = player->get(); diff --git a/src/systems/ScoreSystem.cpp b/src/systems/ScoreSystem.cpp index b10ebd5..fc5ac3d 100644 --- a/src/systems/ScoreSystem.cpp +++ b/src/systems/ScoreSystem.cpp @@ -109,7 +109,19 @@ void ScoreSystem::tick(World* world) { } time--; - timeLeftEntity->assign(std::to_string(time / 60)); + if (time >= 0) { + auto final = std::string{}; + auto timeString = std::to_string(time/60); + auto zeros = 3 - timeString.length(); + while (zeros > 0) { + zeros--; + final += '0'; + } + final += timeString; + timeLeftEntity->assign(std::move(final)); + if (time <= 0) timeLeftEntity->assign(); + } + } void ScoreSystem::handleEvent(SDL_Event& event) {