axmol/extensions/Particle3D/PU/CCPUTextureRotator.cpp

178 lines
6.5 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (C) 2013 Henry van Merode. All rights reserved.
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-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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.
****************************************************************************/
#include "CCPUTextureRotator.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
// Constants
2021-12-25 10:04:45 +08:00
const bool PUTextureRotator::DEFAULT_USE_OWN_SPEED = false;
2019-11-23 20:27:39 +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;
2019-11-23 20:27:39 +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))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_dynRotation = new PUDynamicAttributeFixed();
2019-11-23 20:27:39 +08:00
(static_cast<PUDynamicAttributeFixed*>(_dynRotation))->setValue(DEFAULT_ROTATION);
2021-12-08 00:11:53 +08:00
_dynRotationSpeed = new PUDynamicAttributeFixed();
2019-11-23 20:27:39 +08:00
(static_cast<PUDynamicAttributeFixed*>(_dynRotationSpeed))->setValue(DEFAULT_ROTATION_SPEED);
}
//-----------------------------------------------------------------------
PUTextureRotator::~PUTextureRotator()
{
if (_dynRotation)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_dynRotation);
2019-11-23 20:27:39 +08:00
}
if (_dynRotationSpeed)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_dynRotationSpeed);
2019-11-23 20:27:39 +08:00
}
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUTextureRotator::useOwnRotationSpeed() const
2019-11-23 20:27:39 +08:00
{
return _useOwnRotationSpeed;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUTextureRotator::setUseOwnRotationSpeed(bool useOwnRotationSpeed)
2019-11-23 20:27:39 +08:00
{
_useOwnRotationSpeed = useOwnRotationSpeed;
}
//-----------------------------------------------------------------------
PUDynamicAttribute* PUTextureRotator::getRotation() const
{
return _dynRotation;
}
//-----------------------------------------------------------------------
void PUTextureRotator::setRotation(PUDynamicAttribute* dynRotation)
{
if (_dynRotation)
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_dynRotation);
2019-11-23 20:27:39 +08:00
_dynRotation = dynRotation;
}
//-----------------------------------------------------------------------
PUDynamicAttribute* PUTextureRotator::getRotationSpeed() const
{
return _dynRotationSpeed;
}
//-----------------------------------------------------------------------
void PUTextureRotator::setRotationSpeed(PUDynamicAttribute* dynRotationSpeed)
{
if (_dynRotationSpeed)
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_dynRotationSpeed);
2019-11-23 20:27:39 +08:00
_dynRotationSpeed = dynRotationSpeed;
}
//-----------------------------------------------------------------------
float PUTextureRotator::calculateRotation()
{
2021-12-25 10:04:45 +08:00
return _dynamicAttributeHelper.calculate(
_dynRotation, (static_cast<PUParticleSystem3D*>(_particleSystem))->getTimeElapsedSinceStart());
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
float PUTextureRotator::calculateRotationSpeed(PUParticle3D* particle)
{
return _dynamicAttributeHelper.calculate(_dynRotationSpeed, particle->timeFraction);
}
//-----------------------------------------------------------------------
void PUTextureRotator::initParticleForEmission(PUParticle3D* particle)
{
//// Only continue if the particle is a visual particle
2021-12-25 10:04:45 +08:00
// if (particle->particleType != PUParticle3D::PT_VISUAL)
2019-11-23 20:27:39 +08:00
// return;
// Set initial random zRotation
particle->zRotation = calculateRotation();
2021-12-25 10:04:45 +08:00
// FIXME
// if (particle->parentEmitter->getParentTechnique()->getRenderer())
2019-11-23 20:27:39 +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
// }
2019-11-23 20:27:39 +08:00
// Set the zRotationSpeed
particle->zRotationSpeed = calculateRotationSpeed(particle);
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUTextureRotator::updatePUAffector(PUParticle3D* particle, float deltaTime)
2019-11-23 20:27:39 +08:00
{
// for (auto&& iter : _particleSystem->getParticles())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// PUParticle3D *particle = iter;
2019-11-23 20:27:39 +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)
2019-11-23 20:27:39 +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())
2019-11-23 20:27:39 +08:00
//{
// particleTechnique->getRenderer()->_notifyParticleZRotated();
2021-12-25 10:04:45 +08:00
// }
2019-11-23 20:27:39 +08:00
}
}
PUTextureRotator* PUTextureRotator::create()
{
2021-12-08 00:11:53 +08:00
auto ptr = new PUTextureRotator();
2019-11-23 20:27:39 +08:00
ptr->autorelease();
return ptr;
}
2021-12-25 10:04:45 +08:00
void PUTextureRotator::copyAttributesTo(PUAffector* affector)
2019-11-23 20:27:39 +08:00
{
PUAffector::copyAttributesTo(affector);
PUTextureRotator* textureRotator = static_cast<PUTextureRotator*>(affector);
textureRotator->setRotation(getRotation()->clone());
textureRotator->setRotationSpeed(getRotationSpeed()->clone());
textureRotator->_useOwnRotationSpeed = _useOwnRotationSpeed;
}
NS_AX_END