Added restart game feature

This commit is contained in:
pandamicro 2014-12-28 11:55:06 +08:00
parent f01cc596a3
commit e3795c0223
4 changed files with 100 additions and 1 deletions

View File

@ -61,6 +61,10 @@ THE SOFTWARE.
#include "platform/CCApplication.h"
//#include "platform/CCGLViewImpl.h"
#if CC_ENABLE_SCRIPT_BINDING
#include "CCScriptSupport.h"
#endif
/**
Position of the FPS
@ -126,6 +130,9 @@ bool Director::init(void)
// purge ?
_purgeDirectorInNextLoop = false;
// restart ?
_restartDirectorInNextLoop = false;
_winSizeInPoints = Size::ZERO;
@ -1017,6 +1024,78 @@ void Director::purgeDirector()
release();
}
void Director::restart()
{
_restartDirectorInNextLoop = true;
}
void Director::restartDirector()
{
// cleanup scheduler
getScheduler()->unscheduleAll();
// Disable event dispatching
if (_eventDispatcher)
{
_eventDispatcher->setEnabled(false);
}
if (_runningScene)
{
_runningScene->onExit();
_runningScene->cleanup();
_runningScene->release();
}
_runningScene = nullptr;
_nextScene = nullptr;
// remove all objects, but don't release it.
// runWithScene might be executed after 'end'.
_scenesStack.clear();
stopAnimation();
CC_SAFE_RELEASE_NULL(_FPSLabel);
CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
// purge bitmap cache
FontFNT::purgeCachedData();
FontFreeType::shutdownFreeType();
// purge all managed caches
AnimationCache::destroyInstance();
SpriteFrameCache::destroyInstance();
GLProgramCache::destroyInstance();
GLProgramStateCache::destroyInstance();
std::vector<std::string> searchPaths;
FileUtils::getInstance()->setSearchPaths(searchPaths);
FileUtils::getInstance()->purgeCachedEntries();
// cocos2d-x specific data structures
UserDefault::destroyInstance();
GL::invalidateStateCache();
//destroyTextureCache();
_textureCache->removeAllTextures();
// Disable event dispatching
if (_eventDispatcher)
{
_eventDispatcher->setEnabled(true);
}
// release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
#if CC_ENABLE_SCRIPT_BINDING
ScriptEvent scriptEvent(kRestartGame, NULL);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
#endif
}
void Director::setNextScene()
{
bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;
@ -1304,6 +1383,11 @@ void DisplayLinkDirector::mainLoop()
_purgeDirectorInNextLoop = false;
purgeDirector();
}
else if (_restartDirectorInNextLoop)
{
_restartDirectorInNextLoop = false;
restartDirector();
}
else if (! _invalid)
{
drawScene();

View File

@ -287,6 +287,10 @@ public:
The "delta time" will be 0 (as if the game wasn't paused)
*/
void resume();
/** Restart the director
*/
void restart();
/** Stops the animation. Nothing will be drawn. The main loop won't be triggered anymore.
If you don't want to pause your animation call [pause] instead.
@ -399,6 +403,9 @@ protected:
void purgeDirector();
bool _purgeDirectorInNextLoop; // this flag will be set to true in end()
void restartDirector();
bool _restartDirectorInNextLoop; // this flag will be set to true in restart()
void setNextScene();
void showStats();

View File

@ -211,7 +211,8 @@ enum ScriptEventType
kAccelerometerEvent,
kControlEvent,
kCommonEvent,
kComponentEvent
kComponentEvent,
kRestartGame
};
struct BasicScriptData

View File

@ -256,6 +256,13 @@ int LuaEngine::sendEvent(ScriptEvent* evt)
return handlerControlEvent(evt->data);
}
break;
case kRestartGame:
{
// TODO: Clean up the LUA VM
//cleanup();
CCApplication::getInstance()->applicationDidFinishLaunching();
return 0;
}
default:
break;
}