123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /****************************************************************************
- Copyright (c) 2015-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- http://www.cocos2d-x.org
-
- 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:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- 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.
- ****************************************************************************/
- #include "CCParticleSystem3D.h"
- #include "CCParticle3DEmitter.h"
- #include "CCParticle3DAffector.h"
- #include "CCParticle3DRender.h"
- NS_CC_BEGIN
- Particle3D::Particle3D()
- : color(Vec4::ONE)
- , rt_uv(Vec2::ONE)
- , width(1.0f)
- , height(1.0f)
- , depth(1.0f)
- {
- }
- Particle3D::~Particle3D()
- {
- }
- ParticleSystem3D::ParticleSystem3D()
- : _state(State::STOP)
- , _emitter(nullptr)
- , _render(nullptr)
- , _aliveParticlesCnt(0)
- , _particleQuota(0)
- , _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
- , _keepLocal(false)
- , _isEnabled(true)
- {
-
- }
- ParticleSystem3D::~ParticleSystem3D()
- {
- //stopParticle();
- removeAllAffector();
- CC_SAFE_RELEASE(_emitter);
- CC_SAFE_RELEASE(_render);
- }
- void ParticleSystem3D::startParticleSystem()
- {
- if (_state != State::RUNNING)
- {
- if (_render)
- _render->notifyStart();
-
- scheduleUpdate();
- _state = State::RUNNING;
- }
- }
- void ParticleSystem3D::stopParticleSystem()
- {
- if (_state != State::STOP)
- {
- if (_render)
- _render->notifyStop();
-
- unscheduleUpdate();
- _state = State::STOP;
- }
- }
- void ParticleSystem3D::pauseParticleSystem()
- {
- if (_state == State::RUNNING)
- {
- _state = State::PAUSE;
- }
- }
- void ParticleSystem3D::resumeParticleSystem()
- {
- if (_state == State::PAUSE)
- {
- _state = State::RUNNING;
- }
- }
- void ParticleSystem3D::setEmitter(Particle3DEmitter* emitter)
- {
- if (_emitter != emitter)
- {
- CC_SAFE_RELEASE(_emitter);
- emitter->_particleSystem = this;
- _emitter = emitter;
- CC_SAFE_RETAIN(_emitter);
- }
- }
- void ParticleSystem3D::setRender(Particle3DRender* render)
- {
- if (_render != render)
- {
- CC_SAFE_RELEASE(_render);
- _render = render;
- _render->_particleSystem = this;
- CC_SAFE_RETAIN(_render);
- }
- }
- void ParticleSystem3D::addAffector(Particle3DAffector* affector)
- {
- if (affector && std::find(_affectors.begin(), _affectors.end(), affector) == _affectors.end()){
- affector->_particleSystem = this;
- affector->retain();
- _affectors.push_back(affector);
- }
- }
- void ParticleSystem3D::removeAffector(int index)
- {
- CCASSERT((unsigned int)index < _affectors.size(), "wrong index");
- _affectors.erase(_affectors.begin() + index);
- }
- void ParticleSystem3D::removeAllAffector()
- {
- //release all affectors
- for (auto it : _affectors) {
- it->release();
- }
- _affectors.clear();
- }
- Particle3DAffector* ParticleSystem3D::getAffector(int index)
- {
- CCASSERT(index < (int)_affectors.size(), "wrong index");
- return _affectors[index];
- }
- void ParticleSystem3D::update(float delta)
- {
- if (_state != State::RUNNING)
- return;
-
- Particle3D *particle = _particlePool.getFirst();
- while (particle)
- {
- if (_emitter)
- {
- _emitter->updateEmitter(particle, delta);
- }
- for (auto& it : _affectors) {
- it->updateAffector(particle, delta);
- }
- particle = _particlePool.getNext();
- }
- }
- void ParticleSystem3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t /*flags*/)
- {
- if (getAliveParticleCount() && _render)
- {
- _render->render(renderer, transform, this);
- }
- }
- void ParticleSystem3D::setBlendFunc(const BlendFunc &blendFunc)
- {
- _blend = blendFunc;
- }
- const BlendFunc &ParticleSystem3D::getBlendFunc() const
- {
- return _blend;
- }
- void ParticleSystem3D::setParticleQuota( unsigned int quota )
- {
- _particleQuota = quota;
- }
- unsigned int ParticleSystem3D::getParticleQuota() const
- {
- return _particleQuota;
- }
- void ParticleSystem3D::setKeepLocal( bool keepLocal )
- {
- _keepLocal = keepLocal;
- }
- void ParticleSystem3D::setEnabled( bool enabled )
- {
- _isEnabled = enabled;
- }
- NS_CC_END
|