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
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
http://www.cocos2d-x.org
|
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 "CCPUScaleAffector.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 PUScaleAffector::DEFAULT_X_SCALE = 1.0f;
|
|
|
|
const float PUScaleAffector::DEFAULT_Y_SCALE = 1.0f;
|
|
|
|
const float PUScaleAffector::DEFAULT_Z_SCALE = 1.0f;
|
2015-03-05 13:24:06 +08:00
|
|
|
const float PUScaleAffector::DEFAULT_XYZ_SCALE = 1.0f;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUScaleAffector::PUScaleAffector()
|
|
|
|
: PUAffector()
|
|
|
|
, _dynScaleXSet(false)
|
|
|
|
, _dynScaleYSet(false)
|
|
|
|
, _dynScaleZSet(false)
|
|
|
|
, _dynScaleXYZSet(false)
|
|
|
|
, _sinceStartSystem(false)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleX = new PUDynamicAttributeFixed();
|
|
|
|
_dynScaleY = new PUDynamicAttributeFixed();
|
|
|
|
_dynScaleZ = new PUDynamicAttributeFixed();
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleXYZ = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleX))->setValue(DEFAULT_X_SCALE);
|
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleY))->setValue(DEFAULT_Y_SCALE);
|
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleZ))->setValue(DEFAULT_Z_SCALE);
|
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleXYZ))->setValue(DEFAULT_XYZ_SCALE);
|
|
|
|
_latestTimeElapsed = 1.0f;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUScaleAffector::~PUScaleAffector()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynScaleX)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleX);
|
|
|
|
}
|
|
|
|
if (_dynScaleY)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleY);
|
|
|
|
}
|
|
|
|
if (_dynScaleZ)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleZ);
|
|
|
|
}
|
|
|
|
if (_dynScaleXYZ)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleXYZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::setDynScaleX(PUDynamicAttribute* dynScaleX)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynScaleX)
|
|
|
|
CC_SAFE_DELETE(_dynScaleX);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleX = dynScaleX;
|
2015-02-11 18:14:22 +08:00
|
|
|
_dynScaleXSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUScaleAffector::resetDynScaleX(bool resetToDefault)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleX);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleX = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleX))->setValue(DEFAULT_X_SCALE);
|
|
|
|
_dynScaleXSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleXSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::setDynScaleY(PUDynamicAttribute* dynScaleY)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynScaleY)
|
|
|
|
CC_SAFE_DELETE(_dynScaleY);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleY = dynScaleY;
|
2015-02-11 18:14:22 +08:00
|
|
|
_dynScaleYSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::resetDynScaleY(bool resetToDefault)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(_dynScaleY);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleY = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleY))->setValue(DEFAULT_X_SCALE);
|
|
|
|
_dynScaleYSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleYSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::setDynScaleZ(PUDynamicAttribute* dynScaleZ)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynScaleZ)
|
|
|
|
CC_SAFE_DELETE(_dynScaleZ);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleZ = dynScaleZ;
|
2015-02-11 18:14:22 +08:00
|
|
|
_dynScaleZSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::resetDynScaleZ(bool resetToDefault)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleZ);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleZ = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleZ))->setValue(DEFAULT_X_SCALE);
|
|
|
|
_dynScaleYSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleYSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::setDynScaleXYZ(PUDynamicAttribute* dynScaleXYZ)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_dynScaleXYZ)
|
|
|
|
CC_SAFE_DELETE(_dynScaleXYZ);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleXYZ = dynScaleXYZ;
|
2015-02-11 18:14:22 +08:00
|
|
|
_dynScaleXYZSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUScaleAffector::resetDynScaleXYZ(bool resetToDefault)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleXYZ);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleXYZ = new PUDynamicAttributeFixed();
|
2015-02-11 18:14:22 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleXYZ))->setValue(DEFAULT_XYZ_SCALE);
|
|
|
|
_dynScaleXYZSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleXYZSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PUScaleAffector::calculateScale(PUDynamicAttribute* dynScale, PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (!particle)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
if (_sinceStartSystem)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// If control points are used (curved type), the first value of each control point is seconds from the start of
|
|
|
|
// the system
|
|
|
|
return _dynamicAttributeHelper.calculate(
|
|
|
|
dynScale, (static_cast<PUParticleSystem3D*>(_particleSystem))->getTimeElapsedSinceStart());
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// If control points are used (curved type), the first value of each control point is the fraction of the
|
|
|
|
// particle lifetime [0..1]
|
2015-02-11 18:14:22 +08:00
|
|
|
return _dynamicAttributeHelper.calculate(dynScale, particle->timeFraction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUScaleAffector::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;
|
|
|
|
float ds = 0;
|
|
|
|
float width = 0;
|
|
|
|
float height = 0;
|
|
|
|
float depth = 0;
|
|
|
|
float dimension = 0; // Added in V1.4
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
if (_dynScaleXYZSet)
|
|
|
|
{
|
|
|
|
ds = calculateScale(_dynScaleXYZ, particle) * deltaTime * calculateAffectSpecialisationFactor(particle);
|
|
|
|
dimension = particle->width + ds * _affectorScale.x;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
width = dimension;
|
|
|
|
}
|
|
|
|
dimension = particle->height + ds * _affectorScale.y;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
height = dimension;
|
|
|
|
}
|
|
|
|
dimension = particle->depth + ds * _affectorScale.z;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
depth = dimension;
|
|
|
|
}
|
|
|
|
particle->setOwnDimensions(width, height, depth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_dynScaleXSet)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ds = calculateScale(_dynScaleX, particle) * deltaTime;
|
2015-02-11 18:14:22 +08:00
|
|
|
dimension = particle->width + ds * _affectorScale.x;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
width = dimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_dynScaleYSet)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ds = calculateScale(_dynScaleY, particle) * deltaTime;
|
2015-02-11 18:14:22 +08:00
|
|
|
dimension = particle->height + ds * _affectorScale.y;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
height = dimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_dynScaleZSet)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ds = calculateScale(_dynScaleZ, particle) * deltaTime;
|
2015-02-11 18:14:22 +08:00
|
|
|
dimension = particle->depth + ds * _affectorScale.z;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
depth = dimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
particle->setOwnDimensions(width, height, depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUScaleAffector* PUScaleAffector::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto psa = new PUScaleAffector();
|
2015-02-11 18:14:22 +08:00
|
|
|
psa->autorelease();
|
|
|
|
return psa;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUScaleAffector::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
|
|
|
PUScaleAffector* scaleAffector = static_cast<PUScaleAffector*>(affector);
|
2015-03-02 13:07:32 +08:00
|
|
|
scaleAffector->setDynScaleX(getDynScaleX()->clone());
|
|
|
|
scaleAffector->setDynScaleY(getDynScaleY()->clone());
|
|
|
|
scaleAffector->setDynScaleZ(getDynScaleZ()->clone());
|
|
|
|
scaleAffector->setDynScaleXYZ(getDynScaleXYZ()->clone());
|
2021-12-25 10:04:45 +08:00
|
|
|
scaleAffector->_dynScaleXSet = _dynScaleXSet;
|
|
|
|
scaleAffector->_dynScaleYSet = _dynScaleYSet;
|
|
|
|
scaleAffector->_dynScaleZSet = _dynScaleZSet;
|
|
|
|
scaleAffector->_dynScaleXYZSet = _dynScaleXYZSet;
|
2015-03-02 13:07:32 +08:00
|
|
|
scaleAffector->_sinceStartSystem = _sinceStartSystem;
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:13:47 +08:00
|
|
|
NS_CC_END
|