diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d760d5..1178fa5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,8 @@ set(GAME_SOURCES "Application.cpp" "Game.cpp" - "Stages.cpp" + "MenuStages.cpp" + "GameStages.cpp" ) # To be used instead of set(GAME_SOURCES ...) diff --git a/include/framework/BaseStage.hpp b/include/framework/BaseStage.hpp index aba61ae..60396b2 100644 --- a/include/framework/BaseStage.hpp +++ b/include/framework/BaseStage.hpp @@ -16,6 +16,7 @@ namespace Framework { // Called when stage stops being the current stage virtual void end(); + // Returns false if the application should close virtual bool update(float dt) = 0; virtual void render() = 0; diff --git a/include/framework/File.hpp b/include/framework/File.hpp index 99f6ec5..6279118 100644 --- a/include/framework/File.hpp +++ b/include/framework/File.hpp @@ -56,7 +56,7 @@ namespace Framework { // This shouldn't ever be needed, so it isn't implemented //void write(std::string filepath, TMX data, TMXFormat file_format = TMXFormat::AUTO); - /// Determines the Tiled level file format from the file name + // Determines the Tiled level file format from the file name TMXFormat get_format(std::string filepath); // Changes the null tile index from 0 to the value specified, reducing all other indices by 1 diff --git a/include/framework/Maths.hpp b/include/framework/Maths.hpp index 991eb3e..c5fae6c 100644 --- a/include/framework/Maths.hpp +++ b/include/framework/Maths.hpp @@ -43,6 +43,9 @@ namespace Framework { vec2 size; }; + Rect operator*(const Rect& r, const float s); + Rect operator*(const float s, const Rect& r); + extern const Rect RECT_NULL; bool colliding(Rect a, vec2 b); diff --git a/include/game/Constants.hpp b/include/game/Constants.hpp index f608ea6..f84acca 100644 --- a/include/game/Constants.hpp +++ b/include/game/Constants.hpp @@ -104,7 +104,7 @@ namespace COLOURS { } namespace TIMINGS { - + const float INTRO_OPEN_TIME = 4.0f; } namespace TRANSITIONS { @@ -152,4 +152,4 @@ namespace BUTTONS { namespace GAME { -} \ No newline at end of file +} diff --git a/include/game/Game.hpp b/include/game/Game.hpp index 7caaed6..7c08ec9 100644 --- a/include/game/Game.hpp +++ b/include/game/Game.hpp @@ -2,7 +2,7 @@ #include "BaseGame.hpp" -#include "Stages.hpp" +#include "MenuStages.hpp" #include "FadeTransition.hpp" diff --git a/include/game/Stages.hpp b/include/game/GameStages.hpp similarity index 71% rename from include/game/Stages.hpp rename to include/game/GameStages.hpp index c737e4f..5cbbb62 100644 --- a/include/game/Stages.hpp +++ b/include/game/GameStages.hpp @@ -4,16 +4,7 @@ #include "Constants.hpp" -class TitleStage : public Framework::BaseStage { -public: - void start(); - - bool update(float dt); - void render(); - -private: - Framework::Timer _timer; -}; +#include "MenuStages.hpp" class GameStage : public Framework::BaseStage { public: @@ -34,4 +25,4 @@ class PausedStage : public Framework::BaseStage { private: BaseStage* _background_stage; -}; \ No newline at end of file +}; diff --git a/include/game/MenuStages.hpp b/include/game/MenuStages.hpp new file mode 100644 index 0000000..8c7e7c1 --- /dev/null +++ b/include/game/MenuStages.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include "BaseStage.hpp" +#include "Font.hpp" +#include "Timer.hpp" + +#include "Constants.hpp" + +#include "GameStages.hpp" + +class IntroStage : public Framework::BaseStage { +public: + void start(); + + bool update(float dt); + void render(); + +private: + Framework::Timer intro_timer; + Framework::Text intro_text; +}; + +class TitleStage : public Framework::BaseStage { +public: + void start(); + + bool update(float dt); + void render(); + +private: + Framework::Timer _timer; +}; diff --git a/src/framework/Maths.cpp b/src/framework/Maths.cpp index e48bcf4..3821b6a 100644 --- a/src/framework/Maths.cpp +++ b/src/framework/Maths.cpp @@ -47,6 +47,14 @@ namespace Framework { return position + size; } + Rect operator*(const Rect& r, const float s) { + return Rect(r.position * s, r.size * s); + } + + Rect operator*(const float s, const Rect & r) { + return r * s; + } + bool colliding(Rect a, vec2 b) { return b.x >= a.position.x && b.x <= a.position.x + a.size.x && b.y >= a.position.y && b.y <= a.position.y + a.size.y; diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 485ab12..7553728 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -5,7 +5,7 @@ Game::Game() : BaseGame() { } void Game::start() { - stage = new TitleStage(); + stage = new IntroStage(); } void Game::end() { @@ -64,4 +64,4 @@ void Game::clear_data() { // Don't need to clear up graphics objects items - it's done for us in BaseGame // Clear up anything else we need to -} \ No newline at end of file +} diff --git a/src/game/GameStages.cpp b/src/game/GameStages.cpp new file mode 100644 index 0000000..88012a2 --- /dev/null +++ b/src/game/GameStages.cpp @@ -0,0 +1,100 @@ +#include "GameStages.hpp" + +// GameStage + +void GameStage::start() { + // Set transition + set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); + + // Start transition + transition->open(); +} + +bool GameStage::update(float dt) { + transition->update(dt); + + if (input->just_down(Framework::KeyHandler::Key::ESCAPE) || input->just_down(Framework::KeyHandler::Key::P)) { + finish(new PausedStage(this), false); + } + + return true; +} + +void GameStage::render() { + graphics_objects->graphics_ptr->fill(COLOURS::BLUE); + + graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(0, Framework::Vec(128, 64)); + + transition->render(); +} + +// PausedStage + +PausedStage::PausedStage(BaseStage* background_stage) : BaseStage() { + // Save the background stage so we can still render it, and then go back to it when done + _background_stage = background_stage; +} + +void PausedStage::start() { + // Create buttons + buttons.clear(); + buttons.emplace_back( + Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, 32), Framework::Vec(256, 64)), + graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], + Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Resume", COLOURS::BLACK, 4.0f), + BUTTONS::PAUSED::RESUME + ); + buttons.emplace_back( + Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, -64), Framework::Vec(256, 64)), + graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], + Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Exit", COLOURS::BLACK, 4.0f), + BUTTONS::PAUSED::EXIT + ); + + // Set transition + set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); +} + +bool PausedStage::update(float dt) { + transition->update(dt); + + if (input->just_down(Framework::KeyHandler::Key::ESCAPE) || input->just_down(Framework::KeyHandler::Key::P)) { + transition->close(); + } + + // Update buttons + for (Framework::Button& button : buttons) { + button.update(input); + + if (button.pressed() && transition->is_open()) { + button_selected = button.get_id(); + transition->close(); + } + } + + if (transition->is_closed()) { + if (button_selected == BUTTONS::PAUSED::EXIT) { + finish(new TitleStage()); + } + else { + // Return to game (exit pause) + finish(_background_stage); + } + } + + return true; +} + +void PausedStage::render() { + // Render background stage + _background_stage->render(); + + // Render pause menu + graphics_objects->graphics_ptr->fill(COLOURS::BLACK, 0x7f); + + for (const Framework::Button& button : buttons) { + button.render(); + } + + transition->render(); +} \ No newline at end of file diff --git a/src/game/Stages.cpp b/src/game/MenuStages.cpp similarity index 54% rename from src/game/Stages.cpp rename to src/game/MenuStages.cpp index aeb78d6..a064183 100644 --- a/src/game/Stages.cpp +++ b/src/game/MenuStages.cpp @@ -1,4 +1,48 @@ -#include "Stages.hpp" +#include "MenuStages.hpp" + +// IntroStage + +void IntroStage::start() { + intro_text = Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Some intro text", COLOURS::BLACK); + + intro_timer.stop(); + + // Start transition + set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); + transition->open(); +} + +bool IntroStage::update(float dt) { + transition->update(dt); + intro_timer.update(dt); + + if (transition->is_open()) { + if (intro_timer.running()) { + if (intro_timer.time() >= TIMINGS::INTRO_OPEN_TIME) { + transition->close(); + } + } + else { + intro_timer.reset(); + intro_timer.start(); + } + } + else if (transition->is_closed()) { + // Finish intro + finish(new TitleStage()); + } + + return true; +} + +void IntroStage::render() { + graphics_objects->graphics_ptr->fill(COLOURS::BLUE); + + // Display some intro text in the centre of the display + intro_text.render(WINDOW::SIZE_HALF); + + transition->render(); +} // TitleStage @@ -81,102 +125,3 @@ void TitleStage::render() { transition->render(); } - -// GameStage - -void GameStage::start() { - // Set transition - set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); - - // Start transition - transition->open(); -} - -bool GameStage::update(float dt) { - transition->update(dt); - - if (input->just_down(Framework::KeyHandler::Key::ESCAPE) || input->just_down(Framework::KeyHandler::Key::P)) { - finish(new PausedStage(this), false); - } - - return true; -} - -void GameStage::render() { - graphics_objects->graphics_ptr->fill(COLOURS::BLUE); - - graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(0, Framework::Vec(128, 64)); - - transition->render(); -} - -// PausedStage - -PausedStage::PausedStage(BaseStage* background_stage) : BaseStage() { - // Save the background stage so we can still render it, and then go back to it when done - _background_stage = background_stage; -} - -void PausedStage::start() { - // Create buttons - buttons.clear(); - buttons.emplace_back( - Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, 32), Framework::Vec(256, 64)), - graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], - Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Resume", COLOURS::BLACK, 4.0f), - BUTTONS::PAUSED::RESUME - ); - buttons.emplace_back( - Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, -64), Framework::Vec(256, 64)), - graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], - Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Exit", COLOURS::BLACK, 4.0f), - BUTTONS::PAUSED::EXIT - ); - - // Set transition - set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); -} - -bool PausedStage::update(float dt) { - transition->update(dt); - - if (input->just_down(Framework::KeyHandler::Key::ESCAPE) || input->just_down(Framework::KeyHandler::Key::P)) { - transition->close(); - } - - // Update buttons - for (Framework::Button& button : buttons) { - button.update(input); - - if (button.pressed() && transition->is_open()) { - button_selected = button.get_id(); - transition->close(); - } - } - - if (transition->is_closed()) { - if (button_selected == BUTTONS::PAUSED::EXIT) { - finish(new TitleStage()); - } - else { - // Return to game (exit pause) - finish(_background_stage); - } - } - - return true; -} - -void PausedStage::render() { - // Render background stage - _background_stage->render(); - - // Render pause menu - graphics_objects->graphics_ptr->fill(COLOURS::BLACK, 0x7f); - - for (const Framework::Button& button : buttons) { - button.render(); - } - - transition->render(); -} \ No newline at end of file