CCPUSineForceAffector.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "CCPUSineForceAffector.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUSineForceAffector::DEFAULT_FREQ_MIN = 1.0f;
  27. const float PUSineForceAffector::DEFAULT_FREQ_MAX = 1.0f;
  28. //-----------------------------------------------------------------------
  29. PUSineForceAffector::PUSineForceAffector(void) :
  30. PUBaseForceAffector(),
  31. _angle(361),
  32. _frequencyMin(DEFAULT_FREQ_MIN),
  33. _frequencyMax(DEFAULT_FREQ_MAX),
  34. _frequency(1.0f)
  35. {
  36. }
  37. PUSineForceAffector::~PUSineForceAffector( void )
  38. {
  39. }
  40. //-----------------------------------------------------------------------
  41. void PUSineForceAffector::preUpdateAffector(float deltaTime)
  42. {
  43. // Scale by time
  44. _angle += _frequency * deltaTime;
  45. float sineValue = sin(_angle);
  46. _scaledVector = _forceVector * deltaTime * sineValue;
  47. if (_angle > M_PI * 2.0f)
  48. {
  49. _angle = 0.0f;
  50. if (_frequencyMin != _frequencyMax)
  51. {
  52. _frequency = cocos2d::random(_frequencyMin, _frequencyMax);
  53. }
  54. }
  55. }
  56. //-----------------------------------------------------------------------
  57. float PUSineForceAffector::getFrequencyMin() const
  58. {
  59. return _frequencyMin;
  60. }
  61. //-----------------------------------------------------------------------
  62. void PUSineForceAffector::setFrequencyMin(const float frequencyMin)
  63. {
  64. _frequencyMin = frequencyMin;
  65. if (frequencyMin > _frequencyMax)
  66. {
  67. _frequency = frequencyMin;
  68. }
  69. }
  70. //-----------------------------------------------------------------------
  71. float PUSineForceAffector::getFrequencyMax() const
  72. {
  73. return _frequencyMax;
  74. }
  75. //-----------------------------------------------------------------------
  76. void PUSineForceAffector::setFrequencyMax(const float frequencyMax)
  77. {
  78. _frequencyMax = frequencyMax;
  79. _frequency = frequencyMax;
  80. }
  81. //-----------------------------------------------------------------------
  82. void PUSineForceAffector::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  83. {
  84. //for (auto iter : _particleSystem->getParticles())
  85. {
  86. //PUParticle3D *particle = iter;
  87. // Affect the direction
  88. if (_forceApplication == FA_ADD)
  89. {
  90. particle->direction += _scaledVector;
  91. }
  92. else
  93. {
  94. particle->direction = (particle->direction + _forceVector) / 2;
  95. }
  96. }
  97. }
  98. PUSineForceAffector* PUSineForceAffector::create()
  99. {
  100. auto psfa = new (std::nothrow) PUSineForceAffector();
  101. psfa->autorelease();
  102. return psfa;
  103. }
  104. void PUSineForceAffector::copyAttributesTo( PUAffector* affector )
  105. {
  106. PUAffector::copyAttributesTo(affector);
  107. PUSineForceAffector* sineForceAffector = static_cast<PUSineForceAffector*>(affector);
  108. sineForceAffector->_frequencyMin = _frequencyMin;
  109. sineForceAffector->_frequencyMax = _frequencyMax;
  110. sineForceAffector->_frequency = _frequency;
  111. sineForceAffector->_angle = _angle;
  112. }
  113. NS_CC_END