Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StateMachine.h"
- namespace station {
- void StateMachine::AddState(StateReference newState, bool isReplacing) {
- /*since we are adding a State to the Stack, lets
- set _isAdding to true*/
- this->_isAdding = true;
- /*we established whether we're replacing the top level active State
- or not in the method parameters already*/
- this->_isReplacing = isReplacing;
- /*set given newState StateReference as _newState in the Statemachine*/
- this->_newState = std::move(newState);
- }
- void StateMachine::RemoveState() {
- /*sets _isRemoving to true, so when we call ProcessStateChanges, it will
- pop the top level active State */
- this->_isRemoving = true;
- }
- void StateMachine::ProcessStateChanges() {
- if (!this->_states.empty() && this->_isRemoving) {
- /*If the stack is not empty and we want to pop the top level active State,
- do just that*/
- this->_states.pop();
- if (!this->_states.empty()) {
- /*If the stack isnt empty now, the new top level State is not the active
- State and needs to be resumed*/
- this->_states.top()->resume();
- }
- /*done removing state*/
- this->_isRemoving = false;
- }
- if (this->_isAdding) {
- /*if we want to add a state...*/
- if (this->_isReplacing) {
- /*check if we want to replace the top level State,
- in which case we need to pop it first.*/
- this->_states.pop();
- }
- else {
- /*if we dont replace the top level active State, we will need to pause it
- since it wont be active anymore when we push the new State to the Stack*/
- this->_states.top()->pause();
- }
- /*push the new State to the stack and call its initialize function*/
- this->_states.push(std::move(this->_newState));
- this->_states.top()->initialize();
- /*done adding state*/
- this->_isAdding = false;
- }
- }
- StateReference &StateMachine::GetActiveState() {
- return this->_states.top();
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement