250 lines
5.2 KiB
C++
250 lines
5.2 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);
|
|
if (_bReadOverwrite)
|
|
{
|
|
json j = config._config.Save();
|
|
Json::SaveFile(j, path_name);
|
|
log_notice("更新粒子配置:{} -> {}", res_name, path_name);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
//------------------发射
|
|
unsigned real_shoot_num = 0;
|
|
|
|
if (_state == ParticleState::NORMAL)
|
|
{
|
|
// 每个粒子 发射需要多少毫秒
|
|
float dt_shoot_one = 1.0f / _config->_config.emitter.rate;
|
|
|
|
float shoot_num = (t - _tEmitter) / dt_shoot_one;
|
|
if (shoot_num < 1.0f)
|
|
;
|
|
else
|
|
{
|
|
_tEmitter = 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 = _posEmitter;
|
|
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._tLife = Random::Float(_config->_config.node.lifetime);
|
|
node._tEnd = t + node._tLife;
|
|
--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._v += _config->_gravity * dt;
|
|
// 位置计算
|
|
node._pos += node._v * dt;
|
|
// 插值
|
|
float k = 1.0f - (node._tEnd - t) / node._tLife;
|
|
// 颜色插值
|
|
float alpha = std::lerp(_config->_config.node.alpha[0], _config->_config.node.alpha[1], k);
|
|
float r = std::lerp(_config->_config.node.color_beg[0], _config->_config.node.color_end[0], k);
|
|
float g = std::lerp(_config->_config.node.color_beg[1], _config->_config.node.color_end[1], k);
|
|
float b = std::lerp(_config->_config.node.color_beg[2], _config->_config.node.color_end[2], k);
|
|
|
|
// 更新精灵
|
|
node._spr->SetColor({ r, g, b, alpha });
|
|
node._spr->SetQuad(_sprQuad + node._pos + _pos);
|
|
node._spr->Render();
|
|
}
|
|
|
|
// 彻底结束
|
|
if (_state == ParticleState::WAIT && !any_active)
|
|
{
|
|
_state = ParticleState::STOP;
|
|
}
|
|
}
|
|
|
|
void Particle::SetSprite(gcSprite spr)
|
|
{
|
|
_spr = spr;
|
|
_sprQuad = _spr->GetQuad();
|
|
// todo refersh
|
|
}
|
|
|
|
void Particle::Replay()
|
|
{
|
|
/*if (_state == ParticleState::WAIT)
|
|
{
|
|
log_warn("对象已经进入删除队列,不允许再次播放!");
|
|
return;
|
|
}*/
|
|
float t = g_time->GetCurrent();
|
|
_state = ParticleState::NORMAL;
|
|
_tCreate = t;
|
|
_tEmitter = t;
|
|
}
|
|
|
|
Particle::Particle(ParticleConfigX* config) :
|
|
_config{ config },
|
|
_pos{},
|
|
_posEmitter{}
|
|
{
|
|
_reset();
|
|
_spr = g_factory->CreateSpriteDefault();
|
|
_sprQuad = _spr->GetQuad();
|
|
}
|
|
|
|
void Particle::_reset()
|
|
{
|
|
float t = g_time->GetCurrent();
|
|
_state = ParticleState::NORMAL;
|
|
_tCreate = t;
|
|
_tEmitter = t;
|
|
_allNode.resize(_config->_config.system.max_num);
|
|
}
|
|
|
|
}
|
|
|