axmol/cocos/2d/CCAction.cpp

285 lines
6.3 KiB
C++
Raw Normal View History

2011-03-19 10:34:26 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies 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.
****************************************************************************/
#include "2d/CCAction.h"
#include "2d/CCActionInterval.h"
#include "2d/CCNode.h"
#include "2d/CCDirector.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
//
// Action Base Class
//
Action::Action()
2013-11-16 21:08:00 +08:00
:_originalTarget(nullptr)
,_target(nullptr)
,_tag(Action::INVALID_TAG)
{
}
2012-04-18 18:43:45 +08:00
Action::~Action()
{
CCLOGINFO("deallocing Action: %p - tag: %i", this, _tag);
}
2012-04-18 18:43:45 +08:00
std::string Action::description() const
{
return StringUtils::format("<Action | Tag = %d", _tag);
}
2012-04-14 19:11:57 +08:00
void Action::startWithTarget(Node *aTarget)
{
_originalTarget = _target = aTarget;
}
void Action::stop()
{
2013-11-16 21:08:00 +08:00
_target = nullptr;
}
bool Action::isDone() const
{
return true;
}
void Action::step(float dt)
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM(dt);
CCLOG("[Action step]. override me");
}
void Action::update(float time)
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM(time);
CCLOG("[Action update]. override me");
}
//
// Speed
//
Speed::Speed()
: _speed(0.0)
2013-11-16 21:08:00 +08:00
, _innerAction(nullptr)
{
}
Speed::~Speed()
2011-03-19 10:34:26 +08:00
{
CC_SAFE_RELEASE(_innerAction);
}
2013-11-16 21:08:00 +08:00
Speed* Speed::create(ActionInterval* action, float speed)
{
2013-11-16 21:08:00 +08:00
Speed *ret = new Speed();
if (ret && ret->initWithAction(action, speed))
{
2013-11-16 21:08:00 +08:00
ret->autorelease();
return ret;
}
2013-11-16 21:08:00 +08:00
CC_SAFE_DELETE(ret);
return nullptr;
2011-03-19 10:34:26 +08:00
}
2010-08-25 16:08:10 +08:00
2013-11-16 21:08:00 +08:00
bool Speed::initWithAction(ActionInterval *action, float speed)
{
2013-11-16 21:08:00 +08:00
CCASSERT(action != nullptr, "");
action->retain();
_innerAction = action;
_speed = speed;
return true;
2011-03-19 10:34:26 +08:00
}
2010-08-25 16:08:10 +08:00
Speed *Speed::clone() const
{
// no copy constructor
auto a = new Speed();
a->initWithAction(_innerAction->clone(), _speed);
a->autorelease();
return a;
}
void Speed::startWithTarget(Node* target)
{
Action::startWithTarget(target);
_innerAction->startWithTarget(target);
}
void Speed::stop()
2011-03-19 10:34:26 +08:00
{
_innerAction->stop();
Action::stop();
}
void Speed::step(float dt)
2011-03-19 10:34:26 +08:00
{
_innerAction->step(dt * _speed);
}
bool Speed::isDone() const
2011-03-19 10:34:26 +08:00
{
return _innerAction->isDone();
}
Speed *Speed::reverse() const
2011-03-19 10:34:26 +08:00
{
return Speed::create(_innerAction->reverse(), _speed);
}
2013-11-16 21:08:00 +08:00
void Speed::setInnerAction(ActionInterval *action)
{
2013-11-16 21:08:00 +08:00
if (_innerAction != action)
{
CC_SAFE_RELEASE(_innerAction);
2013-11-16 21:08:00 +08:00
_innerAction = action;
CC_SAFE_RETAIN(_innerAction);
}
}
//
// Follow
//
Follow::~Follow()
{
CC_SAFE_RELEASE(_followedNode);
}
2013-11-16 21:08:00 +08:00
Follow* Follow::create(Node *followedNode, const Rect& rect/* = Rect::ZERO*/)
{
2013-11-16 21:08:00 +08:00
Follow *follow = new Follow();
if (follow && follow->initWithTarget(followedNode, rect))
{
2013-11-16 21:08:00 +08:00
follow->autorelease();
return follow;
}
2013-11-16 21:08:00 +08:00
CC_SAFE_DELETE(follow);
return nullptr;
}
Follow* Follow::clone() const
{
// no copy constructor
auto a = new Follow();
a->initWithTarget(_followedNode, _worldRect);
a->autorelease();
return a;
}
Follow* Follow::reverse() const
{
return clone();
}
2013-11-16 21:08:00 +08:00
bool Follow::initWithTarget(Node *followedNode, const Rect& rect/* = Rect::ZERO*/)
{
2013-11-16 21:08:00 +08:00
CCASSERT(followedNode != nullptr, "");
2013-11-16 21:08:00 +08:00
followedNode->retain();
_followedNode = followedNode;
_worldRect = rect;
if (rect.equals(Rect::ZERO))
{
_boundarySet = false;
}
else
{
_boundarySet = true;
}
_boundaryFullyCovered = false;
Size winSize = Director::getInstance()->getWinSize();
_fullScreenSize = Vector2(winSize.width, winSize.height);
_halfScreenSize = _fullScreenSize * 0.5f;
if (_boundarySet)
{
_leftBoundary = -((rect.origin.x+rect.size.width) - _fullScreenSize.x);
_rightBoundary = -rect.origin.x ;
_topBoundary = -rect.origin.y;
_bottomBoundary = -((rect.origin.y+rect.size.height) - _fullScreenSize.y);
if(_rightBoundary < _leftBoundary)
{
// screen width is larger than world's boundary width
//set both in the middle of the world
_rightBoundary = _leftBoundary = (_leftBoundary + _rightBoundary) / 2;
}
if(_topBoundary < _bottomBoundary)
{
// screen width is larger than world's boundary width
//set both in the middle of the world
_topBoundary = _bottomBoundary = (_topBoundary + _bottomBoundary) / 2;
}
if( (_topBoundary == _bottomBoundary) && (_leftBoundary == _rightBoundary) )
{
_boundaryFullyCovered = true;
}
}
return true;
}
void Follow::step(float dt)
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM(dt);
if(_boundarySet)
{
// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
if(_boundaryFullyCovered)
return;
Vector2 tempPos = _halfScreenSize - _followedNode->getPosition();
_target->setPosition(Vector2(clampf(tempPos.x, _leftBoundary, _rightBoundary),
clampf(tempPos.y, _bottomBoundary, _topBoundary)));
}
else
{
_target->setPosition(_halfScreenSize - _followedNode->getPosition());
}
}
bool Follow::isDone() const
{
return ( !_followedNode->isRunning() );
}
void Follow::stop()
{
2013-11-16 21:08:00 +08:00
_target = nullptr;
Action::stop();
}
2012-04-18 18:43:45 +08:00
NS_CC_END