axmol/cocos2dx/actions/CCActionInstant.cpp

682 lines
13 KiB
C++
Raw Normal View History

2010-11-13 11:34:49 +08:00
/****************************************************************************
2012-06-14 17:18:05 +08:00
Copyright (c) 2010-2012 cocos2d-x.org
2011-09-05 10:12:14 +08:00
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
2011-03-19 10:34:26 +08:00
2011-09-05 10:12:14 +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-11-13 11:34:49 +08:00
#include "CCActionInstant.h"
#include "base_nodes/CCNode.h"
#include "sprite_nodes/CCSprite.h"
#include "script_support/CCScriptSupport.h"
#include "cocoa/CCZone.h"
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
2011-09-05 10:12:14 +08:00
//
// InstantAction
//
bool ActionInstant::isDone() {
return true;
2011-09-05 10:12:14 +08:00
}
void ActionInstant::step(float dt) {
CC_UNUSED_PARAM(dt);
update(1);
2011-09-05 10:12:14 +08:00
}
void ActionInstant::update(float time) {
CC_UNUSED_PARAM(time);
// nothing
2011-09-05 10:12:14 +08:00
}
//
// Show
//
2012-06-14 17:18:05 +08:00
Show* Show::create()
2012-06-14 17:18:05 +08:00
{
Show* pRet = new Show();
2011-09-05 10:12:14 +08:00
if (pRet) {
pRet->autorelease();
}
return pRet;
2011-09-05 10:12:14 +08:00
}
void Show::update(float time) {
CC_UNUSED_PARAM(time);
_target->setVisible(true);
2011-09-05 10:12:14 +08:00
}
ActionInstant* Show::reverse() const
{
return Hide::create();
2011-09-05 10:12:14 +08:00
}
Show * Show::clone() const
{
// no copy constructor
auto a = new Show();
a->autorelease();
return a;
}
Object* Show::copyWithZone(Zone *pZone) {
2011-09-05 10:12:14 +08:00
Zone *pNewZone = NULL;
Show *pRet = NULL;
if (pZone && pZone->_copyObject) {
pRet = (Show*) (pZone->_copyObject);
} else {
pRet = new Show();
pZone = pNewZone = new Zone(pRet);
}
2011-09-05 10:12:14 +08:00
ActionInstant::copyWithZone(pZone);
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
//
// Hide
//
Hide * Hide::create()
2012-06-14 17:18:05 +08:00
{
Hide *pRet = new Hide();
2011-09-05 10:12:14 +08:00
if (pRet) {
pRet->autorelease();
}
return pRet;
2011-09-05 10:12:14 +08:00
}
void Hide::update(float time) {
CC_UNUSED_PARAM(time);
_target->setVisible(false);
2011-09-05 10:12:14 +08:00
}
ActionInstant *Hide::reverse() const
{
return Show::create();
2011-09-05 10:12:14 +08:00
}
Hide * Hide::clone() const
{
// no copy constructor
auto a = new Hide();
a->autorelease();
return a;
}
Object* Hide::copyWithZone(Zone *pZone) {
Zone *pNewZone = NULL;
Hide *pRet = NULL;
2011-09-05 10:12:14 +08:00
if (pZone && pZone->_copyObject) {
pRet = (Hide*) (pZone->_copyObject);
} else {
pRet = new Hide();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
//
// ToggleVisibility
//
ToggleVisibility * ToggleVisibility::create()
{
ToggleVisibility *pRet = new ToggleVisibility();
2011-09-05 10:12:14 +08:00
if (pRet)
{
pRet->autorelease();
}
return pRet;
2011-09-05 10:12:14 +08:00
}
void ToggleVisibility::update(float time)
{
CC_UNUSED_PARAM(time);
_target->setVisible(!_target->isVisible());
2011-09-05 10:12:14 +08:00
}
ToggleVisibility * ToggleVisibility::reverse() const
{
return ToggleVisibility::create();
}
ToggleVisibility * ToggleVisibility::clone() const
{
// no copy constructor
auto a = new ToggleVisibility();
a->autorelease();
return a;
}
Object* ToggleVisibility::copyWithZone(Zone *pZone)
{
Zone *pNewZone = NULL;
ToggleVisibility *pRet = NULL;
if (pZone && pZone->_copyObject) {
pRet = (ToggleVisibility*) (pZone->_copyObject);
} else {
pRet = new ToggleVisibility();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
CC_SAFE_DELETE(pNewZone);
return pRet;
}
//
// Remove Self
//
RemoveSelf * RemoveSelf::create(bool isNeedCleanUp /*= true*/)
{
RemoveSelf *pRet = new RemoveSelf();
2013-03-25 18:18:38 +08:00
if (pRet && pRet->init(isNeedCleanUp)) {
pRet->autorelease();
}
return pRet;
}
bool RemoveSelf::init(bool isNeedCleanUp) {
_isNeedCleanUp = isNeedCleanUp;
2013-03-25 18:18:38 +08:00
return true;
}
void RemoveSelf::update(float time) {
CC_UNUSED_PARAM(time);
_target->removeFromParentAndCleanup(_isNeedCleanUp);
}
RemoveSelf *RemoveSelf::reverse() const
{
return RemoveSelf::create(_isNeedCleanUp);
}
RemoveSelf * RemoveSelf::clone() const
{
// no copy constructor
auto a = new RemoveSelf();
a->init(_isNeedCleanUp);
a->autorelease();
return a;
}
Object* RemoveSelf::copyWithZone(Zone *pZone) {
Zone *pNewZone = NULL;
RemoveSelf *pRet = NULL;
if (pZone && pZone->_copyObject) {
pRet = (RemoveSelf*) (pZone->_copyObject);
} else {
pRet = new RemoveSelf();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
pRet->init(_isNeedCleanUp);
CC_SAFE_DELETE(pNewZone);
return pRet;
}
2011-09-05 10:12:14 +08:00
//
// FlipX
//
2012-06-14 17:18:05 +08:00
FlipX *FlipX::create(bool x)
2012-06-14 17:18:05 +08:00
{
FlipX *pRet = new FlipX();
if (pRet && pRet->initWithFlipX(x)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2011-09-05 10:12:14 +08:00
}
bool FlipX::initWithFlipX(bool x) {
_flipX = x;
return true;
2011-09-05 10:12:14 +08:00
}
void FlipX::update(float time) {
CC_UNUSED_PARAM(time);
((Sprite*) (_target))->setFlipX(_flipX);
2011-09-05 10:12:14 +08:00
}
FlipX* FlipX::reverse() const
{
return FlipX::create(!_flipX);
2011-09-05 10:12:14 +08:00
}
FlipX * FlipX::clone() const
{
// no copy constructor
auto a = new FlipX();
a->initWithFlipX(_flipX);
a->autorelease();
return a;
}
Object * FlipX::copyWithZone(Zone *pZone) {
Zone *pNewZone = NULL;
FlipX *pRet = NULL;
2011-09-05 10:12:14 +08:00
if (pZone && pZone->_copyObject) {
pRet = (FlipX*) (pZone->_copyObject);
} else {
pRet = new FlipX();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
pRet->initWithFlipX(_flipX);
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
2011-09-05 10:12:14 +08:00
//
// FlipY
//
2012-06-14 17:18:05 +08:00
FlipY * FlipY::create(bool y)
2012-06-14 17:18:05 +08:00
{
FlipY *pRet = new FlipY();
if (pRet && pRet->initWithFlipY(y)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2011-09-05 10:12:14 +08:00
}
bool FlipY::initWithFlipY(bool y) {
_flipY = y;
return true;
2011-09-05 10:12:14 +08:00
}
void FlipY::update(float time) {
CC_UNUSED_PARAM(time);
((Sprite*) (_target))->setFlipY(_flipY);
2011-09-05 10:12:14 +08:00
}
FlipY* FlipY::reverse() const
{
return FlipY::create(!_flipY);
2011-09-05 10:12:14 +08:00
}
FlipY * FlipY::clone() const
{
// no copy constructor
auto a = new FlipY();
a->initWithFlipY(_flipY);
a->autorelease();
return a;
}
Object* FlipY::copyWithZone(Zone *pZone) {
Zone *pNewZone = NULL;
FlipY *pRet = NULL;
2011-09-05 10:12:14 +08:00
if (pZone && pZone->_copyObject) {
pRet = (FlipY*) (pZone->_copyObject);
} else {
pRet = new FlipY();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
pRet->initWithFlipY(_flipY);
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
//
// Place
//
2012-06-14 17:18:05 +08:00
Place* Place::create(const Point& pos)
2012-06-14 17:18:05 +08:00
{
Place *pRet = new Place();
2011-09-05 10:12:14 +08:00
if (pRet && pRet->initWithPosition(pos)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2011-09-05 10:12:14 +08:00
}
bool Place::initWithPosition(const Point& pos) {
_position = pos;
return true;
2011-09-05 10:12:14 +08:00
}
Place * Place::clone() const
{
// no copy constructor
auto a = new Place();
a->initWithPosition(_position);
a->autorelease();
return a;
}
Place * Place::reverse() const
{
// no reverse, just clone
return this->clone();
}
Object * Place::copyWithZone(Zone *pZone) {
Zone *pNewZone = NULL;
Place *pRet = NULL;
2011-09-05 10:12:14 +08:00
if (pZone && pZone->_copyObject) {
pRet = (Place*) (pZone->_copyObject);
} else {
pRet = new Place();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
pRet->initWithPosition(_position);
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
void Place::update(float time) {
CC_UNUSED_PARAM(time);
_target->setPosition(_position);
2011-09-05 10:12:14 +08:00
}
//
// CallFunc
//
CallFunc * CallFunc::create(const std::function<void()> &func)
{
CallFunc *pRet = new CallFunc();
if (pRet && pRet->initWithFunction(func) ) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
CallFunc * CallFunc::create(Object* pSelectorTarget, SEL_CallFunc selector)
2012-06-14 17:18:05 +08:00
{
CallFunc *pRet = new CallFunc();
2011-09-05 10:12:14 +08:00
if (pRet && pRet->initWithTarget(pSelectorTarget)) {
pRet->_callFunc = selector;
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2011-09-05 10:12:14 +08:00
}
CallFunc * CallFunc::create(int nHandler)
{
CallFunc *pRet = new CallFunc();
if (pRet) {
pRet->_scriptHandler = nHandler;
pRet->autorelease();
}
else{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
bool CallFunc::initWithFunction(const std::function<void()> &func)
{
_function = func;
return true;
}
bool CallFunc::initWithTarget(Object* pSelectorTarget) {
if (pSelectorTarget)
{
pSelectorTarget->retain();
}
2011-09-05 10:12:14 +08:00
if (_selectorTarget)
{
_selectorTarget->release();
}
_selectorTarget = pSelectorTarget;
return true;
2011-09-05 10:12:14 +08:00
}
CallFunc::~CallFunc(void)
{
if (_scriptHandler)
{
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(_scriptHandler);
}
CC_SAFE_RELEASE(_selectorTarget);
}
CallFunc * CallFunc::clone() const
2013-07-05 11:24:28 +08:00
{
// no copy constructor
auto a = new CallFunc();
if( _selectorTarget) {
a->initWithTarget(_selectorTarget);
a->_callFunc = _callFunc;
}
else if( _function ){
a->initWithFunction(_function);
}
else if (_scriptHandler > 0 ) {
a->_scriptHandler = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->reallocateScriptHandler(_scriptHandler);
}
2013-07-05 11:29:21 +08:00
a->autorelease();
return a;
}
CallFunc * CallFunc::reverse() const
{
// no reverse here, just return a clone
return this->clone();
}
Object * CallFunc::copyWithZone(Zone *pZone) {
Zone* pNewZone = NULL;
CallFunc* pRet = NULL;
2011-09-05 10:12:14 +08:00
if (pZone && pZone->_copyObject) {
//in case of being called at sub class
pRet = (CallFunc*) (pZone->_copyObject);
} else {
pRet = new CallFunc();
pZone = pNewZone = new Zone(pRet);
}
ActionInstant::copyWithZone(pZone);
if( _selectorTarget) {
pRet->initWithTarget(_selectorTarget);
pRet->_callFunc = _callFunc;
}
else if( _function )
pRet->initWithFunction(_function);
if (_scriptHandler > 0 ) {
pRet->_scriptHandler = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->reallocateScriptHandler(_scriptHandler);
}
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
void CallFunc::update(float time) {
CC_UNUSED_PARAM(time);
this->execute();
2011-09-05 10:12:14 +08:00
}
void CallFunc::execute() {
if (_callFunc) {
(_selectorTarget->*_callFunc)();
} else if( _function )
_function();
if (0 != _scriptHandler) {
BasicScriptData data((void*)this);
ScriptEvent event(kCallFuncEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
2011-09-05 10:12:14 +08:00
}
2011-09-05 10:12:14 +08:00
//
// CallFuncN
//
CallFuncN * CallFuncN::create(const std::function<void(Node*)> &func)
{
auto ret = new CallFuncN();
if (ret && ret->initWithFunction(func) ) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return NULL;
2011-09-05 10:12:14 +08:00
}
// XXX deprecated
CallFuncN * CallFuncN::create(Object* pSelectorTarget, SEL_CallFuncN selector)
2012-06-14 17:18:05 +08:00
{
CallFuncN *pRet = new CallFuncN();
2012-06-14 17:18:05 +08:00
if (pRet && pRet->initWithTarget(pSelectorTarget, selector))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2011-09-05 10:12:14 +08:00
}
CallFuncN * CallFuncN::create(int nHandler)
{
CallFuncN *pRet = new CallFuncN();
if (pRet) {
pRet->_scriptHandler = nHandler;
pRet->autorelease();
}
else{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
2011-09-05 10:12:14 +08:00
void CallFuncN::execute() {
if (_callFuncN) {
(_selectorTarget->*_callFuncN)(_target);
}
else if (_functionN) {
_functionN(_target);
}
2013-07-05 11:24:28 +08:00
if (0 != _scriptHandler)
{
BasicScriptData data((void*)this,(void*)_target);
ScriptEvent event(kCallFuncEvent,(void*)&data);
2013-07-05 11:24:28 +08:00
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
2011-09-05 10:12:14 +08:00
}
bool CallFuncN::initWithFunction(const std::function<void (Node *)> &func)
{
_functionN = func;
return true;
2011-09-05 10:12:14 +08:00
}
bool CallFuncN::initWithTarget(Object* pSelectorTarget, SEL_CallFuncN selector)
2012-06-14 17:18:05 +08:00
{
if (CallFunc::initWithTarget(pSelectorTarget)) {
_callFuncN = selector;
return true;
}
return false;
2011-09-05 10:12:14 +08:00
}
CallFuncN * CallFuncN::clone() const
{
// no copy constructor
auto a = new CallFuncN();
a->initWithTarget(_selectorTarget, _callFuncN);
a->autorelease();
return a;
}
Object * CallFuncN::copyWithZone(Zone* zone)
{
Zone* pNewZone = NULL;
CallFuncN* pRet = NULL;
if (zone && zone->_copyObject) {
//in case of being called at sub class
pRet = (CallFuncN*) (zone->_copyObject);
} else {
pRet = new CallFuncN();
zone = pNewZone = new Zone(pRet);
}
CallFunc::copyWithZone(zone);
pRet->initWithTarget(_selectorTarget, _callFuncN);
CC_SAFE_DELETE(pNewZone);
return pRet;
2011-09-05 10:12:14 +08:00
}
2012-04-18 18:43:45 +08:00
NS_CC_END