diff options
| author | ne_mene <[email protected]> | 2026-03-25 14:00:47 +0100 |
|---|---|---|
| committer | ne_mene <[email protected]> | 2026-03-25 14:00:47 +0100 |
| commit | 34535e47d69c3dabf0c3ec4c6434ae9bc99c618d (patch) | |
| tree | f7627f3ecde6b3d6386b7cf6e4ad4cc616faeeba /src/objs/specks.lua | |
| parent | 442e6282d1bf824260cc132e874ab66623cf447c (diff) | |
speck system export/load
Diffstat (limited to 'src/objs/specks.lua')
| -rw-r--r-- | src/objs/specks.lua | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/src/objs/specks.lua b/src/objs/specks.lua index 5044c9c..75e3a77 100644 --- a/src/objs/specks.lua +++ b/src/objs/specks.lua @@ -1,23 +1,28 @@ Speck_Sys = {} Speck_Sys.__index = Speck_Sys -function speck_shape_rectangle(speck_sys) - local w = (speck_sys.spawn_width or 0) / 2 - local h = (speck_sys.spawn_height or 0) / 2 - return randf_range(-w, w), randf_range(-h, h) -end -function speck_shape_point(speck_sys) - return 0, 0 -end -function speck_shape_circle(speck_sys) - local angle = randf_range(0, math.pi * 2) - local r = speck_sys.spawn_radius or 0 - r = r * lmath.random() - return r*math.cos(angle), r*math.sin(angle) -end +local SPAWN_FUNCTIONS = { + Rectangle = function (speck_sys) + local w = (speck_sys.spawn_width or 0) / 2 + local h = (speck_sys.spawn_height or 0) / 2 + + return randf_range(-w, w), randf_range(-h, h) + end, + + Circle = function (speck_sys) + local angle = randf_range(0, math.pi * 2) + local r = speck_sys.spawn_radius or 0 + r = r * lmath.random() + return r*math.cos(angle), r*math.sin(angle) + end, + + Point = function (speck_sys) + return 0, 0 + end +} function Speck_Sys.new() local self = setmetatable({}, Speck_Sys) @@ -38,12 +43,12 @@ function Speck_Sys.new() self.free_ids = {} self.free_ids_size = 0 - self.spawn_shape = speck_shape_point + self.spawn_shape = "Point" self.spawn_amount_min = 1 self.spawn_amount_max = 1 - self.scale_curve = lerp + self.scale_curve = "Lerp" self.scale_start_min = 0.8 self.scale_start_max = 1.25 @@ -61,12 +66,12 @@ function Speck_Sys.new() self.lifetime_max = 5 self.interval = 0.1 - self.spawn_timer = self.interval - self.texture = get_tex("res/img/speck.png") + self.texture_path = "res/img/speck.png" self.x = 0 self.y = 0 + self.spawn_timer = self.interval return self end @@ -83,10 +88,12 @@ function Speck_Sys:check_bounce(i) to_tile_coords(self.data.pos[i].x, self.data.pos[i].y)) end + function Speck_Sys:spawn_particles() local data = self.data local amt = lmath.random(self.spawn_amount_min, self.spawn_amount_max) + local shape_func = SPAWN_FUNCTIONS[self.spawn_shape] for i = 1, amt do local id if self.free_ids_size > 0 then @@ -99,7 +106,7 @@ function Speck_Sys:spawn_particles() data.alive[id] = true - local offx, offy = self.spawn_shape(self) + local offx, offy = shape_func(self) data.pos[id] = { x = self.x + offx, y = self.y + offy } data.vel[id] = { x = self.initial_velx, y = self.initial_vely } @@ -173,8 +180,9 @@ function Speck_Sys:update(dt) end function Speck_Sys:draw() - local scale_curve = self.scale_curve + local scale_curve = EASING_FUNCTIONS[self.scale_curve] local data = self.data + local tex = get_tex(self.texture_path) for i = 1, data.size do if not data.alive[i] then @@ -184,10 +192,10 @@ function Speck_Sys:draw() local anim = 1 - data.lifetime[i] / data.lifetime_max[i] local scale = scale_curve(data.scale_start[i], data.scale_end[i], anim) - local w, h = self.texture:getDimensions() + local w, h = tex:getDimensions() lg.draw( - self.texture, data.pos[i].x, data.pos[i].y, 0, scale, scale, w / 2, h / 2 + tex, data.pos[i].x, data.pos[i].y, 0, scale, scale, w / 2, h / 2 ) ::next_speck_draw:: end @@ -207,3 +215,43 @@ end function speck_draw_sys(ent) ent.speck_sys:draw() end + +function export_speck_sys(sys, filename) + local EXPORTED_ARGS = { + "spawn_shape", + "spawn_amount_min", + "spawn_amount_max", + "spawn_width", + "spawn_height", + "spawn_radius", + "scale_curve", + "scale_start_min", + "scale_start_max", + "scale_end_min", + "scale_end_max", + "forcex", + "forcey", + "initial_velx", + "initial_vely", + "spread", + "lifetime_min", + "lifetime_max", + "interval", + "texture_path" + } + local exp = {} + + for i, arg in ipairs(EXPORTED_ARGS) do + exp[arg] = sys[arg] + end + export_to_source(exp, filename) +end + +function load_speck_sys(filename) + local sys = Speck_Sys.new() + local loaded = lf.load(filename)() + for key, data in pairs(loaded) do + sys[key] = data + end + return sys +end |
