2013-12-27 00:18:55 +01:00
|
|
|
#ifndef _GAME_STATE_HPP_
|
|
|
|
#define _GAME_STATE_HPP_
|
|
|
|
#include <functional>
|
|
|
|
#include <queue>
|
2014-08-12 22:15:26 +02:00
|
|
|
#include <render/ViewCamera.hpp>
|
2013-12-27 00:18:55 +01:00
|
|
|
#include "MenuSystem.hpp"
|
2014-08-12 22:15:26 +02:00
|
|
|
#include <glm/gtc/quaternion.hpp>
|
2016-08-08 17:27:17 +02:00
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_events.h"
|
2016-06-22 12:29:39 +02:00
|
|
|
#include "GameWindow.hpp"
|
2013-12-27 00:18:55 +01:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
class RWGame;
|
|
|
|
class GameWorld;
|
|
|
|
|
2013-12-27 00:18:55 +01:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
// Helper for global menu behaviour
|
|
|
|
Menu* currentMenu;
|
2014-01-01 04:13:23 +01:00
|
|
|
Menu* nextMenu;
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
RWGame* game;
|
2013-12-27 00:18:55 +01:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
State(RWGame* game)
|
|
|
|
: currentMenu(nullptr), nextMenu(nullptr), game(game) {}
|
2013-12-27 00:18:55 +01:00
|
|
|
|
|
|
|
virtual void enter() = 0;
|
|
|
|
virtual void exit() = 0;
|
|
|
|
|
|
|
|
virtual void tick(float dt) = 0;
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
virtual void draw(GameRenderer* r)
|
2013-12-27 00:18:55 +01:00
|
|
|
{
|
2014-01-01 04:13:23 +01:00
|
|
|
if(getCurrentMenu()) {
|
2015-02-07 23:55:06 +01:00
|
|
|
getCurrentMenu()->draw(r);
|
2014-01-01 04:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~State() {
|
|
|
|
if(getCurrentMenu()) {
|
|
|
|
delete getCurrentMenu();
|
2013-12-27 00:18:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enterMenu(Menu* menu)
|
|
|
|
{
|
2014-01-01 04:13:23 +01:00
|
|
|
nextMenu = menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
Menu* getCurrentMenu()
|
|
|
|
{
|
|
|
|
if(nextMenu) {
|
|
|
|
if(currentMenu) {
|
|
|
|
delete currentMenu;
|
|
|
|
}
|
|
|
|
currentMenu = nextMenu;
|
|
|
|
nextMenu = nullptr;
|
|
|
|
}
|
|
|
|
return currentMenu;
|
2013-12-27 00:18:55 +01:00
|
|
|
}
|
2016-06-22 12:29:39 +02:00
|
|
|
|
|
|
|
virtual void handleEvent(const SDL_Event& e);
|
2013-12-27 00:18:55 +01:00
|
|
|
|
2014-08-12 22:15:26 +02:00
|
|
|
virtual const ViewCamera& getCamera();
|
2015-04-03 16:38:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns false if the game world should not should
|
|
|
|
* not update while this state is active
|
|
|
|
*/
|
|
|
|
virtual bool shouldWorldUpdate();
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
GameWorld* getWorld();
|
2016-06-22 12:29:39 +02:00
|
|
|
GameWindow& getWindow();
|
2013-12-27 00:18:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct StateManager
|
|
|
|
{
|
|
|
|
static StateManager& get()
|
|
|
|
{
|
|
|
|
static StateManager m;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::deque<State*> states;
|
2016-07-31 15:57:20 +02:00
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
states.clear();
|
|
|
|
}
|
2013-12-27 00:18:55 +01:00
|
|
|
|
|
|
|
void enter(State* state)
|
|
|
|
{
|
|
|
|
states.push_back(state);
|
|
|
|
state->enter();
|
|
|
|
}
|
2014-06-06 13:18:32 +02:00
|
|
|
|
|
|
|
void exec(State* state)
|
|
|
|
{
|
|
|
|
exit();
|
|
|
|
enter(state);
|
|
|
|
}
|
2013-12-27 00:18:55 +01:00
|
|
|
|
|
|
|
void tick(float dt)
|
|
|
|
{
|
|
|
|
states.back()->tick(dt);
|
|
|
|
}
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
void draw(GameRenderer* r)
|
2013-12-27 00:18:55 +01:00
|
|
|
{
|
2015-02-07 23:55:06 +01:00
|
|
|
states.back()->draw(r);
|
2013-12-27 00:18:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit()
|
|
|
|
{
|
|
|
|
// TODO: Resole states being leaked.
|
|
|
|
states.back()->exit();
|
|
|
|
states.pop_back();
|
2013-12-27 22:58:27 +01:00
|
|
|
if(states.size() > 0) {
|
2013-12-27 00:18:55 +01:00
|
|
|
states.back()->enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-25 23:30:50 +02:00
|
|
|
#endif
|