CCPUTextureRotator.cpp 6.5 KB

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