From 0bee8f9118171a20fab654459ca73a1b6bf35112 Mon Sep 17 00:00:00 2001 From: marco_a <4279489-marco_a@users.noreply.gitlab.com> Date: Thu, 20 Jun 2024 23:35:42 +0200 Subject: [PATCH] Unify trail definition for both hitscan and bullet weapons (closes #10) --- DOCS.md | 9 +++++---- src/api/bullets.lua | 15 +++++++++++---- src/api/weapons.lua | 5 ++--- src/tests/test3.lua | 14 +++++++------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/DOCS.md b/DOCS.md index e06e685..e756d3b 100644 --- a/DOCS.md +++ b/DOCS.md @@ -77,10 +77,11 @@ Actions are tables containing info about how they should work. Actions also supp * It takes either a Minetest physics table or the string `"FREEZE"`, which will block the player * If this parameter is declared, `slow_down_user` won't get called * `sound`: (string) ⚡️🥏👊 the sound to play when the action is run -* `trail`: (table) ⚡️👊 the trail the action should leave. Fields are: - * `image`: (string) the particle to spawn - * `amount`: (int) how many particles to draw along the line - * `size`: (int) the size of each particle +* `trail`: (table) ⚡️🥏👊 the trail the action should leave. Fields are: + * `image`: (string) the particle to spawn -- TODO: rendi obbligatorio + * `amount`: (int) how many particles to draw along the line. In `bullet` type it's the amount to spawn every second. Defaults to `5` in non bullet types and to `2` in bullet types + * `size`: (int) the size of each particle. Default is `1` + * `life`: (int) how's gonna last, in seconds. Default is `0.3` * `on_use`: (function(player, weapon, action, \)) ⚡️🥏👊 additional behaviour when the action is successfully run * `pointed_thing` is only passed with custom and punch action types * if `continuous_fire` is on, the callback is run repeatedly diff --git a/src/api/bullets.lua b/src/api/bullets.lua index e1fd0ac..2557d84 100644 --- a/src/api/bullets.lua +++ b/src/api/bullets.lua @@ -23,6 +23,7 @@ end ---------------------------------------------- function generate_entity(w_name, def, action) + local bullet = { initial_properties = { name = def.name, @@ -48,7 +49,7 @@ function generate_entity(w_name, def, action) _explosion = def.explosion, _remove_on_contact = def.remove_on_contact, - _bullet_trail = def.trail, -- TODO: time qui è expirationtime (life?); amount qui è interval (amount?) + _bullet_trail = action.trail, _spawn_particle_time = 0, _gravity = def.gravity, @@ -59,6 +60,12 @@ function generate_entity(w_name, def, action) _is_sliding = false, -- per capire se si sta muovendo su superfici piane (granate) } + -- più chiaro di amount, ma al tempo stesso mantengo standard con altre armi nella dichiarazione + if bullet._bullet_trail then + bullet._bullet_trail.interval = 20 / (bullet._bullet_trail.amount or 2) + bullet._bullet_trail.amount = nil + end + function bullet:_destroy() if self._explosion then spawn_particles_sphere(self.object:get_pos(), self._explosion.texture) @@ -95,7 +102,7 @@ function generate_entity(w_name, def, action) function bullet:on_step(dtime, moveresult) - self._curr_lifetime = self._curr_lifetime + dtime + self._curr_lifetime = self._curr_lifetime + (dtime * 1.6) -- per convertire in secondi if self._curr_lifetime >= self._lifetime then self:_destroy() @@ -112,9 +119,9 @@ function generate_entity(w_name, def, action) -- Aggiunge le particelle di tracciamento minetest.add_particle({ pos = obj:get_pos(), - velocity = vector.divide(velocity, 2), + velocity = vector.divide(velocity, 5), acceleration = vector.divide(obj:get_acceleration(), -5), - expirationtime = self._bullet_trail.life, + expirationtime = self._bullet_trail.life or 0.3, size = self._bullet_trail.size, texture = self._bullet_trail.image }) diff --git a/src/api/weapons.lua b/src/api/weapons.lua index 91db5e2..c39b155 100644 --- a/src/api/weapons.lua +++ b/src/api/weapons.lua @@ -680,7 +680,6 @@ function attack_bullet(user, action) -- TODO: non si può fare su on_activate quello che segue? local bullet_ent = bullet:get_luaentity() - bullet_ent._action = table.copy(action) local speed = bullet_ent._speed local dir = user:get_look_dir() @@ -942,8 +941,8 @@ function draw_particles(particle, dir, origin, range, pierce) local check_coll = not pierce minetest.add_particlespawner({ - amount = particle.amount, - time = 0.3, -- TODO: meglio funzione che approssima distanza? Time era 0.3, min/max erano impact_dist/(range * 1.5) + amount = particle.amount or 5, + time = particle.life or 0.3, pos = vector.new(origin), vel = vector.multiply(dir, range), size = particle.size or 1, diff --git a/src/tests/test3.lua b/src/tests/test3.lua index 0c672e4..0b8ae1a 100644 --- a/src/tests/test3.lua +++ b/src/tests/test3.lua @@ -27,6 +27,13 @@ weapons_lib.register_weapon("weapons_lib:test3", { -- TODO: trail direttamente nel proiettile o mantengo coerenza con armi generiche? -- `damage` x es. ora lo prende da qua, non da proiettile; eviterei forse 1 e 1 + trail = { + image = "test1_trail2.png", + life = 1, + size = 2, + amount = 5, -- 20/amount + }, + bullet = { name = "weapons_lib:bullet_test", -- TODO: ha senso impostare il nome? @@ -48,13 +55,6 @@ weapons_lib.register_weapon("weapons_lib:test3", { gravity = false, -- TODO particelle per quando svanisce? Tipo effetto fumo - - trail = { - image = "test1_trail2.png", - life = 1, - size = 2, - interval = 5, -- in step - } }, },