diff --git a/CMakeLists.txt b/CMakeLists.txt index a272b75..eb17f94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,10 @@ set(SOURCES src/config.h src/debug/grid_debug_layer.cpp src/debug/grid_debug_layer.h - src/game/input/input_mapper.cpp src/game/input/input_mapper.h src/game/action_controller.cpp src/game/action_controller.h) + src/game/input/input_mapper.cpp + src/game/input/input_mapper.h + src/game/input/action_controller.cpp + src/game/input/action_controller.h src/game/input/direction.h src/game/input/direction.cpp) set(PHYSICS_00_SOURCES src/prototypes/physics_00.cpp) diff --git a/src/config.h b/src/config.h index 3703d04..842ad34 100644 --- a/src/config.h +++ b/src/config.h @@ -18,4 +18,10 @@ #define ISOMETRIC_SKEW 0.3f #define WORLD_TO_ISO_SCALE 10.0f + + +// DEBUG + +#define DB_ISOPLANE_CORNER_RADIUS 2 + #endif //HOLESOME_CONFIG_H diff --git a/src/debug/grid_debug_layer.cpp b/src/debug/grid_debug_layer.cpp index 5b53063..030cf70 100644 --- a/src/debug/grid_debug_layer.cpp +++ b/src/debug/grid_debug_layer.cpp @@ -1,5 +1,6 @@ #include "grid_debug_layer.h" #include "../primitives/circle_object.h" +#include "../config.h" GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY) { @@ -8,7 +9,7 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY) { for (int y = minY; y <= maxY; y++) { - auto *gameObject = new CircleObject(5, sf::Color::Red); + auto *gameObject = new CircleObject(DB_ISOPLANE_CORNER_RADIUS, sf::Color::Red); gameObject->coordinates.set(WorldCoordinates(x, y, 0)); marker.push_back(gameObject); } diff --git a/src/game/game.cpp b/src/game/game.cpp index 6f5e814..fa833da 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5,10 +5,21 @@ #include "game.h" #include "../config.h" -Game::Game(std::shared_ptr window) : window(std::move(window)), gameObjects() +Game::Game(std::shared_ptr window) : window(std::move(window)), + gameObjects(), + actionController(new ActionController(this)), + inputMapper(new InputMapper(this)) { } +Game::~Game() +{ + for (auto &gameObject: gameObjects) + { + delete gameObject; + } +} + void Game::run() { sf::Clock clock; @@ -16,14 +27,15 @@ void Game::run() while (window->isOpen()) { - processEvents(); + + inputMapper->processEvents(); TimeSinceLastUpdate += clock.restart(); while (TimeSinceLastUpdate >= FRAME_TIME) { TimeSinceLastUpdate -= FRAME_TIME; update(); - processEvents(); + inputMapper->processEvents(); } drawFrame(); } @@ -46,14 +58,6 @@ void Game::drawFrame() window->display(); } -Game::~Game() -{ - for (auto &gameObject: gameObjects) - { - delete gameObject; - } -} - void Game::addGameObject(GameObject *gameObject) { gameObjects.push_back(gameObject); @@ -66,36 +70,3 @@ void Game::update() gameObject->update(); } } - -void Game::processEvents() -{ - sf::Event event; - while (window->pollEvent(event)) - { - switch (event.type) - { - case sf::Event::KeyPressed: - // Close game on Escape or Q in DEV Mode - if (DEVELOPER_MODE && (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q)) - { - exit(); - } - break; - case sf::Event::KeyReleased: - break; - case sf::Event::Closed: - exit(); - break; - case sf::Event::Resized: - handleWindowResize(event); - break; - default: - break; - } - } -} - -void Game::handleWindowResize(const sf::Event &event) -{ - window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height))); -} diff --git a/src/game/game.h b/src/game/game.h index 760f75c..72ca7fc 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -1,7 +1,3 @@ -// -// Created by max on 26.04.23. -// - #ifndef HOLESOME_GAME_H #define HOLESOME_GAME_H @@ -9,11 +5,19 @@ #include #include "../logging/easylogging++.h" #include "game_object.h" +#include "input/action_controller.h" +#include "input/input_mapper.h" + + +class ActionController; +class InputMapper; + class Game { public: explicit Game(std::shared_ptr window); + ~Game(); void run(); @@ -22,17 +26,15 @@ public: void addGameObject(GameObject *gameObject); -private: std::shared_ptr window; + ActionController *actionController; + InputMapper *inputMapper; +private: std::vector gameObjects; void drawFrame(); void update(); - - void processEvents(); - - void handleWindowResize(const sf::Event &event); }; diff --git a/src/game/input/action_controller.cpp b/src/game/input/action_controller.cpp new file mode 100644 index 0000000..2adf8c5 --- /dev/null +++ b/src/game/input/action_controller.cpp @@ -0,0 +1,15 @@ +#include "action_controller.h" + +ActionController::ActionController(Game *game) : game(game) +{ +} + +void ActionController::moveView(InputDirection direction) +{ + float stepSize = 100.0f; + auto delta = stepSize * Direction::getVector(direction); + + auto view = game->window->getView(); + view.move(delta); + game->window->setView(view); +} diff --git a/src/game/input/action_controller.h b/src/game/input/action_controller.h new file mode 100644 index 0000000..d814bdf --- /dev/null +++ b/src/game/input/action_controller.h @@ -0,0 +1,26 @@ +#ifndef HOLESOME_ACTION_CONTROLLER_H +#define HOLESOME_ACTION_CONTROLLER_H + + +#include "../game.h" +#include "direction.h" + +class Game; + + +/** + * Holds all the actions that can be performed and executes them when requested. + */ +class ActionController +{ +public: + explicit ActionController(Game *game); + + void moveView(InputDirection direction); + +private: + Game *game; +}; + + +#endif //HOLESOME_ACTION_CONTROLLER_H diff --git a/src/game/input/direction.cpp b/src/game/input/direction.cpp new file mode 100644 index 0000000..09cec27 --- /dev/null +++ b/src/game/input/direction.cpp @@ -0,0 +1,50 @@ +#include +#include "direction.h" + +InputDirection Direction::getDirection(sf::Keyboard::Key key) +{ + auto map = std::map(); + map[sf::Keyboard::W] = InputDirection::UP; + map[sf::Keyboard::S] = InputDirection::DOWN; + map[sf::Keyboard::A] = InputDirection::LEFT; + map[sf::Keyboard::D] = InputDirection::RIGHT; + map[sf::Keyboard::Up] = InputDirection::UP; + map[sf::Keyboard::Down] = InputDirection::DOWN; + map[sf::Keyboard::Left] = InputDirection::LEFT; + map[sf::Keyboard::Right] = InputDirection::RIGHT; + + if (map.find(key) == map.end()) + return InputDirection::NONE; + + return map[key]; +} + +sf::Vector2f Direction::getVector(InputDirection direction) +{ + auto vector = sf::Vector2f(0.0f, 0.0f); + + if (direction == InputDirection::NONE) + { + return vector; + } + + // Combine all relevant directions into one vector + if (direction & InputDirection::UP) + { + vector.y -= 1; + } + if (direction & InputDirection::DOWN) + { + vector.y += 1; + } + if (direction & InputDirection::LEFT) + { + vector.x -= 1; + } + if (direction & InputDirection::RIGHT) + { + vector.x += 1; + } + + return vector; +} diff --git a/src/game/input/direction.h b/src/game/input/direction.h new file mode 100644 index 0000000..47b7dd9 --- /dev/null +++ b/src/game/input/direction.h @@ -0,0 +1,23 @@ +#ifndef HOLESOME_DIRECTION_H +#define HOLESOME_DIRECTION_H + +#include +#include + +enum InputDirection +{ + NONE = 0, + UP = 1, + DOWN = 2, + LEFT = 4, + RIGHT = 8 +}; + +class Direction +{ +public: + static InputDirection getDirection(sf::Keyboard::Key key); + static sf::Vector2f getVector(InputDirection direction); +}; + +#endif //HOLESOME_DIRECTION_H diff --git a/src/game/input/input_mapper.cpp b/src/game/input/input_mapper.cpp new file mode 100644 index 0000000..be863b0 --- /dev/null +++ b/src/game/input/input_mapper.cpp @@ -0,0 +1,48 @@ +#include "input_mapper.h" + + +InputMapper::InputMapper(Game *game) : game(game) +{ + +} + +void InputMapper::processEvents() +{ + sf::Event event{}; + while (game->window->pollEvent(event)) + { + switch (event.type) + { + case sf::Event::KeyPressed: + handleKeyPress(event.key); + break; + case sf::Event::KeyReleased: + break; + case sf::Event::Closed: + game->exit(); + break; + case sf::Event::Resized: + game->window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height))); + break; + default: + break; + } + } +} + +void InputMapper::handleKeyPress(sf::Event::KeyEvent event) +{ + // Close game on Escape or Q in DEV Mode + if (DEVELOPER_MODE && (event.code == sf::Keyboard::Escape || event.code == sf::Keyboard::Q)) + { + game->exit(); + return; + } + + // Move view + auto direction = Direction::getDirection(event.code); + if (direction != InputDirection::NONE) + { + game->actionController->moveView(direction); + } +} diff --git a/src/game/input/input_mapper.h b/src/game/input/input_mapper.h new file mode 100644 index 0000000..8fc61c8 --- /dev/null +++ b/src/game/input/input_mapper.h @@ -0,0 +1,27 @@ +#ifndef HOLESOME_INPUT_MAPPER_H +#define HOLESOME_INPUT_MAPPER_H + +#include "../game.h" +#include +#include "../../config.h" + +class Game; + +/** + * Maps the inputs to actions. + */ +class InputMapper +{ +public: + explicit InputMapper(Game *game); + + void processEvents(); + +private: + Game *game; + + void handleKeyPress(sf::Event::KeyEvent event); +}; + + +#endif //HOLESOME_INPUT_MAPPER_H