#ifndef __ACTION_CCTILEDGRID_ACTION_H__
#define __ACTION_CCTILEDGRID_ACTION_H__
#include "CCActionGrid.h"
NS_CC_BEGIN
/** @brief CCShakyTiles3D action */
class CC_DLL CCShakyTiles3D : public CCTiledGrid3DAction //摇动的3D格子动画。
{
public:
//初始化 1、时间 2、格子大小 3、范围大小 4、是否在z轴摇动
virtual bool initWithDuration(float duration, const CCSize& gridSize, int nRange, bool bShakeZ);
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void update(float time);
struct Tile
{
CCPoint position;
CCPoint startPosition;
ccGridSize delta;
};
void CCShakyTiles3D::update(float time)
{
CC_UNUSED_PARAM(time);
int i, j;
//双循环遍历每个格子。
for (i = 0; i < m_sGridSize.x; ++i)
{
for (j = 0; j < m_sGridSize.y; ++j)
{
//取得格子的位置。
ccQuad3 coords = originalTile(ccg(i, j));
// 计算格子四个顶点的X坐标。
coords.bl.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.br.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tl.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tr.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
// 计算格子四个顶点的Y坐标。
coords.bl.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.br.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tl.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tr.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
// 是否在Z轴上摇动。
if (m_bShakeZ)
{
//如果摇动在四个顶点的Z值上有随机的加成。
coords.bl.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.br.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tl.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tr.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
}
//重新设置格子的顶点。
setTile(ccg(i, j), coords);
}
}
}
public:
//创建
static CCShakyTiles3D* create(float duration, const CCSize& gridSize, int nRange, bool bShakeZ);
protected:
int m_nRandrange;
bool m_bShakeZ;
};
对应图:
/** @brief CCShatteredTiles3D action */
class CC_DLL CCShatteredTiles3D : public CCTiledGrid3DAction //Shattered破碎的 随机抖动效果的3D网格动画
{
public:
//初始化 1、时间 2、格子大小 3、范围 4、是否在z抖动
virtual bool initWithDuration(float duration, const CCSize& gridSize, int nRange, bool bShatterZ);
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void update(float time);
<span style="color:#ff0000;">void CCShatteredTiles3D::update(float time)
{
CC_UNUSED_PARAM(time);
int i, j;
//如果是第一次渲染。
if (m_bOnce == false)
{
//双循环遍历格子。
for (i = 0; i < m_sGridSize.x; ++i)
{
for (j = 0; j < m_sGridSize.y; ++j)
{
//取得格子的坐标。
ccQuad3 coords = originalTile(ccg(i ,j));
// 计算格子四个顶点的X坐标。
coords.bl.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.br.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tl.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tr.x += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
// 计算格子四个顶点的Y坐标。
coords.bl.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.br.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tl.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tr.y += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
if (m_bShatterZ)
{
coords.bl.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.br.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tl.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
coords.tr.z += ( rand() % (m_nRandrange*2) ) - m_nRandrange;
}
//重新设置格子的顶点。
setTile(ccg(i, j), coords);
}
}
//设置不是第一次更新。
m_bOnce = true;
}
}</span>
public:
//创建
static CCShatteredTiles3D* create(float duration, const CCSize& gridSize, int nRange, bool bShatterZ);
protected:
int m_nRandrange;//随机范围
bool m_bOnce;//区分第一次渲染所用。
bool m_bShatterZ;//是否在Z抽上震动。
};
对应图:
struct Tile;
/** @brief CCShuffleTiles action
Shuffle the tiles in random order
*/
class CC_DLL CCShuffleTiles : public CCTiledGrid3DAction//Shuffle 洗牌 重新排列格子效果的3D网格动画。
{
public:
~CCShuffleTiles(void);
//初始化 1、时间 2、格子大小 3、种子(随机种子)
virtual bool initWithDuration(float duration, const CCSize& gridSize, unsigned int seed);
//重新排列处理
void shuffle(unsigned int *pArray, unsigned int nLen);
<span style="color:#ff0000;">void CCShuffleTiles::shuffle(int *pArray, unsigned int nLen)
{
//定义临时变量,并循环进行随机排列。
int i;
for( i = nLen - 1; i >= 0; i-- )
{
unsigned int j = rand() % (i+1);
//随机交换
int v = pArray[i];
pArray[i] = pArray[j];
pArray[j] = v;
}
}</span>
//得到
CCSize getDelta(const CCSize& pos);
<span style="color:#ff0000;">ccGridSize CCShuffleTiles::getDelta(const ccGridSize& pos)
{
CCPoint pos2;
//计算格子的位置索引
unsigned int idx = pos.x * m_sGridSize.y + pos.y;
//取出
pos2.x = (float)(m_pTilesOrder[idx] / (int)m_sGridSize.y);
pos2.y = (float)(m_pTilesOrder[idx] % (int)m_sGridSize.y);
//
return ccg((int)(pos2.x - pos.x), (int)(pos2.y - pos.y));
}</span>
void placeTile(const CCPoint& pos, Tile *t);
<span style="color:#ff0000;">//替换指定格子中的数据
void CCShuffleTiles::placeTile(const ccGridSize& pos, Tile *t)
{
//取得对应格子大小的原始顶点数据
ccQuad3 coords = originalTile(pos);
//取得演员所用格子数据中格子的间隔。
CCPoint step = m_pTarget->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);
}</span>
virtual void startWithTarget(CCNode *pTarget);
virtual void update(float time);
<span style="color:#ff0000;">void CCShuffleTiles::update(float time)
{
int i, j;
//取得格子的数据指针。
Tile *tileArray = (Tile*)m_pTiles;
//双循环遍历。
for (i = 0; i < m_sGridSize.x; ++i)
{
for (j = 0; j < m_sGridSize.y; ++j)
{
//重新计算格子位置。
tileArray->position = ccpMult(ccp((float)tileArray->delta.x, (float)tileArray->delta.y), time);
//将指针数据填充到对应格子。
placeTile(ccg(i, j), tileArray);
++tileArray;
}
}
}</span>
virtual CCObject* copyWithZone(CCZone* pZone);
public:
//创建
static CCShuffleTiles* create(float duration, const CCSize& gridSize, unsigned int seed);
protected:
unsigned int m_nSeed;
unsigned int m_nTilesCount;
unsigned int* m_pTilesOrder;
Tile* m_pTiles;
};
对应图
/** @brief CCFadeOutTRTiles action
Fades out the tiles in a Top-Right direction
*/
class CC_DLL CCFadeOutTRTiles : public CCTiledGrid3DAction//从右上角开始渐隐格子的动画。
{
public:
//为了检测是否满足相应条件的函数。
virtual float testFunc(const CCSize& pos, float time);
<span style="color:#ff0000;">float CCFadeOutTRTiles::testFunc(const ccGridSize& pos, float time)
{
//计算出格子的位置。
CCPoint n = ccpMult(ccp((float)m_sGridSize.x, (float)m_sGridSize.y), time);
//为了后面powf中的除法避免除零而做的判断。
if ((n.x + n.y) == 0.0f)
{
return 1.0f;
}
//计算出一个powf处理后的值。
return powf((pos.x + pos.y) / (n.x + n.y), 6);
}</span>
//开启关闭格子。
void turnOnTile(const CCPoint& pos);
<span style="color:#ff0000;">void CCFadeOutTRTiles::turnOnTile(const ccGridSize& pos)
{
//设置格子位置为原始数据。
setTile(pos, originalTile(pos));
}</span>
void turnOffTile(const CCPoint& pos);
<span style="color:#ff0000;">void CCFadeOutTRTiles::turnOffTile(const ccGridSize& pos)
{
//定义一个新的顶点数据,都清零后设置为对应格子的顶点数据。
ccQuad3 coords;
memset(&coords, 0, sizeof(ccQuad3));
setTile(pos, coords);
}</span>
//跟据距离转换格子。
virtual void transformTile(const CCPoint& pos, float distance);
<span style="color:#ff0000;">void CCFadeOutTRTiles::transformTile(const ccGridSize& pos, float distance)
{
//取得对应格子的顶点数据。
ccQuad3 coords = originalTile(pos);
//取得每个像素所占的图像大小。
CCPoint step = m_pTarget->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);
}</span>
virtual void update(float time);
<span style="color:#ff0000;">void CCFadeOutTRTiles::update(float time)
{
int i, j;
//双循环遍历。
for (i = 0; i < m_sGridSize.x; ++i)
{
for (j = 0; j < m_sGridSize.y; ++j)
{
//为了检测是否满足相应条件的函数,返回距离值。
float distance = testFunc(ccg(i, j), time);
if ( distance == 0 )
{
//如果距离为0,关闭格子。
turnOffTile(ccg(i, j));
} else
if (distance < 1)
{
//如果距离在0~1间,进行格子的转换处理。
transformTile(ccg(i, j), distance);
}
else
{
//如果距离大于等于1,开启格子。
turnOnTile(ccg(i, j));
}
}
}
}</span>
public:
//创建 1、时间 2、格子大小
static CCFadeOutTRTiles* create(float duration, const CCSize& gridSize);
};
/** @brief CCFadeOutBLTiles action.
Fades out the tiles in a Bottom-Left direction
*/
class CC_DLL CCFadeOutBLTiles : public CCFadeOutTRTiles
{
public:
virtual float testFunc(const CCSize& pos, float time);
public:
/** creates the action with the grid size and the duration */
static CCFadeOutBLTiles* create(float duration, const CCSize& gridSize);
};
对应图
/** @brief CCFadeOutUpTiles action.
Fades out the tiles in upwards direction
*/
class CC_DLL CCFadeOutUpTiles : public CCFadeOutTRTiles //向上部渐隐格子的动画。
{
public:
virtual float testFunc(const CCSize& pos, float time);
virtual void transformTile(const CCPoint& pos, float distance);
public:
/** creates the action with the grid size and the duration */
static CCFadeOutUpTiles* create(float duration, const CCSize& gridSize);
};
对应图
/** @brief CCFadeOutDownTiles action.
Fades out the tiles in downwards direction
*/
class CC_DLL CCFadeOutDownTiles : public CCFadeOutUpTiles //向下部渐隐格子的动画。
{
public:
virtual float testFunc(const CCSize& pos, float time);
public:
/** creates the action with the grid size and the duration */
static CCFadeOutDownTiles* create(float duration, const CCSize& gridSize);
};
对应图
/** @brief CCTurnOffTiles action.
Turn off the files in random order
*/
class CC_DLL CCTurnOffTiles : public CCTiledGrid3DAction //格子的消失动画
{
public:
~CCTurnOffTiles(void);
/** initializes the action with a random seed, the grid size and the duration */
virtual bool initWithDuration(float duration, const CCSize& gridSize, unsigned int seed);
void shuffle(unsigned int *pArray, unsigned int nLen);
<span style="color:#ff0000;">//重新随机排列。
void CCTurnOffTiles::shuffle(int *pArray, unsigned int nLen)
{
//定义临时变量,并循环进行随机排列。
int i;
for (i = nLen - 1; i >= 0; i--)
{
//随机交换。
unsigned int j = rand() % (i+1);
int v = pArray[i];
pArray[i] = pArray[j];
pArray[j] = v;
}
}</span>
//开启关闭格子
void turnOnTile(const CCPoint& pos);
<span style="color:#ff0000;">void CCTurnOffTiles::turnOnTile(const ccGridSize& pos)
{
//设置格子的数据为原始数据。
setTile(pos, originalTile(pos));
}</span>
void turnOffTile(const CCPoint& pos);
<span style="color:#ff0000;">//关闭格子。
void CCTurnOffTiles::turnOffTile(const ccGridSize& pos)
{
//定义一个新的顶点数据,都清零后设置为对应格子的顶点数据。
ccQuad3 coords;
memset(&coords, 0, sizeof(ccQuad3));
setTile(pos, coords);
}</span>
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void startWithTarget(CCNode *pTarget);
virtual void update(float time);
<span style="color:#ff0000;">void CCTurnOffTiles::update(float time)
{
unsigned int i, l, t;
//通过进度值计算要消失的格子的数量。
l = (int)(time * (float)m_nTilesCount);
//遍历每个格子
for( i = 0; i < m_nTilesCount; i++ )
{
//取出顺序索引,并计算出相应的格子位置。
t = m_pTilesOrder[i];
ccGridSize tilePos = ccg( t / m_sGridSize.y, t % m_sGridSize.y );
//如果当前索引i小于要消失的可子的数量值,关闭格子。
if ( i < l )
{
turnOffTile(tilePos);
}
else
{ //开启格子。
turnOnTile(tilePos);
}
}
}</span>
public:
/** creates the action with the grid size and the duration */
static CCTurnOffTiles* create(float duration, const CCSize& gridSize);
/** creates the action with a random seed, the grid size and the duration */
static CCTurnOffTiles* create(float duration, const CCSize& gridSize, unsigned int seed);
protected:
unsigned int m_nSeed;
unsigned int m_nTilesCount;
unsigned int* m_pTilesOrder;
};
/** @brief CCWavesTiles3D action. */
class CC_DLL CCWavesTiles3D : public CCTiledGrid3DAction //波浪状的格子动画。
{
public:
/** waves amplitude */
inline float getAmplitude(void) { return m_fAmplitude; }
inline void setAmplitude(float fAmplitude) { m_fAmplitude = fAmplitude; }
/** waves amplitude rate */
inline float getAmplitudeRate(void) { return m_fAmplitudeRate; }
inline void setAmplitudeRate(float fAmplitudeRate) { m_fAmplitudeRate = fAmplitudeRate; }
//初始化 1、时间 2、格子大小 3、波浪数 4、振幅
virtual bool initWithDuration(float duration, const CCSize& gridSize, unsigned int waves, float amplitude);
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void update(float time);
<span style="color:#ff0000;"><span style="color:#ff0000;">void CCWavesTiles3D::update(float time)
{
int i, j;
//双循环遍历。
for( i = 0; i < m_sGridSize.x; i++ )
{
for( j = 0; j < m_sGridSize.y; j++ )
{
//取得对应格子的顶点数据。
ccQuad3 coords = originalTile(ccg(i, j));
//重新设置顶点数据位置。
coords.bl.z = (sinf(time * (float)M_PI *m_nWaves * 2 +
(coords.bl.y+coords.bl.x) * .01f) * m_fAmplitude * m_fAmplitudeRate );
coords.br.z = coords.bl.z;
coords.tl.z = coords.bl.z;
coords.tr.z = coords.bl.z;
//将新的位置数据设置给相应格子。
setTile(ccg(i, j), coords);
}
}
}</span></span>
public:
//创建
static CCWavesTiles3D* create(float duration, const CCSize& gridSize, unsigned int waves, float amplitude);
protected:
unsigned int m_nWaves;
float m_fAmplitude;
float m_fAmplitudeRate;
};
~~~~~~
/** @brief CCJumpTiles3D action.
A sin function is executed to move the tiles across the Z axis
*/
class CC_DLL CCJumpTiles3D : public CCTiledGrid3DAction //跳动的格子动画。
{
public:
/** amplitude of the sin*/
inline float getAmplitude(void) { return m_fAmplitude; }
inline void setAmplitude(float fAmplitude) { m_fAmplitude = fAmplitude; }
/** amplitude rate */
inline float getAmplitudeRate(void) { return m_fAmplitudeRate; }
inline void setAmplitudeRate(float fAmplitudeRate) { m_fAmplitudeRate = fAmplitudeRate; }
//初始化 1、时间 2、格子大小 3、跳跃次数 4、振幅
bool initWithDuration(float duration, const CCSize& gridSize, unsigned int numberOfJumps, float amplitude);
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void update(float time);
<span style="color:#ff0000;">void CCJumpTiles3D::update(float time)
{
int i, j;
//定义两个临时变量,分别设置为不同的sin曲线变化值。
float sinz = (sinf((float)M_PI * time * m_nJumps * 2) * m_fAmplitude * m_fAmplitudeRate );
float sinz2 = (sinf((float)M_PI * (time * m_nJumps * 2 + 1)) * m_fAmplitude * m_fAmplitudeRate );
//双循环遍历。
for( i = 0; i < m_sGridSize.x; i++ )
{
for( j = 0; j < m_sGridSize.y; j++ )
{
//取得对应格子的顶点数据。
ccQuad3 coords = originalTile(ccg(i, j));
//(i+j)的值对2取模,可以实现上,下,左,右相邻的Z值加成不同。
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(ccg(i, j), coords);
}
}
}</span>
public:
//创建
static CCJumpTiles3D* create(float duration, const CCSize& gridSize, unsigned int numberOfJumps, float amplitude);
protected:
unsigned int m_nJumps;
float m_fAmplitude;
float m_fAmplitudeRate;
};
/** @brief CCSplitRows action */
class CC_DLL CCSplitRows : public CCTiledGrid3DAction //横向分割的格子动画。
{
public :
//初始化 1、时间 2、横向分的份数
virtual bool initWithDuration(float duration, unsigned int nRows);
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void update(float time);
<span style="color:#ff0000;">void CCSplitRows::update(float time)
{
int j;
//遍历切分的格子。在初始化时说过格子大小的Y值中保存了切分的份数。
for (j = 0; j < m_sGridSize.y; ++j)
{
//通过格子的的索引取得原始的顶点位置。
ccQuad3 coords = originalTile(ccg(0, j));
//创建临时变量保存方向。
float direction = 1;
//对i取2的模,可使相邻的切分块方向不同。
if ( (j % 2 ) == 0 )
{
direction = -1;
}
//根据方向来对顶点X值进行偏移。
coords.bl.x += direction * m_winSize.width * time;
coords.br.x += direction * m_winSize.width * time;
coords.tl.x += direction * m_winSize.width * time;
coords.tr.x += direction * m_winSize.width * time;
//设置格子的顶点数据,这样就可以实现每个切分格子的运动了。
setTile(ccg(0, j), coords);
}
}</span>
virtual void startWithTarget(CCNode *pTarget);
public:
/** creates the action with the number of rows to split and the duration */
static CCSplitRows* create(float duration, unsigned int nRows);
protected:
//切分的行数。
unsigned int m_nRows;
//屏幕大小
CCSize m_winSize;
};
/** @brief CCSplitCols action */
class CC_DLL CCSplitCols : public CCTiledGrid3DAction //纵向切分的格子运动动画。
{
public:
/** initializes the action with the number of columns to split and the duration */
virtual bool initWithDuration(float duration, unsigned int nCols);
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void update(float time);
virtual void startWithTarget(CCNode *pTarget);
public:
/** creates the action with the number of columns to split and the duration */
static CCSplitCols* create(float duration, unsigned int nCols);
protected:
unsigned int m_nCols;
CCSize m_winSize;
};
// end of actions group
/// @}
NS_CC_END
#endif // __ACTION_CCTILEDGRID_ACTION_H__