CCPUDoPlacementParticleEventHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "extensions/Particle3D/PU/CCPUDoPlacementParticleEventHandler.h"
  23. #include "extensions/Particle3D/PU/CCPUAffector.h"
  24. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  25. NS_CC_BEGIN
  26. // Constants
  27. const unsigned int PUDoPlacementParticleEventHandler::DEFAULT_NUMBER_OF_PARTICLES = 1;
  28. //-----------------------------------------------------------------------
  29. PUDoPlacementParticleEventHandler::PUDoPlacementParticleEventHandler(void) :
  30. PUEventHandler(),
  31. PUListener(),
  32. _numberOfParticles(DEFAULT_NUMBER_OF_PARTICLES),
  33. _system(0),
  34. _emitter(0),
  35. _found(false),
  36. _alwaysUsePosition(true),
  37. _baseParticle(0),
  38. _inheritPosition(true),
  39. _inheritDirection(false),
  40. _inheritOrientation(false),
  41. _inheritTimeToLive(false),
  42. _inheritMass(false),
  43. _inheritTextureCoordinate(false),
  44. _inheritColour(false),
  45. _inheritParticleWidth(false),
  46. _inheritParticleHeight(false),
  47. _inheritParticleDepth(false)
  48. {
  49. }
  50. //-----------------------------------------------------------------------
  51. PUDoPlacementParticleEventHandler::~PUDoPlacementParticleEventHandler(void)
  52. {
  53. // We cannot remove this listener from mTechnique, because it is undetermined whether the ParticleTechnique
  54. // still exist.
  55. }
  56. //-----------------------------------------------------------------------
  57. void PUDoPlacementParticleEventHandler::handle (PUParticleSystem3D* particleSystem, PUParticle3D* particle, float /*timeElapsed*/)
  58. {
  59. if (!particle)
  60. return;
  61. if (!_found)
  62. {
  63. auto system = particleSystem;
  64. auto emitter = system->getEmitter(_forceEmitterName);
  65. //ParticleTechnique* technique = particleTechnique;
  66. //ParticleEmitter* emitter = particleTechnique->getEmitter(_forceEmitterName);
  67. if (!emitter)
  68. {
  69. // Search all techniques in this ParticleSystem for an emitter with the correct name
  70. PUParticleSystem3D* parentSystem = particleSystem->getParentParticleSystem();
  71. if (parentSystem){
  72. auto children = parentSystem->getChildren();
  73. for(auto iter : children)
  74. {
  75. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  76. if (child){
  77. system = child;
  78. emitter = system->getEmitter(_forceEmitterName);
  79. if (emitter)
  80. {
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. if (emitter)
  88. {
  89. _system = system;
  90. _emitter = emitter;
  91. if (_system)
  92. {
  93. _system->addListener(this);
  94. }
  95. _found = true;
  96. }
  97. else
  98. {
  99. return;
  100. }
  101. }
  102. // Emit 1 or more particles
  103. if (_system)
  104. {
  105. _baseParticle = particle;
  106. _system->forceEmission(_emitter, _numberOfParticles);
  107. }
  108. _baseParticle = 0;
  109. }
  110. //-----------------------------------------------------------------------
  111. void PUDoPlacementParticleEventHandler::particleEmitted(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle)
  112. {
  113. if (!_baseParticle)
  114. return;
  115. if (particle && _emitter == particle->parentEmitter)
  116. {
  117. if (_inheritPosition)
  118. {
  119. #ifdef PU_PHYSICS
  120. // Do not assume that the contact point is always used if a physics engine is used.
  121. if (!_alwaysUsePosition && particle->physicsActor)
  122. {
  123. particle->position = _baseParticle->physicsActor->contactPoint; // Store the contact point to spawn new particles on that position.
  124. }
  125. else
  126. {
  127. particle->position = _baseParticle->position; // Store the particles' position to spawn new particles on that position.
  128. }
  129. #else
  130. particle->position = _baseParticle->position; // Store the particles' position to spawn new particles on that position.
  131. #endif // PU_PHYSICS
  132. particle->originalPosition = particle->position;
  133. }
  134. if (_inheritDirection)
  135. {
  136. particle->direction = _baseParticle->direction;
  137. particle->originalDirection = particle->direction;
  138. particle->originalDirectionLength = _baseParticle->originalDirectionLength;
  139. particle->originalScaledDirectionLength = _baseParticle->originalScaledDirectionLength;
  140. particle->originalVelocity = _baseParticle->originalVelocity;
  141. }
  142. if (_inheritOrientation)
  143. {
  144. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  145. {
  146. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  147. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  148. particle->orientation = _baseParticle->orientation;
  149. particle->originalOrientation = _baseParticle->originalOrientation;
  150. }
  151. }
  152. if (_inheritTimeToLive)
  153. {
  154. particle->timeToLive = _baseParticle->timeToLive;
  155. particle->totalTimeToLive = _baseParticle->totalTimeToLive;
  156. particle->timeFraction = _baseParticle->timeFraction;
  157. }
  158. if (_inheritMass)
  159. {
  160. particle->mass = _baseParticle->mass;
  161. }
  162. if (_inheritTextureCoordinate)
  163. {
  164. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  165. {
  166. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  167. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  168. particle->textureAnimationTimeStep = _baseParticle->textureAnimationTimeStep;
  169. particle->textureAnimationTimeStepCount = _baseParticle->textureAnimationTimeStepCount;
  170. particle->textureCoordsCurrent = _baseParticle->textureCoordsCurrent;
  171. particle->textureAnimationDirectionUp = _baseParticle->textureAnimationDirectionUp;
  172. }
  173. }
  174. if (_inheritColour)
  175. {
  176. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  177. {
  178. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  179. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  180. particle->color = _baseParticle->color;
  181. particle->originalColor = _baseParticle->originalColor;
  182. }
  183. }
  184. if (_inheritParticleWidth)
  185. {
  186. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  187. {
  188. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  189. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  190. particle->setOwnDimensions(_baseParticle->width, particle->height, particle->depth);
  191. }
  192. }
  193. if (_inheritParticleHeight)
  194. {
  195. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  196. {
  197. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  198. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  199. particle->setOwnDimensions(particle->width, _baseParticle->height, particle->depth);
  200. }
  201. }
  202. if (_inheritParticleDepth)
  203. {
  204. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  205. {
  206. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  207. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  208. particle->setOwnDimensions(particle->width, particle->height, _baseParticle->depth);
  209. }
  210. }
  211. }
  212. }
  213. void PUDoPlacementParticleEventHandler::particleExpired(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* /*particle*/)
  214. {}
  215. //-----------------------------------------------------------------------
  216. void PUDoPlacementParticleEventHandler::setForceEmitterName(const std::string& forceEmitterName)
  217. {
  218. _forceEmitterName = forceEmitterName;
  219. }
  220. //-----------------------------------------------------------------------
  221. PUEmitter* PUDoPlacementParticleEventHandler::getForceEmitter(void) const
  222. {
  223. return _emitter;
  224. }
  225. //-----------------------------------------------------------------------
  226. void PUDoPlacementParticleEventHandler::removeAsListener(void)
  227. {
  228. // Reset some values and remove this as a listener from the old technique.
  229. if (_system)
  230. {
  231. _system->removeListener(this);
  232. }
  233. _found = false;
  234. _emitter = 0;
  235. _system = 0;
  236. }
  237. PUDoPlacementParticleEventHandler* PUDoPlacementParticleEventHandler::create()
  238. {
  239. auto peh = new (std::nothrow) PUDoPlacementParticleEventHandler();
  240. peh->autorelease();
  241. return peh;
  242. }
  243. void PUDoPlacementParticleEventHandler::copyAttributesTo( PUEventHandler* eventHandler )
  244. {
  245. PUEventHandler::copyAttributesTo(eventHandler);
  246. PUDoPlacementParticleEventHandler* doPlacementParticleEventHandler = static_cast<PUDoPlacementParticleEventHandler*>(eventHandler);
  247. doPlacementParticleEventHandler->setForceEmitterName(_forceEmitterName);
  248. doPlacementParticleEventHandler->setNumberOfParticles(_numberOfParticles);
  249. doPlacementParticleEventHandler->_alwaysUsePosition = _alwaysUsePosition;
  250. doPlacementParticleEventHandler->_inheritPosition = _inheritPosition;
  251. doPlacementParticleEventHandler->_inheritDirection = _inheritDirection;
  252. doPlacementParticleEventHandler->_inheritOrientation = _inheritOrientation;
  253. doPlacementParticleEventHandler->_inheritTimeToLive = _inheritTimeToLive;
  254. doPlacementParticleEventHandler->_inheritMass = _inheritMass;
  255. doPlacementParticleEventHandler->_inheritTextureCoordinate = _inheritTextureCoordinate;
  256. doPlacementParticleEventHandler->_inheritColour = _inheritColour;
  257. doPlacementParticleEventHandler->_inheritParticleWidth = _inheritParticleWidth;
  258. doPlacementParticleEventHandler->_inheritParticleHeight = _inheritParticleHeight;
  259. doPlacementParticleEventHandler->_inheritParticleDepth = _inheritParticleDepth;
  260. }
  261. NS_CC_END