code:
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2009 Jason Booth

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.
****************************************************************************/

/*
 * Elastic, Back and Bounce actions based on code from:
 * http://github.com/NikhilK/silverlightfx/
 *
 * by http://github.com/NikhilK
 */

#include "CCActionEase.h"
#include "CCObject.h"

namespace cocos2d {

#ifndef M_PI_X_2
#define M_PI_X_2 (float)M_PI * 2.0f
#endif

	//
	// EaseAction
	//
	CCActionEase* CCActionEase::actionWithAction(CCActionInterval *pAction)
	{
		CCActionEase *pRet = new CCActionEase();
		if (pRet)
		{
	        if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	bool CCActionEase::initWithAction(CCActionInterval *pAction)
	{
		CCAssert(pAction != NULL, "");

		if (CCActionInterval::initWithDuration(pAction->getDuration()))
		{
			m_pOther = pAction;
			pAction->retain();

			return true;
		}

		return false;
	}

	CCObject* CCActionEase::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCActionEase* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCActionEase*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCActionEase();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCActionInterval::copyWithZone(pZone);

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	CCActionEase::~CCActionEase(void)
	{
		CC_SAFE_RELEASE(m_pOther);
	}

	void CCActionEase::startWithTarget(CCNode *pTarget)
	{
		CCActionInterval::startWithTarget(pTarget);
		m_pOther->startWithTarget(m_pTarget);
	}

	void CCActionEase::stop(void)
	{
		m_pOther->stop();
		CCActionInterval::stop();
	}

	void CCActionEase::update(ccTime time)
	{
		m_pOther->update(time);
	}

	CCActionInterval* CCActionEase::reverse(void)
	{
		return CCActionEase::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseRateAction
	//
	CCEaseRateAction* CCEaseRateAction::actionWithAction(CCActionInterval *pAction, float fRate)
	{
		CCEaseRateAction *pRet = new CCEaseRateAction();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fRate))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	bool CCEaseRateAction::initWithAction(CCActionInterval *pAction, float fRate)
	{
		if (CCActionEase::initWithAction(pAction))
		{
			m_fRate = fRate;
			return true;
		}

		return false;
	}

	CCObject* CCEaseRateAction::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseRateAction* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseRateAction*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseRateAction();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	CCEaseRateAction::~CCEaseRateAction(void)
	{
	}

	CCActionInterval* CCEaseRateAction::reverse(void)
	{
		return CCEaseRateAction::actionWithAction(m_pOther->reverse(), 1 / m_fRate);
	}

	//
	// EeseIn
	//
	CCEaseIn* CCEaseIn::actionWithAction(CCActionInterval *pAction, float fRate)
	{
		CCEaseIn *pRet = new CCEaseIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fRate))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseIn::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseIn* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseIn*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseIn();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseIn::update(ccTime time)
	{
		m_pOther->update(powf(time, m_fRate));
	}

	//
	// EaseOut
	//
	CCEaseOut* CCEaseOut::actionWithAction(CCActionInterval *pAction, float fRate)
	{
		CCEaseOut *pRet = new CCEaseOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fRate))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseOut::update(ccTime time)
	{
		m_pOther->update(powf(time, 1 / m_fRate));
	}

	//
	// EaseInOut
	//
	CCEaseInOut* CCEaseInOut::actionWithAction(CCActionInterval *pAction, float fRate)
	{
		CCEaseInOut *pRet = new CCEaseInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fRate))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseInOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseInOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseInOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseInOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseInOut::update(ccTime time)
	{
		int sign = 1;
		int r = (int) m_fRate;

		if (r % 2 == 0)
		{
			sign = -1;
		}

		time *= 2;
        if (time < 1)
		{
			m_pOther->update(0.5f * powf(time, m_fRate));
		}
		else
		{
			m_pOther->update(sign * 0.5f * (powf(time - 2, m_fRate) + sign * 2));
		}
	}

	// InOut and OutIn are symmetrical
	CCActionInterval* CCEaseInOut::reverse(void)
	{
		return CCEaseInOut::actionWithAction(m_pOther->reverse(), m_fRate);
	}

	//
	// EaseExponentialIn
	//
	CCEaseExponentialIn* CCEaseExponentialIn::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseExponentialIn *pRet = new CCEaseExponentialIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseExponentialIn::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseExponentialIn* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseExponentialIn*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseExponentialIn();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseExponentialIn::update(ccTime time)
	{
		m_pOther->update(time == 0 ? 0 : powf(2, 10 * (time/1 - 1)) - 1 * 0.001f);
	}

	CCActionInterval* CCEaseExponentialIn::reverse(void)
	{
		return CCEaseExponentialOut::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseExponentialOut
	//
	CCEaseExponentialOut* CCEaseExponentialOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseExponentialOut *pRet = new CCEaseExponentialOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseExponentialOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseExponentialOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseExponentialOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseExponentialOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseExponentialOut::update(ccTime time)
	{
		m_pOther->update(time == 1 ? 1 : (-powf(2, -10 * time / 1) + 1));
	}

	CCActionInterval* CCEaseExponentialOut::reverse(void)
	{
		return CCEaseExponentialIn::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseExponentialInOut
	//
	CCEaseExponentialInOut* CCEaseExponentialInOut::actionWithAction(CCActionInterval *pAction)
	{
		CCEaseExponentialInOut *pRet = new CCEaseExponentialInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseExponentialInOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseExponentialInOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseExponentialInOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseExponentialInOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseExponentialInOut::update(ccTime time)
	{
		time /= 0.5f;
		if (time < 1)
		{
			time = 0.5f * powf(2, 10 * (time - 1));
		}
		else
		{
			time = 0.5f * (-powf(2, 10 * (time - 1)) + 2);
		}

		m_pOther->update(time);
	}

	//
	// EaseSineIn
	//
	CCEaseSineIn* CCEaseSineIn::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseSineIn *pRet = new CCEaseSineIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseSineIn::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseSineIn* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseSineIn*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseSineIn();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseSineIn::update(ccTime time)
	{
		m_pOther->update(-1 * cosf(time * (float)M_PI_2) + 1);
	}

	CCActionInterval* CCEaseSineIn::reverse(void)
	{
		return CCEaseSineOut::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseSineOut
	//
	CCEaseSineOut* CCEaseSineOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseSineOut *pRet = new CCEaseSineOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseSineOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseSineOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseSineOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseSineOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseSineOut::update(ccTime time)
	{
		m_pOther->update(sinf(time * (float)M_PI_2));
	}

	CCActionInterval* CCEaseSineOut::reverse(void)
	{
		return CCEaseSineIn::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseSineInOut
	//
	CCEaseSineInOut* CCEaseSineInOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseSineInOut *pRet = new CCEaseSineInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseSineInOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseSineInOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseSineInOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseSineInOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseSineInOut::update(ccTime time)
	{
		m_pOther->update(-0.5f * (cosf((float)M_PI * time) - 1));
	}

	//
	// EaseElastic
	//
	CCEaseElastic* CCEaseElastic::actionWithAction(CCActionInterval *pAction)
	{
		CCEaseElastic *pRet = new CCEaseElastic();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCEaseElastic* CCEaseElastic::actionWithAction(CCActionInterval *pAction, float fPeriod)
	{
		CCEaseElastic *pRet = new CCEaseElastic();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fPeriod))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	bool CCEaseElastic::initWithAction(CCActionInterval *pAction)
	{
		return initWithAction(pAction, 0.3f);
	}

	bool CCEaseElastic::initWithAction(CCActionInterval *pAction, float fPeriod)
	{
		if (CCActionEase::initWithAction(pAction))
		{
			m_fPeriod = fPeriod;
			return true;
		}

		return false;
	}

	CCObject* CCEaseElastic::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseElastic* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseElastic*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseElastic();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	CCActionInterval* CCEaseElastic::reverse(void)
	{
		CCAssert(0, "");

		return NULL;
	}

    //
	// EaseElasticIn
	//
	CCEaseElasticIn* CCEaseElasticIn::actionWithAction(CCActionInterval *pAction, float fPeriod)
	{
		CCEaseElasticIn *pRet = new CCEaseElasticIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fPeriod))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCEaseElasticIn* CCEaseElasticIn::actionWithAction(CCActionInterval *pAction)
	{
		CCEaseElasticIn *pRet = new CCEaseElasticIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseElasticIn::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseElasticIn* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseElasticIn*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseElasticIn();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseElasticIn::update(ccTime time)
	{
		ccTime newT = 0;
		if (time == 0 || time == 1)
		{
			newT = time;
		}
		else
		{
			float s = m_fPeriod / 4;
			time = time - 1;
			newT = -powf(2, 10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod);
		}

		m_pOther->update(newT);
	}

	CCActionInterval* CCEaseElasticIn::reverse(void)
	{
		return CCEaseElasticOut::actionWithAction(m_pOther->reverse(), m_fPeriod);
	}

	//
	// EaseElasticOut
	//
	CCEaseElasticOut* CCEaseElasticOut::actionWithAction(CCActionInterval *pAction)
	{
		CCEaseElasticOut *pRet = new CCEaseElasticOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCEaseElasticOut* CCEaseElasticOut::actionWithAction(CCActionInterval *pAction, float fPeriod)
	{
		CCEaseElasticOut *pRet = new CCEaseElasticOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fPeriod))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject *CCEaseElasticOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseElasticOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseElasticOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseElasticOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseElasticOut::update(ccTime time)
	{
		ccTime newT = 0;
		if (time == 0 || time == 1)
		{
			newT = time;
		}
		else
		{
			float s = m_fPeriod / 4;
			newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod) + 1;
		}

		m_pOther->update(newT);
	}

	CCActionInterval* CCEaseElasticOut::reverse(void)
	{
		return CCEaseElasticIn::actionWithAction(m_pOther->reverse(), m_fPeriod);
	}

	//
	// EaseElasticInOut
	//
	CCEaseElasticInOut* CCEaseElasticInOut::actionWithAction(CCActionInterval *pAction)
	{
		CCEaseElasticInOut *pRet = new CCEaseElasticInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCEaseElasticInOut* CCEaseElasticInOut::actionWithAction(CCActionInterval *pAction, float fPeriod)
	{
		CCEaseElasticInOut *pRet = new CCEaseElasticInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, fPeriod))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseElasticInOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseElasticInOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseElasticInOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseElasticInOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;

	}

	void CCEaseElasticInOut::update(ccTime time)
	{
		ccTime newT = 0;
		if (time == 0 || time == 1)
		{
			newT = time;
		}
		else
		{
			time = time * 2;
			if (! m_fPeriod)
			{
				m_fPeriod = 0.3f * 1.5f;
			}

			ccTime s = m_fPeriod / 4;

			time = time - 1;
			if (time < 0)
			{
				newT = -0.5f * powf(2, 10 * time) * sinf((time -s) * M_PI_X_2 / m_fPeriod);
			}
			else
			{
				newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod) * 0.5f + 1;
			}
		}

		m_pOther->update(newT);
	}

	CCActionInterval* CCEaseElasticInOut::reverse(void)
	{
		return CCEaseInOut::actionWithAction(m_pOther->reverse(), m_fPeriod);
	}

	//
	// EaseBounce
	//
	CCEaseBounce* CCEaseBounce::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseBounce *pRet = new CCEaseBounce();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBounce::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBounce* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBounce*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBounce();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	ccTime CCEaseBounce::bounceTime(ccTime time)
	{
		if (time < 1 / 2.75)
		{
			return 7.5625f * time * time;
		} else
		if (time < 2 / 2.75)
		{
			time -= 1.5f / 2.75f;
			return 7.5625f * time * time + 0.75f;
		} else
		if(time < 2.5 / 2.75)
		{
			time -= 2.25f / 2.75f;
			return 7.5625f * time * time + 0.9375f;
		}

		time -= 2.625f / 2.75f;
		return 7.5625f * time * time + 0.984375f;
	}

	//
	// EaseBounceIn
	//
	CCEaseBounceIn* CCEaseBounceIn::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseBounceIn *pRet = new CCEaseBounceIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBounceIn::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBounceIn* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBounceIn*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBounceIn();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseBounceIn::update(ccTime time)
	{
		ccTime newT = 1 - bounceTime(1 - time);
		m_pOther->update(newT);
	}

	CCActionInterval* CCEaseBounceIn::reverse(void)
	{
		return CCEaseBounceOut::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseBounceOut
	//
	CCEaseBounceOut* CCEaseBounceOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseBounceOut *pRet = new CCEaseBounceOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBounceOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBounceOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBounceOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBounceOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseBounceOut::update(ccTime time)
	{
		ccTime newT = bounceTime(time);
		m_pOther->update(newT);
	}

	CCActionInterval* CCEaseBounceOut::reverse(void)
	{
		return CCEaseBounceIn::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseBounceInOut
	//
	CCEaseBounceInOut* CCEaseBounceInOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseBounceInOut *pRet = new CCEaseBounceInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBounceInOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBounceInOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBounceInOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBounceInOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseBounceInOut::update(ccTime time)
	{
		ccTime newT = 0;
		if (time < 0.5f)
		{
			time = time * 2;
			newT = (1 - bounceTime(1 - time)) * 0.5f;
		}
		else
		{
			newT = bounceTime(time * 2 - 1) * 0.5f + 0.5f;
		}

		m_pOther->update(newT);
	}

	//
	// EaseBackIn
	//
	CCEaseBackIn* CCEaseBackIn::actionWithAction(CCActionInterval *pAction)
	{
		CCEaseBackIn *pRet = new CCEaseBackIn();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBackIn::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBackIn* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBackIn*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBackIn();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseBackIn::update(ccTime time)
	{
		ccTime overshoot = 1.70158f;
		m_pOther->update(time * time * ((overshoot + 1) * time - overshoot));
	}

	CCActionInterval* CCEaseBackIn::reverse(void)
	{
		return CCEaseBackOut::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseBackOut
	//
	CCEaseBackOut* CCEaseBackOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseBackOut *pRet = new CCEaseBackOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBackOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBackOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBackOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBackOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseBackOut::update(ccTime time)
	{
		ccTime overshoot = 1.70158f;

		time = time - 1;
		m_pOther->update(time * time * ((overshoot + 1) * time + overshoot) + 1);
	}

	CCActionInterval* CCEaseBackOut::reverse(void)
	{
		return CCEaseBackIn::actionWithAction(m_pOther->reverse());
	}

	//
	// EaseBackInOut
	//
	CCEaseBackInOut* CCEaseBackInOut::actionWithAction(CCActionInterval* pAction)
	{
		CCEaseBackInOut *pRet = new CCEaseBackInOut();
		if (pRet)
		{
			if (pRet->initWithAction(pAction))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pRet);
			}
		}

		return pRet;
	}

	CCObject* CCEaseBackInOut::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCEaseBackInOut* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCEaseBackInOut*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCEaseBackInOut();
			pZone = pNewZone = new CCZone(pCopy);
		}

		pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCEaseBackInOut::update(ccTime time)
	{
		ccTime overshoot = 1.70158f * 1.525f;

		time = time * 2;
		if (time < 1)
		{
			m_pOther->update((time * time * ((overshoot + 1) * time - overshoot)) / 2);
		}
		else
		{
			time = time - 2;
			m_pOther->update((time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1);
		}
	}
}


code:
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2009      On-Core

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 "CCActionGrid.h"
#include "CCDirector.h"
#include "effects/CCGrid.h"

namespace cocos2d
{
	// implementation of CCGridAction

	CCGridAction* CCGridAction::actionWithSize(const ccGridSize& gridSize, ccTime duration)
	{
		CCGridAction *pAction = new CCGridAction();
		if (pAction)
		{
			if (pAction->initWithSize(gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_DELETE(pAction);
			}
		}

		return pAction;
	}

	bool CCGridAction::initWithSize(const ccGridSize& gridSize, ccTime duration)
	{
		if (CCActionInterval::initWithDuration(duration))
		{
			m_sGridSize = gridSize;

			return true;
		}

		return false;
	}

	void CCGridAction::startWithTarget(CCNode *pTarget)
	{
		CCActionInterval::startWithTarget(pTarget);

		CCGridBase *newgrid = this->getGrid();

		CCNode *t = m_pTarget;
		CCGridBase *targetGrid = t->getGrid();

		if (targetGrid && targetGrid->getReuseGrid() > 0)
		{
			if (targetGrid->isActive() && targetGrid->getGridSize().x == m_sGridSize.x
				&& targetGrid->getGridSize().y == m_sGridSize.y /*&& dynamic_cast<CCGridBase*>(targetGrid) != NULL*/)
			{
				targetGrid->reuse();
			}
			else
			{
				CCAssert(0, "");
			}
		}
		else
		{
			if (targetGrid && targetGrid->isActive())
			{
				targetGrid->setActive(false);
			}

			t->setGrid(newgrid);
			t->getGrid()->setActive(true);
		}
	}

	CCGridBase* CCGridAction::getGrid(void)
	{
		// Abstract class needs implementation
		CCAssert(0, "");

		return NULL;
	}

	CCActionInterval* CCGridAction::reverse(void)
	{
		return CCReverseTime::actionWithAction(this);
	}

	CCObject* CCGridAction::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCGridAction* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCGridAction*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCGridAction();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCActionInterval::copyWithZone(pZone);

		pCopy->initWithSize(m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	// implementation of Grid3DAction

	CCGridBase* CCGrid3DAction::getGrid(void)
	{
		return CCGrid3D::gridWithSize(m_sGridSize);
	}

	ccVertex3F CCGrid3DAction::vertex(const ccGridSize& pos)
	{
		CCGrid3D *g = (CCGrid3D*)m_pTarget->getGrid();
		return g->vertex(pos);
	}

	ccVertex3F CCGrid3DAction::originalVertex(const ccGridSize& pos)
	{
		CCGrid3D *g = (CCGrid3D*)m_pTarget->getGrid();
		return g->originalVertex(pos);
	}

	void CCGrid3DAction::setVertex(const ccGridSize& pos, const ccVertex3F& vertex)
	{
		CCGrid3D *g = (CCGrid3D*)m_pTarget->getGrid();
		g->setVertex(pos, vertex);
	}

	// implementation of TiledGrid3DAction

	CCGridBase* CCTiledGrid3DAction::getGrid(void)
	{
		return CCTiledGrid3D::gridWithSize(m_sGridSize);
	}

	ccQuad3 CCTiledGrid3DAction::tile(const ccGridSize& pos)
	{
		CCTiledGrid3D *g = (CCTiledGrid3D*)m_pTarget->getGrid();
		return g->tile(pos);
	}

	ccQuad3 CCTiledGrid3DAction::originalTile(const ccGridSize& pos)
	{
		CCTiledGrid3D *g = (CCTiledGrid3D*)m_pTarget->getGrid();
		return g->originalTile(pos);
	}

	void CCTiledGrid3DAction::setTile(const ccGridSize& pos, const ccQuad3& coords)
	{
		CCTiledGrid3D *g = (CCTiledGrid3D*)m_pTarget->getGrid();
		return g->setTile(pos, coords);
	}

	// implementation CCAccelDeccelAmplitude

	CCAccelDeccelAmplitude* CCAccelDeccelAmplitude::actionWithAction(CCAction *pAction, ccTime duration)
	{
		CCAccelDeccelAmplitude *pRet = new CCAccelDeccelAmplitude();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, duration))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_DELETE(pRet);
			}
		}

		return pRet;
	}

	bool CCAccelDeccelAmplitude::initWithAction(CCAction *pAction, ccTime duration)
	{
		if (CCActionInterval::initWithDuration(duration))
		{
			m_fRate = 1.0f;
			m_pOther = (CCActionInterval*)(pAction);
			pAction->retain();

			return true;
		}

		return false;
	}

	CCAccelDeccelAmplitude::~CCAccelDeccelAmplitude(void)
	{
		CC_SAFE_RELEASE(m_pOther);
	}

	void CCAccelDeccelAmplitude::startWithTarget(CCNode *pTarget)
	{
		CCActionInterval::startWithTarget(pTarget);
		m_pOther->startWithTarget(pTarget);
	}

	void CCAccelDeccelAmplitude::update(ccTime time)
	{
		float f = time * 2;

		if (f > 1)
		{
			f -= 1;
			f = 1 - f;
		}

		((CCAccelDeccelAmplitude*)(m_pOther))->setAmplitudeRate(powf(f, m_fRate));
	}

	CCActionInterval* CCAccelDeccelAmplitude::reverse(void)
	{
		return CCAccelDeccelAmplitude::actionWithAction(m_pOther->reverse(), m_fDuration);
	}

	// implementation of AccelAmplitude

	CCAccelAmplitude* CCAccelAmplitude::actionWithAction(CCAction *pAction, ccTime duration)
	{
		CCAccelAmplitude *pRet = new CCAccelAmplitude();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, duration))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_DELETE(pRet);
			}
		}

		return pRet;
	}

	bool CCAccelAmplitude::initWithAction(CCAction *pAction, ccTime duration)
	{
		if (CCActionInterval::initWithDuration(duration))
		{
			m_fRate = 1.0f;
			m_pOther = (CCActionInterval*)(pAction);
			pAction->retain();

			return true;
		}

		return false;
	}

	CCAccelAmplitude::~CCAccelAmplitude(void)
	{
		CC_SAFE_DELETE(m_pOther);
	}

	void CCAccelAmplitude::startWithTarget(CCNode *pTarget)
	{
		CCActionInterval::startWithTarget(pTarget);
		m_pOther->startWithTarget(pTarget);
	}

	void CCAccelAmplitude::update(ccTime time)
	{
        ((CCAccelAmplitude*)(m_pOther))->setAmplitudeRate(powf(time, m_fRate));
		m_pOther->update(time);
	}

	CCActionInterval* CCAccelAmplitude::reverse(void)
	{
		return CCAccelAmplitude::actionWithAction(m_pOther->reverse(), m_fDuration);
	}

	// DeccelAmplitude

	CCDeccelAmplitude* CCDeccelAmplitude::actionWithAction(CCAction *pAction, ccTime duration)
	{
		CCDeccelAmplitude *pRet = new CCDeccelAmplitude();
		if (pRet)
		{
			if (pRet->initWithAction(pAction, duration))
			{
				pRet->autorelease();
			}
			else
			{
				CC_SAFE_DELETE(pRet);
			}
		}

		return pRet;
	}

	bool CCDeccelAmplitude::initWithAction(CCAction *pAction, ccTime duration)
	{
		if (CCActionInterval::initWithDuration(duration))
		{
			m_fRate = 1.0f;
			m_pOther = (CCActionInterval*)(pAction);
			pAction->retain();

			return true;
		}

		return false;
	}

	CCDeccelAmplitude::~CCDeccelAmplitude(void)
	{
		CC_SAFE_RELEASE(m_pOther);
	}

	void CCDeccelAmplitude::startWithTarget(CCNode *pTarget)
	{
		CCActionInterval::startWithTarget(pTarget);
		m_pOther->startWithTarget(pTarget);
	}

	void CCDeccelAmplitude::update(ccTime time)
	{
        ((CCDeccelAmplitude*)(m_pOther))->setAmplitudeRate(powf((1 - time), m_fRate));
		m_pOther->update(time);
	}

	CCActionInterval* CCDeccelAmplitude::reverse(void)
	{
		return CCDeccelAmplitude::actionWithAction(m_pOther->reverse(), m_fDuration);
	}

	// implementation of StopGrid

	void CCStopGrid::startWithTarget(CCNode *pTarget)
	{
		CCActionInstant::startWithTarget(pTarget);

		CCGridBase *pGrid = m_pTarget->getGrid();
		if (pGrid && pGrid->isActive())
		{
			pGrid->setActive(false);
		}
	}

	CCStopGrid* CCStopGrid::action(void)
	{
		CCStopGrid* pAction = new CCStopGrid();
		pAction->autorelease();

		return pAction;
	}

	// implementation of CCReuseGrid

	CCReuseGrid* CCReuseGrid::actionWithTimes(int times)
	{
		CCReuseGrid *pAction = new CCReuseGrid();
		if (pAction)
		{
			if (pAction->initWithTimes(times))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_DELETE(pAction);
			}
		}

		return pAction;
	}

	bool CCReuseGrid::initWithTimes(int times)
	{
		m_nTimes = times;

		return true;
	}

	void CCReuseGrid::startWithTarget(CCNode *pTarget)
	{
		CCActionInstant::startWithTarget(pTarget);

		if (m_pTarget->getGrid() && m_pTarget->getGrid()->isActive())
		{
			m_pTarget->getGrid()->setReuseGrid(m_pTarget->getGrid()->getReuseGrid() + m_nTimes);
		}
	}
} // end of namespace cocos2d

license: mit


code:
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2009      On-Core

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 "CCActionGrid3D.h"
#include "CCPointExtension.h"
#include "CCDirector.h"

#include <stdlib.h>

namespace cocos2d
{
	// implementation of CCWaves3D

	CCWaves3D* CCWaves3D::actionWithWaves(int wav, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		CCWaves3D *pAction = new CCWaves3D();

		if (pAction)
		{
			if (pAction->initWithWaves(wav, amp, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCWaves3D::initWithWaves(int wav, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			m_nWaves = wav;
			m_fAmplitude = amp;
			m_fAmplitudeRate = 1.0f;

			return true;
		}

		return false;
	}

	CCObject* CCWaves3D::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCWaves3D* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCWaves3D*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCWaves3D();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithWaves(m_nWaves, m_fAmplitude, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCWaves3D::update(ccTime time)
	{
		int i, j;
		for (i = 0; i < m_sGridSize.x + 1; ++i)
		{
			for (j = 0; j < m_sGridSize.y + 1; ++j)
			{
				ccVertex3F v = originalVertex(ccg(i ,j));
				v.z += (sinf((CGFloat)M_PI * time * m_nWaves * 2 + (v.y+v.x) * .01f) * m_fAmplitude * m_fAmplitudeRate);
				CCLog("v.z offset is %f\n", (sinf((CGFloat)M_PI * time * m_nWaves * 2 + (v.y+v.x) * .01f) * m_fAmplitude * m_fAmplitudeRate));
				setVertex(ccg(i, j), v);
			}
		}
	}

	// implementation of CCFlipX3D

	CCFlipX3D* CCFlipX3D::actionWithDuration(ccTime duration)
	{
		CCFlipX3D *pAction = new CCFlipX3D();

		if (pAction)
		{
			if (pAction->initWithSize(ccg(1, 1), duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCFlipX3D::initWithDuration(ccTime duration)
	{
		return CCGrid3DAction::initWithSize(ccg(1, 1), duration);
	}

	bool CCFlipX3D::initWithSize(const ccGridSize& gridSize, ccTime duration)
	{
		if (gridSize.x != 1 || gridSize.y != 1)
		{
			// Grid size must be (1,1)
			CCAssert(0, "");

			return false;
		}

		return CCGrid3DAction::initWithSize(gridSize, duration);
	}

	CCObject* CCFlipX3D::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCFlipX3D* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCFlipX3D*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCFlipX3D();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithSize(m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCFlipX3D::update(ccTime time)
	{
		CGFloat angle = (CGFloat)M_PI * time; // 180 degrees
		CGFloat mz = sinf(angle);
		angle = angle / 2.0f; // x calculates degrees from 0 to 90
		CGFloat mx = cosf(angle);

		ccVertex3F v0, v1, v, diff;

		v0 = originalVertex(ccg(1, 1));
		v1 = originalVertex(ccg(0, 0));

		CGFloat	x0 = v0.x;
		CGFloat	x1 = v1.x;
		CGFloat x;
		ccGridSize	a, b, c, d;

		if ( x0 > x1 )
		{
			// Normal Grid
			a = ccg(0,0);
			b = ccg(0,1);
			c = ccg(1,0);
			d = ccg(1,1);
			x = x0;
		}
		else
		{
			// Reversed Grid
			c = ccg(0,0);
			d = ccg(0,1);
			a = ccg(1,0);
			b = ccg(1,1);
			x = x1;
		}

		diff.x = ( x - x * mx );
		diff.z = fabsf( floorf( (x * mz) / 4.0f ) );

		// bottom-left
		v = originalVertex(a);
		v.x = diff.x;
		v.z += diff.z;
		setVertex(a, v);

		// upper-left
		v = originalVertex(b);
		v.x = diff.x;
		v.z += diff.z;
		setVertex(b, v);

		// bottom-right
		v = originalVertex(c);
		v.x -= diff.x;
		v.z -= diff.z;
		setVertex(c, v);

		// upper-right
		v = originalVertex(d);
		v.x -= diff.x;
		v.z -= diff.z;
		setVertex(d, v);
	}

	// implementation of FlipY3D

	CCFlipY3D* CCFlipY3D::actionWithDuration(ccTime duration)
	{
		CCFlipY3D *pAction = new CCFlipY3D();

		if (pAction)
		{
			if (pAction->initWithSize(ccg(1, 1), duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	CCObject* CCFlipY3D::copyWithZone(CCZone* pZone)
	{
		CCZone* pNewZone = NULL;
		CCFlipY3D* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCFlipY3D*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCFlipY3D();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCFlipX3D::copyWithZone(pZone);

		pCopy->initWithSize(m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCFlipY3D::update(ccTime time)
	{
		CGFloat angle = (CGFloat)M_PI * time; // 180 degrees
		CGFloat mz = sinf( angle );
		angle = angle / 2.0f;     // x calculates degrees from 0 to 90
		CGFloat my = cosf(angle);

		ccVertex3F	v0, v1, v, diff;

		v0 = originalVertex(ccg(1, 1));
		v1 = originalVertex(ccg(0, 0));

		CGFloat	y0 = v0.y;
		CGFloat	y1 = v1.y;
		CGFloat y;
		ccGridSize	a, b, c, d;

		if (y0 > y1)
		{
			// Normal Grid
			a = ccg(0,0);
			b = ccg(0,1);
			c = ccg(1,0);
			d = ccg(1,1);
			y = y0;
		}
		else
		{
			// Reversed Grid
			b = ccg(0,0);
			a = ccg(0,1);
			d = ccg(1,0);
			c = ccg(1,1);
			y = y1;
		}

		diff.y = y - y * my;
		diff.z = fabsf(floorf((y * mz) / 4.0f));

		// bottom-left
		v = originalVertex(a);
		v.y = diff.y;
		v.z += diff.z;
		setVertex(a, v);

		// upper-left
		v = originalVertex(b);
		v.y -= diff.y;
		v.z -= diff.z;
		setVertex(b, v);

		// bottom-right
		v = originalVertex(c);
		v.y = diff.y;
		v.z += diff.z;
		setVertex(c, v);

		// upper-right
		v = originalVertex(d);
		v.y -= diff.y;
		v.z -= diff.z;
		setVertex(d, v);
	}

	// implementation of Lens3D

	CCLens3D* CCLens3D::actionWithPosition(const CCPoint& pos, float r, const ccGridSize& gridSize, ccTime duration)
	{
		CCLens3D *pAction = new CCLens3D();

		if (pAction)
		{
			if (pAction->initWithPosition(pos, r, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCLens3D::initWithPosition(const CCPoint& pos, float r, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			m_position = ccp(-1, -1);
            setPosition(pos);
			m_fRadius = r;
			m_fLensEffect = 0.7f;
            m_bDirty = true;

			return true;
		}

		return false;
	}

	CCObject* CCLens3D::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCLens3D* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCLens3D*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCLens3D();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithPosition(m_position, m_fRadius, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

    void CCLens3D::setPosition(const CCPoint& pos)
    {
        if( ! CCPoint::CCPointEqualToPoint(pos, m_position) ) {
            m_position = pos;
            m_positionInPixels.x = pos.x * CC_CONTENT_SCALE_FACTOR();
            m_positionInPixels.y = pos.y * CC_CONTENT_SCALE_FACTOR();

            m_bDirty = true;
        }
    }

	void CCLens3D::update(ccTime time)
	{
        CC_UNUSED_PARAM(time);
		if (m_bDirty)
		{
			int i, j;

			for (i = 0; i < m_sGridSize.x + 1; ++i)
			{
				for (j = 0; j < m_sGridSize.y + 1; ++j)
				{
					ccVertex3F v = originalVertex(ccg(i, j));
					CCPoint vect = ccpSub(m_positionInPixels, ccp(v.x, v.y));
					CGFloat r = ccpLength(vect);

					if (r < m_fRadius)
					{
						r = m_fRadius - r;
						CGFloat pre_log = r / m_fRadius;
						if ( pre_log == 0 )
						{
							pre_log = 0.001f;
						}

						float l = logf(pre_log) * m_fLensEffect;
						float new_r = expf( l ) * m_fRadius;

						if (ccpLength(vect) > 0)
						{
							vect = ccpNormalize(vect);
							CCPoint new_vect = ccpMult(vect, new_r);
							v.z += ccpLength(new_vect) * m_fLensEffect;
						}
					}

					setVertex(ccg(i, j), v);
				}
			}

			m_bDirty = false;
		}
	}

	// implementation of Ripple3D

	CCRipple3D* CCRipple3D::actionWithPosition(const CCPoint& pos, float r, int wav, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		CCRipple3D *pAction = new CCRipple3D();

		if (pAction)
		{
			if (pAction->initWithPosition(pos, r, wav, amp, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCRipple3D::initWithPosition(const CCPoint& pos, float r, int wav, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			setPosition(pos);
			m_fRadius = r;
			m_nWaves = wav;
			m_fAmplitude = amp;
			m_fAmplitudeRate = 1.0f;

			return true;
		}

		return false;
	}

    void CCRipple3D::setPosition(const CCPoint& position)
    {
        m_position = position;
        m_positionInPixels.x = position.x * CC_CONTENT_SCALE_FACTOR();
        m_positionInPixels.y = position.y * CC_CONTENT_SCALE_FACTOR();
    }

	CCObject* CCRipple3D::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCRipple3D* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCRipple3D*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCRipple3D();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithPosition(m_position, m_fRadius, m_nWaves, m_fAmplitude, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCRipple3D::update(ccTime time)
	{
		int i, j;

		for (i = 0; i < (m_sGridSize.x+1); ++i)
		{
			for (j = 0; j < (m_sGridSize.y+1); ++j)
			{
				ccVertex3F v = originalVertex(ccg(i, j));
				CCPoint vect = ccpSub(m_positionInPixels, ccp(v.x,v.y));
				CGFloat r = ccpLength(vect);

				if (r < m_fRadius)
				{
					r = m_fRadius - r;
					CGFloat rate = powf(r / m_fRadius, 2);
					v.z += (sinf( time*(CGFloat)M_PI * m_nWaves * 2 + r * 0.1f) * m_fAmplitude * m_fAmplitudeRate * rate);
				}

				setVertex(ccg(i, j), v);
			}
		}
	}

	// implementation of Shaky3D

	CCShaky3D* CCShaky3D::actionWithRange(int range, bool shakeZ, const ccGridSize& gridSize, ccTime duration)
	{
		CCShaky3D *pAction = new CCShaky3D();

		if (pAction)
		{
			if (pAction->initWithRange(range, shakeZ, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCShaky3D::initWithRange(int range, bool shakeZ, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			m_nRandrange = range;
			m_bShakeZ = shakeZ;

			return true;
		}

		return false;
	}

	CCObject* CCShaky3D::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCShaky3D* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCShaky3D*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCShaky3D();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithRange(m_nRandrange, m_bShakeZ, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCShaky3D::update(ccTime time)
	{
        CC_UNUSED_PARAM(time);
		int i, j;

		for (i = 0; i < (m_sGridSize.x+1); ++i)
		{
			for (j = 0; j < (m_sGridSize.y+1); ++j)
			{
				ccVertex3F v = originalVertex(ccg(i ,j));
				v.x += (rand() % (m_nRandrange*2)) - m_nRandrange;
				v.y += (rand() % (m_nRandrange*2)) - m_nRandrange;
				if (m_bShakeZ)
				{
					v.z += (rand() % (m_nRandrange*2)) - m_nRandrange;
				}

				setVertex(ccg(i, j), v);
			}
		}
	}

	// implementation of Liquid

	CCLiquid* CCLiquid::actionWithWaves(int wav, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		CCLiquid *pAction = new CCLiquid();

		if (pAction)
		{
			if (pAction->initWithWaves(wav, amp, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCLiquid::initWithWaves(int wav, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			m_nWaves = wav;
			m_fAmplitude = amp;
			m_fAmplitudeRate = 1.0f;

			return true;
		}

		return false;
	}

	CCObject* CCLiquid::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCLiquid* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCLiquid*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCLiquid();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithWaves(m_nWaves, m_fAmplitude, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCLiquid::update(ccTime time)
	{
		int i, j;

		for (i = 1; i < m_sGridSize.x; ++i)
		{
			for (j = 1; j < m_sGridSize.y; ++j)
			{
				ccVertex3F v = originalVertex(ccg(i, j));
				v.x = (v.x + (sinf(time * (CGFloat)M_PI * m_nWaves * 2 + v.x * .01f) * m_fAmplitude * m_fAmplitudeRate));
				v.y = (v.y + (sinf(time * (CGFloat)M_PI * m_nWaves * 2 + v.y * .01f) * m_fAmplitude * m_fAmplitudeRate));
				setVertex(ccg(i, j), v);
			}
		}
	}

	// implementation of Waves

	CCWaves* CCWaves::actionWithWaves(int wav, float amp, bool h, bool v, const ccGridSize& gridSize, ccTime duration)
	{
		CCWaves *pAction = new CCWaves();

		if (pAction)
		{
			if (pAction->initWithWaves(wav, amp, h, v, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCWaves::initWithWaves(int wav, float amp, bool h, bool v, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			m_nWaves = wav;
			m_fAmplitude = amp;
			m_fAmplitudeRate = 1.0f;
			m_bHorizontal = h;
			m_bVertical = v;

			return true;
		}

		return false;
	}

	CCObject* CCWaves::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCWaves* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCWaves*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCWaves();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithWaves(m_nWaves, m_fAmplitude, m_bHorizontal, m_bVertical, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCWaves::update(ccTime time)
	{
		int i, j;

		for (i = 0; i < m_sGridSize.x + 1; ++i)
		{
			for (j = 0; j < m_sGridSize.y + 1; ++j)
			{
				ccVertex3F v = originalVertex(ccg(i, j));

				if (m_bVertical)
				{
					v.x = (v.x + (sinf(time * (CGFloat)M_PI * m_nWaves * 2 + v.y * .01f) * m_fAmplitude * m_fAmplitudeRate));
				}

				if (m_bHorizontal)
				{
                    v.y = (v.y + (sinf(time * (CGFloat)M_PI * m_nWaves * 2 + v.x * .01f) * m_fAmplitude * m_fAmplitudeRate));
				}

				setVertex(ccg(i, j), v);
			}
		}
	}

	// implementation of Twirl

	CCTwirl* CCTwirl::actionWithPosition(CCPoint pos, int t, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		CCTwirl *pAction = new CCTwirl();

		if (pAction)
		{
			if (pAction->initWithPosition(pos, t, amp, gridSize, duration))
			{
				pAction->autorelease();
			}
			else
			{
				CC_SAFE_RELEASE_NULL(pAction);
			}
		}

		return pAction;
	}

	bool CCTwirl::initWithPosition(const CCPoint& pos, int t, float amp, const ccGridSize& gridSize, ccTime duration)
	{
		if (CCGrid3DAction::initWithSize(gridSize, duration))
		{
			setPosition(pos);
			m_nTwirls = t;
			m_fAmplitude = amp;
			m_fAmplitudeRate = 1.0f;

			return true;
		}

		return false;
	}

    void CCTwirl::setPosition(const CCPoint& position)
    {
        m_position = position;
        m_positionInPixels.x = position.x * CC_CONTENT_SCALE_FACTOR();
        m_positionInPixels.y = position.y * CC_CONTENT_SCALE_FACTOR();
    }

	CCObject* CCTwirl::copyWithZone(CCZone *pZone)
	{
		CCZone* pNewZone = NULL;
		CCTwirl* pCopy = NULL;
		if(pZone && pZone->m_pCopyObject)
		{
			//in case of being called at sub class
			pCopy = (CCTwirl*)(pZone->m_pCopyObject);
		}
		else
		{
			pCopy = new CCTwirl();
			pZone = pNewZone = new CCZone(pCopy);
		}

		CCGrid3DAction::copyWithZone(pZone);

		pCopy->initWithPosition(m_position, m_nTwirls, m_fAmplitude, m_sGridSize, m_fDuration);

		CC_SAFE_DELETE(pNewZone);
		return pCopy;
	}

	void CCTwirl::update(ccTime time)
	{
		int i, j;
		CCPoint	c = m_positionInPixels;

		for (i = 0; i < (m_sGridSize.x+1); ++i)
		{
			for (j = 0; j < (m_sGridSize.y+1); ++j)
			{
				ccVertex3F v = originalVertex(ccg(i ,j));

				CCPoint	avg = ccp(i-(m_sGridSize.x/2.0f), j-(m_sGridSize.y/2.0f));
				CGFloat r = ccpLength(avg);

				CGFloat amp = 0.1f * m_fAmplitude * m_fAmplitudeRate;
				CGFloat a = r * cosf( (CGFloat)M_PI/2.0f + time * (CGFloat)M_PI * m_nTwirls * 2 ) * amp;

				CCPoint	d;

				d.x = sinf(a) * (v.y-c.y) + cosf(a) * (v.x-c.x);
				d.y = cosf(a) * (v.y-c.y) - sinf(a) * (v.x-c.x);

				v.x = c.x + d.x;
				v.y = c.y + d.y;

				setVertex(ccg(i ,j), v);
			}
		}
	}

} // end of namespace cocos2d


code:
/****************************************************************************
 Copyright (c) 2010-2011 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011 Zynga Inc.

 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 "CCActionInstant.h"
#include "CCNode.h"
#include "CCSprite.h"

namespace cocos2d {
//
// InstantAction
//
CCActionInstant::CCActionInstant() {
}

CCObject * CCActionInstant::copyWithZone(CCZone *pZone) {
	CCZone *pNewZone = NULL;
	CCActionInstant *pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		pRet = (CCActionInstant*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCActionInstant();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCFiniteTimeAction::copyWithZone(pZone);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

bool CCActionInstant::isDone() {
	return true;
}

void CCActionInstant::step(ccTime dt) {
	CC_UNUSED_PARAM(dt);
	update(1);
}

void CCActionInstant::update(ccTime time) {
	CC_UNUSED_PARAM(time);
	// ignore
}

CCFiniteTimeAction * CCActionInstant::reverse() {
	return (CCFiniteTimeAction*) (copy()->autorelease());
}

//
// Show
//
CCShow* CCShow::action() {
	CCShow* pRet = new CCShow();

	if (pRet) {
		pRet->autorelease();
	}

	return pRet;
}

void CCShow::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	pTarget->setIsVisible(true);
}

CCFiniteTimeAction* CCShow::reverse() {
	return (CCFiniteTimeAction*) (CCHide::action());
}

CCObject* CCShow::copyWithZone(CCZone *pZone) {

	CCZone *pNewZone = NULL;
	CCShow *pRet = NULL;
	if (pZone && pZone->m_pCopyObject) {
		pRet = (CCShow*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCShow();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCFiniteTimeAction::copyWithZone(pZone);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

//
// Hide
//
CCHide * CCHide::action() {
	CCHide *pRet = new CCHide();

	if (pRet) {
		pRet->autorelease();
	}

	return pRet;
}

void CCHide::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	pTarget->setIsVisible(false);
}

CCFiniteTimeAction *CCHide::reverse() {
	return (CCFiniteTimeAction*) (CCShow::action());
}

CCObject* CCHide::copyWithZone(CCZone *pZone) {
	CCZone *pNewZone = NULL;
	CCHide *pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		pRet = (CCHide*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCHide();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCFiniteTimeAction::copyWithZone(pZone);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

//
// ToggleVisibility
//
CCToggleVisibility * CCToggleVisibility::action() {
	CCToggleVisibility *pRet = new CCToggleVisibility();

	if (pRet) {
		pRet->autorelease();
	}

	return pRet;
}

void CCToggleVisibility::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	pTarget->setIsVisible(!pTarget->getIsVisible());
}

//
// FlipX
//
CCFlipX *CCFlipX::actionWithFlipX(bool x) {
	CCFlipX *pRet = new CCFlipX();

	if (pRet && pRet->initWithFlipX(x)) {
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet)
	return NULL;
}

bool CCFlipX::initWithFlipX(bool x) {
	m_bFlipX = x;
	return true;
}

void CCFlipX::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	((CCSprite*) (pTarget))->setFlipX(m_bFlipX);
}

CCFiniteTimeAction* CCFlipX::reverse() {
	return CCFlipX::actionWithFlipX(!m_bFlipX);
}

CCObject * CCFlipX::copyWithZone(CCZone *pZone) {
	CCZone *pNewZone = NULL;
	CCFlipX *pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		pRet = (CCFlipX*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCFlipX();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCActionInstant::copyWithZone(pZone);
	pRet->initWithFlipX(m_bFlipX);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

//
// FlipY
//
CCFlipY * CCFlipY::actionWithFlipY(bool y) {
	CCFlipY *pRet = new CCFlipY();

	if (pRet && pRet->initWithFlipY(y)) {
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCFlipY::initWithFlipY(bool y) {
	m_bFlipY = y;
	return true;
}

void CCFlipY::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	((CCSprite*) (pTarget))->setFlipY(m_bFlipY);
}

CCFiniteTimeAction* CCFlipY::reverse() {
	return CCFlipY::actionWithFlipY(!m_bFlipY);
}

CCObject* CCFlipY::copyWithZone(CCZone *pZone) {
	CCZone *pNewZone = NULL;
	CCFlipY *pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		pRet = (CCFlipY*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCFlipY();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCActionInstant::copyWithZone(pZone);
	pRet->initWithFlipY(m_bFlipY);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

//
// Place
//
CCPlace* CCPlace::actionWithPosition(const CCPoint& pos) {
	CCPlace *pRet = new CCPlace();

	if (pRet && pRet->initWithPosition(pos)) {
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCPlace::initWithPosition(const CCPoint& pos) {
	m_tPosition = pos;
	return true;
}

CCObject * CCPlace::copyWithZone(CCZone *pZone) {
	CCZone *pNewZone = NULL;
	CCPlace *pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		pRet = (CCPlace*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCPlace();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCActionInstant::copyWithZone(pZone);
	pRet->initWithPosition(m_tPosition);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

void CCPlace::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	m_pTarget->setPosition(m_tPosition);
}

//
// CallFunc
//

CCCallFunc * CCCallFunc::actionWithTarget(CCObject* pSelectorTarget,
		SEL_CallFunc selector) {
	CCCallFunc *pRet = new CCCallFunc();

	if (pRet && pRet->initWithTarget(pSelectorTarget)) {
		pRet->m_pCallFunc = selector;
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCCallFunc::initWithTarget(CCObject* pSelectorTarget) {
	if (pSelectorTarget)
	{
		pSelectorTarget->retain();
	}

	if (m_pSelectorTarget)
	{
		m_pSelectorTarget->release();
	}

	m_pSelectorTarget = pSelectorTarget;
	return true;
}

CCObject * CCCallFunc::copyWithZone(CCZone *pZone) {
	CCZone* pNewZone = NULL;
	CCCallFunc* pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		//in case of being called at sub class
		pRet = (CCCallFunc*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCCallFunc();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCActionInstant::copyWithZone(pZone);
	pRet->initWithTarget(m_pSelectorTarget);
	pRet->m_pCallFunc = m_pCallFunc;
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

void CCCallFunc::startWithTarget(CCNode *pTarget) {
	CCActionInstant::startWithTarget(pTarget);
	this->execute();
}

void CCCallFunc::execute() {
	if (m_pCallFunc) {
		(m_pSelectorTarget->*m_pCallFunc)();
	}
}

//
// CallFuncN
//
void CCCallFuncN::execute() {
	if (m_pCallFuncN) {
		(m_pSelectorTarget->*m_pCallFuncN)(m_pTarget);
	}
}

CCCallFuncN * CCCallFuncN::actionWithTarget(CCObject* pSelectorTarget,
		SEL_CallFuncN selector) {
	CCCallFuncN *pRet = new CCCallFuncN();

	if (pRet && pRet->initWithTarget(pSelectorTarget, selector)) {
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCCallFuncN::initWithTarget(CCObject* pSelectorTarget,
		SEL_CallFuncN selector) {
	if (CCCallFunc::initWithTarget(pSelectorTarget)) {
		m_pCallFuncN = selector;
		return true;
	}

	return false;
}

CCObject * CCCallFuncN::copyWithZone(CCZone* zone) {
	CCZone* pNewZone = NULL;
	CCCallFuncN* pRet = NULL;

	if (zone && zone->m_pCopyObject) {
		//in case of being called at sub class
		pRet = (CCCallFuncN*) (zone->m_pCopyObject);
	} else {
		pRet = new CCCallFuncN();
		zone = pNewZone = new CCZone(pRet);
	}

	CCCallFunc::copyWithZone(zone);
	pRet->initWithTarget(m_pSelectorTarget, m_pCallFuncN);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

//
// CallFuncND
//
CCCallFuncND * CCCallFuncND::actionWithTarget(CCObject* pSelectorTarget,
		SEL_CallFuncND selector, void* d) {
	CCCallFuncND* pRet = new CCCallFuncND();

	if (pRet && pRet->initWithTarget(pSelectorTarget, selector, d)) {
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCCallFuncND::initWithTarget(CCObject* pSelectorTarget,
		SEL_CallFuncND selector, void* d) {
	if (CCCallFunc::initWithTarget(pSelectorTarget)) {
		m_pData = d;
		m_pCallFuncND = selector;
		return true;
	}

	return false;
}

CCObject * CCCallFuncND::copyWithZone(CCZone* zone) {
	CCZone* pNewZone = NULL;
	CCCallFuncND* pRet = NULL;

	if (zone && zone->m_pCopyObject) {
		//in case of being called at sub class
		pRet = (CCCallFuncND*) (zone->m_pCopyObject);
	} else {
		pRet = new CCCallFuncND();
		zone = pNewZone = new CCZone(pRet);
	}

	CCCallFunc::copyWithZone(zone);
	pRet->initWithTarget(m_pSelectorTarget, m_pCallFuncND, m_pData);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

void CCCallFuncND::execute() {
	if (m_pCallFuncND) {
		(m_pSelectorTarget->*m_pCallFuncND)(m_pTarget, m_pData);
	}
}

//
// CCCallFuncO
//
CCCallFuncO::CCCallFuncO() :
		m_pObject(NULL) {
}

CCCallFuncO::~CCCallFuncO() {
	CC_SAFE_RELEASE(m_pObject);
}

void CCCallFuncO::execute() {
	if (m_pCallFuncO) {
		(m_pSelectorTarget->*m_pCallFuncO)(m_pObject);
	}
}

CCCallFuncO * CCCallFuncO::actionWithTarget(CCObject* pSelectorTarget,
		SEL_CallFuncO selector, CCObject* pObject) {
	CCCallFuncO *pRet = new CCCallFuncO();

	if (pRet && pRet->initWithTarget(pSelectorTarget, selector, pObject)) {
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCCallFuncO::initWithTarget(CCObject* pSelectorTarget,
		SEL_CallFuncO selector, CCObject* pObject) {
	if (CCCallFunc::initWithTarget(pSelectorTarget)) {
		m_pObject = pObject;
		CC_SAFE_RETAIN(m_pObject)

		m_pCallFuncO = selector;
		return true;
	}

	return false;
}

CCObject * CCCallFuncO::copyWithZone(CCZone* zone) {
	CCZone* pNewZone = NULL;
	CCCallFuncO* pRet = NULL;

	if (zone && zone->m_pCopyObject) {
		//in case of being called at sub class
		pRet = (CCCallFuncO*) (zone->m_pCopyObject);
	} else {
		pRet = new CCCallFuncO();
		zone = pNewZone = new CCZone(pRet);
	}

	CCCallFunc::copyWithZone(zone);
	pRet->initWithTarget(m_pSelectorTarget, m_pCallFuncO, m_pObject);
	CC_SAFE_DELETE(pNewZone);
	return pRet;
}
}