CCPUScaleAffector.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "CCPUScaleAffector.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUScaleAffector::DEFAULT_X_SCALE = 1.0f;
  27. const float PUScaleAffector::DEFAULT_Y_SCALE = 1.0f;
  28. const float PUScaleAffector::DEFAULT_Z_SCALE = 1.0f;
  29. const float PUScaleAffector::DEFAULT_XYZ_SCALE = 1.0f;
  30. //-----------------------------------------------------------------------
  31. PUScaleAffector::PUScaleAffector(void) :
  32. PUAffector(),
  33. _dynScaleXSet(false),
  34. _dynScaleYSet(false),
  35. _dynScaleZSet(false),
  36. _dynScaleXYZSet(false),
  37. _sinceStartSystem(false)
  38. {
  39. _dynScaleX = new (std::nothrow) PUDynamicAttributeFixed();
  40. _dynScaleY = new (std::nothrow) PUDynamicAttributeFixed();
  41. _dynScaleZ = new (std::nothrow) PUDynamicAttributeFixed();
  42. _dynScaleXYZ = new (std::nothrow) PUDynamicAttributeFixed();
  43. (static_cast<PUDynamicAttributeFixed*>(_dynScaleX))->setValue(DEFAULT_X_SCALE);
  44. (static_cast<PUDynamicAttributeFixed*>(_dynScaleY))->setValue(DEFAULT_Y_SCALE);
  45. (static_cast<PUDynamicAttributeFixed*>(_dynScaleZ))->setValue(DEFAULT_Z_SCALE);
  46. (static_cast<PUDynamicAttributeFixed*>(_dynScaleXYZ))->setValue(DEFAULT_XYZ_SCALE);
  47. _latestTimeElapsed = 1.0f;
  48. }
  49. //-----------------------------------------------------------------------
  50. PUScaleAffector::~PUScaleAffector(void)
  51. {
  52. if (_dynScaleX)
  53. {
  54. CC_SAFE_DELETE(_dynScaleX);
  55. }
  56. if (_dynScaleY)
  57. {
  58. CC_SAFE_DELETE(_dynScaleY);
  59. }
  60. if (_dynScaleZ)
  61. {
  62. CC_SAFE_DELETE(_dynScaleZ);
  63. }
  64. if (_dynScaleXYZ)
  65. {
  66. CC_SAFE_DELETE(_dynScaleXYZ);
  67. }
  68. }
  69. //-----------------------------------------------------------------------
  70. void PUScaleAffector::setDynScaleX(PUDynamicAttribute* dynScaleX)
  71. {
  72. if (_dynScaleX)
  73. CC_SAFE_DELETE(_dynScaleX);
  74. _dynScaleX = dynScaleX;
  75. _dynScaleXSet = true;
  76. }
  77. //-----------------------------------------------------------------------
  78. void PUScaleAffector::resetDynScaleX(bool resetToDefault)
  79. {
  80. if (resetToDefault)
  81. {
  82. CC_SAFE_DELETE(_dynScaleX);
  83. _dynScaleX = new (std::nothrow) PUDynamicAttributeFixed();
  84. (static_cast<PUDynamicAttributeFixed*>(_dynScaleX))->setValue(DEFAULT_X_SCALE);
  85. _dynScaleXSet = false;
  86. }
  87. else
  88. {
  89. _dynScaleXSet = true;
  90. }
  91. }
  92. //-----------------------------------------------------------------------
  93. void PUScaleAffector::setDynScaleY(PUDynamicAttribute* dynScaleY)
  94. {
  95. if (_dynScaleY)
  96. CC_SAFE_DELETE(_dynScaleY);
  97. _dynScaleY = dynScaleY;
  98. _dynScaleYSet = true;
  99. }
  100. //-----------------------------------------------------------------------
  101. void PUScaleAffector::resetDynScaleY(bool resetToDefault)
  102. {
  103. if (resetToDefault)
  104. {
  105. CC_SAFE_DELETE(_dynScaleY);
  106. _dynScaleY = new (std::nothrow) PUDynamicAttributeFixed();
  107. (static_cast<PUDynamicAttributeFixed*>(_dynScaleY))->setValue(DEFAULT_X_SCALE);
  108. _dynScaleYSet = false;
  109. }
  110. else
  111. {
  112. _dynScaleYSet = true;
  113. }
  114. }
  115. //-----------------------------------------------------------------------
  116. void PUScaleAffector::setDynScaleZ(PUDynamicAttribute* dynScaleZ)
  117. {
  118. if (_dynScaleZ)
  119. CC_SAFE_DELETE(_dynScaleZ);
  120. _dynScaleZ = dynScaleZ;
  121. _dynScaleZSet = true;
  122. }
  123. //-----------------------------------------------------------------------
  124. void PUScaleAffector::resetDynScaleZ(bool resetToDefault)
  125. {
  126. if (resetToDefault)
  127. {
  128. CC_SAFE_DELETE(_dynScaleZ);
  129. _dynScaleZ = new (std::nothrow) PUDynamicAttributeFixed();
  130. (static_cast<PUDynamicAttributeFixed*>(_dynScaleZ))->setValue(DEFAULT_X_SCALE);
  131. _dynScaleYSet = false;
  132. }
  133. else
  134. {
  135. _dynScaleYSet = true;
  136. }
  137. }
  138. //-----------------------------------------------------------------------
  139. void PUScaleAffector::setDynScaleXYZ(PUDynamicAttribute* dynScaleXYZ)
  140. {
  141. if (_dynScaleXYZ)
  142. CC_SAFE_DELETE(_dynScaleXYZ);
  143. _dynScaleXYZ = dynScaleXYZ;
  144. _dynScaleXYZSet = true;
  145. }
  146. //-----------------------------------------------------------------------
  147. void PUScaleAffector::resetDynScaleXYZ(bool resetToDefault)
  148. {
  149. if (resetToDefault)
  150. {
  151. CC_SAFE_DELETE(_dynScaleXYZ);
  152. _dynScaleXYZ = new (std::nothrow) PUDynamicAttributeFixed();
  153. (static_cast<PUDynamicAttributeFixed*>(_dynScaleXYZ))->setValue(DEFAULT_XYZ_SCALE);
  154. _dynScaleXYZSet = false;
  155. }
  156. else
  157. {
  158. _dynScaleXYZSet = true;
  159. }
  160. }
  161. //-----------------------------------------------------------------------
  162. float PUScaleAffector::calculateScale(PUDynamicAttribute* dynScale, PUParticle3D* particle)
  163. {
  164. if (!particle)
  165. return 0.0f;
  166. if (_sinceStartSystem)
  167. {
  168. // If control points are used (curved type), the first value of each control point is seconds from the start of the system
  169. return _dynamicAttributeHelper.calculate(dynScale, (static_cast<PUParticleSystem3D *>(_particleSystem))->getTimeElapsedSinceStart());
  170. }
  171. else
  172. {
  173. // If control points are used (curved type), the first value of each control point is the fraction of the particle lifetime [0..1]
  174. return _dynamicAttributeHelper.calculate(dynScale, particle->timeFraction);
  175. }
  176. }
  177. void PUScaleAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
  178. {
  179. //// Only continue if the particle is a visual particle
  180. //if (particle->particleType != Particle::PT_VISUAL)
  181. // return;
  182. //for (auto iter : _particleSystem->getParticles())
  183. {
  184. //PUParticle3D *particle = iter;
  185. float ds = 0;
  186. float width = 0;
  187. float height = 0;
  188. float depth = 0;
  189. float dimension = 0; // Added in V1.4
  190. if (_dynScaleXYZSet)
  191. {
  192. ds = calculateScale(_dynScaleXYZ, particle) * deltaTime * calculateAffectSpecialisationFactor(particle);
  193. dimension = particle->width + ds * _affectorScale.x;
  194. if (dimension > 0)
  195. {
  196. width = dimension;
  197. }
  198. dimension = particle->height + ds * _affectorScale.y;
  199. if (dimension > 0)
  200. {
  201. height = dimension;
  202. }
  203. dimension = particle->depth + ds * _affectorScale.z;
  204. if (dimension > 0)
  205. {
  206. depth = dimension;
  207. }
  208. particle->setOwnDimensions(width, height, depth);
  209. }
  210. else
  211. {
  212. if (_dynScaleXSet)
  213. {
  214. ds = calculateScale(_dynScaleX, particle) * deltaTime;
  215. dimension = particle->width + ds * _affectorScale.x;
  216. if (dimension > 0)
  217. {
  218. width = dimension;
  219. }
  220. }
  221. if (_dynScaleYSet)
  222. {
  223. ds = calculateScale(_dynScaleY, particle) * deltaTime;
  224. dimension = particle->height + ds * _affectorScale.y;
  225. if (dimension > 0)
  226. {
  227. height = dimension;
  228. }
  229. }
  230. if (_dynScaleZSet)
  231. {
  232. ds = calculateScale(_dynScaleZ, particle) * deltaTime;
  233. dimension = particle->depth + ds * _affectorScale.z;
  234. if (dimension > 0)
  235. {
  236. depth = dimension;
  237. }
  238. }
  239. particle->setOwnDimensions(width, height, depth);
  240. }
  241. }
  242. }
  243. PUScaleAffector* PUScaleAffector::create()
  244. {
  245. auto psa = new (std::nothrow) PUScaleAffector();
  246. psa->autorelease();
  247. return psa;
  248. }
  249. void PUScaleAffector::copyAttributesTo( PUAffector* affector )
  250. {
  251. PUAffector::copyAttributesTo(affector);
  252. PUScaleAffector* scaleAffector = static_cast<PUScaleAffector*>(affector);
  253. scaleAffector->setDynScaleX(getDynScaleX()->clone());
  254. scaleAffector->setDynScaleY(getDynScaleY()->clone());
  255. scaleAffector->setDynScaleZ(getDynScaleZ()->clone());
  256. scaleAffector->setDynScaleXYZ(getDynScaleXYZ()->clone());
  257. scaleAffector->_dynScaleXSet = _dynScaleXSet;
  258. scaleAffector->_dynScaleYSet = _dynScaleYSet;
  259. scaleAffector->_dynScaleZSet = _dynScaleZSet;
  260. scaleAffector->_dynScaleXYZSet = _dynScaleXYZSet;
  261. scaleAffector->_sinceStartSystem = _sinceStartSystem;
  262. }
  263. NS_CC_END