dl
dl_particle_config.h
浏览该文件的文档.
1
12#pragma once
13
14#include <array>
15#include <vector>
16#include <string>
17#include <map>
18
19#include <json.hpp>
20using json = nlohmann::json;
21
22#define LUA_USE_APICHECK
23#include <lua.hpp>
24#include <LuaBridge/LuaBridge.h>
25
26namespace JsonAuto
27{
31template<typename T>
32bool Get(const json& j, const std::string& name, T& t)
33{
34 auto iter = j.find(name);
35 if (iter == j.end())
36 {
37 dl::log_warn0(std::string("未加载到配置:") + name);
38 return false;
39 }
40 try
41 {
42 json v = *iter;
43 if (v.find("value") != v.end())
44 t = (v["value"]).get<T>();
45 else
46 t = v.get<T>();
47 return true;
48 }
49 catch (nlohmann::detail::exception& e)
50 {
51 dl::log_warn0(std::string("配置取值失败:") + name + " " + e.what());
52 }
53 return false;
54}
55}
56
57namespace dl
58{
61{
63 struct Node
64 {
65 std::array<float, 2> alpha = { 1, 0 };
66 std::array<float, 2> angle = { 0, 360 };
67 std::array<float, 3> color_beg = { 1, 1, 1 };
68 std::array<float, 2> rotate_speed = { 0, 0 };
69 std::array<float, 3> color_end = { 1, 1, 1 };
70 std::array<float, 2> lifetime = { 1, 2 };
71 bool rotate_to_dir = false;
72 std::array<float, 2> rotate_angle = { 0, 0 };
73 std::array<float, 2> scale_x_beg = { 1, 1 };
74 std::array<float, 2> scale_x_end = { 1, 1 };
75 std::array<float, 2> scale_y_beg = { 1, 1 };
76 std::array<float, 2> scale_y_end = { 1, 1 };
77 std::array<float, 2> speed = { 100, 200 };
80 struct Emitter
81 {
82 std::array<float, 2> offset = { 0, 0 };
83 std::array<float, 2> offset1 = { 0, 0 };
84 unsigned rate = 20;
85 std::string shape = "circle";
86 std::array<float, 2> size = { 100, 100 };
89 struct System
90 {
91 float delay = 0;
92 std::array<float, 2> gravity = { 0, 0 };
93 float duration = 0;
94 unsigned max_num = 1024;
96 bool Load(json& j_root)
97 {
98 {
99 json j;
100 JsonAuto::Get(j_root, "emitter", j);
101 JsonAuto::Get(j, "offset", emitter.offset);
102 JsonAuto::Get(j, "offset1", emitter.offset1);
103 JsonAuto::Get(j, "rate", emitter.rate);
104 JsonAuto::Get(j, "shape", emitter.shape);
105 JsonAuto::Get(j, "size", emitter.size);
106 }
107 {
108 json j;
109 JsonAuto::Get(j_root, "node", j);
110 JsonAuto::Get(j, "alpha", node.alpha);
111 JsonAuto::Get(j, "angle", node.angle);
112 JsonAuto::Get(j, "color_beg", node.color_beg);
113 JsonAuto::Get(j, "rotate_speed", node.rotate_speed);
114 JsonAuto::Get(j, "color_end", node.color_end);
115 JsonAuto::Get(j, "lifetime", node.lifetime);
116 JsonAuto::Get(j, "rotate_to_dir", node.rotate_to_dir);
117 JsonAuto::Get(j, "rotate_angle", node.rotate_angle);
118 JsonAuto::Get(j, "scale_x_beg", node.scale_x_beg);
119 JsonAuto::Get(j, "scale_x_end", node.scale_x_end);
120 JsonAuto::Get(j, "scale_y_beg", node.scale_y_beg);
121 JsonAuto::Get(j, "scale_y_end", node.scale_y_end);
122 JsonAuto::Get(j, "speed", node.speed);
123 }
124 {
125 json j;
126 JsonAuto::Get(j_root, "system", j);
127 JsonAuto::Get(j, "delay", system.delay);
128 JsonAuto::Get(j, "gravity", system.gravity);
129 JsonAuto::Get(j, "duration", system.duration);
130 JsonAuto::Get(j, "max_num", system.max_num);
131 }
132 return true;
133 }
135 {
136 json j;
137 {
138 json& j_emitter = j["emitter"];
139 j_emitter["offset"] = emitter.offset;
140 j_emitter["offset1"] = emitter.offset1;
141 j_emitter["rate"] = emitter.rate;
142 j_emitter["shape"] = emitter.shape;
143 j_emitter["size"] = emitter.size;
144 }
145 {
146 json& j_node = j["node"];
147 j_node["alpha"] = node.alpha;
148 j_node["angle"] = node.angle;
149 j_node["color_beg"] = node.color_beg;
150 j_node["rotate_speed"] = node.rotate_speed;
151 j_node["color_end"] = node.color_end;
152 j_node["lifetime"] = node.lifetime;
153 j_node["rotate_to_dir"] = node.rotate_to_dir;
154 j_node["rotate_angle"] = node.rotate_angle;
155 j_node["scale_x_beg"] = node.scale_x_beg;
156 j_node["scale_x_end"] = node.scale_x_end;
157 j_node["scale_y_beg"] = node.scale_y_beg;
158 j_node["scale_y_end"] = node.scale_y_end;
159 j_node["speed"] = node.speed;
160 }
161 {
162 json& j_system = j["system"];
163 j_system["delay"] = system.delay;
164 j_system["gravity"] = system.gravity;
165 j_system["duration"] = system.duration;
166 j_system["max_num"] = system.max_num;
167 }
168 return j;
169 }
170 void RegisterLua(lua_State* L, const std::string& lua_name)
171 {
172 luabridge::getGlobalNamespace(L)
173 .beginClass<Emitter>("emitter")
174 .addConstructor<void(*)()>()
175 .addProperty("offset", &Emitter::offset)
176 .addProperty("offset1", &Emitter::offset1)
177 .addProperty("rate", &Emitter::rate)
178 .addProperty("shape", &Emitter::shape)
179 .addProperty("size", &Emitter::size)
180 .endClass();
181
182 luabridge::getGlobalNamespace(L)
183 .beginClass<Node>("node")
184 .addConstructor<void(*)()>()
185 .addProperty("alpha", &Node::alpha)
186 .addProperty("angle", &Node::angle)
187 .addProperty("color_beg", &Node::color_beg)
188 .addProperty("rotate_speed", &Node::rotate_speed)
189 .addProperty("color_end", &Node::color_end)
190 .addProperty("lifetime", &Node::lifetime)
191 .addProperty("rotate_to_dir", &Node::rotate_to_dir)
192 .addProperty("rotate_angle", &Node::rotate_angle)
193 .addProperty("scale_x_beg", &Node::scale_x_beg)
194 .addProperty("scale_x_end", &Node::scale_x_end)
195 .addProperty("scale_y_beg", &Node::scale_y_beg)
196 .addProperty("scale_y_end", &Node::scale_y_end)
197 .addProperty("speed", &Node::speed)
198 .endClass();
199
200 luabridge::getGlobalNamespace(L)
201 .beginClass<System>("system")
202 .addConstructor<void(*)()>()
203 .addProperty("delay", &System::delay)
204 .addProperty("gravity", &System::gravity)
205 .addProperty("duration", &System::duration)
206 .addProperty("max_num", &System::max_num)
207 .endClass();
208
209 luabridge::getGlobalNamespace(L)
210 .beginClass<ParticleConfig>(lua_name.c_str())
211 .addConstructor<void(*)()>()
212 .addProperty("emitter", &ParticleConfig::emitter)
213 .addProperty("node", &ParticleConfig::node)
214 .addProperty("system", &ParticleConfig::system)
215 .endClass();
216
217 auto result = luabridge::push(L, this);
218 lua_setglobal(L, lua_name.c_str());
219 }
220};
221
222}
nlohmann::json json
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false
void log_warn0(std::string_view str, DL_SOURCE_LOCATION)
发射器 -> 发射器设置
std::array< float, 2 > size
发射区域类型 -> 发射区域类型(point、circle、box、line)
std::array< float, 2 > offset1
位置偏移 -> 位置偏移
unsigned rate
位置偏移1,作为line模式的结束点 -> 位置偏移1,作为line模式的结束点
std::string shape
发射速率 -> 发射速率,每秒多少个
粒子参数 -> 粒子参数
std::array< float, 2 > lifetime
结束颜色(RGB) -> 结束颜色(RGB)
std::array< float, 2 > rotate_speed
起始颜色(RGB) -> 起始颜色(RGB)
std::array< float, 3 > color_beg
方向角度(范围) -> 方向角度(范围)
std::array< float, 2 > scale_x_end
起始缩放比例x轴(范围) -> 起始缩放比例x轴(范围)
std::array< float, 2 > scale_y_end
起始缩放比例y轴(范围) -> 起始缩放比例y轴(范围)
std::array< float, 3 > color_end
旋转速度(范围) -> 旋转速度(范围)
std::array< float, 2 > scale_x_beg
初始旋转角度(范围) -> 初始旋转角度(范围)
std::array< float, 2 > angle
透明度(范围) -> 透明度(范围)
std::array< float, 2 > scale_y_beg
结束缩放比例x轴(范围) -> 结束缩放比例x轴(范围)
std::array< float, 2 > rotate_angle
旋转至运动方向 -> 旋转至运动方向(启用后,其他旋转控制失效)
std::array< float, 2 > speed
结束缩放比例y轴(范围) -> 结束缩放比例y轴(范围)
bool rotate_to_dir
存活时间(范围) -> 存活时间(范围)
主要参数 -> 主要参数
float duration
重力方向和大小 -> 重力方向和大小
unsigned max_num
持续时间 -> 持续时间(单位秒)
std::array< float, 2 > gravity
启动延时 -> 启动延时
2d粒子配置 -> 2d粒子配置
struct dl::ParticleConfig::Node node
struct dl::ParticleConfig::Emitter emitter
void RegisterLua(lua_State *L, const std::string &lua_name)
struct dl::ParticleConfig::System system