2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 15:08:23 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2011-03-19 10:34:26 +08:00
|
|
|
|
|
|
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCAction.h"
|
|
|
|
#include "2d/CCActionInterval.h"
|
|
|
|
#include "2d/CCNode.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2010-08-05 14:32:04 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2010-08-05 14:32:04 +08:00
|
|
|
//
|
|
|
|
// Action Base Class
|
|
|
|
//
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Action::Action() : _originalTarget(nullptr), _target(nullptr), _tag(Action::INVALID_TAG), _flags(0) {}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Action::~Action()
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("deallocing Action: %p - tag: %i", this, _tag);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
std::string Action::description() const
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2013-12-12 09:37:56 +08:00
|
|
|
return StringUtils::format("<Action | Tag = %d", _tag);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-04-14 19:11:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Action::startWithTarget(Node* aTarget)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_originalTarget = _target = aTarget;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Action::stop()
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
_target = nullptr;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool Action::isDone() const
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void Action::step(float /*dt*/)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("[Action step]. override me");
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void Action::update(float /*time*/)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("[Action update]. override me");
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Speed
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
Speed::Speed() : _speed(0.0), _innerAction(nullptr) {}
|
2013-06-14 08:25:14 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Speed::~Speed()
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_innerAction);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
Speed* Speed::create(ActionInterval* action, float speed)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Speed* ret = new Speed();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->initWithAction(action, speed))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2013-11-16 21:08:00 +08:00
|
|
|
return nullptr;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2010-08-25 16:08:10 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Speed::initWithAction(ActionInterval* action, float speed)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(action != nullptr, "action must not be NULL");
|
2016-04-08 13:40:36 +08:00
|
|
|
if (action == nullptr)
|
|
|
|
{
|
|
|
|
log("Speed::initWithAction error: action is nullptr!");
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
action->retain();
|
|
|
|
_innerAction = action;
|
2021-12-25 10:04:45 +08:00
|
|
|
_speed = speed;
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2010-08-25 16:08:10 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Speed* Speed::clone() const
|
2013-06-14 08:25:14 +08:00
|
|
|
{
|
2015-01-07 22:46:28 +08:00
|
|
|
// no copy constructor
|
2016-04-08 13:40:36 +08:00
|
|
|
if (_innerAction)
|
|
|
|
return Speed::create(_innerAction->clone(), _speed);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-04-08 13:40:36 +08:00
|
|
|
return nullptr;
|
2013-06-14 08:25:14 +08:00
|
|
|
}
|
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
void Speed::startWithTarget(Node* target)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2016-04-08 13:40:36 +08:00
|
|
|
if (target && _innerAction)
|
|
|
|
{
|
|
|
|
Action::startWithTarget(target);
|
|
|
|
_innerAction->startWithTarget(target);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log("Speed::startWithTarget error: target(%p) or _innerAction(%p) is nullptr!", target, _innerAction);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Speed::stop()
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2016-04-08 13:40:36 +08:00
|
|
|
if (_innerAction)
|
|
|
|
_innerAction->stop();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Action::stop();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Speed::step(float dt)
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_innerAction->step(dt * _speed);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool Speed::isDone() const
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _innerAction->isDone();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Speed* Speed::reverse() const
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2016-04-08 13:40:36 +08:00
|
|
|
if (_innerAction)
|
|
|
|
return Speed::create(_innerAction->reverse(), _speed);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-04-08 13:40:36 +08:00
|
|
|
return nullptr;
|
2011-07-01 15:08:23 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Speed::setInnerAction(ActionInterval* action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (_innerAction != action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_innerAction);
|
2013-11-16 21:08:00 +08:00
|
|
|
_innerAction = action;
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_innerAction);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Follow
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
Follow::~Follow()
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_followedNode);
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Follow* Follow::create(Node* followedNode, const Rect& rect /* = Rect::ZERO*/)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return createWithOffset(followedNode, 0.0, 0.0, rect);
|
2016-04-20 17:25:31 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Follow* Follow::createWithOffset(Node* followedNode, float xOffset, float yOffset, const Rect& rect /*= Rect::ZERO*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
Follow* follow = new Follow();
|
|
|
|
|
|
|
|
bool valid = follow->initWithTargetAndOffset(followedNode, xOffset, yOffset, rect);
|
2016-04-20 17:25:31 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (valid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
follow->autorelease();
|
|
|
|
return follow;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-04-08 13:40:36 +08:00
|
|
|
delete follow;
|
2013-11-16 21:08:00 +08:00
|
|
|
return nullptr;
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Follow* Follow::clone() const
|
2013-06-14 08:25:14 +08:00
|
|
|
{
|
2015-01-07 22:46:28 +08:00
|
|
|
// no copy constructor
|
2021-12-25 10:04:45 +08:00
|
|
|
return Follow::createWithOffset(_followedNode, _offsetX, _offsetY, _worldRect);
|
2013-06-14 08:25:14 +08:00
|
|
|
}
|
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
Follow* Follow::reverse() const
|
|
|
|
{
|
|
|
|
return clone();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Follow::initWithTargetAndOffset(Node* followedNode, float xOffset, float yOffset, const Rect& rect)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(followedNode != nullptr, "FollowedNode can't be NULL");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (followedNode == nullptr)
|
2016-04-08 13:40:36 +08:00
|
|
|
{
|
|
|
|
log("Follow::initWithTarget error: followedNode is nullptr!");
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
followedNode->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
_followedNode = followedNode;
|
|
|
|
_worldRect = rect;
|
|
|
|
_boundarySet = !rect.equals(Rect::ZERO);
|
2013-06-15 14:03:30 +08:00
|
|
|
_boundaryFullyCovered = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 winSize = Director::getInstance()->getWinSize();
|
2015-04-20 01:40:52 +08:00
|
|
|
_fullScreenSize.set(winSize.width, winSize.height);
|
2013-07-11 16:38:58 +08:00
|
|
|
_halfScreenSize = _fullScreenSize * 0.5f;
|
2021-12-25 10:04:45 +08:00
|
|
|
_offsetX = xOffset;
|
|
|
|
_offsetY = yOffset;
|
2016-04-20 17:25:31 +08:00
|
|
|
_halfScreenSize.x += _offsetX;
|
|
|
|
_halfScreenSize.y += _offsetY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_boundarySet)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_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);
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_rightBoundary < _leftBoundary)
|
2012-06-14 15:13:16 +08:00
|
|
|
{
|
|
|
|
// screen width is larger than world's boundary width
|
2021-12-25 10:04:45 +08:00
|
|
|
// set both in the middle of the world
|
2013-06-15 14:03:30 +08:00
|
|
|
_rightBoundary = _leftBoundary = (_leftBoundary + _rightBoundary) / 2;
|
2012-06-14 15:13:16 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_topBoundary < _bottomBoundary)
|
2012-06-14 15:13:16 +08:00
|
|
|
{
|
|
|
|
// screen width is larger than world's boundary width
|
2021-12-25 10:04:45 +08:00
|
|
|
// set both in the middle of the world
|
2013-06-15 14:03:30 +08:00
|
|
|
_topBoundary = _bottomBoundary = (_topBoundary + _bottomBoundary) / 2;
|
2012-06-14 15:13:16 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((_topBoundary == _bottomBoundary) && (_leftBoundary == _rightBoundary))
|
2012-06-14 15:13:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_boundaryFullyCovered = true;
|
2012-06-14 15:13:16 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +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
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Follow::initWithTarget(Node* followedNode, const Rect& rect /*= Rect::ZERO*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
return initWithTargetAndOffset(followedNode, 0.0, 0.0, rect);
|
2016-04-20 17:25:31 +08:00
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
void Follow::step(float /*dt*/)
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_boundarySet)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_boundaryFullyCovered)
|
2015-02-13 13:00:12 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
2015-02-13 13:00:12 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 tempPos = _halfScreenSize - _followedNode->getPosition();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-08-28 11:41:18 +08:00
|
|
|
_target->setPosition(clampf(tempPos.x, _leftBoundary, _rightBoundary),
|
2021-12-25 10:04:45 +08:00
|
|
|
clampf(tempPos.y, _bottomBoundary, _topBoundary));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
_target->setPosition(_halfScreenSize - _followedNode->getPosition());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool Follow::isDone() const
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (!_followedNode->isRunning());
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Follow::stop()
|
2010-08-05 14:32:04 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
_target = nullptr;
|
2013-06-20 14:13:12 +08:00
|
|
|
Action::stop();
|
2010-08-05 14:32:04 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|