axmol/cocos/2d/CCActionGrid.cpp

445 lines
9.9 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/CCActionGrid.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCGrid.h"
#include "2d/CCNodeGrid.h"
NS_CC_BEGIN
// implementation of GridAction
bool GridAction::initWithDuration(float duration, const Size& gridSize)
{
if (ActionInterval::initWithDuration(duration))
{
_gridSize = gridSize;
return true;
}
return false;
}
void GridAction::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
cacheTargetAsGridNode();
GridBase *newgrid = this->getGrid();
GridBase *targetGrid = _gridNodeTarget->getGrid();
if (targetGrid && targetGrid->getReuseGrid() > 0)
{
if (targetGrid->isActive() && targetGrid->getGridSize().width == _gridSize.width
&& targetGrid->getGridSize().height == _gridSize.height /*&& dynamic_cast<GridBase*>(targetGrid) != nullptr*/)
{
targetGrid->reuse();
}
else
{
CCASSERT(0, "");
}
}
else
{
if (targetGrid && targetGrid->isActive())
{
targetGrid->setActive(false);
}
_gridNodeTarget->setGrid(newgrid);
_gridNodeTarget->getGrid()->setActive(true);
}
}
void GridAction::cacheTargetAsGridNode()
{
_gridNodeTarget = dynamic_cast<NodeGrid*> (_target);
CCASSERT(_gridNodeTarget, "GridActions can only used on NodeGrid");
}
GridAction* GridAction::reverse() const
{
// FIXME: This conversion isn't safe.
return (GridAction*)ReverseTime::create( this->clone() );
}
2013-11-16 21:08:00 +08:00
GridBase* GridAction::getGrid()
{
// Abstract class needs implementation
CCASSERT(0, "");
2013-11-16 21:08:00 +08:00
return nullptr;
}
// implementation of Grid3DAction
2013-11-16 21:08:00 +08:00
GridBase* Grid3DAction::getGrid()
{
return Grid3D::create(_gridSize);
}
Vector3 Grid3DAction::getVertex(const Vector2& position) const
{
Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
return g->getVertex(position);
}
Vector3 Grid3DAction::getOriginalVertex(const Vector2& position) const
{
Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
return g->getOriginalVertex(position);
}
void Grid3DAction::setVertex(const Vector2& position, const Vector3& vertex)
{
Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
g->setVertex(position, vertex);
}
// implementation of TiledGrid3DAction
GridBase* TiledGrid3DAction::getGrid(void)
{
return TiledGrid3D::create(_gridSize);
}
Quad3 TiledGrid3DAction::getTile(const Vector2& pos) const
{
TiledGrid3D *g = (TiledGrid3D*)_gridNodeTarget->getGrid();
return g->getTile(pos);
}
Quad3 TiledGrid3DAction::getOriginalTile(const Vector2& pos) const
{
TiledGrid3D *g = (TiledGrid3D*)_gridNodeTarget->getGrid();
return g->getOriginalTile(pos);
}
void TiledGrid3DAction::setTile(const Vector2& pos, const Quad3& coords)
{
TiledGrid3D *g = (TiledGrid3D*)_gridNodeTarget->getGrid();
return g->setTile(pos, coords);
}
// implementation AccelDeccelAmplitude
2013-11-16 21:08:00 +08:00
AccelDeccelAmplitude* AccelDeccelAmplitude::create(Action *action, float duration)
{
2013-11-16 21:08:00 +08:00
AccelDeccelAmplitude *ret = new AccelDeccelAmplitude();
if (ret)
{
2013-11-16 21:08:00 +08:00
if (ret->initWithAction(action, duration))
{
2013-11-16 21:08:00 +08:00
ret->autorelease();
}
else
{
2013-11-16 21:08:00 +08:00
CC_SAFE_DELETE(ret);
}
}
2013-11-16 21:08:00 +08:00
return ret;
}
2013-11-16 21:08:00 +08:00
bool AccelDeccelAmplitude::initWithAction(Action *action, float duration)
{
if (ActionInterval::initWithDuration(duration))
{
_rate = 1.0f;
2013-11-16 21:08:00 +08:00
_other = (ActionInterval*)(action);
action->retain();
return true;
}
return false;
}
AccelDeccelAmplitude* AccelDeccelAmplitude::clone() const
{
// no copy constructor
auto a = new AccelDeccelAmplitude();
a->initWithAction(_other->clone(), _rate);
a->autorelease();
return a;
}
2013-11-16 21:08:00 +08:00
AccelDeccelAmplitude::~AccelDeccelAmplitude()
{
CC_SAFE_RELEASE(_other);
}
void AccelDeccelAmplitude::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
_other->startWithTarget(target);
}
void AccelDeccelAmplitude::update(float time)
{
float f = time * 2;
if (f > 1)
{
f -= 1;
f = 1 - f;
}
((AccelDeccelAmplitude*)(_other))->setAmplitudeRate(powf(f, _rate));
}
AccelDeccelAmplitude* AccelDeccelAmplitude::reverse() const
{
return AccelDeccelAmplitude::create(_other->reverse(), _duration);
}
// implementation of AccelAmplitude
2013-11-16 21:08:00 +08:00
AccelAmplitude* AccelAmplitude::create(Action *action, float duration)
{
2013-11-16 21:08:00 +08:00
AccelAmplitude *ret = new AccelAmplitude();
if (ret)
{
2013-11-16 21:08:00 +08:00
if (ret->initWithAction(action, duration))
{
2013-11-16 21:08:00 +08:00
ret->autorelease();
}
else
{
2013-11-16 21:08:00 +08:00
CC_SAFE_DELETE(ret);
}
}
2013-11-16 21:08:00 +08:00
return ret;
}
2013-11-16 21:08:00 +08:00
bool AccelAmplitude::initWithAction(Action *action, float duration)
{
if (ActionInterval::initWithDuration(duration))
{
_rate = 1.0f;
2013-11-16 21:08:00 +08:00
_other = (ActionInterval*)(action);
action->retain();
return true;
}
return false;
}
AccelAmplitude* AccelAmplitude::clone() const
{
// no copy constructor
auto a = new AccelAmplitude();
a->initWithAction(_other->clone(), _duration);
a->autorelease();
return a;
}
AccelAmplitude::~AccelAmplitude(void)
{
CC_SAFE_DELETE(_other);
}
void AccelAmplitude::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
_other->startWithTarget(target);
}
void AccelAmplitude::update(float time)
{
((AccelAmplitude*)(_other))->setAmplitudeRate(powf(time, _rate));
_other->update(time);
}
AccelAmplitude* AccelAmplitude::reverse() const
{
return AccelAmplitude::create(_other->reverse(), _duration);
}
// DeccelAmplitude
2013-11-16 21:08:00 +08:00
DeccelAmplitude* DeccelAmplitude::create(Action *action, float duration)
{
2013-11-16 21:08:00 +08:00
DeccelAmplitude *ret = new DeccelAmplitude();
if (ret)
{
2013-11-16 21:08:00 +08:00
if (ret->initWithAction(action, duration))
{
2013-11-16 21:08:00 +08:00
ret->autorelease();
}
else
{
2013-11-16 21:08:00 +08:00
CC_SAFE_DELETE(ret);
}
}
2013-11-16 21:08:00 +08:00
return ret;
}
2013-11-16 21:08:00 +08:00
bool DeccelAmplitude::initWithAction(Action *action, float duration)
{
if (ActionInterval::initWithDuration(duration))
{
_rate = 1.0f;
2013-11-16 21:08:00 +08:00
_other = (ActionInterval*)(action);
action->retain();
return true;
}
return false;
}
2013-11-16 21:08:00 +08:00
DeccelAmplitude::~DeccelAmplitude()
{
CC_SAFE_RELEASE(_other);
}
void DeccelAmplitude::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
_other->startWithTarget(target);
}
void DeccelAmplitude::update(float time)
{
((DeccelAmplitude*)(_other))->setAmplitudeRate(powf((1 - time), _rate));
_other->update(time);
}
DeccelAmplitude* DeccelAmplitude::clone() const
{
// no copy constructor
auto a = new DeccelAmplitude();
a->initWithAction(_other->clone(), _duration);
a->autorelease();
return a;
}
DeccelAmplitude* DeccelAmplitude::reverse() const
{
return DeccelAmplitude::create(_other->reverse(), _duration);
}
// implementation of StopGrid
void StopGrid::startWithTarget(Node *target)
{
ActionInstant::startWithTarget(target);
cacheTargetAsGridNode();
GridBase *grid = _gridNodeTarget->getGrid();
2013-11-16 21:08:00 +08:00
if (grid && grid->isActive())
{
2013-11-16 21:08:00 +08:00
grid->setActive(false);
}
}
void StopGrid::cacheTargetAsGridNode()
{
_gridNodeTarget = dynamic_cast<NodeGrid*> (_target);
CCASSERT(_gridNodeTarget, "GridActions can only used on NodeGrid");
}
2013-11-16 21:08:00 +08:00
StopGrid* StopGrid::create()
{
StopGrid* pAction = new StopGrid();
pAction->autorelease();
return pAction;
}
StopGrid* StopGrid::clone() const
{
return StopGrid::create();
}
StopGrid* StopGrid::reverse() const
{
// no reverse, just clone it
return this->clone();
}
// implementation of ReuseGrid
ReuseGrid* ReuseGrid::create(int times)
{
2013-11-16 21:08:00 +08:00
ReuseGrid *action = new ReuseGrid();
if (action)
{
2013-11-16 21:08:00 +08:00
if (action->initWithTimes(times))
{
2013-11-16 21:08:00 +08:00
action->autorelease();
}
else
{
2013-11-16 21:08:00 +08:00
CC_SAFE_DELETE(action);
}
}
2013-11-16 21:08:00 +08:00
return action;
}
bool ReuseGrid::initWithTimes(int times)
{
_times = times;
return true;
}
void ReuseGrid::startWithTarget(Node *target)
{
ActionInstant::startWithTarget(target);
cacheTargetAsGridNode();
if (_gridNodeTarget->getGrid() && _gridNodeTarget->getGrid()->isActive())
{
_gridNodeTarget->getGrid()->setReuseGrid(_gridNodeTarget->getGrid()->getReuseGrid() + _times);
}
}
void ReuseGrid::cacheTargetAsGridNode()
{
_gridNodeTarget = dynamic_cast<NodeGrid*> (_target);
CCASSERT(_gridNodeTarget, "GridActions can only used on NodeGrid");
}
ReuseGrid* ReuseGrid::clone() const
{
return ReuseGrid::create(_times);
}
ReuseGrid* ReuseGrid::reverse() const
{
// no reverse, just clone it
return this->clone();
}
NS_CC_END