Skip to content

Commit

Permalink
Add Score - Time - Coins indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
feresr committed May 24, 2020
1 parent 06fd6b5 commit 724f710
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 19 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ add_executable(smb
src/systems/EnemySystem.cpp
src/AABB.cpp
src/SoundManager.cpp
src/systems/SoundSystem.cpp)
src/systems/SoundSystem.cpp
src/systems/ScoreSystem.cpp
)


set_property(TARGET smb PROPERTY CXX_STANDARD 17)
Expand Down
Binary file modified assets/tileset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion include/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ struct FloatingPointsComponent : public Component {
};

struct TextComponent : public Component {
explicit TextComponent(std::string& text) : text{std::move(text)} {}
explicit TextComponent(std::string&& text) : text{std::move(text)} {}

std::string text;
SDL_Texture* texture = nullptr;
Expand All @@ -268,6 +268,12 @@ struct TextComponent : public Component {
}
};

struct AddScoreComponent : public Component {
explicit AddScoreComponent(int score, bool addCoin = false) : score{score}, coin{addCoin} {}
int score = 0;
bool coin = false;
};

struct TileMapComponent : public Component {
TileMapComponent(uint16_t width, uint16_t height) : mapWidth{width},
mapHeight{height},
Expand Down
1 change: 1 addition & 0 deletions include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "systems/AnimationSystem.h"
#include "systems/PlayerSystem.h"
#include "systems/SoundSystem.h"
#include "systems/ScoreSystem.h"
#include "Constants.h"

class Game {
Expand Down
3 changes: 3 additions & 0 deletions include/TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ enum TextureId {
CASTLE_6,
CASTLE_7,
CASTLE_8,
COIN_SMALL_1,
COIN_SMALL_2,
COIN_SMALL_3,
};

class TextureManager {
Expand Down
2 changes: 1 addition & 1 deletion include/systems/RenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RenderSystem : public System {
TextureManager* textureManager{};
CameraComponent* camera{};

void renderEntities(std::vector<Entity*> entities);
void renderEntities(std::vector<Entity*> entities, bool followCamera = true);

void renderText(std::vector<Entity*> entities);

Expand Down
27 changes: 27 additions & 0 deletions include/systems/ScoreSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "ecs/ecs.h"
#include "Components.h"
#include "Constants.h"

class ScoreSystem : public System {

void onAddedToWorld(World* world) override;

void tick(World* world) override;

void handleEvent(SDL_Event& event) override;

void onRemovedFromWorld(World* world) override;

~ScoreSystem() override = default;

private:
Entity* scoreEntity;
Entity* coinsEntity;
Entity* timeLeftEntity;

int score = 0;
int coins = 0;
int time = 255 * 60;
};
1 change: 1 addition & 0 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void Game::init(const char* title, int width, int height, bool fullscreen) {
world.registerSystem(new EnemySystem());
world.registerSystem(new CallbackSystem());
world.registerSystem(new AnimationSystem());
world.registerSystem(new ScoreSystem());
world.registerSystem(new TileSystem());
world.registerSystem(new PhysicsSystem());
}
Expand Down
4 changes: 4 additions & 0 deletions src/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ TextureManager::TextureManager(SDL_Renderer* renderer) : renderer{renderer} {
textures.insert_or_assign(CASTLE_6, new SDL_Rect{34, 85, TILE_SIZE, TILE_SIZE});
textures.insert_or_assign(CASTLE_7, new SDL_Rect{17, 102, TILE_SIZE, TILE_SIZE});
textures.insert_or_assign(CASTLE_8, new SDL_Rect{34, 102, TILE_SIZE, TILE_SIZE});

textures.insert_or_assign(COIN_SMALL_1, new SDL_Rect{119, 254, 5, 8});
textures.insert_or_assign(COIN_SMALL_2, new SDL_Rect{129, 254, 5, 8});
textures.insert_or_assign(COIN_SMALL_3, new SDL_Rect{124, 262, 5, 8});
}

void TextureManager::renderTexture(TextureId textureId, SDL_Rect& dstRect, bool flipH, bool flipV) {
Expand Down
4 changes: 4 additions & 0 deletions src/systems/EnemySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void turtleShellInteractions(World* world, Entity* shell) {
auto transform = other->get<TransformComponent>();
if (AABBCollision(shell->get<TransformComponent>(), other->get<TransformComponent>())) {
world->create()->assign<FloatingPointsComponent>("100", transform->getCenterX(), transform->y);
world->create()->assign<AddScoreComponent>(100);
flipEnemy(other);
shell->remove<LeftCollisionComponent>();
shell->remove<RightCollisionComponent>();
Expand Down Expand Up @@ -92,11 +93,13 @@ void EnemySystem::tick(World* world) {
enemy->assign<TextureComponent>(TextureId::GOOMBA_CRUSHED);
enemy->assign<TransformComponent>(*enemyTransform);
world->create()->assign<FloatingPointsComponent>("100", enemyTransform->getCenterX(), enemyTransform->y);
world->create()->assign<AddScoreComponent>(100);
}
break;
case Enemy::Type::TURTLE:
stepOnTurtle(enemy);
world->create()->assign<FloatingPointsComponent>("200", enemyTransform->getCenterX(), enemyTransform->y);
world->create()->assign<AddScoreComponent>(200);
break;
case Enemy::Type::TURTLE_SHELL: {
if (enemy->get<KineticComponent>()->accX == 0) {
Expand Down Expand Up @@ -133,6 +136,7 @@ void EnemySystem::tick(World* world) {
if (bottomTile && bottomTile->has<BreakableComponent>()) {
if (bottomTile->get<BreakableComponent>()->hit) {
world->create()->assign<FloatingPointsComponent>("100", transform->getCenterX(), transform->y);
world->create()->assign<AddScoreComponent>(100);
flipEnemy(entity);
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/systems/MapSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ void MapSystem::tick(World* world) {
|| transform->left() > camera->right() + CAMERA_WORLD_OFFSET
|| transform->top() > camera->bottom()
) {
tileMap->set(
(int) (transform->getCenterX() / TILE_SIZE),
(int) (transform->getCenterY() / TILE_SIZE),
nullptr
);
world->destroy(entity);
if (entity->has<TileComponent>()) {
tileMap->set(
(int) (transform->getCenterX() / TILE_SIZE),
(int) (transform->getCenterY() / TILE_SIZE),
nullptr
);
}

if (!entity->has<TextComponent>()) world->destroy(entity);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/systems/PlayerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ void PlayerSystem::tick(World* world) {
if (sprint) kinetic->accX *= 1.5;
if (jump) {
player->get<KineticComponent>()->accY = -MARIO_JUMP_ACCELERATION;
auto jumpsound = world->create();
jumpsound->assign<SoundComponent>(Sound::Id::JUMP);
world->create()->assign<SoundComponent>(Sound::Id::JUMP);
}
if ((bool) std::abs(kinetic->speedX) || (bool) std::abs(kinetic->accX)) {
if ((kinetic->speedX > 0 && kinetic->accX < 0) ||
Expand Down Expand Up @@ -303,6 +302,7 @@ void PlayerSystem::eatMushroom(World* world) {
player->get<TransformComponent>()->getCenterX(),
player->get<TransformComponent>()->y
);
world->create()->assign<AddScoreComponent>(1000);
if (player->has<SuperMarioComponent>()) return;
player->assign<SuperMarioComponent>();
player->assign<AnimationComponent>(
Expand Down Expand Up @@ -386,5 +386,5 @@ void PlayerSystem::onAddedToWorld(World* world) {
player->assign<GravityComponent>();
player->assign<SolidComponent>();
player->assign<KineticComponent>();
player->assign<TransformComponent>(40, 40, TILE_SIZE - 4, SMALL_MARIO_COLLIDER_HEIGHT);
player->assign<TransformComponent>(40, 140, TILE_SIZE - 4, SMALL_MARIO_COLLIDER_HEIGHT);
}
16 changes: 11 additions & 5 deletions src/systems/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void RenderSystem::tick(World* world) {
renderEntities(world->find<TileComponent, TransformComponent, TextureComponent>());
renderEntities(world->find<EnemyComponent, TransformComponent, TextureComponent>());
renderEntities(world->find<PlayerComponent, TransformComponent, TextureComponent>());
renderEntities(world->find<TextComponent, TransformComponent, TextureComponent>(), false);
renderText(world->find<TextComponent, TransformComponent>());

//Editor
Expand Down Expand Up @@ -68,12 +69,17 @@ RenderSystem::~RenderSystem() {
SDL_DestroyRenderer(renderer);
}

void RenderSystem::renderEntities(std::vector<Entity*> entities) {
void RenderSystem::renderEntities(std::vector<Entity*> entities, bool followCamera) {
for (auto entity : entities) {
auto transform = entity->get<TransformComponent>();
auto texture = entity->get<TextureComponent>();
dstRect.x = transform->left() - camera->left() + texture->offSetX;
dstRect.y = transform->top() - camera->top() + texture->offSetY;
if (followCamera) {
dstRect.x = transform->left() - camera->left() + texture->offSetX;
dstRect.y = transform->top() - camera->top() + texture->offSetY;
} else {
dstRect.x = transform->left() + texture->offSetX;
dstRect.y = transform->top() + texture->offSetY;
}

dstRect.w = texture->w > 0 ? texture->w : transform->w;
dstRect.h = texture->h > 0 ? texture->h : transform->h;
Expand All @@ -93,8 +99,8 @@ void RenderSystem::renderText(std::vector<Entity*> entities) {
SDL_FreeSurface(surface);
}

dstRect.x = transformComponent->left() - camera->left();
dstRect.y = transformComponent->top() - camera->top();
dstRect.x = transformComponent->left();
dstRect.y = transformComponent->top();

dstRect.w = transformComponent->w;
dstRect.h = transformComponent->h;
Expand Down
123 changes: 123 additions & 0 deletions src/systems/ScoreSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "systems/ScoreSystem.h"

void ScoreSystem::onAddedToWorld(World* world) {
System::onAddedToWorld(world);

auto paddingH = 22;
auto paddingV = 12;
auto textHeight = 10;
auto spacingV = -3;

auto availableWidth = SNES_RESOLUTION_WIDTH - paddingH * 2;
auto columns = 4;
auto columnWidth = availableWidth / columns;

//FIRST COLUMN (left aligned)
auto marioEntity = world->create();
marioEntity->assign<TextComponent>("mario");
marioEntity->assign<TransformComponent>(paddingH, paddingV, 40, textHeight);

scoreEntity = world->create();
scoreEntity->assign<TextComponent>("000000");
scoreEntity->assign<TransformComponent>(paddingH, paddingV + textHeight + spacingV, 48, textHeight);

// SECOND COLUMN (center aligned)
coinsEntity = world->create();
coinsEntity->assign<TextComponent>("x00");
auto w = 24;
coinsEntity->assign<TransformComponent>(paddingH + columnWidth + (columnWidth - w) / 2 + 5,
paddingV + textHeight + spacingV, w, textHeight);
auto coinIco = world->create();
coinIco->assign<TextureComponent>(TextureId::COIN_SMALL_1);
coinIco->assign<AnimationComponent>(
std::vector<TextureId>{
TextureId::COIN_SMALL_1,
TextureId::COIN_SMALL_1,
TextureId::COIN_SMALL_1,
TextureId::COIN_SMALL_3,
TextureId::COIN_SMALL_2,
TextureId::COIN_SMALL_3
}, 10);

coinIco->assign<TextComponent>("");
coinIco->assign<TransformComponent>(
paddingH + columnWidth + (columnWidth - w) / 2 - 4,
paddingV + textHeight + spacingV + 1, 5, 8
);

// THIRD COLUMN (center aligned)
auto worldEntity = world->create();
worldEntity->assign<TextComponent>("world");
w = 40;
worldEntity->assign<TransformComponent>(paddingH + columnWidth * 2 + (columnWidth - w) / 2,
paddingV, w, textHeight);

auto worldNumberEntity = world->create();
worldNumberEntity->assign<TextComponent>("1 1");
w = 20;
worldNumberEntity->assign<TransformComponent>(paddingH + columnWidth * 2 + (columnWidth - w) / 2,
paddingV + textHeight + spacingV, w, textHeight);

// FOURTH COLUMN (right aligned)
auto timeEntity = world->create();
timeEntity->assign<TextComponent>("time");
timeEntity->assign<TransformComponent>(SNES_RESOLUTION_WIDTH - paddingH - 32, paddingV, 32, textHeight);

timeLeftEntity = world->create();
timeLeftEntity->assign<TextComponent>("000");
timeLeftEntity->assign<TransformComponent>(SNES_RESOLUTION_WIDTH - paddingH - 24,
paddingV + textHeight + spacingV, 24, textHeight);
}

void ScoreSystem::tick(World* world) {
bool updateScore = false;
bool updateCoins = false;

for (auto e : world->find<AddScoreComponent>()) {
auto s = e->get<AddScoreComponent>();
score += s->score;
updateScore = true;
if (s->coin) {
coins++;
updateCoins = true;
}
world->destroy(e);
}

if (updateScore) {
auto scoreString = std::to_string(score);
auto final = std::string{};
auto zeros = 6 - scoreString.length();
while (zeros > 0) {
zeros--;
final += '0';
}
final += scoreString;
scoreEntity->assign<TextComponent>(std::move(final));
}

if (updateCoins) {
auto final = std::string{};
auto coinString = std::to_string(coins);
auto zeros = 2 - coinString.length();
while (zeros > 0) {
zeros--;
final += '0';
}
final += coinString;
coinsEntity->assign<TextComponent>("x" + final);
}

time--;
timeLeftEntity->assign<TextComponent>(std::to_string(time / 60));
}

void ScoreSystem::handleEvent(SDL_Event& event) {
System::handleEvent(event);
}

void ScoreSystem::onRemovedFromWorld(World* world) {
System::onRemovedFromWorld(world);
world->destroy(coinsEntity);
world->destroy(scoreEntity);
}
7 changes: 5 additions & 2 deletions src/systems/TileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void createCoin(World* world, Entity* block) {
coin->assign<CallbackComponent>([=]() {
auto transform = coin->get<TransformComponent>();
world->create()->assign<FloatingPointsComponent>("100", transform->getCenterX(), transform->y);
world->create()->assign<AddScoreComponent>(100, true);
world->destroy(coin);
}, 20);

Expand Down Expand Up @@ -132,13 +133,15 @@ void TileSystem::tick(World* world) {
entity->remove<RightCollisionComponent>();
}


for (auto points: world->find<FloatingPointsComponent>()) {
auto camera = world->findFirst<CameraComponent>()->get<CameraComponent>();
auto pointsComponent = points->get<FloatingPointsComponent>();
auto textLength = pointsComponent->text.length();
auto pointEntity = world->create();
pointEntity->assign<TextComponent>(pointsComponent->text);
pointEntity->assign<TextComponent>(std::move(pointsComponent->text));
auto w = textLength * 2;
pointEntity->assign<TransformComponent>(pointsComponent->x - w / 2, pointsComponent->y, w, 14);
pointEntity->assign<TransformComponent>((pointsComponent->x - w / 2) - camera->left(), pointsComponent->y, w, 14);
pointEntity->assign<KineticComponent>(0, 0);
pointEntity->get<KineticComponent>()->speedY = -2.0f;
pointEntity->assign<CallbackComponent>([=]() { world->destroy(pointEntity); }, 50);
Expand Down

0 comments on commit 724f710

Please sign in to comment.