2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2009 On-Core
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCActionGrid3D.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#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
|
|
|
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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(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));
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(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-11-16 21:08:00 +08:00
|
|
|
FlipX3D *action = new FlipX3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FlipX3D::initWithDuration(float duration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
return Grid3DAction::initWithDuration(duration, Size(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)
|
2013-07-20 13:01:27 +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
|
|
|
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
|
|
|
|
2014-04-02 15:06:16 +08:00
|
|
|
Vector3 v0, v1, v, diff;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
v0 = getOriginalVertex(Vector2(1, 1));
|
|
|
|
v1 = getOriginalVertex(Vector2(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;
|
2013-11-16 21:08:00 +08:00
|
|
|
float x;
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 a, b, c, d;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if ( x0 > x1 )
|
|
|
|
{
|
|
|
|
// Normal Grid
|
2014-04-15 18:13:57 +08:00
|
|
|
a = Vector2(0,0);
|
|
|
|
b = Vector2(0,1);
|
|
|
|
c = Vector2(1,0);
|
|
|
|
d = Vector2(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
x = x0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reversed Grid
|
2014-04-15 18:13:57 +08:00
|
|
|
c = Vector2(0,0);
|
|
|
|
d = Vector2(0,1);
|
|
|
|
a = Vector2(1,0);
|
|
|
|
b = Vector2(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
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(a);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.x = diff.x;
|
|
|
|
v.z += diff.z;
|
|
|
|
setVertex(a, v);
|
|
|
|
|
|
|
|
// upper-left
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(b);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.x = diff.x;
|
|
|
|
v.z += diff.z;
|
|
|
|
setVertex(b, v);
|
|
|
|
|
|
|
|
// bottom-right
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(c);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.x -= diff.x;
|
|
|
|
v.z -= diff.z;
|
|
|
|
setVertex(c, v);
|
|
|
|
|
|
|
|
// upper-right
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(d);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.x -= diff.x;
|
|
|
|
v.z -= diff.z;
|
|
|
|
setVertex(d, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of FlipY3D
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
FlipY3D* FlipY3D::clone() const
|
|
|
|
{
|
|
|
|
// no copy constructor
|
|
|
|
auto a = new FlipY3D();
|
|
|
|
a->initWithSize(_gridSize, _duration);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FlipY3D* FlipY3D::create(float duration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
FlipY3D *action = new FlipY3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-04-02 15:06:16 +08:00
|
|
|
Vector3 v0, v1, v, diff;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
v0 = getOriginalVertex(Vector2(1, 1));
|
|
|
|
v1 = getOriginalVertex(Vector2(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;
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 a, b, c, d;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (y0 > y1)
|
|
|
|
{
|
|
|
|
// Normal Grid
|
2014-04-15 18:13:57 +08:00
|
|
|
a = Vector2(0,0);
|
|
|
|
b = Vector2(0,1);
|
|
|
|
c = Vector2(1,0);
|
|
|
|
d = Vector2(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
y = y0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reversed Grid
|
2014-04-15 18:13:57 +08:00
|
|
|
b = Vector2(0,0);
|
|
|
|
a = Vector2(0,1);
|
|
|
|
d = Vector2(1,0);
|
|
|
|
c = Vector2(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
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(a);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.y = diff.y;
|
|
|
|
v.z += diff.z;
|
|
|
|
setVertex(a, v);
|
|
|
|
|
|
|
|
// upper-left
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(b);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.y -= diff.y;
|
|
|
|
v.z -= diff.z;
|
|
|
|
setVertex(b, v);
|
|
|
|
|
|
|
|
// bottom-right
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(c);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.y = diff.y;
|
|
|
|
v.z += diff.z;
|
|
|
|
setVertex(c, v);
|
|
|
|
|
|
|
|
// upper-right
|
2013-07-08 23:05:47 +08:00
|
|
|
v = getOriginalVertex(d);
|
2012-04-19 14:35:52 +08:00
|
|
|
v.y -= diff.y;
|
|
|
|
v.z -= diff.z;
|
|
|
|
setVertex(d, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// implementation of Lens3D
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Lens3D* Lens3D::create(float duration, const Size& gridSize, const Vector2& position, float radius)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
Lens3D *action = new Lens3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration, gridSize, position, radius))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
bool Lens3D::initWithDuration(float duration, const Size& gridSize, const Vector2& 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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
_position = Vector2(-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;
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:16:14 +08:00
|
|
|
void Lens3D::setPosition(const Vector2& 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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(i, j));
|
|
|
|
Vector2 vect = _position - Vector2(v.x, v.y);
|
2013-07-11 16:38:58 +08:00
|
|
|
float r = vect.getLength();
|
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
|
|
|
|
2013-07-11 16:38:58 +08:00
|
|
|
if (vect.getLength() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
vect = vect.normalize();
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 new_vect = vect * new_r;
|
2013-07-11 16:38:58 +08:00
|
|
|
v.z += (_concave ? -1.0f : 1.0f) * new_vect.getLength() * _lensEffect;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(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
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Ripple3D* Ripple3D::create(float duration, const Size& gridSize, const Vector2& position, float radius, unsigned int waves, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
Ripple3D *action = new Ripple3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration, gridSize, position, radius, waves, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
bool Ripple3D::initWithDuration(float duration, const Size& gridSize, const Vector2& 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;
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:16:14 +08:00
|
|
|
void Ripple3D::setPosition(const Vector2& 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
|
|
|
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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(i, j));
|
|
|
|
Vector2 vect = _position - Vector2(v.x,v.y);
|
2013-07-11 16:38:58 +08:00
|
|
|
float r = vect.getLength();
|
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
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(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-11-16 21:08:00 +08:00
|
|
|
Shaky3D *action = new Shaky3D();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration, gridSize, range, shakeZ))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(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
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(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-11-16 21:08:00 +08:00
|
|
|
Liquid *action = new Liquid();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration, gridSize, waves, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(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));
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(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-11-16 21:08:00 +08:00
|
|
|
Waves *action = new Waves();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(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
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(i, j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Twirl
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Twirl* Twirl::create(float duration, const Size& gridSize, Vector2 position, unsigned int twirls, float amplitude)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
Twirl *action = new Twirl();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
if (action->initWithDuration(duration, gridSize, position, twirls, amplitude))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
action->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
bool Twirl::initWithDuration(float duration, const Size& gridSize, Vector2 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;
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:16:14 +08:00
|
|
|
void Twirl::setPosition(const Vector2& 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
|
|
|
void Twirl::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 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
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector3 v = getOriginalVertex(Vector2(i ,j));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 avg = Vector2(i-(_gridSize.width/2.0f), j-(_gridSize.height/2.0f));
|
2013-07-11 16:38:58 +08:00
|
|
|
float r = avg.getLength();
|
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
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 d = Vector2(
|
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;
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
setVertex(Vector2(i ,j), v);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|