CCPURandomiser.cpp 6.0 KB

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