CCPUSlaveEmitter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUSlaveEmitter.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. #include "extensions/Particle3D/PU/CCPUSlaveBehaviour.h"
  25. NS_CC_BEGIN
  26. //-----------------------------------------------------------------------
  27. PUSlaveEmitter::PUSlaveEmitter(void) :
  28. PUEmitter(),
  29. PUListener(),
  30. _masterParticle(0),
  31. _masterTechniqueName(),
  32. _masterEmitterName(),
  33. _masterEmitterNameSet(false)
  34. {
  35. }
  36. //-----------------------------------------------------------------------
  37. const std::string& PUSlaveEmitter::getMasterTechniqueName(void) const
  38. {
  39. return _masterTechniqueName;
  40. }
  41. //-----------------------------------------------------------------------
  42. void PUSlaveEmitter::setMasterTechniqueName(const std::string& masterTechniqueName)
  43. {
  44. _masterTechniqueName = masterTechniqueName;
  45. }
  46. //-----------------------------------------------------------------------
  47. const std::string& PUSlaveEmitter::getMasterEmitterName(void) const
  48. {
  49. return _masterEmitterName;
  50. }
  51. //-----------------------------------------------------------------------
  52. void PUSlaveEmitter::setMasterEmitterName(const std::string& masterEmitterName)
  53. {
  54. _masterEmitterName = masterEmitterName;
  55. _masterEmitterNameSet = true;
  56. }
  57. //-----------------------------------------------------------------------
  58. void PUSlaveEmitter::particleEmitted(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle)
  59. {
  60. if (_masterEmitterNameSet && _masterEmitterName != particle->parentEmitter->getName())
  61. {
  62. // Ignore particle
  63. return;
  64. }
  65. /** Keep the data of the emitted particle from the master technique/emitter.
  66. Emission if a particle in this emitter may NOT be done in the main _update() method of the ParticleSystem,
  67. but only from here ( a slave afterall). That is why the emitter is enabled and disabled again.
  68. */
  69. _masterPosition = particle->position;
  70. _masterDirection = particle->direction;
  71. _masterParticle = particle;
  72. _isEnabled = true;
  73. static_cast<PUParticleSystem3D *>(_particleSystem)->forceEmission(this, 1); // Just emit one, to be in sync with the master.
  74. _isEnabled = false;
  75. }
  76. void PUSlaveEmitter::particleExpired(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* /*particle*/)
  77. {}
  78. //-----------------------------------------------------------------------
  79. void PUSlaveEmitter::initParticlePosition(PUParticle3D* particle)
  80. {
  81. // Remark: Don't take the orientation of the node into account, because the position of the master particle is leading.
  82. particle->position = _masterPosition;
  83. particle->originalPosition = particle->position;
  84. }
  85. //-----------------------------------------------------------------------
  86. void PUSlaveEmitter::initParticleDirection(PUParticle3D* particle)
  87. {
  88. particle->direction = _masterDirection;
  89. particle->originalDirection = particle->direction;
  90. particle->originalDirectionLength = particle->direction.length();
  91. // Make use of the opportunity to set the master particle in the behaviour object (if available)
  92. for (auto iter : particle->behaviours) {
  93. if (iter->getBehaviourType() == "Slave"){
  94. static_cast<PUSlaveBehaviour *>(iter)->masterParticle = _masterParticle;
  95. }
  96. }
  97. }
  98. //-----------------------------------------------------------------------
  99. void PUSlaveEmitter::prepare()
  100. {
  101. PUEmitter::prepare();
  102. PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D *>(_particleSystem)->getParentParticleSystem();
  103. if (system)
  104. {
  105. auto children = system->getChildren();
  106. for (auto it : children){
  107. if (it->getName() == _masterTechniqueName){
  108. static_cast<PUParticleSystem3D *>(it)->addListener(this);
  109. break;
  110. }
  111. }
  112. _isEnabled = false;
  113. }
  114. }
  115. //-----------------------------------------------------------------------
  116. void PUSlaveEmitter::unPrepare()
  117. {
  118. PUEmitter::unPrepare();
  119. PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D *>(_particleSystem)->getParentParticleSystem();
  120. if (system)
  121. {
  122. auto children = system->getChildren();
  123. for (auto it : children){
  124. if (it->getName() == _masterTechniqueName){
  125. static_cast<PUParticleSystem3D *>(it)->removeListener(this);
  126. break;
  127. }
  128. }
  129. }
  130. PUEmitter::unPrepare();
  131. }
  132. //-----------------------------------------------------------------------
  133. void PUSlaveEmitter::notifyStart (void)
  134. {
  135. PUEmitter::notifyStart();
  136. setEnabled(false);
  137. }
  138. PUSlaveEmitter* PUSlaveEmitter::create()
  139. {
  140. auto pe = new (std::nothrow) PUSlaveEmitter();
  141. pe->autorelease();
  142. return pe;
  143. }
  144. void PUSlaveEmitter::copyAttributesTo( PUEmitter* emitter )
  145. {
  146. PUEmitter::copyAttributesTo(emitter);
  147. PUSlaveEmitter* slaveEmitter = static_cast<PUSlaveEmitter*>(emitter);
  148. slaveEmitter->_masterTechniqueName = _masterTechniqueName;
  149. slaveEmitter->_masterEmitterName = _masterEmitterName;
  150. slaveEmitter->_masterEmitterNameSet = _masterEmitterNameSet;
  151. }
  152. PUSlaveEmitter* PUSlaveEmitter::clone()
  153. {
  154. auto be = PUSlaveEmitter::create();
  155. copyAttributesTo(be);
  156. return be;
  157. }
  158. NS_CC_END