2015-02-11 18:14:22 +08:00
|
|
|
/****************************************************************************
|
2015-03-02 16:05:26 +08:00
|
|
|
Copyright (C) 2013 Henry van Merode. All rights reserved.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-02-24 18:51:36 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
#include "CCPUTextureAnimator.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
// Constants
|
2021-12-25 10:04:45 +08:00
|
|
|
const float PUTextureAnimator::DEFAULT_TIME_STEP = 0.0f;
|
|
|
|
const unsigned short PUTextureAnimator::DEFAULT_TEXCOORDS_START = 0;
|
|
|
|
const unsigned short PUTextureAnimator::DEFAULT_TEXCOORDS_END = 0;
|
2015-03-05 13:24:06 +08:00
|
|
|
const PUTextureAnimator::TextureAnimationType PUTextureAnimator::DEFAULT_ANIMATION_TYPE = PUTextureAnimator::TAT_LOOP;
|
2021-12-25 10:04:45 +08:00
|
|
|
const bool PUTextureAnimator::DEFAULT_START_RANDOM = true;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUTextureAnimator::PUTextureAnimator()
|
|
|
|
: PUAffector()
|
|
|
|
, _animationTimeStep(DEFAULT_TIME_STEP)
|
|
|
|
, _animationTimeStepCount(0.0f)
|
|
|
|
, _startRandom(DEFAULT_START_RANDOM)
|
|
|
|
, _animationTimeStepSet(false)
|
|
|
|
, _nextIndex(false)
|
|
|
|
, _textureAnimationType(DEFAULT_ANIMATION_TYPE)
|
|
|
|
, _textureCoordsStart(DEFAULT_TEXCOORDS_START)
|
|
|
|
, _textureCoordsEnd(DEFAULT_TEXCOORDS_END)
|
|
|
|
{}
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUTextureAnimator::~PUTextureAnimator() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PUTextureAnimator::getAnimationTimeStep() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _animationTimeStep;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::setAnimationTimeStep(float animationTimeStep)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationTimeStep = animationTimeStep;
|
2015-02-11 18:14:22 +08:00
|
|
|
_animationTimeStepSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUTextureAnimator::TextureAnimationType PUTextureAnimator::getTextureAnimationType() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _textureAnimationType;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::setTextureAnimationType(PUTextureAnimator::TextureAnimationType textureAnimationType)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_textureAnimationType = textureAnimationType;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
unsigned short PUTextureAnimator::getTextureCoordsStart() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _textureCoordsStart;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::setTextureCoordsStart(unsigned short textureCoordsStart)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_textureCoordsStart = textureCoordsStart;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
unsigned short PUTextureAnimator::getTextureCoordsEnd() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _textureCoordsEnd;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::setTextureCoordsEnd(unsigned short textureCoordsEnd)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_textureCoordsEnd = textureCoordsEnd;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
bool PUTextureAnimator::isStartRandom() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _startRandom;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::setStartRandom(bool startRandom)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_startRandom = startRandom;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::initParticleForEmission(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
//// Only continue if the particle is a visual particle
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (particle->particleType != Particle::PT_VISUAL)
|
2015-02-11 18:14:22 +08:00
|
|
|
// return;
|
|
|
|
|
|
|
|
// Set first image
|
|
|
|
if (_startRandom)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->textureCoordsCurrent =
|
|
|
|
(unsigned short)cocos2d::random((float)_textureCoordsStart, (float)_textureCoordsEnd + 0.999f);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
particle->textureCoordsCurrent = _textureCoordsStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the animationTimeStep
|
|
|
|
if (!_animationTimeStepSet)
|
|
|
|
{
|
|
|
|
// Set the animation time step for each particle
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (_textureAnimationType)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
case TAT_LOOP:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
particle->textureAnimationTimeStep = particle->timeToLive / (_textureCoordsEnd - _textureCoordsStart + 1);
|
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
case TAT_UP_DOWN:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
particle->textureAnimationTimeStep =
|
|
|
|
particle->timeToLive / (2 * (_textureCoordsEnd - _textureCoordsStart) + 1);
|
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
case TAT_RANDOM:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
particle->textureAnimationTimeStep = particle->timeToLive;
|
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::preUpdateAffector(float deltaTime)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Determine the next texture coords index (global)
|
|
|
|
if (_animationTimeStepSet)
|
|
|
|
{
|
|
|
|
_nextIndex = false;
|
|
|
|
_animationTimeStepCount += deltaTime;
|
|
|
|
if (_animationTimeStepCount > _animationTimeStep)
|
|
|
|
{
|
|
|
|
_animationTimeStepCount -= _animationTimeStep;
|
|
|
|
_nextIndex = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureAnimator::determineNextTextureCoords(PUParticle3D* visualParticle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (_textureAnimationType)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
case TAT_LOOP:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (visualParticle->textureCoordsCurrent >= _textureCoordsEnd)
|
|
|
|
{
|
|
|
|
visualParticle->textureCoordsCurrent = _textureCoordsStart;
|
|
|
|
}
|
|
|
|
else
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
(visualParticle->textureCoordsCurrent)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAT_UP_DOWN:
|
|
|
|
{
|
|
|
|
if (visualParticle->textureAnimationDirectionUp == true)
|
|
|
|
{
|
|
|
|
// Going up
|
2015-02-11 18:14:22 +08:00
|
|
|
if (visualParticle->textureCoordsCurrent >= _textureCoordsEnd)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
(visualParticle->textureCoordsCurrent)--;
|
|
|
|
visualParticle->textureAnimationDirectionUp = false;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(visualParticle->textureCoordsCurrent)++;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// Going down
|
|
|
|
if (visualParticle->textureCoordsCurrent <= _textureCoordsStart)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
(visualParticle->textureCoordsCurrent)++;
|
|
|
|
visualParticle->textureAnimationDirectionUp = true;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
(visualParticle->textureCoordsCurrent)--;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
case TAT_RANDOM:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
// Generate a random texcoord index
|
|
|
|
visualParticle->textureCoordsCurrent =
|
|
|
|
(unsigned short)cocos2d::random((float)_textureCoordsStart, (float)_textureCoordsEnd + 0.999f);
|
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUTextureAnimator::updatePUAffector(PUParticle3D* particle, float deltaTime)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
//// Only continue if the particle is a visual particle
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (particle->particleType != Particle::PT_VISUAL)
|
2015-02-11 18:14:22 +08:00
|
|
|
// return;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// for (auto iter : _particleSystem->getParticles())
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// PUParticle3D *particle = iter;
|
|
|
|
// Determine the next texture coords index
|
2015-02-11 18:14:22 +08:00
|
|
|
if (_animationTimeStepSet)
|
|
|
|
{
|
|
|
|
if (_nextIndex)
|
|
|
|
{
|
|
|
|
// Use the global one for all particles
|
|
|
|
determineNextTextureCoords(particle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
particle->textureAnimationTimeStepCount += deltaTime;
|
|
|
|
if (particle->textureAnimationTimeStepCount > particle->textureAnimationTimeStep)
|
|
|
|
{
|
|
|
|
particle->textureAnimationTimeStepCount -= particle->textureAnimationTimeStep;
|
|
|
|
determineNextTextureCoords(particle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUTextureAnimator* PUTextureAnimator::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto pta = new PUTextureAnimator();
|
2015-02-11 18:14:22 +08:00
|
|
|
pta->autorelease();
|
|
|
|
return pta;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUTextureAnimator::copyAttributesTo(PUAffector* affector)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUAffector::copyAttributesTo(affector);
|
2021-12-25 10:04:45 +08:00
|
|
|
PUTextureAnimator* textureAnimator = static_cast<PUTextureAnimator*>(affector);
|
|
|
|
textureAnimator->_animationTimeStep = _animationTimeStep;
|
2015-03-02 13:07:32 +08:00
|
|
|
textureAnimator->_animationTimeStepSet = _animationTimeStepSet;
|
|
|
|
textureAnimator->_textureAnimationType = _textureAnimationType;
|
2021-12-25 10:04:45 +08:00
|
|
|
textureAnimator->_textureCoordsStart = _textureCoordsStart;
|
|
|
|
textureAnimator->_textureCoordsEnd = _textureCoordsEnd;
|
|
|
|
textureAnimator->_startRandom = _startRandom;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 15:13:47 +08:00
|
|
|
NS_CC_END
|