diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 61c1087..293a0e4 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -47,6 +47,7 @@ https://github.com/sago007/saland #include #include "console/Console.hpp" #include "GameConsoleCommand.hpp" +#include "GameSpellState.hpp" #include "fmt/core.h" int32 velocityIterations = 6; @@ -199,7 +200,9 @@ struct Game::GameImpl { size_t slot_selected = 0; std::array slot_spell; std::shared_ptr console; + std::shared_ptr spellSelect; bool consoleActive = false; + bool spellSelectActive = false; }; static SpawnPoint GetSpawnpoint(const sago::tiled::TileMap& tm) { @@ -443,6 +446,9 @@ void Game::Draw(SDL_Renderer* target) { DrawTile(target, globalData.spriteHolder.get(), data->gameRegion.world.tm, current_spell.tile, 20+i*56, 20); } } + if (data->spellSelectActive && data->spellSelect) { + data->spellSelect->Draw(target); + } if (data->consoleActive && data->console) { data->console->Draw(target); } @@ -474,7 +480,23 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { return; } } + if (data->spellSelectActive && data->spellSelect) { + data->spellSelect->ProcessInput(event, processed); + if (processed) { + return; + } + } if (event.type == SDL_KEYDOWN) { + // Console activation is the first thing we check and we stop if it is activated. + if (event.key.keysym.sym == SDLK_ESCAPE && event.key.keysym.mod & KMOD_LSHIFT) { + if (!data->console) { + data->console = std::make_shared(); + } + data->console->Activate(); + data->consoleActive = true; + processed = true; + return; + } if (event.key.keysym.sym == SDLK_1 || event.key.keysym.sym == SDLK_KP_1) { data->slot_selected = 0; } @@ -505,12 +527,9 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { if (event.key.keysym.sym == SDLK_0 || event.key.keysym.sym == SDLK_KP_0) { data->slot_selected = 9; } - if (event.key.keysym.sym == SDLK_ESCAPE && event.key.keysym.mod & KMOD_LSHIFT) { - if (!data->console) { - data->console = std::make_shared(); - } - data->console->Activate(); - data->consoleActive = true; + if (event.key.keysym.sym == SDLK_ESCAPE) { + data->spellSelect = std::make_shared(); + data->spellSelectActive = true; } if (event.key.keysym.sym == SDLK_F12) { data->isActive = false; @@ -548,6 +567,9 @@ void Game::Update() { if (data->consoleActive && data->console) { data->console->Update(); } + if (data->spellSelectActive && data->spellSelect) { + data->spellSelect->Update(); + } Uint32 nowTime = SDL_GetTicks(); Uint32 deltaTime = nowTime - data->lastUpdate; const Uint8* state = SDL_GetKeyboardState(NULL); @@ -711,6 +733,9 @@ void Game::Update() { if (data->consoleActive && data->console && !data->console->IsActive()) { data->consoleActive = false; } + if (data->spellSelectActive && data->spellSelect && !data->spellSelect->IsActive()) { + data->spellSelectActive = false; + } const Spell& selectedSpell = data->slot_spell.at(data->slot_selected); data->human->weapon = ""; if (selectedSpell.name == "weapon_slash_long_knife") { diff --git a/src/saland/GameSpellState.cpp b/src/saland/GameSpellState.cpp new file mode 100644 index 0000000..8ad9574 --- /dev/null +++ b/src/saland/GameSpellState.cpp @@ -0,0 +1,57 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2022 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + +#include "GameSpellState.hpp" +#include "GameDraw.hpp" +#include "globals.hpp" + +struct GameSpellState::GameSpellStateImpl { + bool active = true; +}; + +GameSpellState::GameSpellState() { + data.reset(new GameSpellState::GameSpellStateImpl()); +} + +void GameSpellState::GameSpellState::Draw(SDL_Renderer* target) { + int sideBoarder = 20; + DrawRectYellow(target, sideBoarder, 10, globalData.ysize - sideBoarder*2, globalData.xsize - sideBoarder*2); +} + +bool GameSpellState::IsActive() { + return data->active; +}; + +void GameSpellState::ProcessInput(const SDL_Event& event, bool& processed) { + if (event.type == SDL_KEYDOWN) { + if (event.key.keysym.sym == SDLK_ESCAPE) { + data->active = false; + processed = true; + } + } +}; + +void GameSpellState::Update() {}; + +GameSpellState::~GameSpellState() { +} \ No newline at end of file diff --git a/src/saland/GameSpellState.hpp b/src/saland/GameSpellState.hpp new file mode 100644 index 0000000..4db670c --- /dev/null +++ b/src/saland/GameSpellState.hpp @@ -0,0 +1,44 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2022 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + +#ifndef GAME_SPELL_STATE_HPP +#define GAME_SPELL_STATE_HPP + +#include + +#include "../sago/GameStateInterface.hpp" + +class GameSpellState : public sago::GameStateInterface { +public: + GameSpellState(); + virtual bool IsActive() override; + virtual void Draw(SDL_Renderer* target) override; + virtual void ProcessInput(const SDL_Event& event, bool& processed) override; + virtual void Update() override; + virtual ~GameSpellState(); +private: + struct GameSpellStateImpl; + std::unique_ptr data; +}; + +#endif /* GAME_SPELL_STATE_HPP */ \ No newline at end of file