1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-10 04:42:38 +01:00
openrw/rwgame/State.hpp

144 lines
2.3 KiB
C++
Raw Normal View History

2013-12-27 00:18:55 +01:00
#ifndef _GAME_STATE_HPP_
#define _GAME_STATE_HPP_
#include <functional>
#include <queue>
2015-02-07 23:55:06 +01:00
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <render/ViewCamera.hpp>
2013-12-27 00:18:55 +01:00
#include "MenuSystem.hpp"
#include <glm/gtc/quaternion.hpp>
2013-12-27 00:18:55 +01: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;
RWGame* game;
2013-12-27 00:18:55 +01: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
}
virtual void handleEvent(const sf::Event& e)
{
2014-01-01 04:13:23 +01:00
auto m = getCurrentMenu();
if(! m) return;
2013-12-27 00:18:55 +01:00
switch(e.type) {
case sf::Event::MouseButtonReleased:
2014-01-01 04:13:23 +01:00
m->click(e.mouseButton.x, e.mouseButton.y);
2013-12-27 00:18:55 +01:00
break;
2014-01-01 01:37:16 +01:00
case sf::Event::MouseMoved:
2014-01-01 04:13:23 +01:00
m->hover(e.mouseMove.x, e.mouseMove.y);
2014-01-01 01:37:16 +01:00
break;
case sf::Event::KeyPressed:
2014-01-01 04:13:23 +01:00
switch(e.key.code) {
2014-05-25 23:30:50 +02:00
default: break;
2014-01-01 04:13:23 +01:00
case sf::Keyboard::Up:
m->move(-1);
break;
case sf::Keyboard::Down:
m->move(1);
break;
case sf::Keyboard::Return:
m->activate();
break;
2014-01-01 01:37:16 +01:00
}
2013-12-27 00:18:55 +01:00
default: break;
};
}
virtual const ViewCamera& getCamera();
/**
* Returns false if the game world should not should
* not update while this state is active
*/
virtual bool shouldWorldUpdate();
GameWorld* getWorld();
sf::RenderWindow& getWindow();
2013-12-27 00:18:55 +01:00
};
struct StateManager
{
static StateManager& get()
{
static StateManager m;
return m;
}
std::deque<State*> states;
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