CCPUScaleVelocityAffector.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "CCPUScaleVelocityAffector.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUScaleVelocityAffector::DEFAULT_VELOCITY_SCALE = 1.0f;
  27. //-----------------------------------------------------------------------
  28. PUScaleVelocityAffector::PUScaleVelocityAffector(void) :
  29. PUAffector(),
  30. _sinceStartSystem(false),
  31. _stopAtFlip(false)
  32. {
  33. _dynScaleVelocity = new (std::nothrow) PUDynamicAttributeFixed();
  34. (static_cast<PUDynamicAttributeFixed*>(_dynScaleVelocity))->setValue(DEFAULT_VELOCITY_SCALE);
  35. }
  36. //-----------------------------------------------------------------------
  37. PUScaleVelocityAffector::~PUScaleVelocityAffector(void)
  38. {
  39. if (_dynScaleVelocity)
  40. {
  41. CC_SAFE_DELETE(_dynScaleVelocity);
  42. }
  43. }
  44. //-----------------------------------------------------------------------
  45. void PUScaleVelocityAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
  46. {
  47. //for (auto iter : _particleSystem->getParticles())
  48. {
  49. // PUParticle3D *particle = iter;
  50. float ds = 0;
  51. if (_sinceStartSystem)
  52. {
  53. // If control points are used (curved type), the first value of each control point is seconds from the start of the system
  54. ds = deltaTime * _dynamicAttributeHelper.calculate(_dynScaleVelocity, (static_cast<PUParticleSystem3D *>(_particleSystem))->getTimeElapsedSinceStart());
  55. }
  56. else
  57. {
  58. // If control points are used (curved type), the first value of each control point is the fraction of the particle lifetime [0..1]
  59. ds = deltaTime * _dynamicAttributeHelper.calculate(_dynScaleVelocity, particle->timeFraction);
  60. }
  61. float length = particle->direction.length(); // Use length for a better delta direction value
  62. Vec3 calculated = particle->direction;
  63. calculated.x += ds * (particle->direction.x / length);
  64. calculated.y += ds * (particle->direction.y / length);
  65. calculated.z += ds * (particle->direction.z / length);
  66. if (_stopAtFlip)
  67. {
  68. if ((calculated.x > 0.0f && particle->direction.x < 0.0f) ||
  69. (calculated.y > 0.0f && particle->direction.y < 0.0f) ||
  70. (calculated.z > 0.0f && particle->direction.z < 0.0f) ||
  71. (calculated.x < 0.0f && particle->direction.x > 0.0f) ||
  72. (calculated.y < 0.0f && particle->direction.y > 0.0f) ||
  73. (calculated.z < 0.0f && particle->direction.z > 0.0f))
  74. return;
  75. }
  76. particle->direction = calculated;
  77. }
  78. }
  79. //-----------------------------------------------------------------------
  80. void PUScaleVelocityAffector::setDynScaleVelocity(PUDynamicAttribute* dynScaleVelocity)
  81. {
  82. if (_dynScaleVelocity)
  83. CC_SAFE_DELETE(_dynScaleVelocity);
  84. _dynScaleVelocity = dynScaleVelocity;
  85. }
  86. //-----------------------------------------------------------------------
  87. void PUScaleVelocityAffector::resetDynScaleVelocity(bool resetToDefault)
  88. {
  89. if (resetToDefault)
  90. {
  91. CC_SAFE_DELETE(_dynScaleVelocity);
  92. _dynScaleVelocity = new (std::nothrow) PUDynamicAttributeFixed();
  93. (static_cast<PUDynamicAttributeFixed*>(_dynScaleVelocity))->setValue(DEFAULT_VELOCITY_SCALE);
  94. }
  95. }
  96. PUScaleVelocityAffector* PUScaleVelocityAffector::create()
  97. {
  98. auto psva = new (std::nothrow) PUScaleVelocityAffector();
  99. psva->autorelease();
  100. return psva;
  101. }
  102. void PUScaleVelocityAffector::copyAttributesTo( PUAffector* affector )
  103. {
  104. PUAffector::copyAttributesTo(affector);
  105. PUScaleVelocityAffector* scaleVelocityAffector = static_cast<PUScaleVelocityAffector*>(affector);
  106. scaleVelocityAffector->setDynScaleVelocity(getDynScaleVelocity()->clone());
  107. scaleVelocityAffector->_sinceStartSystem = _sinceStartSystem;
  108. scaleVelocityAffector->_stopAtFlip = _stopAtFlip;
  109. }
  110. NS_CC_END