Last active
December 14, 2016 14:44
-
-
Save dgellow/4b5bed2ce7ea4f66a62c9a5dcc0b7954 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
class GameObject { | |
public: | |
GameObject(void (*callback) ()) : callback(callback) { | |
} | |
~GameObject() { | |
} | |
void update() { | |
// std::cout << "updated -> before callback\n"; | |
callback(); | |
// std::cout << "updated -> after callback\n"; | |
} | |
private: | |
void (*callback) (); | |
}; | |
class MenuState { | |
public: | |
void update() { | |
std::cout << "update -> gameObjects.size(): " << gameObjects.size() << "\n"; | |
for (size_t i = 0; i < gameObjects.size(); i++) { | |
std::cout << "update(for-loop: " << i << ") -> gameObjects.size(): " << gameObjects.size() << "\n"; | |
gameObjects[i]->update(); | |
} | |
} | |
bool onEnter() { | |
GameObject* object1 = new GameObject(cllbckChangeState); | |
GameObject* object2 = new GameObject(cllbckChangeState); | |
gameObjects.push_back(object1); | |
gameObjects.push_back(object2); | |
return true; | |
} | |
bool onExit() { | |
gameObjects.clear(); | |
return true; | |
} | |
private: | |
std::vector<GameObject*> gameObjects; | |
static void cllbckChangeState(); | |
}; | |
class StateMachine { | |
public: | |
static StateMachine* instance() { | |
if (pInstance == 0) { | |
pInstance = new StateMachine(); | |
} | |
return pInstance; | |
} | |
void update() { | |
std::cout << "StateMachine::update : menuStates.size(): " << menuStates.size() << "\n"; | |
if (!menuStates.empty()) { | |
menuStates.back()->update(); | |
} | |
} | |
void pushState(MenuState* pState) { | |
menuStates.push_back(pState); | |
menuStates.back()->onEnter(); | |
} | |
void popState() { | |
if (!menuStates.empty()) { | |
if (menuStates.back()->onExit()) { | |
delete menuStates.back(); | |
menuStates.pop_back(); | |
} | |
} | |
} | |
void switchState(MenuState* pState) { | |
if (!menuStates.empty()) { | |
popState(); | |
pushState(pState); | |
} | |
} | |
private: | |
StateMachine() { | |
MenuState* state1 = new MenuState(); | |
MenuState* state2 = new MenuState(); | |
menuStates.push_back(state1); | |
menuStates.push_back(state2); | |
} | |
static StateMachine* pInstance; | |
std::vector<MenuState*> menuStates; | |
}; | |
StateMachine* StateMachine::pInstance = 0; | |
void MenuState::cllbckChangeState() { | |
// std::cout << "callback -> before switch\n"; | |
StateMachine::instance()->switchState(new MenuState()); | |
// std::cout << "callback -> after switch\n"; | |
} | |
int main () { | |
StateMachine::instance()->pushState(new MenuState()); | |
long i = 1; | |
while (true) { | |
i++; | |
std::cout << "tick: " << i << "\n"; | |
StateMachine::instance()->update(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment