axmol/extensions/Particle3D/CCParticleSystem3D.h

302 lines
7.2 KiB
C
Raw Normal View History

2015-02-11 18:14:22 +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-02-11 18:14:22 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +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-02-11 18:14:22 +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-02-11 18:14:22 +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.
****************************************************************************/
#ifndef __CC_PARTICLE_SYSTEM_3D_H__
#define __CC_PARTICLE_SYSTEM_3D_H__
#include "2d/CCNode.h"
#include "math/CCMath.h"
#include <vector>
#include <map>
#include <list>
2020-10-07 00:15:11 +08:00
#include "ExtensionExport.h"
2015-02-11 18:14:22 +08:00
NS_CC_BEGIN
/**
* 3d particle system
*/
class Particle3DEmitter;
class Particle3DAffector;
class Particle3DRender;
2020-10-07 00:15:11 +08:00
struct CC_EX_DLL Particle3D
2015-02-11 18:14:22 +08:00
{
Particle3D();
virtual ~Particle3D();
// property of particles
2021-12-25 10:04:45 +08:00
Vec3 position; // position
Quaternion orientation; // Orientation of the particle.
Vec4 color; // particle color
Vec2 lb_uv; // left bottom uv
Vec2 rt_uv; // right top uv
float width; // Own width
float height; // Own height
float depth; // Own depth
// user defined property
std::unordered_map<std::string, void*> userDefs;
2015-02-11 18:14:22 +08:00
};
2021-12-25 10:04:45 +08:00
template <typename T>
2020-10-07 00:15:11 +08:00
class CC_EX_DLL DataPool
2015-02-11 18:14:22 +08:00
{
public:
2015-03-02 13:07:32 +08:00
typedef typename std::list<T*> PoolList;
typedef typename std::list<T*>::iterator PoolIterator;
DataPool(){};
~DataPool(){};
2021-12-25 10:04:45 +08:00
T* createData()
{
if (_locked.empty())
return nullptr;
2015-03-02 13:07:32 +08:00
T* p = _locked.front();
//_released.push_back(p);
//_locked.erase(_locked.begin());
_released.splice(_released.end(), _locked, _locked.begin());
return p;
}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
void lockLatestData()
{
2015-03-02 13:07:32 +08:00
_locked.push_back(*_releasedIter);
_releasedIter = _released.erase(_releasedIter);
if (_releasedIter != _released.begin() && _releasedIter != _released.end())
{
--_releasedIter;
}
}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
void lockData(T* data)
{
2015-03-02 13:07:32 +08:00
PoolIterator tempIter = _releasedIter;
2021-12-25 10:04:45 +08:00
T* ptr = getFirst();
2015-03-02 13:07:32 +08:00
while (ptr)
{
2021-12-25 10:04:45 +08:00
if (ptr == data)
break;
2015-03-02 13:07:32 +08:00
ptr = getNext();
}
if (ptr)
lockLatestData();
_releasedIter = tempIter;
}
2021-12-25 10:04:45 +08:00
void lockAllDatas()
{
2015-03-02 13:07:32 +08:00
_locked.splice(_locked.end(), _released);
//_locked.insert(_locked.end(), _released.begin(), _released.end());
//_released.clear();
_releasedIter = _released.begin();
}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
T* getFirst()
{
2015-03-02 13:07:32 +08:00
_releasedIter = _released.begin();
2021-12-25 10:04:45 +08:00
if (_releasedIter == _released.end())
return nullptr;
2015-03-02 13:07:32 +08:00
return *_releasedIter;
}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
T* getNext()
{
if (_releasedIter == _released.end())
return nullptr;
2015-03-02 13:07:32 +08:00
++_releasedIter;
2021-12-25 10:04:45 +08:00
if (_releasedIter == _released.end())
return nullptr;
2015-03-02 13:07:32 +08:00
return *_releasedIter;
}
2015-03-02 13:07:32 +08:00
const PoolList& getActiveDataList() const { return _released; };
const PoolList& getUnActiveDataList() const { return _locked; };
2021-12-25 10:04:45 +08:00
void addData(T* data) { _locked.push_back(data); }
2015-03-02 13:07:32 +08:00
bool empty() const { return _released.empty(); };
2021-12-25 10:04:45 +08:00
void removeAllDatas()
{
2015-03-02 13:07:32 +08:00
lockAllDatas();
2021-12-25 10:04:45 +08:00
for (auto iter : _locked)
{
2015-03-02 17:47:26 +08:00
delete iter;
2015-03-02 13:07:32 +08:00
}
_locked.clear();
}
2015-02-11 18:14:22 +08:00
private:
PoolIterator _releasedIter;
PoolList _released;
PoolList _locked;
};
2015-03-02 13:07:32 +08:00
typedef DataPool<Particle3D> ParticlePool;
2020-10-07 00:15:11 +08:00
class CC_EX_DLL ParticleSystem3D : public Node, public BlendProtocol
2015-02-11 18:14:22 +08:00
{
public:
enum class State
{
STOP,
RUNNING,
PAUSE,
};
2021-12-25 10:04:45 +08:00
2015-03-06 10:35:42 +08:00
/**
* override function
*/
2015-02-11 18:14:22 +08:00
virtual void update(float delta) override;
2021-12-25 10:04:45 +08:00
2015-03-06 10:35:42 +08:00
/**
* override function
*/
2021-12-25 10:04:45 +08:00
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
2015-03-06 10:35:42 +08:00
/**
* override function
*/
2021-12-25 10:04:45 +08:00
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
2015-03-06 10:35:42 +08:00
/**
* override function
*/
2021-12-25 10:04:45 +08:00
virtual const BlendFunc& getBlendFunc() const override;
2015-02-11 18:14:22 +08:00
/**
* particle system play control
*/
2015-02-12 16:09:08 +08:00
virtual void startParticleSystem();
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* stop particle
*/
2015-02-12 16:09:08 +08:00
virtual void stopParticleSystem();
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* pause particle
*/
2015-02-12 16:09:08 +08:00
virtual void pauseParticleSystem();
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* resume particle
*/
2015-02-12 16:09:08 +08:00
virtual void resumeParticleSystem();
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* set emitter for particle system, can set your own particle emitter
*/
void setEmitter(Particle3DEmitter* emitter);
/**
* set particle render, can set your own particle render
*/
void setRender(Particle3DRender* render);
/**
2021-12-25 10:04:45 +08:00
* return particle render
*/
Particle3DRender* getRender() { return _render; }
2015-02-11 18:14:22 +08:00
/**
* add particle affector
*/
void addAffector(Particle3DAffector* affector);
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* remove affector by index
*/
void removeAffector(int index);
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* remove all particle affector
*/
void removeAllAffector();
2021-12-25 10:04:45 +08:00
/**
2015-03-06 10:35:42 +08:00
* get particle quota
*/
2015-02-11 18:14:22 +08:00
unsigned int getParticleQuota() const;
2015-03-06 10:35:42 +08:00
/**
* set particle quota
*/
2015-02-11 18:14:22 +08:00
void setParticleQuota(unsigned int quota);
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
/**
* get particle affector by index
*/
Particle3DAffector* getAffector(int index);
2021-12-25 10:04:45 +08:00
2015-03-06 10:35:42 +08:00
/**
* get particle pool
*/
2021-12-25 10:04:45 +08:00
const ParticlePool& getParticlePool() { return _particlePool; }
2015-03-06 10:35:42 +08:00
/**
* get alive particles count
*/
2021-12-25 10:04:45 +08:00
virtual int getAliveParticleCount() const { return 0; }
2015-03-06 10:35:42 +08:00
/**
* get particle playing state
*/
2015-02-11 18:14:22 +08:00
State getState() const { return _state; }
bool isKeepLocal() const { return _keepLocal; }
2015-02-11 18:14:22 +08:00
void setKeepLocal(bool keepLocal);
2021-12-25 10:04:45 +08:00
/**
*Enables or disables the system.
*/
void setEnabled(bool enabled);
2015-03-06 10:35:42 +08:00
/**
* is enabled
*/
bool isEnabled() const { return _isEnabled; }
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
CC_CONSTRUCTOR_ACCESS : ParticleSystem3D();
2015-02-11 18:14:22 +08:00
virtual ~ParticleSystem3D();
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
protected:
2021-12-25 10:04:45 +08:00
State _state;
Particle3DEmitter* _emitter;
2015-02-11 18:14:22 +08:00
std::vector<Particle3DAffector*> _affectors;
2021-12-25 10:04:45 +08:00
Particle3DRender* _render;
// particles
ParticlePool _particlePool;
int _aliveParticlesCnt;
unsigned int _particleQuota;
BlendFunc _blend;
2015-02-11 18:14:22 +08:00
bool _keepLocal;
bool _isEnabled;
};
NS_CC_END
#endif