CCPUPositionEmitter.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "CCPUPositionEmitter.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. // Constants
  25. const bool PUPositionEmitter::DEFAULT_RANDOMIZE = true;
  26. //-----------------------------------------------------------------------
  27. PUPositionEmitter::PUPositionEmitter(void) :
  28. PUEmitter(),
  29. _randomized(DEFAULT_RANDOMIZE),
  30. _index(0)
  31. {
  32. }
  33. //-----------------------------------------------------------------------
  34. bool PUPositionEmitter::isRandomized() const
  35. {
  36. return _randomized;
  37. }
  38. //-----------------------------------------------------------------------
  39. void PUPositionEmitter::setRandomized(bool randomized)
  40. {
  41. _randomized = randomized;
  42. }
  43. //-----------------------------------------------------------------------
  44. const std::vector<Vec3>& PUPositionEmitter::getPositions(void) const
  45. {
  46. return _positionList;
  47. }
  48. //-----------------------------------------------------------------------
  49. void PUPositionEmitter::addPosition(const Vec3& position)
  50. {
  51. _positionList.push_back(position);
  52. }
  53. //-----------------------------------------------------------------------
  54. void PUPositionEmitter::notifyStart(void)
  55. {
  56. PUEmitter::notifyStart();
  57. _index = 0;
  58. }
  59. //-----------------------------------------------------------------------
  60. void PUPositionEmitter::removeAllPositions(void)
  61. {
  62. _index = 0;
  63. _positionList.clear();
  64. }
  65. //-----------------------------------------------------------------------
  66. unsigned short PUPositionEmitter::calculateRequestedParticles(float timeElapsed)
  67. {
  68. // Fast rejection
  69. if (_positionList.empty())
  70. return 0;
  71. if (_randomized)
  72. {
  73. return PUEmitter::calculateRequestedParticles(timeElapsed);
  74. }
  75. else if (_index < _positionList.size())
  76. {
  77. unsigned short requested = PUEmitter::calculateRequestedParticles(timeElapsed);
  78. unsigned short size = static_cast<unsigned short>(_positionList.size() - _index);
  79. if (requested > size)
  80. {
  81. return size;
  82. }
  83. else
  84. {
  85. return requested;
  86. }
  87. }
  88. return 0;
  89. }
  90. //-----------------------------------------------------------------------
  91. void PUPositionEmitter::initParticlePosition(PUParticle3D* particle)
  92. {
  93. // Fast rejection
  94. if (_positionList.empty())
  95. return;
  96. /** Remark: Don't take the orientation of the node into account, because the positions shouldn't be affected by the rotated node.
  97. */
  98. if (_randomized)
  99. {
  100. size_t i = (size_t)(CCRANDOM_0_1() * (_positionList.size() - 1));
  101. particle->position = getDerivedPosition() + Vec3(_emitterScale.x * _positionList[i].x, _emitterScale.y * _positionList[i].y, _emitterScale.z * _positionList[i].z);
  102. }
  103. else if (_index < _positionList.size())
  104. {
  105. particle->position = getDerivedPosition() + Vec3(_emitterScale.x * _positionList[_index].x, _emitterScale.y * _positionList[_index].y, _emitterScale.z * _positionList[_index].z);
  106. _index++;
  107. }
  108. particle->originalPosition = particle->position;
  109. }
  110. PUPositionEmitter* PUPositionEmitter::create()
  111. {
  112. auto pe = new (std::nothrow) PUPositionEmitter();
  113. pe->autorelease();
  114. return pe;
  115. }
  116. void PUPositionEmitter::copyAttributesTo( PUEmitter* emitter )
  117. {
  118. PUEmitter::copyAttributesTo(emitter);
  119. PUPositionEmitter* positionEmitter = static_cast<PUPositionEmitter*>(emitter);
  120. positionEmitter->_randomized = _randomized;
  121. positionEmitter->_positionList = _positionList;
  122. }
  123. PUPositionEmitter* PUPositionEmitter::clone()
  124. {
  125. auto be = PUPositionEmitter::create();
  126. copyAttributesTo(be);
  127. return be;
  128. }
  129. NS_CC_END