From 92bec13dec53933a9cf8045ab486b47dc13fe46f Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 12 Feb 2018 20:33:48 +0100 Subject: Remove Actor::Type and Scene::Type in favor of dynamic_cast() --- src/main/cpp/Actor.cpp | 4 ++-- src/main/cpp/Subconscious.cpp | 21 ++++++++++----------- src/main/cpp/WorldScene.cpp | 2 +- src/main/headers/Actor.hpp | 15 +++++---------- src/main/headers/Scene.hpp | 9 +-------- 5 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/main/cpp/Actor.cpp b/src/main/cpp/Actor.cpp index 219488d..a6dc597 100644 --- a/src/main/cpp/Actor.cpp +++ b/src/main/cpp/Actor.cpp @@ -1,7 +1,7 @@ #include "Actor.hpp" -Actor::Actor(std::string _name, unsigned _maxHp, Type _type) : - name(_name), maxHp(_maxHp), type(_type) +Actor::Actor(std::string _name, unsigned _maxHp) : + name(_name), maxHp(_maxHp) {} Actor::~Actor() diff --git a/src/main/cpp/Subconscious.cpp b/src/main/cpp/Subconscious.cpp index a5f8620..5d04e5c 100644 --- a/src/main/cpp/Subconscious.cpp +++ b/src/main/cpp/Subconscious.cpp @@ -34,26 +34,26 @@ void Subconscious::run() case sf::Event::KeyPressed: switch (event.key.code) { case sf::Keyboard::Up: - if (_currentScene->type == Scene::Type::WORLD) { - static_cast(_currentScene)->pan(0, -10); + if (WorldScene *worldScene = dynamic_cast(_currentScene)) { + worldScene->pan(0, -10); } break; case sf::Keyboard::Down: - if (_currentScene->type == Scene::Type::WORLD) { - static_cast(_currentScene)->pan(0, 10); + if (WorldScene *worldScene = dynamic_cast(_currentScene)) { + worldScene->pan(0, 10); } break; case sf::Keyboard::Left: - if (_currentScene->type == Scene::Type::WORLD) { - static_cast(_currentScene)->pan(-10, 0); + if (WorldScene *worldScene = dynamic_cast(_currentScene)) { + worldScene->pan(-10, 0); } break; case sf::Keyboard::Right: - if (_currentScene->type == Scene::Type::WORLD) { - static_cast(_currentScene)->pan(10, 0); + if (WorldScene *worldScene = dynamic_cast(_currentScene)) { + worldScene->pan(10, 0); } break; @@ -83,9 +83,8 @@ void Subconscious::run() // // std::cout << std::fixed << std::setprecision(3) // << event.mouseWheelScroll.delta << std::endl; - - if (_currentScene->type == Scene::Type::WORLD) { - static_cast(_currentScene)->zoom(event.mouseWheelScroll.delta/10.0); + if (WorldScene *worldScene = dynamic_cast(_currentScene)) { + worldScene->zoom(event.mouseWheelScroll.delta/10.0); } break; diff --git a/src/main/cpp/WorldScene.cpp b/src/main/cpp/WorldScene.cpp index 1be2097..8887f6f 100644 --- a/src/main/cpp/WorldScene.cpp +++ b/src/main/cpp/WorldScene.cpp @@ -1,6 +1,6 @@ #include "WorldScene.hpp" -WorldScene::WorldScene(sf::RenderWindow &window) : Scene(window, Scene::Type::WORLD) +WorldScene::WorldScene(sf::RenderWindow &window) : Scene(window) { _tileSize = DEFAULT_TILE_SIZE_PX; } diff --git a/src/main/headers/Actor.hpp b/src/main/headers/Actor.hpp index 7cc3be5..0e74ee0 100644 --- a/src/main/headers/Actor.hpp +++ b/src/main/headers/Actor.hpp @@ -1,29 +1,22 @@ #ifndef ACTOR_HPP #define ACTOR_HPP +#include #include + class Actor { public: - const enum Type - { - PLAYER, ENEMY - } type; - const unsigned maxHp; const std::string name; Actor() = delete; - Actor(std::string _name, unsigned _maxHp, Type _type); virtual ~Actor(); void damage(int amt); void heal(int amt); - // TODO shouldnt be done by the map ? - // bool move(int x, y); - /* accessors */ bool alive() { return _alive; } unsigned hp() { return _hp; } @@ -31,10 +24,12 @@ public: int x() { return _x; } int y() { return _y; } -private: +protected: bool _alive; unsigned _hp; int _x, _y; + + Actor(std::string _name, unsigned _maxHp); }; #endif diff --git a/src/main/headers/Scene.hpp b/src/main/headers/Scene.hpp index 2183d63..2d720a9 100644 --- a/src/main/headers/Scene.hpp +++ b/src/main/headers/Scene.hpp @@ -6,13 +6,6 @@ class Scene { public: - enum Type - { - WORLD, MENU - }; - - const Type type; - Scene() = delete; virtual void render() = 0; @@ -25,7 +18,7 @@ protected: float _zoom = 1; sf::RenderWindow &_window; - Scene(sf::RenderWindow &window, Type _type) : type(_type), _window(window) {} + Scene(sf::RenderWindow &window) : _window(window) {} }; #endif -- cgit v1.2.1