2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-01 15:08:23 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:34:26 +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.
|
2010-08-05 14:32:04 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCAction.h"
|
2010-12-22 15:15:04 +08:00
|
|
|
#include "CCActionInterval.h"
|
2010-08-05 14:32:04 +08:00
|
|
|
#include "CCNode.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2010-08-05 14:32:04 +08:00
|
|
|
#include "CCDirector.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCZone.h"
|
2010-08-05 14:32:04 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-05 14:32:04 +08:00
|
|
|
//
|
|
|
|
// Action Base Class
|
|
|
|
//
|
|
|
|
|
|
|
|
CCAction::CCAction()
|
|
|
|
:m_pOriginalTarget(NULL)
|
|
|
|
,m_pTarget(NULL)
|
|
|
|
,m_nTag(kCCActionTagInvalid)
|
|
|
|
{
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2010-08-05 14:32:04 +08:00
|
|
|
CCAction::~CCAction()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOGINFO("cocos2d: deallocing");
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCAction * CCAction::action()
|
|
|
|
{
|
|
|
|
return CCAction::create();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCAction* CCAction::create()
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAction * pRet = new CCAction();
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
const char* CCAction::description()
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
return CCString::createWithFormat("<CCAction | Tag = %d>", m_nTag)->getCString();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-04-14 19:11:57 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject* CCAction::copyWithZone(CCZone *pZone)
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCZone *pNewZone = NULL;
|
|
|
|
CCAction *pRet = NULL;
|
|
|
|
if (pZone && pZone->m_pCopyObject)
|
|
|
|
{
|
|
|
|
pRet = (CCAction*)(pZone->m_pCopyObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pRet = new CCAction();
|
|
|
|
pZone = pNewZone = new CCZone(pRet);
|
|
|
|
}
|
|
|
|
//copy member data
|
|
|
|
pRet->m_nTag = m_nTag;
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pRet;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2010-08-05 14:32:04 +08:00
|
|
|
|
2010-08-28 12:02:10 +08:00
|
|
|
void CCAction::startWithTarget(CCNode *aTarget)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pOriginalTarget = m_pTarget = aTarget;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAction::stop()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTarget = NULL;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAction::isDone()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCAction::step(float dt)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("[Action step]. override me");
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCAction::update(float time)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(time);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("[Action update]. override me");
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// FiniteTimeAction
|
|
|
|
//
|
|
|
|
|
|
|
|
CCFiniteTimeAction *CCFiniteTimeAction::reverse()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("cocos2d: FiniteTimeAction#reverse: Implement me");
|
|
|
|
return NULL;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Speed
|
|
|
|
//
|
|
|
|
CCSpeed::~CCSpeed()
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pInnerAction);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCSpeed * CCSpeed::actionWithAction(CCActionInterval *pAction, float fSpeed)
|
|
|
|
{
|
|
|
|
return CCSpeed::create(pAction, fSpeed);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCSpeed* CCSpeed::create(CCActionInterval* pAction, float fSpeed)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSpeed *pRet = new CCSpeed();
|
|
|
|
if (pRet && pRet->initWithAction(pAction, fSpeed))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2010-08-25 16:08:10 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
bool CCSpeed::initWithAction(CCActionInterval *pAction, float fSpeed)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(pAction != NULL, "");
|
|
|
|
pAction->retain();
|
|
|
|
m_pInnerAction = pAction;
|
|
|
|
m_fSpeed = fSpeed;
|
|
|
|
return true;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2010-08-25 16:08:10 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject *CCSpeed::copyWithZone(CCZone *pZone)
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCZone* pNewZone = NULL;
|
|
|
|
CCSpeed* pRet = NULL;
|
|
|
|
if(pZone && pZone->m_pCopyObject) //in case of being called at sub class
|
|
|
|
{
|
|
|
|
pRet = (CCSpeed*)(pZone->m_pCopyObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pRet = new CCSpeed();
|
|
|
|
pZone = pNewZone = new CCZone(pRet);
|
|
|
|
}
|
|
|
|
CCAction::copyWithZone(pZone);
|
|
|
|
|
|
|
|
pRet->initWithAction( (CCActionInterval*)(m_pInnerAction->copy()->autorelease()) , m_fSpeed );
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pRet;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2010-08-05 14:32:04 +08:00
|
|
|
|
2010-08-28 12:02:10 +08:00
|
|
|
void CCSpeed::startWithTarget(CCNode* pTarget)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAction::startWithTarget(pTarget);
|
|
|
|
m_pInnerAction->startWithTarget(pTarget);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpeed::stop()
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pInnerAction->stop();
|
|
|
|
CCAction::stop();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCSpeed::step(float dt)
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2011-07-01 15:08:23 +08:00
|
|
|
m_pInnerAction->step(dt * m_fSpeed);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCSpeed::isDone()
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pInnerAction->isDone();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2010-12-22 15:43:54 +08:00
|
|
|
CCActionInterval *CCSpeed::reverse()
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return (CCActionInterval*)(CCSpeed::create(m_pInnerAction->reverse(), m_fSpeed));
|
2011-07-01 15:08:23 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCSpeed::setInnerAction(CCActionInterval *pAction)
|
|
|
|
{
|
|
|
|
if (m_pInnerAction != pAction)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(m_pInnerAction);
|
|
|
|
m_pInnerAction = pAction;
|
|
|
|
CC_SAFE_RETAIN(m_pInnerAction);
|
|
|
|
}
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Follow
|
|
|
|
//
|
|
|
|
CCFollow::~CCFollow()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pobFollowedNode);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCFollow *CCFollow::actionWithTarget(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/)
|
|
|
|
{
|
|
|
|
return CCFollow::create(pFollowedNode, rect);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCFollow* CCFollow::create(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCFollow *pRet = new CCFollow();
|
|
|
|
if (pRet && pRet->initWithTarget(pFollowedNode, rect))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
bool CCFollow::initWithTarget(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(pFollowedNode != NULL, "");
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
pFollowedNode->retain();
|
|
|
|
m_pobFollowedNode = pFollowedNode;
|
2012-06-14 15:13:16 +08:00
|
|
|
if (CCRect::CCRectEqualToRect(rect, CCRectZero))
|
|
|
|
{
|
|
|
|
m_bBoundarySet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bBoundarySet = true;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bBoundaryFullyCovered = false;
|
|
|
|
|
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
m_obFullScreenSize = CCPointMake(winSize.width, winSize.height);
|
|
|
|
m_obHalfScreenSize = ccpMult(m_obFullScreenSize, 0.5f);
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
if (m_bBoundarySet)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
m_fLeftBoundary = -((rect.origin.x+rect.size.width) - m_obFullScreenSize.x);
|
|
|
|
m_fRightBoundary = -rect.origin.x ;
|
|
|
|
m_fTopBoundary = -rect.origin.y;
|
|
|
|
m_fBottomBoundary = -((rect.origin.y+rect.size.height) - m_obFullScreenSize.y);
|
|
|
|
|
|
|
|
if(m_fRightBoundary < m_fLeftBoundary)
|
|
|
|
{
|
|
|
|
// screen width is larger than world's boundary width
|
|
|
|
//set both in the middle of the world
|
|
|
|
m_fRightBoundary = m_fLeftBoundary = (m_fLeftBoundary + m_fRightBoundary) / 2;
|
|
|
|
}
|
|
|
|
if(m_fTopBoundary < m_fBottomBoundary)
|
|
|
|
{
|
|
|
|
// screen width is larger than world's boundary width
|
|
|
|
//set both in the middle of the world
|
|
|
|
m_fTopBoundary = m_fBottomBoundary = (m_fTopBoundary + m_fBottomBoundary) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (m_fTopBoundary == m_fBottomBoundary) && (m_fLeftBoundary == m_fRightBoundary) )
|
|
|
|
{
|
|
|
|
m_bBoundaryFullyCovered = true;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject *CCFollow::copyWithZone(CCZone *pZone)
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCZone *pNewZone = NULL;
|
|
|
|
CCFollow *pRet = NULL;
|
|
|
|
if(pZone && pZone->m_pCopyObject) //in case of being called at sub class
|
|
|
|
{
|
|
|
|
pRet = (CCFollow*)(pZone->m_pCopyObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pRet = new CCFollow();
|
|
|
|
pZone = pNewZone = new CCZone(pRet);
|
|
|
|
}
|
|
|
|
CCAction::copyWithZone(pZone);
|
|
|
|
// copy member data
|
|
|
|
pRet->m_nTag = m_nTag;
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pRet;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCFollow::step(float dt)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(dt);
|
2010-08-05 14:32:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(m_bBoundarySet)
|
|
|
|
{
|
|
|
|
// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
|
|
|
|
if(m_bBoundaryFullyCovered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CCPoint tempPos = ccpSub( m_obHalfScreenSize, m_pobFollowedNode->getPosition());
|
|
|
|
|
|
|
|
m_pTarget->setPosition(ccp(clampf(tempPos.x, m_fLeftBoundary, m_fRightBoundary),
|
|
|
|
clampf(tempPos.y, m_fBottomBoundary, m_fTopBoundary)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pTarget->setPosition(ccpSub(m_obHalfScreenSize, m_pobFollowedNode->getPosition()));
|
|
|
|
}
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCFollow::isDone()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return ( !m_pobFollowedNode->getIsRunning() );
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2010-08-05 14:32:04 +08:00
|
|
|
void CCFollow::stop()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTarget = NULL;
|
|
|
|
CCAction::stop();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
2010-08-05 14:32:04 +08:00
|
|
|
|
|
|
|
|