/**************************************************************************** Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "state.h" #include "cocos2d.h" class HelloWorld : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); /** * Find state by id and return casted state * * @return casted state */ template T* findState() { const std::string name = typeid(T).name(); if (_states.count(name) != 0) { return static_cast(_states[name]); } return nullptr; } /** * Add new state to state machine * * @param args arguments to pass to constructor of state */ template void addState(Args&&... args) { auto typeId = typeid(T).name(); auto state = new T(std::forward(args)...); state->setStateMachine(this); _states.insert({typeId, state}); } /** * Check if we can enter state * * @return true if this state is valid, false otherwise */ template bool canEnterState() { if (_state == nullptr) { return true; } else { auto state = findState(); if (state) { return _state->isValidNextState(state); } } return false; } /** * Enters new state * * Before entering new state old state will check if it is a valid state to execute * transaction * * Order of execution: * * willExitWithNextState will be called on current state * didEnterWithPreviousState will be called on new state * * @return true if entered, false otherwise */ template bool enterState() { auto state = findState(); if (state) { if (_state == nullptr) { _state = state; _state->didEnterWithPreviousState(nullptr); this->addChild(_state,10); return true; } else { if (_state->isValidNextState(state)) { _state->willExitWithNextState(state); state->didEnterWithPreviousState(_state); _state = state; this->addChild(_state,10); return true; } } } return false; } /** * Enters new state without any check if next state is valid * * * Order of execution: * * willExitWithNextState will be called on current state * didEnterWithPreviousState will be called on new state * * @return true if entered, false otherwise */ template bool setState() { auto state = findState(); if (state) { if (_state == nullptr) { _state = state; _state->didEnterWithPreviousState(nullptr); return true; } else { _state->willExitWithNextState(state); state->didEnterWithPreviousState(_state); _state = state; return true; } } return false; } /** * Update state machine delta time, this will call updateWithDeltaTime on current state * * @param delta delta time */ void updateWithDeltaTime(float delta) { if (_state != nullptr) { _state->updateWithDeltaTime(delta); } } /** * Get current state * * @return current state */ State* getState() { return _state; } ~HelloWorld() { _state = nullptr; } private: std::unordered_map _states; State* _state; HelloWorld():_state(nullptr) { } }; #endif // __HELLOWORLD_SCENE_H__