CCPUOnTimeObserver.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "extensions/Particle3D/PU/CCPUOnTimeObserver.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. // Constants
  25. const float PUOnTimeObserver::DEFAULT_THRESHOLD = 0.0f;
  26. const bool PUOnTimeObserver::DEFAULT_SINCE_START_SYSTEM = false;
  27. static bool almostEquals(float a, float b, float epsilon = std::numeric_limits<float>::epsilon())
  28. {
  29. return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
  30. };
  31. //-----------------------------------------------------------------------
  32. PUOnTimeObserver::PUOnTimeObserver(void) : PUObserver(),
  33. _threshold(DEFAULT_THRESHOLD),
  34. _compare(CO_GREATER_THAN),
  35. _sinceStartSystem(DEFAULT_SINCE_START_SYSTEM)
  36. {
  37. };
  38. //-----------------------------------------------------------------------
  39. void PUOnTimeObserver::preUpdateObserver(float deltaTime)
  40. {
  41. // Call parent
  42. PUObserver::preUpdateObserver(deltaTime);
  43. // Also observe if there are no particles emitted, because some of the event handlers do not only
  44. // perform an action on a particle.
  45. if (static_cast<PUParticleSystem3D *>(_particleSystem)->getAliveParticleCount() <= 0)
  46. {
  47. handleObserve(0, deltaTime);
  48. }
  49. }
  50. //-----------------------------------------------------------------------
  51. bool PUOnTimeObserver::observe (PUParticle3D* particle, float /*timeElapsed*/)
  52. {
  53. if (_compare == CO_GREATER_THAN)
  54. {
  55. if (_sinceStartSystem)
  56. {
  57. // Validate whether time since start of the particle system > threshold
  58. return (_particleSystem->getTimeElapsedSinceStart() > _threshold);
  59. }
  60. else
  61. {
  62. // Validate whether time since start of the particle emission > threshold
  63. return (particle && (particle->totalTimeToLive - particle->timeToLive) > _threshold);
  64. }
  65. }
  66. else if (_compare == CO_LESS_THAN)
  67. {
  68. if (_sinceStartSystem)
  69. {
  70. // Validate whether time since start of the particle system < threshold
  71. return (_particleSystem->getTimeElapsedSinceStart() < _threshold);
  72. }
  73. else
  74. {
  75. // Validate whether time since start of the particle emission < threshold
  76. return (particle && (particle->totalTimeToLive - particle->timeToLive) < _threshold);
  77. }
  78. }
  79. else
  80. {
  81. // Equals
  82. if (_sinceStartSystem)
  83. {
  84. // Validate whether time since start of the particle system == threshold
  85. return almostEquals(_particleSystem->getTimeElapsedSinceStart(), _threshold, 0.01f);
  86. }
  87. else
  88. {
  89. // Validate whether time since start of the particle emission == threshold
  90. return particle && almostEquals((particle->totalTimeToLive - particle->timeToLive), _threshold, 0.01f);
  91. }
  92. }
  93. return false;
  94. }
  95. PUOnTimeObserver* PUOnTimeObserver::create()
  96. {
  97. auto pto = new (std::nothrow) PUOnTimeObserver();
  98. pto->autorelease();
  99. return pto;
  100. }
  101. void PUOnTimeObserver::copyAttributesTo( PUObserver* observer )
  102. {
  103. PUObserver::copyAttributesTo(observer);
  104. PUOnTimeObserver* onTimeObserver = static_cast<PUOnTimeObserver*>(observer);
  105. onTimeObserver->_threshold = _threshold;
  106. onTimeObserver->_compare = _compare;
  107. onTimeObserver->_sinceStartSystem = _sinceStartSystem;
  108. }
  109. NS_CC_END