Files
dl_old/dl/render/dl_particle.cpp
T
2026-08-14 19:03:49 +08:00

143 lines
2.5 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_err("查找粒子配置失败:{}", name);
return {};
}
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)
{
std::vector<std::string> vec;
File::GetPathFileAll(path, vec, ".json", true);
for (auto& iter : vec)
{
json j = Json::ParseFile(iter);
if(j.is_null())
continue;
ParticleConfig config;
config.Load(j);
std::string name = String::GetResName(path, iter);
log_msg("成功加载到粒子配置:{} <- {}", name, iter);
_allConfig[name] = config;
}
}
void ParticleSystem::_update()
{
for (auto iter = _listDelete.begin(); iter != _listDelete.end();)
{
Particle* par = *iter;
if (par->GetConfig()->system.duration == 0
|| !par->IsActive())
{
_memoryReuse.Delete(par);
iter = _listDelete.erase(iter);
}
else
++iter;
}
}
void Particle::Render()
{
if (!_active)
return;
float t = g_time->GetCurrent();
float duration = _config->system.duration;
if (duration != 0)
{
if (t >= _tCreate + duration)
{
_active = false;
return;
}
}
//------------------发射
// 每个粒子 发射需要多少毫秒
float dt_shoot_one = 1.0f / _config->shooter.rate;
float shoot_num = (t - _tShoot) / dt_shoot_one;
unsigned real_shoot_num = 0;
if (shoot_num < 1.0f)
;
else
{
_tShoot = t;
real_shoot_num = (unsigned)shoot_num;
}
for (ParicleNode& node : _allNode)
{
if (node._active)
{
if(!node._spr)
node._spr = g_factory->CloneSprite(_spr);
node.Render();
}
else
{
node._active = true;
}
}
}
void Particle::SetSprite(gcSprite spr)
{
_spr = spr;
// todo refersh
}
Particle::Particle(ParticleConfig* config) :
_config{ config }
{
_reset();
_spr = g_factory->CreateSpriteDefault();
}
void Particle::_reset()
{
float t = g_time->GetCurrent();
_active = true;
_tCreate = t;
_tShoot = t;
_allNode.resize(_config->system.max_num);
}
}