2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-06-14 15:13:16 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
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"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCZone.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Waves3D
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Waves3D* Waves3D::create(float duration, const Size& gridSize, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Waves3D *pAction = new Waves3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, waves, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Waves3D::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_waves = waves;
|
|
|
|
_amplitude = amplitude;
|
|
|
|
_amplitudeRate = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Waves3D* Waves3D::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Waves3D();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _waves, _amplitude);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Waves3D::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Waves3D* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Waves3D*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Waves3D();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _waves, _amplitude);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Waves3D::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 0; i < _gridSize.width + 1; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 0; j < _gridSize.height + 1; ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i ,j));
|
2013-06-15 14:03:30 +08:00
|
|
|
v.z += (sinf((float)M_PI * time * _waves * 2 + (v.y+v.x) * 0.01f) * _amplitude * _amplitudeRate);
|
|
|
|
//CCLOG("v.z offset is %f\n", (sinf((float)M_PI * time * _waves * 2 + (v.y+v.x) * .01f) * _amplitude * _amplitudeRate));
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of FlipX3D
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipX3D* FlipX3D::create(float duration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipX3D *pAction = new FlipX3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FlipX3D::initWithDuration(float duration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Grid3DAction::initWithDuration(duration, CCSizeMake(1, 1));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FlipX3D::initWithSize(const Size& gridSize, float duration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (gridSize.width != 1 || gridSize.height != 1)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Grid size must be (1,1)
|
2012-06-11 11:53:17 +08:00
|
|
|
CCAssert(0, "Grid size must be (1,1)");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
return Grid3DAction::initWithDuration(duration, gridSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipX3D* FlipX3D::clone() const
|
2013-06-16 10:44:39 +08:00
|
|
|
{
|
2013-06-19 06:06:53 +08:00
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new FlipX3D();
|
2013-06-19 00:59:34 +08:00
|
|
|
a->initWithSize(_gridSize, _duration);
|
2013-06-16 10:44:39 +08:00
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* FlipX3D::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
FlipX3D* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (FlipX3D*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new FlipX3D();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithSize(_gridSize, _duration);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FlipX3D::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
float angle = (float)M_PI * time; // 180 degrees
|
|
|
|
float mz = sinf(angle);
|
2012-04-19 14:35:52 +08:00
|
|
|
angle = angle / 2.0f; // x calculates degrees from 0 to 90
|
2012-08-01 15:30:12 +08:00
|
|
|
float mx = cosf(angle);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
ccVertex3F v0, v1, v, diff;
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
v0 = originalVertex(ccp(1, 1));
|
|
|
|
v1 = originalVertex(ccp(0, 0));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float x0 = v0.x;
|
|
|
|
float x1 = v1.x;
|
|
|
|
float x;
|
2013-06-20 14:13:12 +08:00
|
|
|
Point a, b, c, d;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if ( x0 > x1 )
|
|
|
|
{
|
|
|
|
// Normal Grid
|
2012-12-26 18:59:31 +08:00
|
|
|
a = ccp(0,0);
|
|
|
|
b = ccp(0,1);
|
|
|
|
c = ccp(1,0);
|
|
|
|
d = ccp(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
x = x0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reversed Grid
|
2012-12-26 18:59:31 +08:00
|
|
|
c = ccp(0,0);
|
|
|
|
d = ccp(0,1);
|
|
|
|
a = ccp(1,0);
|
|
|
|
b = ccp(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipY3D* FlipY3D::create(float duration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipY3D *pAction = new FlipY3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* FlipY3D::copyWithZone(Zone* pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
FlipY3D* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (FlipY3D*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new FlipY3D();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipX3D::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithSize(_gridSize, _duration);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FlipY3D::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
float angle = (float)M_PI * time; // 180 degrees
|
|
|
|
float mz = sinf( angle );
|
2012-04-19 14:35:52 +08:00
|
|
|
angle = angle / 2.0f; // x calculates degrees from 0 to 90
|
2012-08-01 15:30:12 +08:00
|
|
|
float my = cosf(angle);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
ccVertex3F v0, v1, v, diff;
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
v0 = originalVertex(ccp(1, 1));
|
|
|
|
v1 = originalVertex(ccp(0, 0));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float y0 = v0.y;
|
|
|
|
float y1 = v1.y;
|
|
|
|
float y;
|
2013-06-20 14:13:12 +08:00
|
|
|
Point a, b, c, d;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (y0 > y1)
|
|
|
|
{
|
|
|
|
// Normal Grid
|
2012-12-26 18:59:31 +08:00
|
|
|
a = ccp(0,0);
|
|
|
|
b = ccp(0,1);
|
|
|
|
c = ccp(1,0);
|
|
|
|
d = ccp(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
y = y0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reversed Grid
|
2012-12-26 18:59:31 +08:00
|
|
|
b = ccp(0,0);
|
|
|
|
a = ccp(0,1);
|
|
|
|
d = ccp(1,0);
|
|
|
|
c = ccp(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Lens3D* Lens3D::create(float duration, const Size& gridSize, const Point& position, float radius)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Lens3D *pAction = new Lens3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, position, radius))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Lens3D::initWithDuration(float duration, const Size& gridSize, const Point& position, float radius)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_position = ccp(-1, -1);
|
2012-12-26 18:59:31 +08:00
|
|
|
setPosition(position);
|
2013-06-15 14:03:30 +08:00
|
|
|
_radius = radius;
|
|
|
|
_lensEffect = 0.7f;
|
|
|
|
_concave = false;
|
|
|
|
_dirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Lens3D* Lens3D::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Lens3D();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _position, _radius);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Lens3D::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Lens3D* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Lens3D*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Lens3D();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _position, _radius);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Lens3D::setPosition(const Point& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( !pos.equals(_position))
|
2012-08-01 15:30:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_position = pos;
|
|
|
|
_dirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Lens3D::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(time);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dirty)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 0; i < _gridSize.width + 1; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 0; j < _gridSize.height + 1; ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i, j));
|
2013-06-20 14:13:12 +08:00
|
|
|
Point vect = ccpSub(_position, ccp(v.x, v.y));
|
2012-08-01 15:30:12 +08:00
|
|
|
float r = ccpLength(vect);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (r < _radius)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
r = _radius - r;
|
|
|
|
float pre_log = r / _radius;
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pre_log == 0 )
|
|
|
|
{
|
|
|
|
pre_log = 0.001f;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float l = logf(pre_log) * _lensEffect;
|
|
|
|
float new_r = expf( l ) * _radius;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (ccpLength(vect) > 0)
|
|
|
|
{
|
|
|
|
vect = ccpNormalize(vect);
|
2013-06-20 14:13:12 +08:00
|
|
|
Point new_vect = ccpMult(vect, new_r);
|
2013-06-15 14:03:30 +08:00
|
|
|
v.z += (_concave ? -1.0f : 1.0f) * ccpLength(new_vect) * _lensEffect;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Ripple3D
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Ripple3D* Ripple3D::create(float duration, const Size& gridSize, const Point& position, float radius, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Ripple3D *pAction = new Ripple3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, position, radius, waves, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Ripple3D::initWithDuration(float duration, const Size& gridSize, const Point& position, float radius, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
setPosition(position);
|
2013-06-15 14:03:30 +08:00
|
|
|
_radius = radius;
|
|
|
|
_waves = waves;
|
|
|
|
_amplitude = amplitude;
|
|
|
|
_amplitudeRate = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Ripple3D::setPosition(const Point& position)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_position = position;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-19 06:06:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Ripple3D* Ripple3D::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Ripple3D();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _position, _radius, _waves, _amplitude);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Ripple3D::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Ripple3D* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Ripple3D*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Ripple3D();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _position, _radius, _waves, _amplitude);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Ripple3D::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 0; i < (_gridSize.width+1); ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 0; j < (_gridSize.height+1); ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i, j));
|
2013-06-20 14:13:12 +08:00
|
|
|
Point vect = ccpSub(_position, ccp(v.x,v.y));
|
2012-08-01 15:30:12 +08:00
|
|
|
float r = ccpLength(vect);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (r < _radius)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
r = _radius - r;
|
|
|
|
float rate = powf(r / _radius, 2);
|
|
|
|
v.z += (sinf( time*(float)M_PI * _waves * 2 + r * 0.1f) * _amplitude * _amplitudeRate * rate);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Shaky3D
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Shaky3D* Shaky3D::create(float duration, const Size& gridSize, int range, bool shakeZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Shaky3D *pAction = new Shaky3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, range, shakeZ))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Shaky3D::initWithDuration(float duration, const Size& gridSize, int range, bool shakeZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_randrange = range;
|
|
|
|
_shakeZ = shakeZ;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Shaky3D* Shaky3D::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Shaky3D();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _randrange, _shakeZ);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Shaky3D::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Shaky3D* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Shaky3D*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Shaky3D();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _randrange, _shakeZ);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Shaky3D::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(time);
|
|
|
|
int i, j;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 0; i < (_gridSize.width+1); ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 0; j < (_gridSize.height+1); ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i ,j));
|
2013-06-15 14:03:30 +08:00
|
|
|
v.x += (rand() % (_randrange*2)) - _randrange;
|
|
|
|
v.y += (rand() % (_randrange*2)) - _randrange;
|
|
|
|
if (_shakeZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
v.z += (rand() % (_randrange*2)) - _randrange;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Liquid
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Liquid* Liquid::create(float duration, const Size& gridSize, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Liquid *pAction = new Liquid();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, waves, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Liquid::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_waves = waves;
|
|
|
|
_amplitude = amplitude;
|
|
|
|
_amplitudeRate = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Liquid* Liquid::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Liquid();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _waves, _amplitude);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Liquid::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Liquid* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Liquid*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Liquid();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _waves, _amplitude);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Liquid::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 1; i < _gridSize.width; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 1; j < _gridSize.height; ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i, j));
|
2013-06-15 14:03:30 +08:00
|
|
|
v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
|
|
|
|
v.y = (v.y + (sinf(time * (float)M_PI * _waves * 2 + v.y * .01f) * _amplitude * _amplitudeRate));
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Waves
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Waves* Waves::create(float duration, const Size& gridSize, unsigned int waves, float amplitude, bool horizontal, bool vertical)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Waves *pAction = new Waves();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Waves::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude, bool horizontal, bool vertical)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_waves = waves;
|
|
|
|
_amplitude = amplitude;
|
|
|
|
_amplitudeRate = 1.0f;
|
|
|
|
_horizontal = horizontal;
|
|
|
|
_vertical = vertical;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Waves* Waves::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Waves();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _waves, _amplitude, _horizontal, _vertical);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Waves::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Waves* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Waves*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Waves();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _waves, _amplitude, _horizontal, _vertical);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Waves::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 0; i < _gridSize.width + 1; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 0; j < _gridSize.height + 1; ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i, j));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_vertical)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.y * .01f) * _amplitude * _amplitudeRate));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_horizontal)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
v.y = (v.y + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Twirl
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Twirl* Twirl::create(float duration, const Size& gridSize, Point position, unsigned int twirls, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Twirl *pAction = new Twirl();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pAction)
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
if (pAction->initWithDuration(duration, gridSize, position, twirls, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pAction->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pAction;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Twirl::initWithDuration(float duration, const Size& gridSize, Point position, unsigned int twirls, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Grid3DAction::initWithDuration(duration, gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
setPosition(position);
|
2013-06-15 14:03:30 +08:00
|
|
|
_twirls = twirls;
|
|
|
|
_amplitude = amplitude;
|
|
|
|
_amplitudeRate = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Twirl::setPosition(const Point& position)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_position = position;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Twirl *Twirl::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Twirl();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _gridSize, _position, _twirls, _amplitude);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Twirl::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
Twirl* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (Twirl*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new Twirl();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3DAction::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _gridSize, _position, _twirls, _amplitude);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Twirl::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
2013-06-20 14:13:12 +08:00
|
|
|
Point c = _position;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i = 0; i < (_gridSize.width+1); ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (j = 0; j < (_gridSize.height+1); ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ccVertex3F v = originalVertex(ccp(i ,j));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point avg = ccp(i-(_gridSize.width/2.0f), j-(_gridSize.height/2.0f));
|
2012-08-01 15:30:12 +08:00
|
|
|
float r = ccpLength(avg);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float amp = 0.1f * _amplitude * _amplitudeRate;
|
|
|
|
float a = r * cosf( (float)M_PI/2.0f + time * (float)M_PI * _twirls * 2 ) * amp;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point d = ccp(
|
2012-06-11 11:53:17 +08:00
|
|
|
sinf(a) * (v.y-c.y) + cosf(a) * (v.x-c.x),
|
|
|
|
cosf(a) * (v.y-c.y) - sinf(a) * (v.x-c.x));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
v.x = c.x + d.x;
|
|
|
|
v.y = c.y + d.y;
|
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
setVertex(ccp(i ,j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|