CCPUTextureAnimator.cpp 9.4 KB

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