226 lines
4.3 KiB
C++
226 lines
4.3 KiB
C++
#include "dl_particle.h"
|
|
|
|
#include "io/dl_json.h"
|
|
#include "system/dl_time.h"
|
|
#include "io/dl_file.h"
|
|
#include "render/dl_factory.h"
|
|
|
|
namespace dl
|
|
{
|
|
ParticleSystem* g_particle;
|
|
|
|
Particle* ParticleSystem::Create(std::string_view name)
|
|
{
|
|
auto iter = _allConfig.find(std::string{ name });
|
|
if (iter == _allConfig.end())
|
|
{
|
|
log_warn("查找粒子配置失败:{}", name);
|
|
iter = _allConfig.find(":");
|
|
if (iter == _allConfig.end())
|
|
return nullptr;
|
|
}
|
|
Particle* ret = _memoryReuse.New(&iter->second);
|
|
ret->_reset();
|
|
return ret;
|
|
}
|
|
|
|
void ParticleSystem::Delete(Particle* par, bool wait /*= true*/)
|
|
{
|
|
bool delete_now = true;
|
|
if (wait)
|
|
{//
|
|
|
|
}
|
|
if (delete_now)
|
|
{
|
|
_memoryReuse.Delete(par);
|
|
}
|
|
else
|
|
_listDelete.push_back(par);
|
|
}
|
|
|
|
void ParticleSystem::Load(std::string_view path_name, std::string_view i_res_name)
|
|
{
|
|
std::string res_name { i_res_name };
|
|
|
|
auto iter = _allConfig.find(res_name);
|
|
if (iter != _allConfig.end())
|
|
{
|
|
log_err("重复的资源名:{} -> {}", path_name, res_name);
|
|
return;
|
|
}
|
|
|
|
json j = Json::ParseFile(path_name);
|
|
if (j.is_null())
|
|
{
|
|
log_err("配置解析失败:{} -> {}", path_name, res_name);
|
|
return;
|
|
}
|
|
|
|
ParticleConfigX config;
|
|
config._config.Load(j);
|
|
config.Calc();
|
|
|
|
log_msg("成功加载到粒子配置:{} <- {}", res_name, path_name);
|
|
_allConfig[res_name] = config;
|
|
}
|
|
|
|
void ParticleSystem::LoadPath(std::string_view path)
|
|
{
|
|
std::vector<std::string> vec;
|
|
File::GetPathFileAll(path, vec, ".json", true);
|
|
|
|
for (auto& iter : vec)
|
|
{
|
|
std::string res_name = String::GetResName(path, iter);
|
|
Load(iter, res_name);
|
|
}
|
|
}
|
|
|
|
void ParticleSystem::Clear()
|
|
{
|
|
for (auto& iter : _listDelete)
|
|
{
|
|
_memoryReuse.Delete(iter);
|
|
}
|
|
_listDelete.clear();
|
|
}
|
|
|
|
ParticleSystem::~ParticleSystem()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
void ParticleSystem::_update()
|
|
{
|
|
for (auto iter = _listDelete.begin(); iter != _listDelete.end();)
|
|
{
|
|
Particle* par = *iter;
|
|
if (par->GetState() == ParticleState::STOP)
|
|
{
|
|
_memoryReuse.Delete(par);
|
|
iter = _listDelete.erase(iter);
|
|
}
|
|
else
|
|
++iter;
|
|
}
|
|
}
|
|
|
|
void Particle::Render()
|
|
{
|
|
if (_state == ParticleState::STOP)
|
|
return;
|
|
float t = g_time->GetCurrent();
|
|
float dt = g_time->GetDelta();
|
|
|
|
if (_state == ParticleState::NORMAL)
|
|
{
|
|
float duration = _config->_config.system.duration;
|
|
if (duration != 0)
|
|
{
|
|
if (t >= _tCreate + duration)
|
|
{
|
|
_state = ParticleState::WAIT_DELETE;
|
|
}
|
|
}
|
|
}
|
|
|
|
//------------------发射
|
|
unsigned real_shoot_num = 0;
|
|
|
|
if (_state == ParticleState::NORMAL)
|
|
{
|
|
// 每个粒子 发射需要多少毫秒
|
|
float dt_shoot_one = 1.0f / _config->_config.emitter.rate;
|
|
|
|
float shoot_num = (t - _tShoot) / dt_shoot_one;
|
|
if (shoot_num < 1.0f)
|
|
;
|
|
else
|
|
{
|
|
_tShoot = t;
|
|
real_shoot_num = (unsigned)shoot_num;
|
|
}
|
|
}
|
|
|
|
bool any_active = false;
|
|
for (ParicleNode& node : _allNode)
|
|
{
|
|
if (!node._active)
|
|
{
|
|
if (real_shoot_num)
|
|
{
|
|
node._active = true;
|
|
//
|
|
node._pos = {};
|
|
float v = Random::Float(_config->_config.node.speed);
|
|
float radin = Random::Float(_config->_radin);
|
|
|
|
node._v.x = v * std::cosf(radin);
|
|
node._v.y = v * std::sinf(radin);
|
|
|
|
node._tEnd = t + Random::Float(_config->_config.node.lifetime);
|
|
--real_shoot_num;
|
|
}
|
|
else
|
|
continue;
|
|
}
|
|
if (t >= node._tEnd)
|
|
{
|
|
node._active = false;
|
|
continue;
|
|
}
|
|
any_active = true;
|
|
if (!node._spr)
|
|
node._spr = g_factory->CloneSprite(_spr);
|
|
node.Render(_sprQuad, dt, _pos);
|
|
}
|
|
|
|
// 彻底结束
|
|
if (_state == ParticleState::WAIT_DELETE && !any_active)
|
|
{
|
|
_state = ParticleState::STOP;
|
|
}
|
|
}
|
|
|
|
void Particle::SetSprite(gcSprite spr)
|
|
{
|
|
_spr = spr;
|
|
_sprQuad = _spr->GetQuad();
|
|
// todo refersh
|
|
}
|
|
|
|
void Particle::Replay()
|
|
{
|
|
if (_state == ParticleState::WAIT_DELETE)
|
|
{
|
|
log_warn("对象已经进入删除队列,不允许再次播放!");
|
|
return;
|
|
}
|
|
float t = g_time->GetCurrent();
|
|
_state = ParticleState::NORMAL;
|
|
_tCreate = t;
|
|
_tShoot = t;
|
|
}
|
|
|
|
Particle::Particle(ParticleConfigX* config) :
|
|
_config{ config },
|
|
_pos{}
|
|
{
|
|
_reset();
|
|
_spr = g_factory->CreateSpriteDefault();
|
|
_sprQuad = _spr->GetQuad();
|
|
}
|
|
|
|
void Particle::_reset()
|
|
{
|
|
float t = g_time->GetCurrent();
|
|
_state = ParticleState::NORMAL;
|
|
_tCreate = t;
|
|
_tShoot = t;
|
|
_allNode.resize(_config->_config.system.max_num);
|
|
}
|
|
|
|
}
|
|
|