2015-03-02 13:07:32 +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-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-02 13:07:32 +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-03-02 13:07:32 +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-03-02 13:07:32 +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 "extensions/Particle3D/PU/CCPUDoPlacementParticleEventHandler.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUAffector.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-03-02 13:07:32 +08:00
|
|
|
|
|
|
|
// Constants
|
2015-03-05 13:24:06 +08:00
|
|
|
const unsigned int PUDoPlacementParticleEventHandler::DEFAULT_NUMBER_OF_PARTICLES = 1;
|
2015-03-02 13:07:32 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUDoPlacementParticleEventHandler::PUDoPlacementParticleEventHandler()
|
|
|
|
: PUEventHandler()
|
|
|
|
, PUListener()
|
|
|
|
, _numberOfParticles(DEFAULT_NUMBER_OF_PARTICLES)
|
|
|
|
, _system(0)
|
|
|
|
, _emitter(0)
|
|
|
|
, _found(false)
|
|
|
|
, _alwaysUsePosition(true)
|
|
|
|
, _baseParticle(0)
|
|
|
|
, _inheritPosition(true)
|
|
|
|
, _inheritDirection(false)
|
|
|
|
, _inheritOrientation(false)
|
|
|
|
, _inheritTimeToLive(false)
|
|
|
|
, _inheritMass(false)
|
|
|
|
, _inheritTextureCoordinate(false)
|
|
|
|
, _inheritColour(false)
|
|
|
|
, _inheritParticleWidth(false)
|
|
|
|
, _inheritParticleHeight(false)
|
|
|
|
, _inheritParticleDepth(false)
|
|
|
|
{}
|
2015-03-02 13:07:32 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUDoPlacementParticleEventHandler::~PUDoPlacementParticleEventHandler()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// We cannot remove this listener from mTechnique, because it is undetermined whether the ParticleTechnique
|
2015-03-02 13:07:32 +08:00
|
|
|
// still exist.
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUDoPlacementParticleEventHandler::handle(PUParticleSystem3D* particleSystem,
|
|
|
|
PUParticle3D* particle,
|
|
|
|
float /*timeElapsed*/)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (!particle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_found)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto system = particleSystem;
|
2015-03-02 13:07:32 +08:00
|
|
|
auto emitter = system->getEmitter(_forceEmitterName);
|
2021-12-25 10:04:45 +08:00
|
|
|
// ParticleTechnique* technique = particleTechnique;
|
|
|
|
// ParticleEmitter* emitter = particleTechnique->getEmitter(_forceEmitterName);
|
2015-03-02 13:07:32 +08:00
|
|
|
if (!emitter)
|
|
|
|
{
|
|
|
|
// Search all techniques in this ParticleSystem for an emitter with the correct name
|
|
|
|
PUParticleSystem3D* parentSystem = particleSystem->getParentParticleSystem();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (parentSystem)
|
|
|
|
{
|
2015-03-02 13:07:32 +08:00
|
|
|
auto children = parentSystem->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto iter : children)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PUParticleSystem3D* child = dynamic_cast<PUParticleSystem3D*>(iter);
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
system = child;
|
2015-03-02 13:07:32 +08:00
|
|
|
emitter = system->getEmitter(_forceEmitterName);
|
|
|
|
if (emitter)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (emitter)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_system = system;
|
2015-03-02 13:07:32 +08:00
|
|
|
_emitter = emitter;
|
|
|
|
if (_system)
|
|
|
|
{
|
|
|
|
_system->addListener(this);
|
|
|
|
}
|
|
|
|
_found = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit 1 or more particles
|
|
|
|
if (_system)
|
|
|
|
{
|
|
|
|
_baseParticle = particle;
|
|
|
|
_system->forceEmission(_emitter, _numberOfParticles);
|
|
|
|
}
|
|
|
|
|
|
|
|
_baseParticle = 0;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2016-11-16 09:48:37 +08:00
|
|
|
void PUDoPlacementParticleEventHandler::particleEmitted(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (!_baseParticle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (particle && _emitter == particle->parentEmitter)
|
|
|
|
{
|
|
|
|
if (_inheritPosition)
|
|
|
|
{
|
|
|
|
#ifdef PU_PHYSICS
|
|
|
|
// Do not assume that the contact point is always used if a physics engine is used.
|
|
|
|
if (!_alwaysUsePosition && particle->physicsActor)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->position =
|
|
|
|
_baseParticle->physicsActor
|
|
|
|
->contactPoint; // Store the contact point to spawn new particles on that position.
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->position =
|
|
|
|
_baseParticle->position; // Store the particles' position to spawn new particles on that position.
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
#else
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->position =
|
|
|
|
_baseParticle->position; // Store the particles' position to spawn new particles on that position.
|
|
|
|
#endif // PU_PHYSICS
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
if (_inheritDirection)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->direction = _baseParticle->direction;
|
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
particle->originalDirectionLength = _baseParticle->originalDirectionLength;
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->originalScaledDirectionLength = _baseParticle->originalScaledDirectionLength;
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->originalVelocity = _baseParticle->originalVelocity;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
if (_inheritOrientation)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_baseParticle->particleType == PUParticle3D::PT_VISUAL &&
|
|
|
|
particle->particleType == PUParticle3D::PT_VISUAL)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
|
|
|
|
// VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
|
|
|
|
particle->orientation = _baseParticle->orientation;
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->originalOrientation = _baseParticle->originalOrientation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_inheritTimeToLive)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->timeToLive = _baseParticle->timeToLive;
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->totalTimeToLive = _baseParticle->totalTimeToLive;
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->timeFraction = _baseParticle->timeFraction;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
if (_inheritMass)
|
|
|
|
{
|
|
|
|
particle->mass = _baseParticle->mass;
|
|
|
|
}
|
|
|
|
if (_inheritTextureCoordinate)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_baseParticle->particleType == PUParticle3D::PT_VISUAL &&
|
|
|
|
particle->particleType == PUParticle3D::PT_VISUAL)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
|
|
|
|
// VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
|
|
|
|
particle->textureAnimationTimeStep = _baseParticle->textureAnimationTimeStep;
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->textureAnimationTimeStepCount = _baseParticle->textureAnimationTimeStepCount;
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->textureCoordsCurrent = _baseParticle->textureCoordsCurrent;
|
|
|
|
particle->textureAnimationDirectionUp = _baseParticle->textureAnimationDirectionUp;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_inheritColour)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_baseParticle->particleType == PUParticle3D::PT_VISUAL &&
|
|
|
|
particle->particleType == PUParticle3D::PT_VISUAL)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
|
|
|
|
// VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
|
|
|
|
particle->color = _baseParticle->color;
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->originalColor = _baseParticle->originalColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_inheritParticleWidth)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_baseParticle->particleType == PUParticle3D::PT_VISUAL &&
|
|
|
|
particle->particleType == PUParticle3D::PT_VISUAL)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
|
|
|
|
// VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->setOwnDimensions(_baseParticle->width, particle->height, particle->depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_inheritParticleHeight)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_baseParticle->particleType == PUParticle3D::PT_VISUAL &&
|
|
|
|
particle->particleType == PUParticle3D::PT_VISUAL)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
|
|
|
|
// VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->setOwnDimensions(particle->width, _baseParticle->height, particle->depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_inheritParticleDepth)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_baseParticle->particleType == PUParticle3D::PT_VISUAL &&
|
|
|
|
particle->particleType == PUParticle3D::PT_VISUAL)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
|
|
|
|
// VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
|
2015-03-02 13:07:32 +08:00
|
|
|
particle->setOwnDimensions(particle->width, particle->height, _baseParticle->depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUDoPlacementParticleEventHandler::particleExpired(PUParticleSystem3D* /*particleSystem*/,
|
|
|
|
PUParticle3D* /*particle*/)
|
2016-11-16 09:48:37 +08:00
|
|
|
{}
|
|
|
|
|
2015-03-02 13:07:32 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-26 23:26:34 +08:00
|
|
|
void PUDoPlacementParticleEventHandler::setForceEmitterName(std::string_view forceEmitterName)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
_forceEmitterName = forceEmitterName;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUEmitter* PUDoPlacementParticleEventHandler::getForceEmitter() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _emitter;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
void PUDoPlacementParticleEventHandler::removeAsListener()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
// Reset some values and remove this as a listener from the old technique.
|
|
|
|
if (_system)
|
|
|
|
{
|
|
|
|
_system->removeListener(this);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_found = false;
|
2015-03-02 13:07:32 +08:00
|
|
|
_emitter = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_system = 0;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUDoPlacementParticleEventHandler* PUDoPlacementParticleEventHandler::create()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto peh = new PUDoPlacementParticleEventHandler();
|
2015-03-02 13:07:32 +08:00
|
|
|
peh->autorelease();
|
|
|
|
return peh;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUDoPlacementParticleEventHandler::copyAttributesTo(PUEventHandler* eventHandler)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEventHandler::copyAttributesTo(eventHandler);
|
2021-12-25 10:04:45 +08:00
|
|
|
PUDoPlacementParticleEventHandler* doPlacementParticleEventHandler =
|
|
|
|
static_cast<PUDoPlacementParticleEventHandler*>(eventHandler);
|
2015-03-02 13:07:32 +08:00
|
|
|
doPlacementParticleEventHandler->setForceEmitterName(_forceEmitterName);
|
|
|
|
doPlacementParticleEventHandler->setNumberOfParticles(_numberOfParticles);
|
2021-12-25 10:04:45 +08:00
|
|
|
doPlacementParticleEventHandler->_alwaysUsePosition = _alwaysUsePosition;
|
|
|
|
doPlacementParticleEventHandler->_inheritPosition = _inheritPosition;
|
|
|
|
doPlacementParticleEventHandler->_inheritDirection = _inheritDirection;
|
|
|
|
doPlacementParticleEventHandler->_inheritOrientation = _inheritOrientation;
|
|
|
|
doPlacementParticleEventHandler->_inheritTimeToLive = _inheritTimeToLive;
|
|
|
|
doPlacementParticleEventHandler->_inheritMass = _inheritMass;
|
2015-03-02 13:07:32 +08:00
|
|
|
doPlacementParticleEventHandler->_inheritTextureCoordinate = _inheritTextureCoordinate;
|
2021-12-25 10:04:45 +08:00
|
|
|
doPlacementParticleEventHandler->_inheritColour = _inheritColour;
|
|
|
|
doPlacementParticleEventHandler->_inheritParticleWidth = _inheritParticleWidth;
|
|
|
|
doPlacementParticleEventHandler->_inheritParticleHeight = _inheritParticleHeight;
|
|
|
|
doPlacementParticleEventHandler->_inheritParticleDepth = _inheritParticleDepth;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|