CCPUTextureAnimator.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "CCPUTextureAnimator.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUTextureAnimator::DEFAULT_TIME_STEP = 0.0f;
  27. const unsigned short PUTextureAnimator::DEFAULT_TEXCOORDS_START = 0;
  28. const unsigned short PUTextureAnimator::DEFAULT_TEXCOORDS_END = 0;
  29. const PUTextureAnimator::TextureAnimationType PUTextureAnimator::DEFAULT_ANIMATION_TYPE = PUTextureAnimator::TAT_LOOP;
  30. const bool PUTextureAnimator::DEFAULT_START_RANDOM = true;
  31. //-----------------------------------------------------------------------
  32. PUTextureAnimator::PUTextureAnimator(void) :
  33. PUAffector(),
  34. _animationTimeStep(DEFAULT_TIME_STEP),
  35. _animationTimeStepCount(0.0f),
  36. _startRandom(DEFAULT_START_RANDOM),
  37. _animationTimeStepSet(false),
  38. _nextIndex(false),
  39. _textureAnimationType(DEFAULT_ANIMATION_TYPE),
  40. _textureCoordsStart(DEFAULT_TEXCOORDS_START),
  41. _textureCoordsEnd(DEFAULT_TEXCOORDS_END)
  42. {
  43. }
  44. //-----------------------------------------------------------------------
  45. PUTextureAnimator::~PUTextureAnimator(void)
  46. {
  47. }
  48. //-----------------------------------------------------------------------
  49. float PUTextureAnimator::getAnimationTimeStep(void) const
  50. {
  51. return _animationTimeStep;
  52. }
  53. //-----------------------------------------------------------------------
  54. void PUTextureAnimator::setAnimationTimeStep(float animationTimeStep)
  55. {
  56. _animationTimeStep = animationTimeStep;
  57. _animationTimeStepSet = true;
  58. }
  59. //-----------------------------------------------------------------------
  60. PUTextureAnimator::TextureAnimationType PUTextureAnimator::getTextureAnimationType(void) const
  61. {
  62. return _textureAnimationType;
  63. }
  64. //-----------------------------------------------------------------------
  65. void PUTextureAnimator::setTextureAnimationType(PUTextureAnimator::TextureAnimationType textureAnimationType)
  66. {
  67. _textureAnimationType = textureAnimationType;
  68. }
  69. //-----------------------------------------------------------------------
  70. unsigned short PUTextureAnimator::getTextureCoordsStart(void) const
  71. {
  72. return _textureCoordsStart;
  73. }
  74. //-----------------------------------------------------------------------
  75. void PUTextureAnimator::setTextureCoordsStart(unsigned short textureCoordsStart)
  76. {
  77. _textureCoordsStart = textureCoordsStart;
  78. }
  79. //-----------------------------------------------------------------------
  80. unsigned short PUTextureAnimator::getTextureCoordsEnd(void) const
  81. {
  82. return _textureCoordsEnd;
  83. }
  84. //-----------------------------------------------------------------------
  85. void PUTextureAnimator::setTextureCoordsEnd(unsigned short textureCoordsEnd)
  86. {
  87. _textureCoordsEnd = textureCoordsEnd;
  88. }
  89. //-----------------------------------------------------------------------
  90. bool PUTextureAnimator::isStartRandom(void) const
  91. {
  92. return _startRandom;
  93. }
  94. //-----------------------------------------------------------------------
  95. void PUTextureAnimator::setStartRandom(bool startRandom)
  96. {
  97. _startRandom = startRandom;
  98. }
  99. //-----------------------------------------------------------------------
  100. void PUTextureAnimator::initParticleForEmission(PUParticle3D* particle)
  101. {
  102. //// Only continue if the particle is a visual particle
  103. //if (particle->particleType != Particle::PT_VISUAL)
  104. // return;
  105. // Set first image
  106. if (_startRandom)
  107. {
  108. particle->textureCoordsCurrent = (unsigned short)cocos2d::random((float)_textureCoordsStart, (float)_textureCoordsEnd + 0.999f);
  109. }
  110. else
  111. {
  112. particle->textureCoordsCurrent = _textureCoordsStart;
  113. }
  114. // Calculate the animationTimeStep
  115. if (!_animationTimeStepSet)
  116. {
  117. // Set the animation time step for each particle
  118. switch(_textureAnimationType)
  119. {
  120. case TAT_LOOP:
  121. {
  122. particle->textureAnimationTimeStep = particle->timeToLive / (_textureCoordsEnd - _textureCoordsStart + 1);
  123. }
  124. break;
  125. case TAT_UP_DOWN:
  126. {
  127. particle->textureAnimationTimeStep = particle->timeToLive / (2 * (_textureCoordsEnd - _textureCoordsStart) + 1);
  128. }
  129. break;
  130. case TAT_RANDOM:
  131. {
  132. particle->textureAnimationTimeStep = particle->timeToLive;
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. //-----------------------------------------------------------------------
  139. void PUTextureAnimator::preUpdateAffector(float deltaTime)
  140. {
  141. // Determine the next texture coords index (global)
  142. if (_animationTimeStepSet)
  143. {
  144. _nextIndex = false;
  145. _animationTimeStepCount += deltaTime;
  146. if (_animationTimeStepCount > _animationTimeStep)
  147. {
  148. _animationTimeStepCount -= _animationTimeStep;
  149. _nextIndex = true;
  150. }
  151. }
  152. }
  153. //-----------------------------------------------------------------------
  154. void PUTextureAnimator::determineNextTextureCoords(PUParticle3D* visualParticle)
  155. {
  156. switch(_textureAnimationType)
  157. {
  158. case TAT_LOOP:
  159. {
  160. if (visualParticle->textureCoordsCurrent >= _textureCoordsEnd)
  161. {
  162. visualParticle->textureCoordsCurrent = _textureCoordsStart;
  163. }
  164. else
  165. {
  166. (visualParticle->textureCoordsCurrent)++;
  167. }
  168. }
  169. break;
  170. case TAT_UP_DOWN:
  171. {
  172. if (visualParticle->textureAnimationDirectionUp == true)
  173. {
  174. // Going up
  175. if (visualParticle->textureCoordsCurrent >= _textureCoordsEnd)
  176. {
  177. (visualParticle->textureCoordsCurrent)--;
  178. visualParticle->textureAnimationDirectionUp = false;
  179. }
  180. else
  181. {
  182. (visualParticle->textureCoordsCurrent)++;
  183. }
  184. }
  185. else
  186. {
  187. // Going down
  188. if (visualParticle->textureCoordsCurrent <= _textureCoordsStart)
  189. {
  190. (visualParticle->textureCoordsCurrent)++;
  191. visualParticle->textureAnimationDirectionUp = true;
  192. }
  193. else
  194. {
  195. (visualParticle->textureCoordsCurrent)--;
  196. }
  197. }
  198. }
  199. break;
  200. case TAT_RANDOM:
  201. {
  202. // Generate a random texcoord index
  203. visualParticle->textureCoordsCurrent = (unsigned short)cocos2d::random((float)_textureCoordsStart, (float)_textureCoordsEnd + 0.999f);
  204. }
  205. break;
  206. }
  207. }
  208. void PUTextureAnimator::updatePUAffector( PUParticle3D *particle, float deltaTime )
  209. {
  210. //// Only continue if the particle is a visual particle
  211. //if (particle->particleType != Particle::PT_VISUAL)
  212. // return;
  213. //for (auto iter : _particleSystem->getParticles())
  214. {
  215. //PUParticle3D *particle = iter;
  216. // Determine the next texture coords index
  217. if (_animationTimeStepSet)
  218. {
  219. if (_nextIndex)
  220. {
  221. // Use the global one for all particles
  222. determineNextTextureCoords(particle);
  223. }
  224. }
  225. else
  226. {
  227. particle->textureAnimationTimeStepCount += deltaTime;
  228. if (particle->textureAnimationTimeStepCount > particle->textureAnimationTimeStep)
  229. {
  230. particle->textureAnimationTimeStepCount -= particle->textureAnimationTimeStep;
  231. determineNextTextureCoords(particle);
  232. }
  233. }
  234. }
  235. }
  236. PUTextureAnimator* PUTextureAnimator::create()
  237. {
  238. auto pta = new (std::nothrow) PUTextureAnimator();
  239. pta->autorelease();
  240. return pta;
  241. }
  242. void PUTextureAnimator::copyAttributesTo( PUAffector* affector )
  243. {
  244. PUAffector::copyAttributesTo(affector);
  245. PUTextureAnimator* textureAnimator = static_cast<PUTextureAnimator*>(affector);
  246. textureAnimator->_animationTimeStep = _animationTimeStep;
  247. textureAnimator->_animationTimeStepSet = _animationTimeStepSet;
  248. textureAnimator->_textureAnimationType = _textureAnimationType;
  249. textureAnimator->_textureCoordsStart = _textureCoordsStart;
  250. textureAnimator->_textureCoordsEnd = _textureCoordsEnd;
  251. textureAnimator->_startRandom = _startRandom;
  252. }
  253. NS_CC_END