CCPUJetAffector.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "CCPUJetAffector.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. // Constants
  25. const float PUJetAffector::DEFAULT_ACCELERATION = 1.0f;
  26. //-----------------------------------------------------------------------
  27. PUJetAffector::PUJetAffector (void) :
  28. PUAffector(),
  29. _scaled(0.0f)
  30. {
  31. _dynAcceleration = new (std::nothrow) PUDynamicAttributeFixed();
  32. (static_cast<PUDynamicAttributeFixed*>(_dynAcceleration))->setValue(DEFAULT_ACCELERATION);
  33. }
  34. //-----------------------------------------------------------------------
  35. PUJetAffector::~PUJetAffector (void)
  36. {
  37. if (!_dynAcceleration)
  38. return;
  39. CC_SAFE_DELETE(_dynAcceleration);
  40. _dynAcceleration = 0;
  41. }
  42. //-----------------------------------------------------------------------
  43. void PUJetAffector::setDynAcceleration(PUDynamicAttribute* dynAcceleration)
  44. {
  45. if (_dynAcceleration)
  46. CC_SAFE_DELETE(_dynAcceleration);
  47. _dynAcceleration = dynAcceleration;
  48. }
  49. void PUJetAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
  50. {
  51. // for (auto iter : _particleSystem->getParticles())
  52. {
  53. // PUParticle3D *particle = iter;
  54. _scaled = deltaTime * (_dynAcceleration->getValue(particle->timeFraction));
  55. if (particle->direction == Vec3::ZERO)
  56. {
  57. // Existing direction is zero, so use original direction
  58. particle->direction += (particle->originalDirection * _scaled);
  59. }
  60. else
  61. {
  62. particle->direction += (particle->direction * _scaled);
  63. }
  64. }
  65. }
  66. PUJetAffector* PUJetAffector::create()
  67. {
  68. auto pja = new (std::nothrow) PUJetAffector();
  69. pja->autorelease();
  70. return pja;
  71. }
  72. void PUJetAffector::copyAttributesTo( PUAffector* affector )
  73. {
  74. PUAffector::copyAttributesTo(affector);
  75. PUJetAffector* jetAffector = static_cast<PUJetAffector*>(affector);
  76. jetAffector->setDynAcceleration(getDynAcceleration()->clone());
  77. }
  78. NS_CC_END