CCPUDoPlacementParticleEventHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "extensions/Particle3D/PU/CCPUDoPlacementParticleEventHandler.h"
  22. #include "extensions/Particle3D/PU/CCPUAffector.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const unsigned int PUDoPlacementParticleEventHandler::DEFAULT_NUMBER_OF_PARTICLES = 1;
  27. //-----------------------------------------------------------------------
  28. PUDoPlacementParticleEventHandler::PUDoPlacementParticleEventHandler(void) :
  29. PUEventHandler(),
  30. PUListener(),
  31. _numberOfParticles(DEFAULT_NUMBER_OF_PARTICLES),
  32. _system(0),
  33. _emitter(0),
  34. _found(false),
  35. _alwaysUsePosition(true),
  36. _baseParticle(0),
  37. _inheritPosition(true),
  38. _inheritDirection(false),
  39. _inheritOrientation(false),
  40. _inheritTimeToLive(false),
  41. _inheritMass(false),
  42. _inheritTextureCoordinate(false),
  43. _inheritColour(false),
  44. _inheritParticleWidth(false),
  45. _inheritParticleHeight(false),
  46. _inheritParticleDepth(false)
  47. {
  48. }
  49. //-----------------------------------------------------------------------
  50. PUDoPlacementParticleEventHandler::~PUDoPlacementParticleEventHandler(void)
  51. {
  52. // We cannot remove this listener from mTechnique, because it is undetermined whether the ParticleTechnique
  53. // still exist.
  54. }
  55. //-----------------------------------------------------------------------
  56. void PUDoPlacementParticleEventHandler::handle (PUParticleSystem3D* particleSystem, PUParticle3D* particle, float /*timeElapsed*/)
  57. {
  58. if (!particle)
  59. return;
  60. if (!_found)
  61. {
  62. auto system = particleSystem;
  63. auto emitter = system->getEmitter(_forceEmitterName);
  64. //ParticleTechnique* technique = particleTechnique;
  65. //ParticleEmitter* emitter = particleTechnique->getEmitter(_forceEmitterName);
  66. if (!emitter)
  67. {
  68. // Search all techniques in this ParticleSystem for an emitter with the correct name
  69. PUParticleSystem3D* parentSystem = particleSystem->getParentParticleSystem();
  70. if (parentSystem){
  71. auto children = parentSystem->getChildren();
  72. for(auto iter : children)
  73. {
  74. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  75. if (child){
  76. system = child;
  77. emitter = system->getEmitter(_forceEmitterName);
  78. if (emitter)
  79. {
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. if (emitter)
  87. {
  88. _system = system;
  89. _emitter = emitter;
  90. if (_system)
  91. {
  92. _system->addListener(this);
  93. }
  94. _found = true;
  95. }
  96. else
  97. {
  98. return;
  99. }
  100. }
  101. // Emit 1 or more particles
  102. if (_system)
  103. {
  104. _baseParticle = particle;
  105. _system->forceEmission(_emitter, _numberOfParticles);
  106. }
  107. _baseParticle = 0;
  108. }
  109. //-----------------------------------------------------------------------
  110. void PUDoPlacementParticleEventHandler::particleEmitted(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle)
  111. {
  112. if (!_baseParticle)
  113. return;
  114. if (particle && _emitter == particle->parentEmitter)
  115. {
  116. if (_inheritPosition)
  117. {
  118. #ifdef PU_PHYSICS
  119. // Do not assume that the contact point is always used if a physics engine is used.
  120. if (!_alwaysUsePosition && particle->physicsActor)
  121. {
  122. particle->position = _baseParticle->physicsActor->contactPoint; // Store the contact point to spawn new particles on that position.
  123. }
  124. else
  125. {
  126. particle->position = _baseParticle->position; // Store the particles' position to spawn new particles on that position.
  127. }
  128. #else
  129. particle->position = _baseParticle->position; // Store the particles' position to spawn new particles on that position.
  130. #endif // PU_PHYSICS
  131. particle->originalPosition = particle->position;
  132. }
  133. if (_inheritDirection)
  134. {
  135. particle->direction = _baseParticle->direction;
  136. particle->originalDirection = particle->direction;
  137. particle->originalDirectionLength = _baseParticle->originalDirectionLength;
  138. particle->originalScaledDirectionLength = _baseParticle->originalScaledDirectionLength;
  139. particle->originalVelocity = _baseParticle->originalVelocity;
  140. }
  141. if (_inheritOrientation)
  142. {
  143. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  144. {
  145. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  146. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  147. particle->orientation = _baseParticle->orientation;
  148. particle->originalOrientation = _baseParticle->originalOrientation;
  149. }
  150. }
  151. if (_inheritTimeToLive)
  152. {
  153. particle->timeToLive = _baseParticle->timeToLive;
  154. particle->totalTimeToLive = _baseParticle->totalTimeToLive;
  155. particle->timeFraction = _baseParticle->timeFraction;
  156. }
  157. if (_inheritMass)
  158. {
  159. particle->mass = _baseParticle->mass;
  160. }
  161. if (_inheritTextureCoordinate)
  162. {
  163. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  164. {
  165. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  166. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  167. particle->textureAnimationTimeStep = _baseParticle->textureAnimationTimeStep;
  168. particle->textureAnimationTimeStepCount = _baseParticle->textureAnimationTimeStepCount;
  169. particle->textureCoordsCurrent = _baseParticle->textureCoordsCurrent;
  170. particle->textureAnimationDirectionUp = _baseParticle->textureAnimationDirectionUp;
  171. }
  172. }
  173. if (_inheritColour)
  174. {
  175. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  176. {
  177. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  178. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  179. particle->color = _baseParticle->color;
  180. particle->originalColor = _baseParticle->originalColor;
  181. }
  182. }
  183. if (_inheritParticleWidth)
  184. {
  185. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  186. {
  187. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  188. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  189. particle->setOwnDimensions(_baseParticle->width, particle->height, particle->depth);
  190. }
  191. }
  192. if (_inheritParticleHeight)
  193. {
  194. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  195. {
  196. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  197. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  198. particle->setOwnDimensions(particle->width, _baseParticle->height, particle->depth);
  199. }
  200. }
  201. if (_inheritParticleDepth)
  202. {
  203. if (_baseParticle->particleType == PUParticle3D::PT_VISUAL && particle->particleType == PUParticle3D::PT_VISUAL)
  204. {
  205. //VisualParticle* visualBaseParticle = static_cast<VisualParticle*>(_baseParticle);
  206. //VisualParticle* visualParticle = static_cast<VisualParticle*>(particle);
  207. particle->setOwnDimensions(particle->width, particle->height, _baseParticle->depth);
  208. }
  209. }
  210. }
  211. }
  212. void PUDoPlacementParticleEventHandler::particleExpired(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* /*particle*/)
  213. {}
  214. //-----------------------------------------------------------------------
  215. void PUDoPlacementParticleEventHandler::setForceEmitterName(const std::string& forceEmitterName)
  216. {
  217. _forceEmitterName = forceEmitterName;
  218. }
  219. //-----------------------------------------------------------------------
  220. PUEmitter* PUDoPlacementParticleEventHandler::getForceEmitter(void) const
  221. {
  222. return _emitter;
  223. }
  224. //-----------------------------------------------------------------------
  225. void PUDoPlacementParticleEventHandler::removeAsListener(void)
  226. {
  227. // Reset some values and remove this as a listener from the old technique.
  228. if (_system)
  229. {
  230. _system->removeListener(this);
  231. }
  232. _found = false;
  233. _emitter = 0;
  234. _system = 0;
  235. }
  236. PUDoPlacementParticleEventHandler* PUDoPlacementParticleEventHandler::create()
  237. {
  238. auto peh = new (std::nothrow) PUDoPlacementParticleEventHandler();
  239. peh->autorelease();
  240. return peh;
  241. }
  242. void PUDoPlacementParticleEventHandler::copyAttributesTo( PUEventHandler* eventHandler )
  243. {
  244. PUEventHandler::copyAttributesTo(eventHandler);
  245. PUDoPlacementParticleEventHandler* doPlacementParticleEventHandler = static_cast<PUDoPlacementParticleEventHandler*>(eventHandler);
  246. doPlacementParticleEventHandler->setForceEmitterName(_forceEmitterName);
  247. doPlacementParticleEventHandler->setNumberOfParticles(_numberOfParticles);
  248. doPlacementParticleEventHandler->_alwaysUsePosition = _alwaysUsePosition;
  249. doPlacementParticleEventHandler->_inheritPosition = _inheritPosition;
  250. doPlacementParticleEventHandler->_inheritDirection = _inheritDirection;
  251. doPlacementParticleEventHandler->_inheritOrientation = _inheritOrientation;
  252. doPlacementParticleEventHandler->_inheritTimeToLive = _inheritTimeToLive;
  253. doPlacementParticleEventHandler->_inheritMass = _inheritMass;
  254. doPlacementParticleEventHandler->_inheritTextureCoordinate = _inheritTextureCoordinate;
  255. doPlacementParticleEventHandler->_inheritColour = _inheritColour;
  256. doPlacementParticleEventHandler->_inheritParticleWidth = _inheritParticleWidth;
  257. doPlacementParticleEventHandler->_inheritParticleHeight = _inheritParticleHeight;
  258. doPlacementParticleEventHandler->_inheritParticleDepth = _inheritParticleDepth;
  259. }
  260. NS_CC_END