2010-11-13 11:34:49 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:34:26 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Valentin Milea
|
2011-07-01 15:08:23 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:34:26 +08:00
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCActionManager.h"
|
|
|
|
#include "CCScheduler.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "support/data_support/ccCArray.h"
|
|
|
|
#include "support/data_support/uthash.h"
|
|
|
|
|
|
|
|
namespace cocos2d {
|
2010-08-05 14:37:36 +08:00
|
|
|
//
|
|
|
|
// singleton stuff
|
|
|
|
//
|
2010-11-13 11:34:49 +08:00
|
|
|
static CCActionManager *gSharedManager = NULL;
|
|
|
|
|
|
|
|
typedef struct _hashElement
|
|
|
|
{
|
|
|
|
struct _ccArray *actions;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject *target;
|
2010-11-13 11:34:49 +08:00
|
|
|
unsigned int actionIndex;
|
|
|
|
CCAction *currentAction;
|
|
|
|
bool currentActionSalvaged;
|
|
|
|
bool paused;
|
2011-07-01 15:08:23 +08:00
|
|
|
UT_hash_handle hh;
|
2010-09-04 11:48:11 +08:00
|
|
|
} tHashElement;
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2010-11-16 15:12:09 +08:00
|
|
|
CCActionManager* CCActionManager::sharedManager(void)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
CCActionManager *pRet = gSharedManager;
|
|
|
|
|
|
|
|
if (! gSharedManager)
|
|
|
|
{
|
|
|
|
pRet = gSharedManager = new CCActionManager();
|
|
|
|
|
|
|
|
if (! gSharedManager->init())
|
|
|
|
{
|
|
|
|
// delete CCActionManager if init error
|
|
|
|
delete gSharedManager;
|
|
|
|
gSharedManager = NULL;
|
|
|
|
pRet = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCActionManager::purgeSharedManager(void)
|
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCScheduler::sharedScheduler()->unscheduleUpdateForTarget(this);
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE(gSharedManager);
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCActionManager::CCActionManager(void)
|
2011-06-10 17:51:37 +08:00
|
|
|
: m_pTargets(NULL),
|
|
|
|
m_pCurrentTarget(NULL),
|
2011-04-01 16:06:53 +08:00
|
|
|
m_bCurrentTargetSalvaged(false)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(gSharedManager == NULL, "");
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCActionManager::~CCActionManager(void)
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d: deallocing %p", this);
|
|
|
|
|
|
|
|
removeAllActions();
|
|
|
|
|
|
|
|
// ?? do not delete , is it because purgeSharedManager() delete it?
|
|
|
|
gSharedManager = NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCActionManager::init(void)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCScheduler::sharedScheduler()->scheduleUpdateForTarget(this, 0, false);
|
2010-08-05 14:37:36 +08:00
|
|
|
m_pTargets = NULL;
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// private
|
|
|
|
|
|
|
|
void CCActionManager::deleteHashElement(tHashElement *pElement)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
ccArrayFree(pElement->actions);
|
2010-08-05 14:37:36 +08:00
|
|
|
HASH_DEL(m_pTargets, pElement);
|
|
|
|
pElement->target->release();
|
|
|
|
free(pElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCActionManager::actionAllocWithHashElement(tHashElement *pElement)
|
|
|
|
{
|
|
|
|
// 4 actions per Node by default
|
|
|
|
if (pElement->actions == NULL)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
pElement->actions = ccArrayNew(4);
|
|
|
|
}else
|
|
|
|
if (pElement->actions->num == pElement->actions->max)
|
|
|
|
{
|
|
|
|
ccArrayDoubleCapacity(pElement->actions);
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCActionManager::removeActionAtIndex(unsigned int uIndex, tHashElement *pElement)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
CCAction *pAction = (CCAction*)pElement->actions->arr[uIndex];
|
2010-08-05 14:37:36 +08:00
|
|
|
|
|
|
|
if (pAction == pElement->currentAction && (! pElement->currentActionSalvaged))
|
|
|
|
{
|
|
|
|
pElement->currentAction->retain();
|
|
|
|
pElement->currentActionSalvaged = true;
|
|
|
|
}
|
|
|
|
|
2010-08-09 11:46:35 +08:00
|
|
|
ccArrayRemoveObjectAtIndex(pElement->actions, uIndex);
|
2010-08-05 14:37:36 +08:00
|
|
|
|
|
|
|
// update actionIndex in case we are in tick. looping over the actions
|
|
|
|
if (pElement->actionIndex >= uIndex)
|
|
|
|
{
|
|
|
|
pElement->actionIndex--;
|
|
|
|
}
|
|
|
|
|
2010-08-09 11:46:35 +08:00
|
|
|
if (pElement->actions->num == 0)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
if (m_pCurrentTarget == pElement)
|
|
|
|
{
|
|
|
|
m_bCurrentTargetSalvaged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deleteHashElement(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pause / resume
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCActionManager::pauseTarget(CCObject *pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
tHashElement *pElement = NULL;
|
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
pElement->paused = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCActionManager::resumeTarget(CCObject *pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
tHashElement *pElement = NULL;
|
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
pElement->paused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCActionManager::addAction(CCAction *pAction, CCNode *pTarget, bool paused)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(pAction != NULL, "");
|
|
|
|
CCAssert(pTarget != NULL, "");
|
2010-08-05 14:37:36 +08:00
|
|
|
|
|
|
|
tHashElement *pElement = NULL;
|
2011-03-07 17:11:57 +08:00
|
|
|
// we should convert it to CCObject*, because we save it as CCObject*
|
|
|
|
CCObject *tmp = pTarget;
|
2010-09-15 09:31:01 +08:00
|
|
|
HASH_FIND_INT(m_pTargets, &tmp, pElement);
|
2010-08-05 14:37:36 +08:00
|
|
|
if (! pElement)
|
|
|
|
{
|
|
|
|
pElement = (tHashElement*)calloc(sizeof(*pElement), 1);
|
|
|
|
pElement->paused = paused;
|
|
|
|
pTarget->retain();
|
|
|
|
pElement->target = pTarget;
|
|
|
|
HASH_ADD_INT(m_pTargets, target, pElement);
|
|
|
|
}
|
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
actionAllocWithHashElement(pElement);
|
|
|
|
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(! ccArrayContainsObject(pElement->actions, pAction), "");
|
2010-11-13 11:34:49 +08:00
|
|
|
ccArrayAppendObject(pElement->actions, pAction);
|
|
|
|
|
|
|
|
pAction->startWithTarget(pTarget);
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
|
|
|
|
|
|
|
void CCActionManager::removeAllActions(void)
|
|
|
|
{
|
|
|
|
for (tHashElement *pElement = m_pTargets; pElement != NULL; )
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject *pTarget = pElement->target;
|
2010-08-05 14:37:36 +08:00
|
|
|
pElement = (tHashElement*)pElement->hh.next;
|
|
|
|
removeAllActionsFromTarget(pTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCActionManager::removeAllActionsFromTarget(CCObject *pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
// explicit null handling
|
|
|
|
if (pTarget == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tHashElement *pElement = NULL;
|
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
if (ccArrayContainsObject(pElement->actions, pElement->currentAction) && (! pElement->currentActionSalvaged))
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
pElement->currentAction->retain();
|
|
|
|
pElement->currentActionSalvaged = true;
|
|
|
|
}
|
|
|
|
|
2010-08-09 11:46:35 +08:00
|
|
|
ccArrayRemoveAllObjects(pElement->actions);
|
2010-08-05 14:37:36 +08:00
|
|
|
if (m_pCurrentTarget == pElement)
|
|
|
|
{
|
|
|
|
m_bCurrentTargetSalvaged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deleteHashElement(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-02 15:58:10 +08:00
|
|
|
// CCLOG("cocos2d: removeAllActionsFromTarget: Target not found");
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCActionManager::removeAction(CCAction *pAction)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
// explicit null handling
|
|
|
|
if (pAction == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tHashElement *pElement = NULL;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject *pTarget = pAction->getOriginalTarget();
|
2010-08-05 14:37:36 +08:00
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
unsigned int i = ccArrayGetIndexOfObject(pElement->actions, pAction);
|
2011-06-15 09:56:33 +08:00
|
|
|
if (UINT_MAX != i)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
removeActionAtIndex(i, pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: removeAction: Target not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-01 15:08:23 +08:00
|
|
|
void CCActionManager::removeActionByTag(unsigned int tag, CCObject *pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert((int)tag != kCCActionTagInvalid, "");
|
|
|
|
CCAssert(pTarget != NULL, "");
|
2010-08-05 14:37:36 +08:00
|
|
|
|
|
|
|
tHashElement *pElement = NULL;
|
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
|
|
|
|
if (pElement)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
unsigned int limit = pElement->actions->num;
|
|
|
|
for (unsigned int i = 0; i < limit; ++i)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
CCAction *pAction = (CCAction*)pElement->actions->arr[i];
|
|
|
|
|
2011-10-19 15:24:19 +08:00
|
|
|
if (pAction->getTag() == (int)tag && pAction->getOriginalTarget() == pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2011-07-08 15:28:13 +08:00
|
|
|
removeActionAtIndex(i, pElement);
|
|
|
|
break;
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get
|
|
|
|
|
2011-07-01 15:08:23 +08:00
|
|
|
CCAction* CCActionManager::getActionByTag(unsigned int tag, CCObject *pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert((int)tag != kCCActionTagInvalid, "");
|
2010-08-05 14:37:36 +08:00
|
|
|
|
|
|
|
tHashElement *pElement = NULL;
|
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
if (pElement->actions != NULL)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
unsigned int limit = pElement->actions->num;
|
|
|
|
for (unsigned int i = 0; i < limit; ++i)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
CCAction *pAction = (CCAction*)pElement->actions->arr[i];
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2011-10-19 15:24:19 +08:00
|
|
|
if (pAction->getTag() == (int)tag)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CCLOG("cocos2d : getActionByTag: Action not found");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d : getActionByTag: Target not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-01 15:08:23 +08:00
|
|
|
unsigned int CCActionManager::numberOfRunningActionsInTarget(CCObject *pTarget)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
tHashElement *pElement = NULL;
|
|
|
|
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
return pElement->actions ? pElement->actions->num : 0;
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// main loop
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCActionManager::update(ccTime dt)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
for (tHashElement *elt = m_pTargets; elt != NULL; )
|
|
|
|
{
|
|
|
|
m_pCurrentTarget = elt;
|
|
|
|
m_bCurrentTargetSalvaged = false;
|
|
|
|
|
|
|
|
if (! m_pCurrentTarget->paused)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
// The 'actions' CCMutableArray may change while inside this loop.
|
2010-08-09 11:46:35 +08:00
|
|
|
for (m_pCurrentTarget->actionIndex = 0; m_pCurrentTarget->actionIndex < m_pCurrentTarget->actions->num;
|
|
|
|
m_pCurrentTarget->actionIndex++)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2010-08-09 11:46:35 +08:00
|
|
|
m_pCurrentTarget->currentAction = (CCAction*)m_pCurrentTarget->actions->arr[m_pCurrentTarget->actionIndex];
|
|
|
|
if (m_pCurrentTarget->currentAction == NULL)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-05 14:37:36 +08:00
|
|
|
m_pCurrentTarget->currentActionSalvaged = false;
|
|
|
|
|
|
|
|
m_pCurrentTarget->currentAction->step(dt);
|
|
|
|
|
|
|
|
if (m_pCurrentTarget->currentActionSalvaged)
|
|
|
|
{
|
|
|
|
// The currentAction told the node to remove it. To prevent the action from
|
|
|
|
// accidentally deallocating itself before finishing its step, we retained
|
|
|
|
// it. Now that step is done, it's safe to release it.
|
|
|
|
m_pCurrentTarget->currentAction->release();
|
|
|
|
} else
|
|
|
|
if (m_pCurrentTarget->currentAction->isDone())
|
|
|
|
{
|
|
|
|
m_pCurrentTarget->currentAction->stop();
|
|
|
|
|
|
|
|
CCAction *pAction = m_pCurrentTarget->currentAction;
|
|
|
|
// Make currentAction nil to prevent removeAction from salvaging it.
|
|
|
|
m_pCurrentTarget->currentAction = NULL;
|
|
|
|
removeAction(pAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pCurrentTarget->currentAction = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// elt, at this moment, is still valid
|
|
|
|
// so it is safe to ask this here (issue #490)
|
2010-08-31 11:20:37 +08:00
|
|
|
elt = (tHashElement*)(elt->hh.next);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
|
|
|
|
if (m_bCurrentTargetSalvaged && m_pCurrentTarget->actions->num == 0)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
|
|
|
deleteHashElement(m_pCurrentTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// issue #635
|
|
|
|
m_pCurrentTarget = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|