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.
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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 "CCPUSlaveEmitter.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUSlaveBehaviour.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUSlaveEmitter::PUSlaveEmitter() :
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter(),
|
|
|
|
PUListener(),
|
2015-02-11 18:14:22 +08:00
|
|
|
_masterParticle(0),
|
|
|
|
_masterTechniqueName(),
|
|
|
|
_masterEmitterName(),
|
|
|
|
_masterEmitterNameSet(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
const std::string& PUSlaveEmitter::getMasterTechniqueName() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _masterTechniqueName;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::setMasterTechniqueName(const std::string& masterTechniqueName)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_masterTechniqueName = masterTechniqueName;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
const std::string& PUSlaveEmitter::getMasterEmitterName() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _masterEmitterName;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::setMasterEmitterName(const std::string& masterEmitterName)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_masterEmitterName = masterEmitterName;
|
|
|
|
_masterEmitterNameSet = true;
|
|
|
|
}
|
2015-03-02 13:07:32 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2016-11-16 09:48:37 +08:00
|
|
|
void PUSlaveEmitter::particleEmitted(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (_masterEmitterNameSet && _masterEmitterName != particle->parentEmitter->getName())
|
|
|
|
{
|
|
|
|
// Ignore particle
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Keep the data of the emitted particle from the master technique/emitter.
|
|
|
|
Emission if a particle in this emitter may NOT be done in the main _update() method of the ParticleSystem,
|
|
|
|
but only from here ( a slave afterall). That is why the emitter is enabled and disabled again.
|
|
|
|
*/
|
|
|
|
_masterPosition = particle->position;
|
|
|
|
_masterDirection = particle->direction;
|
|
|
|
_masterParticle = particle;
|
|
|
|
_isEnabled = true;
|
|
|
|
static_cast<PUParticleSystem3D *>(_particleSystem)->forceEmission(this, 1); // Just emit one, to be in sync with the master.
|
|
|
|
_isEnabled = false;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
|
|
|
|
void PUSlaveEmitter::particleExpired(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* /*particle*/)
|
|
|
|
{}
|
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::initParticlePosition(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Remark: Don't take the orientation of the node into account, because the position of the master particle is leading.
|
|
|
|
particle->position = _masterPosition;
|
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::initParticleDirection(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
particle->direction = _masterDirection;
|
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
particle->originalDirectionLength = particle->direction.length();
|
|
|
|
|
2015-03-02 13:07:32 +08:00
|
|
|
// Make use of the opportunity to set the master particle in the behaviour object (if available)
|
|
|
|
|
|
|
|
for (auto iter : particle->behaviours) {
|
|
|
|
if (iter->getBehaviourType() == "Slave"){
|
2015-03-05 13:24:06 +08:00
|
|
|
static_cast<PUSlaveBehaviour *>(iter)->masterParticle = _masterParticle;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::prepare()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::prepare();
|
2015-03-02 13:07:32 +08:00
|
|
|
PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D *>(_particleSystem)->getParentParticleSystem();
|
|
|
|
if (system)
|
|
|
|
{
|
|
|
|
auto children = system->getChildren();
|
|
|
|
for (auto it : children){
|
|
|
|
if (it->getName() == _masterTechniqueName){
|
|
|
|
static_cast<PUParticleSystem3D *>(it)->addListener(this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_isEnabled = false;
|
|
|
|
}
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::unPrepare()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::unPrepare();
|
2015-03-02 13:07:32 +08:00
|
|
|
PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D *>(_particleSystem)->getParentParticleSystem();
|
|
|
|
if (system)
|
|
|
|
{
|
|
|
|
auto children = system->getChildren();
|
|
|
|
for (auto it : children){
|
|
|
|
if (it->getName() == _masterTechniqueName){
|
|
|
|
static_cast<PUParticleSystem3D *>(it)->removeListener(this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::unPrepare();
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
void PUSlaveEmitter::notifyStart ()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::notifyStart();
|
2015-02-11 18:14:22 +08:00
|
|
|
setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUSlaveEmitter* PUSlaveEmitter::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto pe = new PUSlaveEmitter();
|
2015-02-11 18:14:22 +08:00
|
|
|
pe->autorelease();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUSlaveEmitter::copyAttributesTo( PUEmitter* emitter )
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::copyAttributesTo(emitter);
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUSlaveEmitter* slaveEmitter = static_cast<PUSlaveEmitter*>(emitter);
|
2015-03-02 13:07:32 +08:00
|
|
|
slaveEmitter->_masterTechniqueName = _masterTechniqueName;
|
|
|
|
slaveEmitter->_masterEmitterName = _masterEmitterName;
|
|
|
|
slaveEmitter->_masterEmitterNameSet = _masterEmitterNameSet;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUSlaveEmitter* PUSlaveEmitter::clone()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
auto be = PUSlaveEmitter::create();
|
2015-03-02 13:07:32 +08:00
|
|
|
copyAttributesTo(be);
|
|
|
|
return be;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
NS_CC_END
|