CCPUTextureRotator.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "CCPUTextureRotator.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const bool PUTextureRotator::DEFAULT_USE_OWN_SPEED = false;
  27. const float PUTextureRotator::DEFAULT_ROTATION_SPEED = 10.0f;
  28. const float PUTextureRotator::DEFAULT_ROTATION = 0.0f;
  29. //-----------------------------------------------------------------------
  30. PUTextureRotator::PUTextureRotator(void) :
  31. PUAffector(),
  32. _useOwnRotationSpeed(DEFAULT_USE_OWN_SPEED),
  33. _scaledRotationSpeed(0.0f),
  34. _twoPiRad(float(2.0 * M_PI))
  35. {
  36. _dynRotation = new (std::nothrow) PUDynamicAttributeFixed();
  37. (static_cast<PUDynamicAttributeFixed*>(_dynRotation))->setValue(DEFAULT_ROTATION);
  38. _dynRotationSpeed = new (std::nothrow) PUDynamicAttributeFixed();
  39. (static_cast<PUDynamicAttributeFixed*>(_dynRotationSpeed))->setValue(DEFAULT_ROTATION_SPEED);
  40. }
  41. //-----------------------------------------------------------------------
  42. PUTextureRotator::~PUTextureRotator(void)
  43. {
  44. if (_dynRotation)
  45. {
  46. CC_SAFE_DELETE(_dynRotation);
  47. }
  48. if (_dynRotationSpeed)
  49. {
  50. CC_SAFE_DELETE(_dynRotationSpeed);
  51. }
  52. }
  53. //-----------------------------------------------------------------------
  54. bool PUTextureRotator::useOwnRotationSpeed (void) const
  55. {
  56. return _useOwnRotationSpeed;
  57. }
  58. //-----------------------------------------------------------------------
  59. void PUTextureRotator::setUseOwnRotationSpeed (bool useOwnRotationSpeed)
  60. {
  61. _useOwnRotationSpeed = useOwnRotationSpeed;
  62. }
  63. //-----------------------------------------------------------------------
  64. PUDynamicAttribute* PUTextureRotator::getRotation(void) const
  65. {
  66. return _dynRotation;
  67. }
  68. //-----------------------------------------------------------------------
  69. void PUTextureRotator::setRotation(PUDynamicAttribute* dynRotation)
  70. {
  71. if (_dynRotation)
  72. CC_SAFE_DELETE(_dynRotation);
  73. _dynRotation = dynRotation;
  74. }
  75. //-----------------------------------------------------------------------
  76. PUDynamicAttribute* PUTextureRotator::getRotationSpeed(void) const
  77. {
  78. return _dynRotationSpeed;
  79. }
  80. //-----------------------------------------------------------------------
  81. void PUTextureRotator::setRotationSpeed(PUDynamicAttribute* dynRotationSpeed)
  82. {
  83. if (_dynRotationSpeed)
  84. CC_SAFE_DELETE(_dynRotationSpeed);
  85. _dynRotationSpeed = dynRotationSpeed;
  86. }
  87. //-----------------------------------------------------------------------
  88. float PUTextureRotator::calculateRotation(void)
  89. {
  90. return _dynamicAttributeHelper.calculate(_dynRotation, (static_cast<PUParticleSystem3D *>(_particleSystem))->getTimeElapsedSinceStart());
  91. }
  92. //-----------------------------------------------------------------------
  93. float PUTextureRotator::calculateRotationSpeed(PUParticle3D* particle)
  94. {
  95. return _dynamicAttributeHelper.calculate(_dynRotationSpeed, particle->timeFraction);
  96. }
  97. //-----------------------------------------------------------------------
  98. void PUTextureRotator::initParticleForEmission(PUParticle3D* particle)
  99. {
  100. //// Only continue if the particle is a visual particle
  101. //if (particle->particleType != PUParticle3D::PT_VISUAL)
  102. // return;
  103. // Set initial random zRotation
  104. particle->zRotation = calculateRotation();
  105. //FIXME
  106. //if (particle->parentEmitter->getParentTechnique()->getRenderer())
  107. //{
  108. // // Assume that all parents exist. That must be the case otherwise particles are not emitted.
  109. // particle->parentEmitter->getParentTechnique()->getRenderer()->_notifyParticleZRotated();
  110. //}
  111. // Set the zRotationSpeed
  112. particle->zRotationSpeed = calculateRotationSpeed(particle);
  113. }
  114. //-----------------------------------------------------------------------
  115. void PUTextureRotator::updatePUAffector( PUParticle3D *particle, float deltaTime )
  116. {
  117. //for (auto iter : _particleSystem->getParticles())
  118. {
  119. //PUParticle3D *particle = iter;
  120. //// Only continue if the particle is a visual particle
  121. //if (particle->particleType != PUParticle3D::PT_VISUAL)
  122. // return;
  123. if (_useOwnRotationSpeed)
  124. {
  125. // Use scaled rotationspeed and adjust the speed according to the velocity
  126. _scaledRotationSpeed = particle->zRotationSpeed * deltaTime;
  127. }
  128. else
  129. {
  130. // Scale speed
  131. _scaledRotationSpeed = calculateRotationSpeed(particle) * deltaTime;
  132. }
  133. particle->zRotation += _scaledRotationSpeed;
  134. particle->zRotation = particle->zRotation > _twoPiRad ? particle->zRotation - _twoPiRad : particle->zRotation;
  135. //FIXME
  136. //if (particleTechnique->getRenderer())
  137. //{
  138. // particleTechnique->getRenderer()->_notifyParticleZRotated();
  139. //}
  140. }
  141. }
  142. PUTextureRotator* PUTextureRotator::create()
  143. {
  144. auto ptr = new (std::nothrow) PUTextureRotator();
  145. ptr->autorelease();
  146. return ptr;
  147. }
  148. void PUTextureRotator::copyAttributesTo( PUAffector* affector )
  149. {
  150. PUAffector::copyAttributesTo(affector);
  151. PUTextureRotator* textureRotator = static_cast<PUTextureRotator*>(affector);
  152. textureRotator->setRotation(getRotation()->clone());
  153. textureRotator->setRotationSpeed(getRotationSpeed()->clone());
  154. textureRotator->_useOwnRotationSpeed = _useOwnRotationSpeed;
  155. }
  156. NS_CC_END