Skip to content

Commit

Permalink
Introduce scenes, editor instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
feresr committed May 24, 2020
1 parent e5ba46c commit 5e6436b
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ add_executable(smb
src/SoundManager.cpp
src/systems/SoundSystem.cpp
src/systems/ScoreSystem.cpp
src/scenes/GameScene.cpp
src/scenes/EditorScene.cpp
)


Expand Down
17 changes: 3 additions & 14 deletions include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@
#include "SDL.h"
#include "SDL_image.h"
#include "ecs/ecs.h"
#include "systems/EditorSystem.h"
#include "systems/RenderSystem.h"
#include "systems/EditorSystem.h"
#include "systems/PhysicsSystem.h"
#include "systems/CallbackSystem.h"
#include "systems/EnemySystem.h"
#include "systems/TileSystem.h"
#include "systems/MapSystem.h"
#include "systems/AnimationSystem.h"
#include "systems/PlayerSystem.h"
#include "systems/SoundSystem.h"
#include "systems/ScoreSystem.h"
#include "scenes/GameScene.h"
#include "scenes/EditorScene.h"
#include "Constants.h"

class Game {
Expand All @@ -36,6 +26,5 @@ class Game {
bool isRunning;
SDL_Window* window;
SDL_Event event;
World world;
EditorSystem* editorSystem;
Scene* currentScene;
};
11 changes: 11 additions & 0 deletions include/scenes/EditorScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "scenes/Scene.h"

class EditorScene : public Scene {

public:
EditorScene(SDL_Window* window);

private:
void update() override;

};
11 changes: 11 additions & 0 deletions include/scenes/GameScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "scenes/Scene.h"

class GameScene : public Scene {

public:
GameScene(SDL_Window* window);

private:
void update() override;

};
32 changes: 32 additions & 0 deletions include/scenes/Scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "systems/EditorSystem.h"
#include "systems/RenderSystem.h"
#include "systems/EditorSystem.h"
#include "systems/PhysicsSystem.h"
#include "systems/CallbackSystem.h"
#include "systems/EnemySystem.h"
#include "systems/TileSystem.h"
#include "systems/MapSystem.h"
#include "systems/AnimationSystem.h"
#include "systems/PlayerSystem.h"
#include "systems/SoundSystem.h"
#include "systems/ScoreSystem.h"
#include "Constants.h"

class Scene {
protected:
World* world;

public:

virtual void update() {};
virtual void handleEvents(SDL_Event& event) {
world->handleEvent(event);
};

virtual ~Scene() {
std::cout << "Scene deleted" << std::endl;
delete world;
};
};
9 changes: 8 additions & 1 deletion include/systems/PlayerSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class PlayerSystem : public System {

std::function<void(void)> gameOverCallback;

Entity* player;
CameraComponent* camera;

Expand All @@ -30,9 +32,14 @@ class PlayerSystem : public System {

void onRemovedFromWorld(World* world) override;

void onGameOver(World* world, Entity* player);

~PlayerSystem() override = default;

private:
void setAnimation(ANIMATION_STATE animationState);

void eatMushroom(World* world);

public:
PlayerSystem(std::function<void(void)> gameOverCallback) : gameOverCallback{gameOverCallback} {}
};
33 changes: 12 additions & 21 deletions src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "Game.h"

bool restartGame = false;

void Game::init(const char* title, int width, int height, bool fullscreen) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
Expand All @@ -17,24 +19,13 @@ void Game::init(const char* title, int width, int height, bool fullscreen) {
}

isRunning = true;
world.registerSystem(new SoundSystem());
world.registerSystem(new RenderSystem(window, SNES_RESOLUTION_WIDTH, SNES_RESOLUTION_HEIGHT));
world.registerSystem(new PlayerSystem());
world.registerSystem(new MapSystem());
world.registerSystem(new EnemySystem());
world.registerSystem(new CallbackSystem());
world.registerSystem(new AnimationSystem());
world.registerSystem(new ScoreSystem());
world.registerSystem(new TileSystem());
world.registerSystem(new PhysicsSystem());
currentScene = new GameScene(window);
}

void gameOver() {
SDL_Delay(800);
}

void Game::update() {
world.tick();
currentScene->update();
if (restartGame) {}
}

bool Game::running() const { return isRunning; }
Expand All @@ -46,17 +37,17 @@ void Game::handleEvents() {
isRunning = false;
return;
}
world.handleEvent(event);
currentScene->handleEvents(event);
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_E:
if (editorSystem) {
world.unregisterSystem(editorSystem);
editorSystem = nullptr;
if (dynamic_cast<GameScene*>(currentScene)) {
delete currentScene;
currentScene = new EditorScene(window);
} else {
editorSystem = new EditorSystem();
world.registerSystem(editorSystem);
delete currentScene;
currentScene = new GameScene(window);
}
break;
default:
Expand All @@ -65,10 +56,10 @@ void Game::handleEvents() {
break;
}
}

}

void Game::clean() {
delete currentScene;
if (window) SDL_DestroyWindow(window);
SDL_Quit();
std::cout << "Game cleaned" << std::endl;
Expand Down
42 changes: 42 additions & 0 deletions src/scenes/EditorScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "scenes/EditorScene.h"

EditorScene::EditorScene(SDL_Window* window) {
world = new World();
world->registerSystem(new RenderSystem(window, SNES_RESOLUTION_WIDTH, SNES_RESOLUTION_HEIGHT));
world->registerSystem(new EditorSystem());
world->registerSystem(new TileSystem());
world->registerSystem(new PhysicsSystem());


//FIRST COLUMN (left aligned)
auto title = world->create();
title->assign<TextComponent>("MAP EDITOR MODE");
title->assign<TransformComponent>(10, 10, 60, 8);

auto instructions1 = world->create();
instructions1->assign<TextComponent>("up down keys change tiles");
instructions1->assign<TransformComponent>(10, 20, 80, 5);

auto instructions2 = world->create();
instructions2->assign<TextComponent>("press keys a and d to pan camera");
instructions2->assign<TransformComponent>(10, 26, 90, 5);

auto instructions3 = world->create();
instructions3->assign<TextComponent>("press s to save the map to disk");
instructions3->assign<TransformComponent>(10, 32, 86, 5);

auto instructions4 = world->create();
instructions4->assign<TextComponent>("click to add and remove tiles");
instructions4->assign<TransformComponent>(10, 38, 80, 5);

auto instructions5 = world->create();
instructions5->assign<TextComponent>("press e to exit the editor");
instructions5->assign<TransformComponent>(10, 44, 80, 5);
}

void EditorScene::update() {
Scene::update();
world->tick();
}


22 changes: 22 additions & 0 deletions src/scenes/GameScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "scenes/GameScene.h"

GameScene::GameScene(SDL_Window* window) {
world = new World();
world->registerSystem(new SoundSystem());
world->registerSystem(new RenderSystem(window, SNES_RESOLUTION_WIDTH, SNES_RESOLUTION_HEIGHT));
world->registerSystem(new PlayerSystem([](){}));
world->registerSystem(new MapSystem());
world->registerSystem(new EnemySystem());
world->registerSystem(new CallbackSystem());
world->registerSystem(new AnimationSystem());
world->registerSystem(new ScoreSystem());
world->registerSystem(new TileSystem());
world->registerSystem(new PhysicsSystem());
}

void GameScene::update() {
Scene::update();
world->tick();
}


3 changes: 3 additions & 0 deletions src/systems/EditorSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ void EditorSystem::onAddedToWorld(World* world) {
int mh;
infile.read(reinterpret_cast<char*>(&mw), sizeof(uint16_t));
infile.read(reinterpret_cast<char*>(&mh), sizeof(uint16_t));

world->create()->assign<TileMapComponent>(mw, mh);

TileType texture;

for (int x = 0; x < DEFAULT_MAP_WIDTH; x++) {
Expand Down
4 changes: 3 additions & 1 deletion src/systems/PlayerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void createDebris(World* world, TransformComponent* brickTransform) {
world->create()->assign<SoundComponent>(Sound::Id::BLOCK_BREAK);
}

void onGameOver(World* world, Entity* player) {
void PlayerSystem::onGameOver(World* world, Entity* player) {
if (player->has<SuperMarioComponent>()) {
//player time ran out while being Super Mario
player->remove<SuperMarioComponent>();
Expand Down Expand Up @@ -109,6 +109,8 @@ void onGameOver(World* world, Entity* player) {
kinetic->speedX = 0.0f;
kinetic->accY = 0.0f;
kinetic->accX = 0.0f;

player->assign<CallbackComponent>([=]() { gameOverCallback(); }, 200);
}, 50);
}

Expand Down
2 changes: 2 additions & 0 deletions src/systems/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void RenderSystem::tick(World* world) {
if (!tileSetComponent->get(x, y).texture) continue;
dstRect.x = x * TILE_SIZE - camera->left();
dstRect.y = y * TILE_SIZE - camera->top();
dstRect.w = TILE_SIZE;
dstRect.h = TILE_SIZE;
textureManager->renderTexture(tileSetComponent->get(x, y).editor_texture, dstRect);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/systems/TileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void TileSystem::tick(World* world) {
auto textLength = pointsComponent->text.length();
auto pointEntity = world->create();
pointEntity->assign<TextComponent>(std::move(pointsComponent->text));
auto w = textLength * 2;
auto w = textLength * 3;
pointEntity->assign<TransformComponent>((pointsComponent->x - w / 2) - camera->left(), pointsComponent->y, w, 14);
pointEntity->assign<KineticComponent>(0, 0);
pointEntity->get<KineticComponent>()->speedY = -2.0f;
Expand Down

0 comments on commit 5e6436b

Please sign in to comment.