123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include "RUEfxShake.h"
- #include "cocos2d.h"
- USING_NS_CC;
- NS_RU_BEGIN
-
- // not really useful, but I like clean default constructors
- RUEfxShake::RUEfxShake() : m_strength_x(0), m_strength_y(0)
- {
- }
- RUEfxShake::~RUEfxShake()
- {
- }
-
- RUEfxShake* RUEfxShake::create(float d, float strength)
- {
- // call other construction method with twice the same strength
- return createWithStrength(d, strength, strength);
- }
-
- RUEfxShake *RUEfxShake::createWithStrength(float duration, float strength_x, float strength_y, int interval)
- {
- RUEfxShake *pRet = new RUEfxShake();
-
- if (pRet && pRet->initWithDuration(duration, strength_x, strength_y,interval))
- {
- pRet->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(pRet);
- }
-
-
- return pRet;
- }
-
- bool RUEfxShake::initWithDuration(float duration, float strength_x, float strength_y, int interval)
- {
- if (ActionInterval::initWithDuration(duration))
- {
- m_strength_x = strength_x;
- m_strength_y = strength_y;
- m_curFrameCount = 0;
- m_interval = interval;
- m_accelEnable = true;
- return true;
- }
-
- return false;
- }
-
- // Helper function. I included it here so that you can compile the whole file it returns a random value between min and max included
- static float fgRangeRand( float min, float max )
- {
- float rnd = ((float)rand() / (float)RAND_MAX);
- return rnd * (max - min) + min;
- }
-
- void RUEfxShake::update(float dt)
- {
- if (m_curFrameCount % m_interval != 0)
- {
- m_curFrameCount++;
- return;
- }
- float scale = m_accelEnable ? dt : 1.0;
- float randx = fgRangeRand(-m_strength_x, m_strength_x ) * scale;
- float randy = fgRangeRand(-m_strength_y, m_strength_y ) * scale;
- // move the target to a shaked position
- if (_target)
- _target->setPosition(m_StartPosition + Vec2( randx, randy));
-
- m_curFrameCount++;
-
-
- // CCLOG("efx shake : %f %f %d",randx,randy,m_curFrameCount);
- }
-
- void RUEfxShake::startWithTarget(Node *pTarget)
- {
- ActionInterval::startWithTarget( pTarget );
-
- // save the initial position
- m_StartPosition=pTarget->getPosition();
- }
-
- void RUEfxShake::stop()
- {
- // Action is done, reset clip position
- if (this->getTarget())
- this->getTarget()->setPosition(m_StartPosition);
-
- ActionInterval::stop();
- }
- NS_RU_END
|