From 82c0d1961a24f90e0ca30f04aed8149c90f78b4a Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 3 Nov 2023 00:40:55 +0100 Subject: [PATCH] Add basic mob sounds --- mods/rp_mobs/api.lua | 51 +++++++++++++++++++++++++++++++++++- mods/rp_mobs/environment.lua | 15 +++-------- mods/rp_mobs/feed_tame.lua | 4 +-- mods/rp_mobs_mobs/boar.lua | 6 +++++ mods/rp_mobs_mobs/sheep.lua | 6 +++++ 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/mods/rp_mobs/api.lua b/mods/rp_mobs/api.lua index 2535aa00..ce5b6c44 100644 --- a/mods/rp_mobs/api.lua +++ b/mods/rp_mobs/api.lua @@ -75,6 +75,7 @@ rp_mobs.register_mob = function(mobname, def) mdef.entity_definition._base_size = table.copy(def.entity_definition.visual_size or { x=1, y=1, z=1 }) mdef.entity_definition._base_selbox = table.copy(def.entity_definition.selectionbox or { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5, rotate = false }) mdef.entity_definition._base_colbox = table.copy(def.entity_definition.collisionbox or { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5}) + mdef.entity_definition._default_sounds = table.copy(def.default_sounds or {}) mdef.entity_definition._dying = false rp_mobs.registered_mobs[mobname] = mdef @@ -151,6 +152,43 @@ end rp_mobs.on_death_default = function(self, killer) rp_mobs.drop_death_items(self) + rp_mobs.default_mob_sound(self, "death") +end + +rp_mobs.on_punch_default = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage) + if damage >= 1 then + rp_mobs.default_mob_sound(self, "damage") + else + rp_mobs.default_mob_sound(self, "hit_no_damage") + end +end + +rp_mobs.damage = function(self, damage, reason, no_sound) + if damage <= 0 then + return false + end + local hp = self.object:get_hp() + hp = math.max(0, hp - damage) + self.object:set_hp(hp, reason) + if hp <= 0 then + if not no_sound then + rp_mobs.mob_sound_default(self, "death") + end + self._dying = true + return true + else + if not no_sound then + rp_mobs.mob_sound_default(self, "damage") + end + end + return false +end + +rp_mobs.heal = function(self, heal, reason) + local hp = self.object:get_hp() + local hp_max = self.object:get_properties().hp_max + hp = math.min(hp_max, hp + heal) + self.object:set_hp(hp, reason) end rp_mobs.init_physics = function(self) @@ -370,7 +408,7 @@ end function rp_mobs.mob_sound(self, sound, keep_pitch) local pitch if not keep_pitch then - if self.child then + if self._child then pitch = 1.5 else pitch = 1.0 @@ -383,3 +421,14 @@ function rp_mobs.mob_sound(self, sound, keep_pitch) }, true) end +function rp_mobs.default_mob_sound(self, default_sound, keep_pitch) + local sound = self._default_sounds[default_sound] + if sound then + rp_mobs.mob_sound(self, sound, keep_pitch) + end +end + +function rp_mobs.default_hurt_sound(self, keep_pitch) + rp_mobs.default_mob_sound(self, "damage", keep_pitch) +end + diff --git a/mods/rp_mobs/environment.lua b/mods/rp_mobs/environment.lua index 74964dbe..64db4e6a 100644 --- a/mods/rp_mobs/environment.lua +++ b/mods/rp_mobs/environment.lua @@ -56,10 +56,7 @@ function rp_mobs.handle_node_damage(self, dtime) self._node_damage_timer = self._node_damage_timer + dtime if self._node_damage_timer >= NODE_DAMAGE_TIME then if def and def.damage_per_second and def.damage_per_second > 0 then - local hp = math.max(0, self.object:get_hp() - def.damage_per_second) - self.object:set_hp(hp, { type = "node_damage" }) - if hp <= 0 then - self._dying = true + if rp_mobs.damage(self, def.damage_per_second, { type = "node_damage" }) then return end end @@ -110,10 +107,7 @@ function rp_mobs.handle_drowning(self, dtime) if self._drowning_timer >= DROWNING_TIME then self._breath = math.max(0, self._breath - 1) if self._breath <= 0 then - local hp = math.max(0, self.object:get_hp() - def.drowning) - self.object:set_hp(hp, { type = "drown" }) - if hp <= 0 then - self._dying = true + if rp_mobs.damage(self, def.drowning, { type = "drown" }) then return end end @@ -181,10 +175,7 @@ function rp_mobs.handle_fall_damage(self, dtime, moveresult) local damage_f = y_diff - FALL_DAMAGE_HEIGHT local damage = math.floor(math.min(damage_f + 0.5, 65535)) if damage > 0 then - local hp = self.object:get_hp() - damage - self.object:set_hp(hp, { type = "fall" }) - if hp <= 0 then - self._dying = true + if rp_mobs.damage(self, damage, { type = "fall" }) then return end end diff --git a/mods/rp_mobs/feed_tame.lua b/mods/rp_mobs/feed_tame.lua index 2ebf6e09..4bb57288 100644 --- a/mods/rp_mobs/feed_tame.lua +++ b/mods/rp_mobs/feed_tame.lua @@ -95,8 +95,8 @@ rp_mobs.feed_tame_breed = function(mob, feeder, allowed_foods, food_till_tamed, end if eat_sound == nil then - rp_mobs.mob_sound(mob, "mobs_eat", true) - elseif eat_sound ~= false then + rp_mobs.default_mob_sound(mob, "eat", true) + else rp_mobs.mob_sound(mob, eat_sound, true) end diff --git a/mods/rp_mobs_mobs/boar.lua b/mods/rp_mobs_mobs/boar.lua index 233e54b2..46678425 100644 --- a/mods/rp_mobs_mobs/boar.lua +++ b/mods/rp_mobs_mobs/boar.lua @@ -28,6 +28,11 @@ rp_mobs.register_mob("rp_mobs_mobs:boar", { rp_mobs.add_microtask_to_task(self, mt_sleep, task) rp_mobs.add_task(self, task) end, + default_sounds = { + death = "mobs_boar_angry", + damage = "mobs_boar", + eat = "mobs_eat", + }, entity_definition = { hp_max = 20, physical = true, @@ -64,6 +69,7 @@ rp_mobs.register_mob("rp_mobs_mobs:boar", { rp_mobs.attempt_capture(self, capturer, { ["rp_mobs:net"] = 5, ["rp_mobs:lasso"] = 40 }) end, on_death = rp_mobs.on_death_default, + on_punch = rp_mobs.on_punch_default, }, }) diff --git a/mods/rp_mobs_mobs/sheep.lua b/mods/rp_mobs_mobs/sheep.lua index 32d20dea..2faa87cb 100644 --- a/mods/rp_mobs_mobs/sheep.lua +++ b/mods/rp_mobs_mobs/sheep.lua @@ -63,6 +63,11 @@ rp_mobs.register_mob("rp_mobs_mobs:sheep", { local mtask = microtask_eat_grass rp_mobs.add_microtask_to_task(self, mtask, task) end, + default_sounds = { + death = "mobs_sheep", + damage = "mobs_sheep", + eat = "mobs_eat", + }, entity_definition = { hp_max = 14, physical = true, @@ -168,6 +173,7 @@ rp_mobs.register_mob("rp_mobs_mobs:sheep", { end, on_death = rp_mobs.on_death_default, + on_punch = rp_mobs.on_punch_default, }, })