axmol/cocos/2d/CCActionInstant.cpp

403 lines
7.0 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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/CCActionInstant.h"
#include "2d/CCNode.h"
#include "2d/CCSprite.h"
NS_CC_BEGIN
//
// InstantAction
//
2021-12-25 10:04:45 +08:00
void ActionInstant::startWithTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
FiniteTimeAction::startWithTarget(target);
_done = false;
}
bool ActionInstant::isDone() const
{
return _done;
}
void ActionInstant::step(float /*dt*/)
{
float updateDt = 1;
update(updateDt);
}
void ActionInstant::update(float /*time*/)
{
_done = true;
}
//
// Show
//
2021-12-25 10:04:45 +08:00
Show* Show::create()
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
Show* ret = new Show();
ret->autorelease();
2019-11-23 20:27:39 +08:00
return ret;
}
void Show::update(float time)
{
ActionInstant::update(time);
_target->setVisible(true);
}
ActionInstant* Show::reverse() const
{
return Hide::create();
}
Show* Show::clone() const
{
// no copy constructor
return Show::create();
}
//
// Hide
//
2021-12-25 10:04:45 +08:00
Hide* Hide::create()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Hide* ret = new Hide();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
ret->autorelease();
2019-11-23 20:27:39 +08:00
return ret;
}
void Hide::update(float time)
{
ActionInstant::update(time);
_target->setVisible(false);
}
2021-12-25 10:04:45 +08:00
ActionInstant* Hide::reverse() const
2019-11-23 20:27:39 +08:00
{
return Show::create();
}
Hide* Hide::clone() const
{
// no copy constructor
return Hide::create();
}
//
// ToggleVisibility
//
2021-12-25 10:04:45 +08:00
ToggleVisibility* ToggleVisibility::create()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ToggleVisibility* ret = new ToggleVisibility();
2021-12-08 00:11:53 +08:00
ret->autorelease();
2019-11-23 20:27:39 +08:00
return ret;
}
void ToggleVisibility::update(float time)
{
ActionInstant::update(time);
_target->setVisible(!_target->isVisible());
}
2021-12-25 10:04:45 +08:00
ToggleVisibility* ToggleVisibility::reverse() const
2019-11-23 20:27:39 +08:00
{
return ToggleVisibility::create();
}
2021-12-25 10:04:45 +08:00
ToggleVisibility* ToggleVisibility::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
return ToggleVisibility::create();
}
//
// Remove Self
//
2021-12-25 10:04:45 +08:00
RemoveSelf* RemoveSelf::create(bool isNeedCleanUp /*= true*/)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
RemoveSelf* ret = new RemoveSelf();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->init(isNeedCleanUp))
2019-11-23 20:27:39 +08:00
ret->autorelease();
2021-12-08 00:11:53 +08:00
else
CC_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return ret;
}
bool RemoveSelf::init(bool isNeedCleanUp)
{
_isNeedCleanUp = isNeedCleanUp;
return true;
}
void RemoveSelf::update(float time)
{
ActionInstant::update(time);
_target->removeFromParentAndCleanup(_isNeedCleanUp);
}
2021-12-25 10:04:45 +08:00
RemoveSelf* RemoveSelf::reverse() const
2019-11-23 20:27:39 +08:00
{
return RemoveSelf::create(_isNeedCleanUp);
}
2021-12-25 10:04:45 +08:00
RemoveSelf* RemoveSelf::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
return RemoveSelf::create(_isNeedCleanUp);
}
//
// FlipX
//
2021-12-25 10:04:45 +08:00
FlipX* FlipX::create(bool x)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
FlipX* ret = new FlipX();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->initWithFlipX(x))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
bool FlipX::initWithFlipX(bool x)
{
_flipX = x;
return true;
}
void FlipX::update(float time)
{
ActionInstant::update(time);
static_cast<Sprite*>(_target)->setFlippedX(_flipX);
}
FlipX* FlipX::reverse() const
{
return FlipX::create(!_flipX);
}
2021-12-25 10:04:45 +08:00
FlipX* FlipX::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
return FlipX::create(_flipX);
}
//
// FlipY
//
2021-12-25 10:04:45 +08:00
FlipY* FlipY::create(bool y)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
FlipY* ret = new FlipY();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->initWithFlipY(y))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
bool FlipY::initWithFlipY(bool y)
{
_flipY = y;
return true;
}
void FlipY::update(float time)
{
ActionInstant::update(time);
static_cast<Sprite*>(_target)->setFlippedY(_flipY);
}
FlipY* FlipY::reverse() const
{
return FlipY::create(!_flipY);
}
2021-12-25 10:04:45 +08:00
FlipY* FlipY::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
return FlipY::create(_flipY);
}
//
// Place
//
Place* Place::create(const Vec2& pos)
{
2021-12-25 10:04:45 +08:00
Place* ret = new Place();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->initWithPosition(pos))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
bool Place::initWithPosition(const Vec2& pos)
{
_position = pos;
return true;
}
2021-12-25 10:04:45 +08:00
Place* Place::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
return Place::create(_position);
}
2021-12-25 10:04:45 +08:00
Place* Place::reverse() const
2019-11-23 20:27:39 +08:00
{
// no reverse, just clone
return this->clone();
}
void Place::update(float time)
{
ActionInstant::update(time);
_target->setPosition(_position);
}
//
// CallFunc
//
2021-12-25 10:04:45 +08:00
CallFunc* CallFunc::create(const std::function<void()>& func)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CallFunc* ret = new CallFunc();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (ret->initWithFunction(func))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool CallFunc::initWithFunction(const std::function<void()>& func)
2019-11-23 20:27:39 +08:00
{
_function = func;
return true;
}
2021-12-25 10:04:45 +08:00
CallFunc* CallFunc::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new CallFunc();
2021-12-25 10:04:45 +08:00
if (_function)
2019-11-23 20:27:39 +08:00
a->initWithFunction(_function);
a->autorelease();
return a;
}
2021-12-25 10:04:45 +08:00
CallFunc* CallFunc::reverse() const
2019-11-23 20:27:39 +08:00
{
// no reverse here, just return a clone
return this->clone();
}
void CallFunc::update(float time)
{
ActionInstant::update(time);
this->execute();
}
void CallFunc::execute()
{
2021-12-25 10:04:45 +08:00
if (_function)
2019-11-23 20:27:39 +08:00
{
_function();
}
}
//
// CallFuncN
//
2021-12-25 10:04:45 +08:00
CallFuncN* CallFuncN::create(const std::function<void(Node*)>& func)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new CallFuncN();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (ret->initWithFunction(func))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
void CallFuncN::execute()
{
if (_functionN)
{
_functionN(_target);
}
}
2021-12-25 10:04:45 +08:00
bool CallFuncN::initWithFunction(const std::function<void(Node*)>& func)
2019-11-23 20:27:39 +08:00
{
_functionN = func;
return true;
}
2021-12-25 10:04:45 +08:00
CallFuncN* CallFuncN::clone() const
2019-11-23 20:27:39 +08:00
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new CallFuncN();
2021-12-25 10:04:45 +08:00
if (_functionN)
2019-11-23 20:27:39 +08:00
a->initWithFunction(_functionN);
a->autorelease();
return a;
}
NS_CC_END