CCPURandomiser.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "CCPURandomiser.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const Vec3 PURandomiser::DEFAULT_MAX_DEVIATION(0, 0, 0);
  27. const float PURandomiser::DEFAULT_TIME_STEP = 0.0f;
  28. const bool PURandomiser::DEFAULT_RANDOM_DIRECTION = true;
  29. //-----------------------------------------------------------------------
  30. PURandomiser::PURandomiser(void) :
  31. PUAffector(),
  32. _maxDeviationX(DEFAULT_MAX_DEVIATION.x),
  33. _maxDeviationY(DEFAULT_MAX_DEVIATION.y),
  34. _maxDeviationZ(DEFAULT_MAX_DEVIATION.z),
  35. _timeSinceLastUpdate(0.0f),
  36. _timeStep(DEFAULT_TIME_STEP),
  37. _randomDirection(DEFAULT_RANDOM_DIRECTION),
  38. _update(true)
  39. {
  40. }
  41. PURandomiser::~PURandomiser( void )
  42. {
  43. }
  44. //-----------------------------------------------------------------------
  45. float PURandomiser::getMaxDeviationX(void) const
  46. {
  47. return _maxDeviationX;
  48. }
  49. //-----------------------------------------------------------------------
  50. void PURandomiser::setMaxDeviationX(float maxDeviationX)
  51. {
  52. _maxDeviationX = maxDeviationX;
  53. }
  54. //-----------------------------------------------------------------------
  55. float PURandomiser::getMaxDeviationY(void) const
  56. {
  57. return _maxDeviationY;
  58. }
  59. //-----------------------------------------------------------------------
  60. void PURandomiser::setMaxDeviationY(float maxDeviationY)
  61. {
  62. _maxDeviationY = maxDeviationY;
  63. }
  64. //-----------------------------------------------------------------------
  65. float PURandomiser::getMaxDeviationZ(void) const
  66. {
  67. return _maxDeviationZ;
  68. }
  69. //-----------------------------------------------------------------------
  70. void PURandomiser::setMaxDeviationZ(float maxDeviationZ)
  71. {
  72. _maxDeviationZ = maxDeviationZ;
  73. }
  74. //-----------------------------------------------------------------------
  75. float PURandomiser::getTimeStep(void) const
  76. {
  77. return _timeStep;
  78. }
  79. //-----------------------------------------------------------------------
  80. void PURandomiser::setTimeStep(float timeStep)
  81. {
  82. _timeStep = timeStep;
  83. _timeSinceLastUpdate = timeStep;
  84. }
  85. //-----------------------------------------------------------------------
  86. bool PURandomiser::isRandomDirection(void) const
  87. {
  88. return _randomDirection;
  89. }
  90. //-----------------------------------------------------------------------
  91. void PURandomiser::setRandomDirection(bool randomDirection)
  92. {
  93. _randomDirection = randomDirection;
  94. }
  95. //-----------------------------------------------------------------------
  96. void PURandomiser::preUpdateAffector(float deltaTime)
  97. {
  98. if (/*technique->getNumberOfEmittedParticles()*/static_cast<PUParticleSystem3D *>(_particleSystem)->getAliveParticleCount() > 0)
  99. {
  100. _timeSinceLastUpdate += deltaTime;
  101. if (_timeSinceLastUpdate > _timeStep)
  102. {
  103. _timeSinceLastUpdate -= _timeStep;
  104. _update = true;
  105. }
  106. }
  107. }
  108. //-----------------------------------------------------------------------
  109. void PURandomiser::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  110. {
  111. //for (auto iter : _particleSystem->getParticles())
  112. {
  113. //PUParticle3D *particle = iter;
  114. if (_update)
  115. {
  116. if (_randomDirection)
  117. {
  118. // Random direction: Change the direction after each update
  119. particle->direction.add(CCRANDOM_MINUS1_1() * _maxDeviationX,
  120. CCRANDOM_MINUS1_1() * _maxDeviationY,
  121. CCRANDOM_MINUS1_1() * _maxDeviationZ);
  122. }
  123. else
  124. {
  125. // Explicitly check on 'freezed', because it passes the techniques' validation.
  126. if (particle->isFreezed())
  127. return;
  128. // Random position: Add the position deviation after each update
  129. particle->position.add(CCRANDOM_MINUS1_1() * _maxDeviationX * _affectorScale.x,
  130. CCRANDOM_MINUS1_1() * _maxDeviationY * _affectorScale.y,
  131. CCRANDOM_MINUS1_1() * _maxDeviationZ * _affectorScale.z);
  132. }
  133. }
  134. }
  135. }
  136. //-----------------------------------------------------------------------
  137. void PURandomiser::postUpdateAffector(float /*deltaTime*/)
  138. {
  139. _update = false;
  140. }
  141. PURandomiser* PURandomiser::create()
  142. {
  143. auto pr = new (std::nothrow) PURandomiser();
  144. pr->autorelease();
  145. return pr;
  146. }
  147. void PURandomiser::copyAttributesTo( PUAffector* affector )
  148. {
  149. PUAffector::copyAttributesTo(affector);
  150. PURandomiser* randomiser = static_cast<PURandomiser*>(affector);
  151. randomiser->_maxDeviationX = _maxDeviationX;
  152. randomiser->_maxDeviationY = _maxDeviationY;
  153. randomiser->_maxDeviationZ = _maxDeviationZ;
  154. randomiser->setTimeStep(_timeStep); // Also sets time since last update to appropriate value
  155. randomiser->_randomDirection = _randomDirection;
  156. }
  157. NS_CC_END