2015-03-02 13:07:32 +08:00
|
|
|
/****************************************************************************
|
2015-03-02 16:05:26 +08:00
|
|
|
Copyright (C) 2013 Henry van Merode. All rights reserved.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-02 13:07:32 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-02 13:07:32 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-02 13:07:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-02 13:07:32 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
#include "extensions/Particle3D/PU/CCPUObserver.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUEventHandler.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUEventHandlerManager.h"
|
2015-03-02 13:07:32 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// Constants
|
2021-12-25 10:04:45 +08:00
|
|
|
const bool PUObserver::DEFAULT_ENABLED = true;
|
2015-03-05 13:24:06 +08:00
|
|
|
const PUParticle3D::ParticleType PUObserver::DEFAULT_PARTICLE_TYPE = PUParticle3D::PT_VISUAL;
|
2021-12-25 10:04:45 +08:00
|
|
|
const float PUObserver::DEFAULT_INTERVAL = 0.05f;
|
|
|
|
const bool PUObserver::DEFAULT_UNTIL_EVENT = false;
|
2015-03-02 13:07:32 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUObserver::PUObserver()
|
|
|
|
: _particleSystem(nullptr)
|
|
|
|
, _enabled(DEFAULT_ENABLED)
|
|
|
|
, _originalEnabled(DEFAULT_ENABLED)
|
|
|
|
, _originalEnabledSet(false)
|
|
|
|
, _observe(true)
|
|
|
|
, _observeUntilEvent(DEFAULT_UNTIL_EVENT)
|
|
|
|
, _eventHandlersExecuted(false)
|
|
|
|
, _observerScale(Vec3::ONE)
|
|
|
|
, _particleTypeToObserve(DEFAULT_PARTICLE_TYPE)
|
|
|
|
, _particleTypeToObserveSet(false)
|
|
|
|
, _observerInterval(DEFAULT_INTERVAL)
|
|
|
|
, _observerIntervalRemainder(0.0)
|
|
|
|
, _observerIntervalSet(false)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// mAliasType = AT_OBSERVER;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
PUObserver::~PUObserver()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
destroyAllEventHandlers();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::notifyStart()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
_eventHandlersExecuted = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
_observe = true;
|
2015-03-02 13:07:32 +08:00
|
|
|
setEnabled(_originalEnabled);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
bool PUObserver::isEnabled() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
bool PUObserver::_getOriginalEnabled() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _originalEnabled;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::setEnabled(bool enabled)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
_enabled = enabled;
|
|
|
|
if (!_originalEnabledSet)
|
|
|
|
{
|
|
|
|
// Only one time is permitted
|
2021-12-25 10:04:45 +08:00
|
|
|
_originalEnabled = enabled;
|
2015-03-02 13:07:32 +08:00
|
|
|
_originalEnabledSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
void PUObserver::_resetEnabled()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
_originalEnabledSet = false;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PUObserver::getObserverInterval() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _observerInterval;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::setObserverInterval(float observerInterval)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_observerInterval = observerInterval;
|
2015-03-02 13:07:32 +08:00
|
|
|
_observerIntervalSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
bool PUObserver::getObserveUntilEvent() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _observeUntilEvent;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::setObserveUntilEvent(bool observeUntilEvent)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
_observeUntilEvent = observeUntilEvent;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::notifyRescaled(const Vec3& scale)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
_observerScale = scale;
|
|
|
|
|
|
|
|
if (_eventHandlers.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParticleEventHandlerConstIterator it;
|
|
|
|
ParticleEventHandlerConstIterator itEnd = _eventHandlers.end();
|
|
|
|
for (it = _eventHandlers.begin(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
(*it)->notifyRescaled(scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-26 23:26:34 +08:00
|
|
|
PUEventHandler* PUObserver::createEventHandler(std::string_view eventHandlerType)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEventHandler* eventHandler = PUEventHandlerManager::Instance()->createEventHandler(eventHandlerType);
|
2015-03-02 13:07:32 +08:00
|
|
|
addEventHandler(eventHandler);
|
|
|
|
return eventHandler;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::addEventHandler(PUEventHandler* eventHandler)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
eventHandler->retain();
|
|
|
|
_eventHandlers.push_back(eventHandler);
|
|
|
|
eventHandler->setParentObserver(this);
|
|
|
|
eventHandler->notifyRescaled(_observerScale);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::removeEventHandler(PUEventHandler* eventHandler)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
ParticleEventHandlerIterator it;
|
|
|
|
for (it = _eventHandlers.begin(); it != _eventHandlers.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == eventHandler)
|
|
|
|
{
|
|
|
|
// Remove it
|
|
|
|
(*it)->release();
|
|
|
|
_eventHandlers.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventHandler->setParentObserver(0);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUEventHandler* PUObserver::getEventHandler(size_t index) const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
CCASSERT(index < _eventHandlers.size(), "EventHandler index out of bounds!");
|
|
|
|
return _eventHandlers[index];
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-26 23:26:34 +08:00
|
|
|
PUEventHandler* PUObserver::getEventHandler(std::string_view eventHandlerName) const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (eventHandlerName.empty())
|
2016-06-23 11:39:23 +08:00
|
|
|
return nullptr;
|
2015-03-02 13:07:32 +08:00
|
|
|
|
|
|
|
ParticleEventHandlerConstIterator it;
|
|
|
|
ParticleEventHandlerConstIterator itEnd = _eventHandlers.end();
|
|
|
|
for (it = _eventHandlers.begin(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
if ((*it)->getName() == eventHandlerName)
|
|
|
|
{
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:39:23 +08:00
|
|
|
return nullptr;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
size_t PUObserver::getNumEventHandlers() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _eventHandlers.size();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::destroyEventHandler(PUEventHandler* eventHandler)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
CCASSERT(eventHandler, "EventHandler is null!");
|
|
|
|
ParticleEventHandlerIterator it;
|
|
|
|
for (it = _eventHandlers.begin(); it != _eventHandlers.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == eventHandler)
|
|
|
|
{
|
2015-12-11 23:37:58 +08:00
|
|
|
// Destroy it
|
2021-12-25 10:04:45 +08:00
|
|
|
// ParticleSystemManager::getSingletonPtr()->destroyEventHandler(*it);
|
2015-03-02 13:07:32 +08:00
|
|
|
(*it)->release();
|
|
|
|
_eventHandlers.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::destroyEventHandler(size_t index)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
destroyEventHandler(getEventHandler(index));
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::destroyAllEventHandlers()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
ParticleEventHandlerIterator it;
|
|
|
|
for (it = _eventHandlers.begin(); it != _eventHandlers.end(); ++it)
|
|
|
|
{
|
|
|
|
(*it)->release();
|
2021-12-25 10:04:45 +08:00
|
|
|
// ParticleSystemManager::getSingletonPtr()->destroyEventHandler(*it);
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
_eventHandlers.clear();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUObserver::setParticleTypeToObserve(const PUParticle3D::ParticleType particleTypeToObserve)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_particleTypeToObserve = particleTypeToObserve;
|
2015-03-02 13:07:32 +08:00
|
|
|
_particleTypeToObserveSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::handleObserve(PUParticle3D* particle, float timeElapsed)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (_enabled && _observe)
|
|
|
|
{
|
|
|
|
if (_observeUntilEvent && _eventHandlersExecuted)
|
|
|
|
{
|
|
|
|
// Don't continue if mObserveUntilEvent is set and the event handlers are already called once.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (observe(particle, timeElapsed))
|
|
|
|
{
|
|
|
|
// Handle the event
|
2021-12-25 10:04:45 +08:00
|
|
|
handleEvent(particle, timeElapsed);
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::handleEvent(PUParticle3D* particle, float timeElapsed)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (_eventHandlers.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParticleEventHandlerConstIterator it;
|
|
|
|
ParticleEventHandlerConstIterator itEnd = _eventHandlers.end();
|
|
|
|
for (it = _eventHandlers.begin(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
(*it)->handle(_particleSystem, particle, timeElapsed);
|
|
|
|
}
|
|
|
|
_eventHandlersExecuted = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
bool PUObserver::isParticleTypeToObserveSet() const
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
return _particleTypeToObserveSet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::preUpdateObserver(float deltaTime)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (!_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_observerIntervalSet)
|
|
|
|
{
|
|
|
|
_observerIntervalRemainder -= deltaTime;
|
|
|
|
if (_observerIntervalRemainder < 0)
|
|
|
|
{
|
|
|
|
_observerIntervalRemainder += _observerInterval;
|
|
|
|
_observe = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_observe = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::updateObserver(PUParticle3D* particle, float deltaTime, bool firstParticle)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
if (!_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Call the _firstParticle() function if the first particle in the update loop is encountered.
|
|
|
|
if (firstParticle)
|
|
|
|
{
|
|
|
|
firstParticleUpdate(particle, deltaTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_particleTypeToObserveSet && particle->particleType != _particleTypeToObserve)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Observe
|
|
|
|
handleObserve(particle, deltaTime);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::postUpdateObserver(float /*deltaTime*/) {}
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::firstParticleUpdate(PUParticle3D* /*particle*/, float /*deltaTime*/) {}
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUObserver::copyAttributesTo(PUObserver* observer)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
// Copy attributes
|
|
|
|
observer->setName(_name);
|
2015-04-15 12:44:03 +08:00
|
|
|
observer->setObserverType(_observerType);
|
2021-12-25 10:04:45 +08:00
|
|
|
observer->_particleTypeToObserve = _particleTypeToObserve;
|
|
|
|
observer->_particleTypeToObserveSet = _particleTypeToObserveSet;
|
|
|
|
observer->_particleSystem = _particleSystem;
|
|
|
|
observer->_observerScale = _observerScale;
|
|
|
|
observer->_observerInterval = _observerInterval;
|
2015-03-02 13:07:32 +08:00
|
|
|
observer->_observerIntervalRemainder = _observerIntervalRemainder;
|
2021-12-25 10:04:45 +08:00
|
|
|
observer->_observerIntervalSet = _observerIntervalSet;
|
|
|
|
observer->_observeUntilEvent = _observeUntilEvent;
|
|
|
|
observer->_eventHandlersExecuted = _eventHandlersExecuted;
|
|
|
|
observer->_enabled = _enabled;
|
|
|
|
observer->_originalEnabled = _originalEnabled;
|
|
|
|
observer->_originalEnabledSet = _originalEnabledSet;
|
2015-03-02 13:07:32 +08:00
|
|
|
|
|
|
|
// Copy event handlers
|
2021-12-25 10:04:45 +08:00
|
|
|
size_t i = 0;
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEventHandler* eventHandler = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (i = 0; i < getNumEventHandlers(); ++i)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
eventHandler = getEventHandler(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
PUEventHandler* clonedEventHandler =
|
|
|
|
PUEventHandlerManager::Instance()->createEventHandler(eventHandler->getEventHandlerType());
|
2015-03-02 13:07:32 +08:00
|
|
|
eventHandler->copyAttributesTo(clonedEventHandler);
|
|
|
|
observer->addEventHandler(clonedEventHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|