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 "CCPUTextureRotator.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 bool PUTextureRotator::DEFAULT_USE_OWN_SPEED = false;
|
2015-03-05 13:24:06 +08:00
|
|
|
const float PUTextureRotator::DEFAULT_ROTATION_SPEED = 10.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
const float PUTextureRotator::DEFAULT_ROTATION = 0.0f;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUTextureRotator::PUTextureRotator()
|
|
|
|
: PUAffector()
|
|
|
|
, _useOwnRotationSpeed(DEFAULT_USE_OWN_SPEED)
|
|
|
|
, _scaledRotationSpeed(0.0f)
|
|
|
|
, _twoPiRad(float(2.0 * M_PI))
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynRotation = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynRotation))->setValue(DEFAULT_ROTATION);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynRotationSpeed = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynRotationSpeed))->setValue(DEFAULT_ROTATION_SPEED);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUTextureRotator::~PUTextureRotator()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynRotation)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynRotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dynRotationSpeed)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynRotationSpeed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
bool PUTextureRotator::useOwnRotationSpeed() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _useOwnRotationSpeed;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUTextureRotator::setUseOwnRotationSpeed(bool useOwnRotationSpeed)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_useOwnRotationSpeed = useOwnRotationSpeed;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUDynamicAttribute* PUTextureRotator::getRotation() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _dynRotation;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureRotator::setRotation(PUDynamicAttribute* dynRotation)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynRotation)
|
|
|
|
CC_SAFE_DELETE(_dynRotation);
|
|
|
|
|
|
|
|
_dynRotation = dynRotation;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUDynamicAttribute* PUTextureRotator::getRotationSpeed() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _dynRotationSpeed;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureRotator::setRotationSpeed(PUDynamicAttribute* dynRotationSpeed)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynRotationSpeed)
|
|
|
|
CC_SAFE_DELETE(_dynRotationSpeed);
|
|
|
|
|
|
|
|
_dynRotationSpeed = dynRotationSpeed;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PUTextureRotator::calculateRotation()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return _dynamicAttributeHelper.calculate(
|
|
|
|
_dynRotation, (static_cast<PUParticleSystem3D*>(_particleSystem))->getTimeElapsedSinceStart());
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PUTextureRotator::calculateRotationSpeed(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _dynamicAttributeHelper.calculate(_dynRotationSpeed, particle->timeFraction);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUTextureRotator::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 != PUParticle3D::PT_VISUAL)
|
2015-02-11 18:14:22 +08:00
|
|
|
// return;
|
|
|
|
|
|
|
|
// Set initial random zRotation
|
|
|
|
particle->zRotation = calculateRotation();
|
2021-12-25 10:04:45 +08:00
|
|
|
// FIXME
|
|
|
|
// if (particle->parentEmitter->getParentTechnique()->getRenderer())
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// // Assume that all parents exist. That must be the case otherwise particles are not emitted.
|
|
|
|
// particle->parentEmitter->getParentTechnique()->getRenderer()->_notifyParticleZRotated();
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
// Set the zRotationSpeed
|
|
|
|
particle->zRotationSpeed = calculateRotationSpeed(particle);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUTextureRotator::updatePUAffector(PUParticle3D* particle, float deltaTime)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
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;
|
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 != PUParticle3D::PT_VISUAL)
|
2015-02-11 18:14:22 +08:00
|
|
|
// return;
|
|
|
|
|
|
|
|
if (_useOwnRotationSpeed)
|
|
|
|
{
|
|
|
|
// Use scaled rotationspeed and adjust the speed according to the velocity
|
|
|
|
_scaledRotationSpeed = particle->zRotationSpeed * deltaTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Scale speed
|
|
|
|
_scaledRotationSpeed = calculateRotationSpeed(particle) * deltaTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
particle->zRotation += _scaledRotationSpeed;
|
|
|
|
particle->zRotation = particle->zRotation > _twoPiRad ? particle->zRotation - _twoPiRad : particle->zRotation;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// FIXME
|
|
|
|
// if (particleTechnique->getRenderer())
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// particleTechnique->getRenderer()->_notifyParticleZRotated();
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUTextureRotator* PUTextureRotator::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ptr = new PUTextureRotator();
|
2015-02-11 18:14:22 +08:00
|
|
|
ptr->autorelease();
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUTextureRotator::copyAttributesTo(PUAffector* affector)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUAffector::copyAttributesTo(affector);
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUTextureRotator* textureRotator = static_cast<PUTextureRotator*>(affector);
|
2015-03-02 13:07:32 +08:00
|
|
|
textureRotator->setRotation(getRotation()->clone());
|
|
|
|
textureRotator->setRotationSpeed(getRotationSpeed()->clone());
|
|
|
|
textureRotator->_useOwnRotationSpeed = _useOwnRotationSpeed;
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:13:47 +08:00
|
|
|
NS_CC_END
|