axmol/cocos/2d/CCActionTiledGrid.cpp

996 lines
23 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2009 On-Core
Copyright (c) 2010-2012 cocos2d-x.org
CopyRight (c) 2013-2014 Chukong Technologies 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 "2d/CCActionTiledGrid.h"
#include "2d/CCDirector.h"
#include "2d/ccMacros.h"
#include "CCGrid.h"
#include "CCNodeGrid.h"
#include <stdlib.h>
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
struct Tile
{
Vector2 position;
Vector2 startPosition;
Size delta;
2012-04-18 18:43:45 +08:00
};
2012-04-18 18:43:45 +08:00
// implementation of ShakyTiles3D
ShakyTiles3D* ShakyTiles3D::create(float duration, const Size& gridSize, int range, bool shakeZ)
2012-04-18 18:43:45 +08:00
{
ShakyTiles3D *action = new ShakyTiles3D();
if (action)
{
if (action->initWithDuration(duration, gridSize, range, shakeZ))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool ShakyTiles3D::initWithDuration(float duration, const Size& gridSize, int range, bool shakeZ)
2012-04-18 18:43:45 +08:00
{
if (TiledGrid3DAction::initWithDuration(duration, gridSize))
{
_randrange = range;
_shakeZ = shakeZ;
2012-04-18 18:43:45 +08:00
return true;
}
2012-04-18 18:43:45 +08:00
return false;
2012-04-18 18:43:45 +08:00
}
ShakyTiles3D* ShakyTiles3D::clone() const
{
// no copy constructor
auto a = new ShakyTiles3D();
a->initWithDuration(_duration, _gridSize, _randrange, _shakeZ);
a->autorelease();
return a;
}
void ShakyTiles3D::update(float time)
2012-04-18 18:43:45 +08:00
{
CC_UNUSED_PARAM(time);
int i, j;
for (i = 0; i < _gridSize.width; ++i)
{
for (j = 0; j < _gridSize.height; ++j)
{
Quad3 coords = getOriginalTile(Vector2(i, j));
// X
coords.bl.x += ( rand() % (_randrange*2) ) - _randrange;
coords.br.x += ( rand() % (_randrange*2) ) - _randrange;
coords.tl.x += ( rand() % (_randrange*2) ) - _randrange;
coords.tr.x += ( rand() % (_randrange*2) ) - _randrange;
// Y
coords.bl.y += ( rand() % (_randrange*2) ) - _randrange;
coords.br.y += ( rand() % (_randrange*2) ) - _randrange;
coords.tl.y += ( rand() % (_randrange*2) ) - _randrange;
coords.tr.y += ( rand() % (_randrange*2) ) - _randrange;
if (_shakeZ)
{
coords.bl.z += ( rand() % (_randrange*2) ) - _randrange;
coords.br.z += ( rand() % (_randrange*2) ) - _randrange;
coords.tl.z += ( rand() % (_randrange*2) ) - _randrange;
coords.tr.z += ( rand() % (_randrange*2) ) - _randrange;
}
setTile(Vector2(i, j), coords);
}
}
2012-04-18 18:43:45 +08:00
}
// implementation of ShatteredTiles3D
2012-04-18 18:43:45 +08:00
ShatteredTiles3D* ShatteredTiles3D::create(float duration, const Size& gridSize, int range, bool shatterZ)
2012-04-18 18:43:45 +08:00
{
ShatteredTiles3D *action = new ShatteredTiles3D();
if (action)
{
if (action->initWithDuration(duration, gridSize, range, shatterZ))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool ShatteredTiles3D::initWithDuration(float duration, const Size& gridSize, int range, bool shatterZ)
2012-04-18 18:43:45 +08:00
{
if (TiledGrid3DAction::initWithDuration(duration, gridSize))
{
_once = false;
_randrange = range;
_shatterZ = shatterZ;
2012-04-18 18:43:45 +08:00
return true;
}
return false;
2012-04-18 18:43:45 +08:00
}
ShatteredTiles3D* ShatteredTiles3D::clone() const
{
// no copy constructor
auto a = new ShatteredTiles3D();
a->initWithDuration(_duration, _gridSize, _randrange, _shatterZ);
a->autorelease();
return a;
}
void ShatteredTiles3D::update(float time)
2012-04-18 18:43:45 +08:00
{
CC_UNUSED_PARAM(time);
int i, j;
if (_once == false)
{
for (i = 0; i < _gridSize.width; ++i)
{
for (j = 0; j < _gridSize.height; ++j)
{
Quad3 coords = getOriginalTile(Vector2(i ,j));
// X
coords.bl.x += ( rand() % (_randrange*2) ) - _randrange;
coords.br.x += ( rand() % (_randrange*2) ) - _randrange;
coords.tl.x += ( rand() % (_randrange*2) ) - _randrange;
coords.tr.x += ( rand() % (_randrange*2) ) - _randrange;
// Y
coords.bl.y += ( rand() % (_randrange*2) ) - _randrange;
coords.br.y += ( rand() % (_randrange*2) ) - _randrange;
coords.tl.y += ( rand() % (_randrange*2) ) - _randrange;
coords.tr.y += ( rand() % (_randrange*2) ) - _randrange;
if (_shatterZ)
{
coords.bl.z += ( rand() % (_randrange*2) ) - _randrange;
coords.br.z += ( rand() % (_randrange*2) ) - _randrange;
coords.tl.z += ( rand() % (_randrange*2) ) - _randrange;
coords.tr.z += ( rand() % (_randrange*2) ) - _randrange;
}
setTile(Vector2(i, j), coords);
}
}
_once = true;
}
2012-04-18 18:43:45 +08:00
}
// implementation of ShuffleTiles
ShuffleTiles* ShuffleTiles::create(float duration, const Size& gridSize, unsigned int seed)
2012-04-18 18:43:45 +08:00
{
ShuffleTiles *action = new ShuffleTiles();
if (action)
{
if (action->initWithDuration(duration, gridSize, seed))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool ShuffleTiles::initWithDuration(float duration, const Size& gridSize, unsigned int seed)
2012-04-18 18:43:45 +08:00
{
if (TiledGrid3DAction::initWithDuration(duration, gridSize))
{
_seed = seed;
_tilesOrder = nullptr;
_tiles = nullptr;
2012-04-18 18:43:45 +08:00
return true;
}
return false;
2012-04-18 18:43:45 +08:00
}
ShuffleTiles* ShuffleTiles::clone() const
{
// no copy constructor
auto a = new ShuffleTiles();
a->initWithDuration(_duration, _gridSize, _seed);
a->autorelease();
return a;
}
ShuffleTiles::~ShuffleTiles(void)
2012-04-18 18:43:45 +08:00
{
CC_SAFE_DELETE_ARRAY(_tilesOrder);
CC_SAFE_DELETE_ARRAY(_tiles);
2012-04-18 18:43:45 +08:00
}
void ShuffleTiles::shuffle(unsigned int *array, unsigned int len)
2012-04-18 18:43:45 +08:00
{
int i;
for( i = len - 1; i >= 0; i-- )
{
unsigned int j = rand() % (i+1);
unsigned int v = array[i];
array[i] = array[j];
array[j] = v;
}
2012-04-18 18:43:45 +08:00
}
Size ShuffleTiles::getDelta(const Size& pos) const
2012-04-18 18:43:45 +08:00
{
Vector2 pos2;
unsigned int idx = pos.width * _gridSize.height + pos.height;
pos2.x = (float)(_tilesOrder[idx] / (int)_gridSize.height);
pos2.y = (float)(_tilesOrder[idx] % (int)_gridSize.height);
return Size((int)(pos2.x - pos.width), (int)(pos2.y - pos.height));
2012-04-18 18:43:45 +08:00
}
void ShuffleTiles::placeTile(const Vector2& pos, Tile *t)
2012-04-18 18:43:45 +08:00
{
2013-07-08 23:12:11 +08:00
Quad3 coords = getOriginalTile(pos);
Vector2 step = _gridNodeTarget->getGrid()->getStep();
coords.bl.x += (int)(t->position.x * step.x);
coords.bl.y += (int)(t->position.y * step.y);
coords.br.x += (int)(t->position.x * step.x);
coords.br.y += (int)(t->position.y * step.y);
coords.tl.x += (int)(t->position.x * step.x);
coords.tl.y += (int)(t->position.y * step.y);
coords.tr.x += (int)(t->position.x * step.x);
coords.tr.y += (int)(t->position.y * step.y);
setTile(pos, coords);
2012-04-18 18:43:45 +08:00
}
void ShuffleTiles::startWithTarget(Node *target)
2012-04-18 18:43:45 +08:00
{
TiledGrid3DAction::startWithTarget(target);
if (_seed != (unsigned int)-1)
{
srand(_seed);
}
_tilesCount = _gridSize.width * _gridSize.height;
_tilesOrder = new unsigned int[_tilesCount];
int i, j;
unsigned int k;
/**
* Use k to loop. Because _tilesCount is unsigned int,
* and i is used later for int.
*/
for (k = 0; k < _tilesCount; ++k)
{
_tilesOrder[k] = k;
}
shuffle(_tilesOrder, _tilesCount);
_tiles = (struct Tile *)new Tile[_tilesCount];
Tile *tileArray = (Tile*) _tiles;
for (i = 0; i < _gridSize.width; ++i)
{
for (j = 0; j < _gridSize.height; ++j)
{
tileArray->position = Vector2((float)i, (float)j);
tileArray->startPosition = Vector2((float)i, (float)j);
tileArray->delta = getDelta(Size(i, j));
++tileArray;
}
}
2012-04-18 18:43:45 +08:00
}
void ShuffleTiles::update(float time)
2012-04-18 18:43:45 +08:00
{
int i, j;
Tile *tileArray = (Tile*)_tiles;
for (i = 0; i < _gridSize.width; ++i)
{
for (j = 0; j < _gridSize.height; ++j)
{
tileArray->position = Vector2((float)tileArray->delta.width, (float)tileArray->delta.height) * time;
placeTile(Vector2(i, j), tileArray);
++tileArray;
}
}
2012-04-18 18:43:45 +08:00
}
// implementation of FadeOutTRTiles
FadeOutTRTiles* FadeOutTRTiles::create(float duration, const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
FadeOutTRTiles *action = new FadeOutTRTiles();
if (action)
{
if (action->initWithDuration(duration, gridSize))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
FadeOutTRTiles* FadeOutTRTiles::clone() const
{
// no copy constructor
auto a = new FadeOutTRTiles();
a->initWithDuration(_duration, _gridSize);
a->autorelease();
return a;
}
float FadeOutTRTiles::testFunc(const Size& pos, float time)
2012-04-18 18:43:45 +08:00
{
Vector2 n = Vector2((float)_gridSize.width, (float)_gridSize.height) * time;
if ((n.x + n.y) == 0.0f)
{
return 1.0f;
}
return powf((pos.width + pos.height) / (n.x + n.y), 6);
2012-04-18 18:43:45 +08:00
}
void FadeOutTRTiles::turnOnTile(const Vector2& pos)
2012-04-18 18:43:45 +08:00
{
2013-07-08 23:12:11 +08:00
setTile(pos, getOriginalTile(pos));
2012-04-18 18:43:45 +08:00
}
void FadeOutTRTiles::turnOffTile(const Vector2& pos)
2012-04-18 18:43:45 +08:00
{
Quad3 coords;
memset(&coords, 0, sizeof(Quad3));
setTile(pos, coords);
2012-04-18 18:43:45 +08:00
}
void FadeOutTRTiles::transformTile(const Vector2& pos, float distance)
2012-04-18 18:43:45 +08:00
{
2013-07-08 23:12:11 +08:00
Quad3 coords = getOriginalTile(pos);
Vector2 step = _gridNodeTarget->getGrid()->getStep();
coords.bl.x += (step.x / 2) * (1.0f - distance);
coords.bl.y += (step.y / 2) * (1.0f - distance);
coords.br.x -= (step.x / 2) * (1.0f - distance);
coords.br.y += (step.y / 2) * (1.0f - distance);
coords.tl.x += (step.x / 2) * (1.0f - distance);
coords.tl.y -= (step.y / 2) * (1.0f - distance);
coords.tr.x -= (step.x / 2) * (1.0f - distance);
coords.tr.y -= (step.y / 2) * (1.0f - distance);
setTile(pos, coords);
2012-04-18 18:43:45 +08:00
}
void FadeOutTRTiles::update(float time)
2012-04-18 18:43:45 +08:00
{
int i, j;
for (i = 0; i < _gridSize.width; ++i)
{
for (j = 0; j < _gridSize.height; ++j)
{
float distance = testFunc(Size(i, j), time);
if ( distance == 0 )
{
turnOffTile(Vector2(i, j));
} else
if (distance < 1)
{
transformTile(Vector2(i, j), distance);
}
else
{
turnOnTile(Vector2(i, j));
}
}
}
2012-04-18 18:43:45 +08:00
}
// implementation of FadeOutBLTiles
FadeOutBLTiles* FadeOutBLTiles::create(float duration, const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
FadeOutBLTiles *action = new FadeOutBLTiles();
if (action)
{
if (action->initWithDuration(duration, gridSize))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
FadeOutBLTiles* FadeOutBLTiles::clone() const
{
// no copy constructor
auto a = new FadeOutBLTiles();
a->initWithDuration(_duration, _gridSize);
a->autorelease();
return a;
}
float FadeOutBLTiles::testFunc(const Size& pos, float time)
2012-04-18 18:43:45 +08:00
{
Vector2 n = Vector2((float)_gridSize.width, (float)_gridSize.height) * (1.0f - time);
if ((pos.width + pos.height) == 0)
{
return 1.0f;
}
return powf((n.x + n.y) / (pos.width + pos.height), 6);
2012-04-18 18:43:45 +08:00
}
// implementation of FadeOutUpTiles
2012-04-18 18:43:45 +08:00
FadeOutUpTiles* FadeOutUpTiles::create(float duration, const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
FadeOutUpTiles *action = new FadeOutUpTiles();
if (action)
{
if (action->initWithDuration(duration, gridSize))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
FadeOutUpTiles* FadeOutUpTiles::clone() const
{
// no copy constructor
auto a = new FadeOutUpTiles();
a->initWithDuration(_duration, _gridSize);
a->autorelease();
return a;
}
float FadeOutUpTiles::testFunc(const Size& pos, float time)
2012-04-18 18:43:45 +08:00
{
Vector2 n = Vector2((float)_gridSize.width, (float)_gridSize.height) * time;
if (n.y == 0.0f)
{
return 1.0f;
}
return powf(pos.height / n.y, 6);
2012-04-18 18:43:45 +08:00
}
void FadeOutUpTiles::transformTile(const Vector2& pos, float distance)
2012-04-18 18:43:45 +08:00
{
2013-07-08 23:12:11 +08:00
Quad3 coords = getOriginalTile(pos);
Vector2 step = _gridNodeTarget->getGrid()->getStep();
coords.bl.y += (step.y / 2) * (1.0f - distance);
coords.br.y += (step.y / 2) * (1.0f - distance);
coords.tl.y -= (step.y / 2) * (1.0f - distance);
coords.tr.y -= (step.y / 2) * (1.0f - distance);
setTile(pos, coords);
2012-04-18 18:43:45 +08:00
}
// implementation of FadeOutDownTiles
FadeOutDownTiles* FadeOutDownTiles::create(float duration, const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
FadeOutDownTiles *action = new FadeOutDownTiles();
if (action)
{
if (action->initWithDuration(duration, gridSize))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
FadeOutDownTiles* FadeOutDownTiles::clone() const
{
// no copy constructor
auto a = new FadeOutDownTiles();
a->initWithDuration(_duration, _gridSize);
a->autorelease();
return a;
}
float FadeOutDownTiles::testFunc(const Size& pos, float time)
2012-04-18 18:43:45 +08:00
{
Vector2 n = Vector2((float)_gridSize.width, (float)_gridSize.height) * (1.0f - time);
if (pos.height == 0)
{
return 1.0f;
}
return powf(n.y / pos.height, 6);
2012-04-18 18:43:45 +08:00
}
2012-04-18 18:43:45 +08:00
// implementation of TurnOffTiles
TurnOffTiles* TurnOffTiles::create(float duration, const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
TurnOffTiles* pAction = new TurnOffTiles();
if (pAction->initWithDuration(duration, gridSize, 0))
{
pAction->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pAction);
}
return pAction;
2012-04-18 18:43:45 +08:00
}
TurnOffTiles* TurnOffTiles::create(float duration, const Size& gridSize, unsigned int seed)
2012-04-18 18:43:45 +08:00
{
TurnOffTiles *action = new TurnOffTiles();
if (action)
{
if (action->initWithDuration(duration, gridSize, seed))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool TurnOffTiles::initWithDuration(float duration, const Size& gridSize, unsigned int seed)
2012-04-18 18:43:45 +08:00
{
if (TiledGrid3DAction::initWithDuration(duration, gridSize))
{
_seed = seed;
_tilesOrder = nullptr;
return true;
}
return false;
2012-04-18 18:43:45 +08:00
}
TurnOffTiles* TurnOffTiles::clone() const
{
// no copy constructor
auto a = new TurnOffTiles();
a->initWithDuration(_duration, _gridSize, _seed );
a->autorelease();
return a;
}
TurnOffTiles::~TurnOffTiles(void)
2012-04-18 18:43:45 +08:00
{
CC_SAFE_DELETE_ARRAY(_tilesOrder);
2012-04-18 18:43:45 +08:00
}
void TurnOffTiles::shuffle(unsigned int *array, unsigned int len)
2012-04-18 18:43:45 +08:00
{
int i;
for (i = len - 1; i >= 0; i--)
{
unsigned int j = rand() % (i+1);
unsigned int v = array[i];
array[i] = array[j];
array[j] = v;
}
2012-04-18 18:43:45 +08:00
}
void TurnOffTiles::turnOnTile(const Vector2& pos)
2012-04-18 18:43:45 +08:00
{
2013-07-08 23:12:11 +08:00
setTile(pos, getOriginalTile(pos));
2012-04-18 18:43:45 +08:00
}
void TurnOffTiles::turnOffTile(const Vector2& pos)
2012-04-18 18:43:45 +08:00
{
Quad3 coords;
memset(&coords, 0, sizeof(Quad3));
setTile(pos, coords);
2012-04-18 18:43:45 +08:00
}
void TurnOffTiles::startWithTarget(Node *target)
2012-04-18 18:43:45 +08:00
{
unsigned int i;
TiledGrid3DAction::startWithTarget(target);
if (_seed != (unsigned int)-1)
{
srand(_seed);
}
_tilesCount = _gridSize.width * _gridSize.height;
_tilesOrder = new unsigned int[_tilesCount];
for (i = 0; i < _tilesCount; ++i)
{
_tilesOrder[i] = i;
}
shuffle(_tilesOrder, _tilesCount);
2012-04-18 18:43:45 +08:00
}
void TurnOffTiles::update(float time)
2012-04-18 18:43:45 +08:00
{
unsigned int i, l, t;
l = (unsigned int)(time * (float)_tilesCount);
for( i = 0; i < _tilesCount; i++ )
{
t = _tilesOrder[i];
Vector2 tilePos = Vector2( (unsigned int)(t / _gridSize.height), t % (unsigned int)_gridSize.height );
if ( i < l )
{
turnOffTile(tilePos);
}
else
{
turnOnTile(tilePos);
}
}
2012-04-18 18:43:45 +08:00
}
// implementation of WavesTiles3D
2012-04-18 18:43:45 +08:00
WavesTiles3D* WavesTiles3D::create(float duration, const Size& gridSize, unsigned int waves, float amplitude)
2012-04-18 18:43:45 +08:00
{
WavesTiles3D *action = new WavesTiles3D();
if (action)
{
if (action->initWithDuration(duration, gridSize, waves, amplitude))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool WavesTiles3D::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude)
2012-04-18 18:43:45 +08:00
{
if (TiledGrid3DAction::initWithDuration(duration, gridSize))
{
_waves = waves;
_amplitude = amplitude;
_amplitudeRate = 1.0f;
return true;
}
return false;
2012-04-18 18:43:45 +08:00
}
WavesTiles3D* WavesTiles3D::clone() const
{
// no copy constructor
auto a = new WavesTiles3D();
a->initWithDuration(_duration, _gridSize, _waves, _amplitude);
a->autorelease();
return a;
}
void WavesTiles3D::update(float time)
2012-04-18 18:43:45 +08:00
{
int i, j;
for( i = 0; i < _gridSize.width; i++ )
{
for( j = 0; j < _gridSize.height; j++ )
{
Quad3 coords = getOriginalTile(Vector2(i, j));
coords.bl.z = (sinf(time * (float)M_PI *_waves * 2 +
(coords.bl.y+coords.bl.x) * .01f) * _amplitude * _amplitudeRate );
coords.br.z = coords.bl.z;
coords.tl.z = coords.bl.z;
coords.tr.z = coords.bl.z;
setTile(Vector2(i, j), coords);
}
}
2012-04-18 18:43:45 +08:00
}
// implementation of JumpTiles3D
2012-04-18 18:43:45 +08:00
JumpTiles3D* JumpTiles3D::create(float duration, const Size& gridSize, unsigned int numberOfJumps, float amplitude)
2012-04-18 18:43:45 +08:00
{
JumpTiles3D *action = new JumpTiles3D();
if (action)
{
if (action->initWithDuration(duration, gridSize, numberOfJumps, amplitude))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool JumpTiles3D::initWithDuration(float duration, const Size& gridSize, unsigned int numberOfJumps, float amplitude)
2012-04-18 18:43:45 +08:00
{
if (TiledGrid3DAction::initWithDuration(duration, gridSize))
{
_jumps = numberOfJumps;
_amplitude = amplitude;
_amplitudeRate = 1.0f;
return true;
}
return false;
2012-04-18 18:43:45 +08:00
}
JumpTiles3D* JumpTiles3D::clone() const
{
// no copy constructor
auto a = new JumpTiles3D();
a->initWithDuration(_duration, _gridSize, _jumps, _amplitude);
a->autorelease();
return a;
}
void JumpTiles3D::update(float time)
2012-04-18 18:43:45 +08:00
{
int i, j;
float sinz = (sinf((float)M_PI * time * _jumps * 2) * _amplitude * _amplitudeRate );
float sinz2 = (sinf((float)M_PI * (time * _jumps * 2 + 1)) * _amplitude * _amplitudeRate );
for( i = 0; i < _gridSize.width; i++ )
{
for( j = 0; j < _gridSize.height; j++ )
{
Quad3 coords = getOriginalTile(Vector2(i, j));
if ( ((i+j) % 2) == 0 )
{
coords.bl.z += sinz;
coords.br.z += sinz;
coords.tl.z += sinz;
coords.tr.z += sinz;
}
else
{
coords.bl.z += sinz2;
coords.br.z += sinz2;
coords.tl.z += sinz2;
coords.tr.z += sinz2;
}
setTile(Vector2(i, j), coords);
}
}
2012-04-18 18:43:45 +08:00
}
// implementation of SplitRows
SplitRows* SplitRows::create(float duration, unsigned int nRows)
2012-04-18 18:43:45 +08:00
{
SplitRows *action = new SplitRows();
if (action)
{
if (action->initWithDuration(duration, nRows))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool SplitRows::initWithDuration(float duration, unsigned int rows)
2012-04-18 18:43:45 +08:00
{
_rows = rows;
return TiledGrid3DAction::initWithDuration(duration, Size(1, rows));
2012-04-18 18:43:45 +08:00
}
SplitRows* SplitRows::clone() const
{
// no copy constructor
auto a = new SplitRows();
a->initWithDuration(_duration, _rows);
a->autorelease();
return a;
}
void SplitRows::startWithTarget(Node *target)
2012-04-18 18:43:45 +08:00
{
TiledGrid3DAction::startWithTarget(target);
_winSize = Director::getInstance()->getWinSizeInPixels();
2012-04-18 18:43:45 +08:00
}
void SplitRows::update(float time)
2012-04-18 18:43:45 +08:00
{
unsigned int j;
for (j = 0; j < _gridSize.height; ++j)
{
Quad3 coords = getOriginalTile(Vector2(0, j));
float direction = 1;
if ( (j % 2 ) == 0 )
{
direction = -1;
}
coords.bl.x += direction * _winSize.width * time;
coords.br.x += direction * _winSize.width * time;
coords.tl.x += direction * _winSize.width * time;
coords.tr.x += direction * _winSize.width * time;
setTile(Vector2(0, j), coords);
}
2012-04-18 18:43:45 +08:00
}
// implementation of SplitCols
2012-04-18 18:43:45 +08:00
SplitCols* SplitCols::create(float duration, unsigned int cols)
2012-04-18 18:43:45 +08:00
{
SplitCols *action = new SplitCols();
if (action)
{
if (action->initWithDuration(duration, cols))
{
action->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(action);
}
}
return action;
2012-04-18 18:43:45 +08:00
}
bool SplitCols::initWithDuration(float duration, unsigned int cols)
2012-04-18 18:43:45 +08:00
{
_cols = cols;
return TiledGrid3DAction::initWithDuration(duration, Size(cols, 1));
2012-04-18 18:43:45 +08:00
}
SplitCols* SplitCols::clone() const
{
// no copy constructor
auto a = new SplitCols();
a->initWithDuration(_duration, _cols);
a->autorelease();
return a;
}
void SplitCols::startWithTarget(Node *target)
2012-04-18 18:43:45 +08:00
{
TiledGrid3DAction::startWithTarget(target);
_winSize = Director::getInstance()->getWinSizeInPixels();
2012-04-18 18:43:45 +08:00
}
void SplitCols::update(float time)
2012-04-18 18:43:45 +08:00
{
unsigned int i;
for (i = 0; i < _gridSize.width; ++i)
{
Quad3 coords = getOriginalTile(Vector2(i, 0));
float direction = 1;
2012-04-18 18:43:45 +08:00
if ( (i % 2 ) == 0 )
{
direction = -1;
}
coords.bl.y += direction * _winSize.height * time;
coords.br.y += direction * _winSize.height * time;
coords.tl.y += direction * _winSize.height * time;
coords.tr.y += direction * _winSize.height * time;
2012-04-18 18:43:45 +08:00
setTile(Vector2(i, 0), coords);
}
2012-04-18 18:43:45 +08:00
}
NS_CC_END