From ed3afc6e490706864359412df7b32db11fdc7995 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 5 Dec 2020 04:33:23 +0100 Subject: [PATCH] Redo the death effect --- mods/ENTITIES/mcl_mobs/api.lua | 111 ++++++++++++--------- mods/ENTITIES/mcl_mobs/api.txt | 7 +- mods/ENTITIES/mobs_mc/bat.lua | 4 +- mods/ENTITIES/mobs_mc/creeper.lua | 1 - mods/ENTITIES/mobs_mc/enderman.lua | 1 - mods/ENTITIES/mobs_mc/horse.lua | 2 - mods/ENTITIES/mobs_mc/llama.lua | 1 - mods/ENTITIES/mobs_mc/pig.lua | 1 - mods/ENTITIES/mobs_mc/slime+magma_cube.lua | 1 + mods/ENTITIES/mobs_mc/villager.lua | 1 - 10 files changed, 71 insertions(+), 59 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index b09f0d4c..9d9c9163 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -9,6 +9,7 @@ local MAX_MOB_NAME_LENGTH = 30 local HORNY_TIME = 30 local HORNY_AGAIN_TIME = 300 local CHILD_GROW_TIME = 60*20 +local DEATH_DELAY = 0.5 local MOB_CAP = {} MOB_CAP.hostile = 70 @@ -334,7 +335,7 @@ local remove_texture_mod = function(self, mod) end -- set defined animation -local set_animation = function(self, anim) +local set_animation = function(self, anim, fixed_frame) if not self.animation or not anim then return end @@ -349,9 +350,17 @@ local set_animation = function(self, anim) self.animation.current = anim + local a_start = self.animation[anim .. "_start"] + local a_end + if fixed_frame then + a_end = a_start + else + a_end = self.animation[anim .. "_end"] + end + self.object:set_animation({ - x = self.animation[anim .. "_start"], - y = self.animation[anim .. "_end"]}, + x = a_start, + y = a_end}, self.animation[anim .. "_speed"] or self.animation.speed_normal or 15, 0, self.animation[anim .. "_loop"] ~= false) end @@ -669,6 +678,10 @@ end -- check if mob is dead or only hurt local check_for_death = function(self, cause, cmi_cause) + if self.state == "die" then + return true + end + -- has health actually changed? if self.health == self.old_health and self.health > 0 then return false @@ -713,33 +726,39 @@ local check_for_death = function(self, cause, cmi_cause) return false end - -- dropped cooked item if mob died in fire or lava - if cause == "lava" or cause == "fire" then - item_drop(self, true) - else - item_drop(self, nil) - end - mob_sound(self, "death") - local pos = self.object:get_pos() + local function death_handle(self) + -- dropped cooked item if mob died in fire or lava + if cause == "lava" or cause == "fire" then + item_drop(self, true) + else + item_drop(self, nil) + end - if mod_experience and self.hp_min and self.hp_max then - mcl_experience.throw_experience(pos, math.ceil( math.random(self.hp_min,self.hp_max+5) / 5) ) + local pos = self.object:get_pos() + + if mod_experience and self.hp_min and self.hp_max then + mcl_experience.throw_experience(pos, math.ceil( math.random(self.hp_min,self.hp_max+5) / 5) ) + end end -- execute custom death function if self.on_die then - self.on_die(self, pos) + death_handle(self) + + local pos = self.object:get_pos() + local on_die_exit = self.on_die(self, pos) if use_cmi then cmi.notify_die(self.object, cmi_cause) end - self.object:remove() - - return true + if on_die_exit == true then + self.object:remove() + return true + end end local collisionbox @@ -747,6 +766,7 @@ local check_for_death = function(self, cause, cmi_cause) collisionbox = table.copy(self.collisionbox) end + local length = 0 -- default death function and die animation (if defined) if self.animation and self.animation.die_start @@ -754,40 +774,41 @@ local check_for_death = function(self, cause, cmi_cause) local frames = self.animation.die_end - self.animation.die_start local speed = self.animation.die_speed or 15 - local length = max(frames / speed, 0) - - self.attack = nil - self.v_start = false - self.timer = 0 - self.blinktimer = 0 - self.passive = true - self.state = "die" - self.object:set_properties({ - pointable = false, - }) - set_velocity(self, 0) + length = max(frames / speed, 0) + DEATH_DELAY set_animation(self, "die") - - minetest.after(length, function(self) - if not self.object:get_luaentity() then - return - end - if use_cmi then - cmi.notify_die(self.object, cmi_cause) - end - - self.object:remove() - mobs.death_effect(pos) - end, self) else + local rot = self.object:get_rotation() + rot.z = math.pi/2 + self.object:set_rotation(rot) + length = 1 + DEATH_DELAY + set_animation(self, "stand", true) + end - if use_cmi then + self.attack = nil + self.v_start = false + self.timer = 0 + self.blinktimer = 0 + self.passive = true + self.state = "die" + self.object:set_properties({ + pointable = false, + }) + set_velocity(self, 0) + + minetest.after(length, function(self) + if not self.object:get_luaentity() then + return + end + if use_cmi then cmi.notify_die(self.object, cmi_cause) end + death_handle(self) + local dpos = self.object:get_pos() self.object:remove() - mobs.death_effect(pos, collisionbox) - end + mobs.death_effect(dpos) + end, self) + return true end @@ -2690,7 +2711,7 @@ end -- returns true if mob died local falling = function(self, pos) - if self.fly then + if self.fly and self.state ~= "die" then return end diff --git a/mods/ENTITIES/mcl_mobs/api.txt b/mods/ENTITIES/mcl_mobs/api.txt index 93c4df66..3ac66161 100644 --- a/mods/ENTITIES/mcl_mobs/api.txt +++ b/mods/ENTITIES/mcl_mobs/api.txt @@ -284,10 +284,9 @@ Custom Definition Functions Along with the above mob registry settings we can also use custom functions to enhance mob functionality and have them do many interesting things: - 'on_die' a function that is called when the mob is killed the - parameters are (self, pos). When this function is used, - the death particles will be skipped on death. You can get - them back by calling mobs:death_effect manually + 'on_die' a function that is called when the mob is killed; the + parameters are (self, pos). Return true to skip the builtin + death animation and death effects 'on_rightclick' its same as in minetest.register_entity() 'on_blast' is called when an explosion happens near mob when using TNT functions, parameters are (object, damage) and returns diff --git a/mods/ENTITIES/mobs_mc/bat.lua b/mods/ENTITIES/mobs_mc/bat.lua index 7110b2b3..be29cb04 100644 --- a/mods/ENTITIES/mobs_mc/bat.lua +++ b/mods/ENTITIES/mobs_mc/bat.lua @@ -33,12 +33,10 @@ mobs:register_mob("mobs_mc:bat", { run_speed = 80, run_start = 0, run_end = 40, - -- TODO: Less ugly death animation ---[[ die_speed = 60, + die_speed = 60, die_start = 40, die_end = 80, die_loop = false, -]] }, walk_chance = 100, fall_damage = 0, diff --git a/mods/ENTITIES/mobs_mc/creeper.lua b/mods/ENTITIES/mobs_mc/creeper.lua index ddba6523..c1d582bb 100644 --- a/mods/ENTITIES/mobs_mc/creeper.lua +++ b/mods/ENTITIES/mobs_mc/creeper.lua @@ -81,7 +81,6 @@ mobs:register_mob("mobs_mc:creeper", { local r = math.random(1, #mobs_mc.items.music_discs) minetest.add_item({x=pos.x, y=pos.y+1, z=pos.z}, mobs_mc.items.music_discs[r]) end - mobs.death_effect(pos, self.collisionbox) end, maxdrops = 2, drops = { diff --git a/mods/ENTITIES/mobs_mc/enderman.lua b/mods/ENTITIES/mobs_mc/enderman.lua index 3f62bdb2..443fa6bd 100644 --- a/mods/ENTITIES/mobs_mc/enderman.lua +++ b/mods/ENTITIES/mobs_mc/enderman.lua @@ -507,7 +507,6 @@ mobs:register_mob("mobs_mc:enderman", { if self._taken_node ~= nil and self._taken_node ~= "" then minetest.add_item(pos, self._taken_node) end - mobs.death_effect(pos, self.collisionbox) end, do_punch = function(self, hitter, tflp, tool_caps, dir) -- damage from rain caused by itself so we don't want it to attack itself. diff --git a/mods/ENTITIES/mobs_mc/horse.lua b/mods/ENTITIES/mobs_mc/horse.lua index fd4926d2..bb4a93e2 100644 --- a/mods/ENTITIES/mobs_mc/horse.lua +++ b/mods/ENTITIES/mobs_mc/horse.lua @@ -175,8 +175,6 @@ local horse = { mobs.detach(self.driver, {x = 1, y = 0, z = 1}) end - mobs.death_effect(pos, self.collisionbox) - end, on_rightclick = function(self, clicker) diff --git a/mods/ENTITIES/mobs_mc/llama.lua b/mods/ENTITIES/mobs_mc/llama.lua index 8cb32a16..6391e867 100644 --- a/mods/ENTITIES/mobs_mc/llama.lua +++ b/mods/ENTITIES/mobs_mc/llama.lua @@ -111,7 +111,6 @@ mobs:register_mob("mobs_mc:llama", { if self.driver then mobs.detach(self.driver, {x = 1, y = 0, z = 1}) end - mobs.death_effect(pos, self.collisionbox) end, diff --git a/mods/ENTITIES/mobs_mc/pig.lua b/mods/ENTITIES/mobs_mc/pig.lua index 3ed821b3..c59ffdab 100644 --- a/mods/ENTITIES/mobs_mc/pig.lua +++ b/mods/ENTITIES/mobs_mc/pig.lua @@ -79,7 +79,6 @@ mobs:register_mob("mobs_mc:pig", { if self.driver then mobs.detach(self.driver, {x = 1, y = 0, z = 1}) end - mobs.death_effect(pos, self.collisionbox) end, on_rightclick = function(self, clicker) diff --git a/mods/ENTITIES/mobs_mc/slime+magma_cube.lua b/mods/ENTITIES/mobs_mc/slime+magma_cube.lua index d361b89b..1c075374 100644 --- a/mods/ENTITIES/mobs_mc/slime+magma_cube.lua +++ b/mods/ENTITIES/mobs_mc/slime+magma_cube.lua @@ -51,6 +51,7 @@ local spawn_children_on_die = function(child_mob, children_count, spawn_distance end end, children, self.attack) end + return true end end diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index babb1d57..9788d58c 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -1053,7 +1053,6 @@ mobs:register_mob("mobs_mc:villager", { return_fields(player) end end - mobs.death_effect(pos, self.collisionbox) end, })