diff --git a/CMakeLists.txt b/CMakeLists.txt index b88f34d..55bc2cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,6 @@ set(${PROJECT_NAME}_SOURCES src/Resources.cpp src/ResourceType.h src/Input.h - src/Input.cpp src/InputAction.h src/TankType.h src/Bullet.cpp @@ -65,6 +64,8 @@ set(${PROJECT_NAME}_SOURCES src/Display.cpp src/Utils.h src/Utils.cpp + src/StandardInput.h + src/StandardInput.cpp src/map/Water.cpp src/map/Water.h src/map/Steel.cpp diff --git a/src/Game.cpp b/src/Game.cpp index 75da870..a7c4a2c 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -8,13 +8,13 @@ #include "Bullet.h" #include "Config.h" #include "Display.h" -#include "Input.h" #include "InputAction.h" #include "Level.h" #include "Map.h" #include "MapUtils.h" #include "PointUtils.h" #include "Resources.h" +#include "StandardInput.h" #include "Tank.h" #include "Utils.h" @@ -87,7 +87,7 @@ bool Game::play(Level level) std::list tanks{map.loadMap(levelContent)}; levelContent.close(); - Input input; + StandardInput input; display_.clearScreenWithBlack(); std::list bullets; @@ -115,13 +115,14 @@ void Game::control(Map& map, std::list& tanks, std::list& bullets) { moveBullets(bullets, tanks, map); + StandardInput input; for (auto& tank : tanks) { Direction direction{Direction::UP}; if (tank.isPlayerControlled()) { setPower(tank, map); - const auto actions{Input::getGameActions()}; + const auto actions{input.getGameActions()}; const auto now{std::chrono::system_clock::now()}; if ((actions.find(InputAction::FIRE) != actions.end()) && tank.canFire(now)) diff --git a/src/Game.h b/src/Game.h index ef3e142..1f1c102 100644 --- a/src/Game.h +++ b/src/Game.h @@ -12,7 +12,6 @@ struct Point; class Tank; class Map; class Display; -class Input; enum class InputAction : char; class Bullet; enum class Level : char; diff --git a/src/Input.h b/src/Input.h index 86879ec..be6491b 100644 --- a/src/Input.h +++ b/src/Input.h @@ -3,56 +3,16 @@ #include #include -struct ALLEGRO_EVENT_QUEUE; -union ALLEGRO_EVENT; -struct ALLEGRO_KEYBOARD_STATE; enum class InputAction : char; -class Input final +class Input { public: - Input(); + virtual ~Input() = default; - static void init(); + virtual InputAction getMenuAction() = 0; - InputAction getMenuAction(); + virtual std::set getGameActions() = 0; - static std::set getGameActions(); - - std::pair getMousePosition() const; - -private: - static InputAction getCommonAction(const ALLEGRO_EVENT& event); - - static bool itemPicked(const ALLEGRO_EVENT& event); - - static bool fired(const ALLEGRO_KEYBOARD_STATE& keyState); - - static bool userWantToExit(const ALLEGRO_EVENT& event); - - static bool keyEscapeUsed(const ALLEGRO_EVENT& event); - - static bool keyUpUsed(const ALLEGRO_EVENT& event); - - static bool keyDownUsed(const ALLEGRO_EVENT& event); - - static bool keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState); - - static bool keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState); - - static bool keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState); - - static bool keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState); - - static bool keyEnterUsed(const ALLEGRO_EVENT& event); - - static bool keySpaceUsed(const ALLEGRO_EVENT& event); - - static bool mouseClickUsed(const ALLEGRO_EVENT& event); - - ALLEGRO_EVENT_QUEUE* events_; - - int mouseX_{0}; - - int mouseY_{0}; + virtual std::pair getMousePosition() const = 0; }; diff --git a/src/Input.cpp b/src/StandardInput.cpp similarity index 73% rename from src/Input.cpp rename to src/StandardInput.cpp index 70ef420..9ac9bce 100644 --- a/src/Input.cpp +++ b/src/StandardInput.cpp @@ -1,11 +1,11 @@ -#include "Input.h" +#include "StandardInput.h" #include #include "Config.h" #include "InputAction.h" -Input::Input() : events_(::al_create_event_queue()) +StandardInput::StandardInput() : events_(::al_create_event_queue()) { ALLEGRO_TIMER* timer{ ::al_create_timer(1.0 / Config::getInstance().getFps())}; @@ -17,13 +17,13 @@ Input::Input() : events_(::al_create_event_queue()) ::al_start_timer(timer); } -void Input::init() +void StandardInput::init() { ::al_install_keyboard(); ::al_install_mouse(); } -InputAction Input::getMenuAction() +InputAction StandardInput::getMenuAction() { ALLEGRO_EVENT event{}; ::al_wait_for_event(events_, &event); @@ -59,7 +59,7 @@ InputAction Input::getMenuAction() return InputAction::MOUSE_MOVE; } -std::set Input::getGameActions() +std::set StandardInput::getGameActions() { ALLEGRO_KEYBOARD_STATE keyState; ::al_get_keyboard_state(&keyState); @@ -83,12 +83,12 @@ std::set Input::getGameActions() return ongoingActions; } -std::pair Input::getMousePosition() const +std::pair StandardInput::getMousePosition() const { return {mouseX_, mouseY_}; } -InputAction Input::getCommonAction(const ALLEGRO_EVENT& event) +InputAction StandardInput::getCommonAction(const ALLEGRO_EVENT& event) { if (userWantToExit(event)) return InputAction::QUIT; @@ -102,73 +102,73 @@ InputAction Input::getCommonAction(const ALLEGRO_EVENT& event) return InputAction::EMPTY; } -bool Input::itemPicked(const ALLEGRO_EVENT& event) +bool StandardInput::itemPicked(const ALLEGRO_EVENT& event) { return keyEnterUsed(event) || keySpaceUsed(event) || mouseClickUsed(event); } -bool Input::fired(const ALLEGRO_KEYBOARD_STATE& keyState) +bool StandardInput::fired(const ALLEGRO_KEYBOARD_STATE& keyState) { return ::al_key_down(&keyState, ALLEGRO_KEY_SPACE) || ::al_key_down(&keyState, ALLEGRO_KEY_ENTER); } -bool Input::userWantToExit(const ALLEGRO_EVENT& event) +bool StandardInput::userWantToExit(const ALLEGRO_EVENT& event) { return event.type == ALLEGRO_EVENT_DISPLAY_CLOSE; } -bool Input::keyEscapeUsed(const ALLEGRO_EVENT& event) +bool StandardInput::keyEscapeUsed(const ALLEGRO_EVENT& event) { return (event.type == ALLEGRO_EVENT_KEY_CHAR) && (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE); } -bool Input::keyUpUsed(const ALLEGRO_EVENT& event) +bool StandardInput::keyUpUsed(const ALLEGRO_EVENT& event) { return (event.type == ALLEGRO_EVENT_KEY_CHAR) && (event.keyboard.keycode == ALLEGRO_KEY_UP); } -bool Input::keyDownUsed(const ALLEGRO_EVENT& event) +bool StandardInput::keyDownUsed(const ALLEGRO_EVENT& event) { return (event.type == ALLEGRO_EVENT_KEY_CHAR) && (event.keyboard.keycode == ALLEGRO_KEY_DOWN); } -bool Input::keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState) +bool StandardInput::keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState) { return ::al_key_down(&keyState, ALLEGRO_KEY_UP); } -bool Input::keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState) +bool StandardInput::keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState) { return ::al_key_down(&keyState, ALLEGRO_KEY_DOWN); } -bool Input::keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState) +bool StandardInput::keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState) { return ::al_key_down(&keyState, ALLEGRO_KEY_LEFT); } -bool Input::keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState) +bool StandardInput::keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState) { return ::al_key_down(&keyState, ALLEGRO_KEY_RIGHT); } -bool Input::keyEnterUsed(const ALLEGRO_EVENT& event) +bool StandardInput::keyEnterUsed(const ALLEGRO_EVENT& event) { return (event.type == ALLEGRO_EVENT_KEY_UP) && (event.keyboard.keycode == ALLEGRO_KEY_ENTER); } -bool Input::keySpaceUsed(const ALLEGRO_EVENT& event) +bool StandardInput::keySpaceUsed(const ALLEGRO_EVENT& event) { return (event.type == ALLEGRO_EVENT_KEY_UP) && (event.keyboard.keycode == ALLEGRO_KEY_SPACE); } -bool Input::mouseClickUsed(const ALLEGRO_EVENT& event) +bool StandardInput::mouseClickUsed(const ALLEGRO_EVENT& event) { return (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) && (event.mouse.button == 1); diff --git a/src/StandardInput.h b/src/StandardInput.h new file mode 100644 index 0000000..e0d0b2f --- /dev/null +++ b/src/StandardInput.h @@ -0,0 +1,57 @@ +#pragma once + +#include "Input.h" + +struct ALLEGRO_EVENT_QUEUE; +union ALLEGRO_EVENT; +struct ALLEGRO_KEYBOARD_STATE; +enum class InputAction : char; + +class StandardInput : public Input +{ +public: + StandardInput(); + + static void init(); + + InputAction getMenuAction() override; + + std::set getGameActions() override; + + std::pair getMousePosition() const override; + +private: + static InputAction getCommonAction(const ALLEGRO_EVENT& event); + + static bool itemPicked(const ALLEGRO_EVENT& event); + + static bool fired(const ALLEGRO_KEYBOARD_STATE& keyState); + + static bool userWantToExit(const ALLEGRO_EVENT& event); + + static bool keyEscapeUsed(const ALLEGRO_EVENT& event); + + static bool keyUpUsed(const ALLEGRO_EVENT& event); + + static bool keyDownUsed(const ALLEGRO_EVENT& event); + + static bool keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState); + + static bool keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState); + + static bool keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState); + + static bool keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState); + + static bool keyEnterUsed(const ALLEGRO_EVENT& event); + + static bool keySpaceUsed(const ALLEGRO_EVENT& event); + + static bool mouseClickUsed(const ALLEGRO_EVENT& event); + + ALLEGRO_EVENT_QUEUE* events_; + + int mouseX_{0}; + + int mouseY_{0}; +}; diff --git a/src/main.cpp b/src/main.cpp index 7c7398a..1a0c59a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,9 @@ #include #include "Game.h" -#include "Input.h" #include "Menu.h" #include "Screen.h" +#include "StandardInput.h" #include "UserChoice.h" namespace @@ -11,7 +11,7 @@ namespace UserChoice getUserChoice(Menu& menu) { UserChoice choice{UserChoice::MAIN_MENU}; - Input input; + StandardInput input; while ((choice != UserChoice::EXIT) && (!menu.isLevelPicked(choice))) { menu.refresh(choice); @@ -27,7 +27,7 @@ int main() if (!screen.init()) return EXIT_FAILURE; - Input::init(); + StandardInput::init(); Menu menu(screen);