From 8f735cc644e38ea802fabbf1a54d1819226c9b95 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 18 Mar 2021 15:05:08 +0100 Subject: [PATCH] New damage system (WIP) --- mods/CORE/mcl_damage_source/init.lua | 10 ++++-- mods/CORE/mcl_object/init.lua | 47 ++++++++++++++-------------- mods/CORE/mcl_object_mgr/init.lua | 11 ++++++- mods/CORE/mcl_player/init.lua | 6 ---- mods/ITEMS/mcl_armor/damage.lua | 2 +- 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/mods/CORE/mcl_damage_source/init.lua b/mods/CORE/mcl_damage_source/init.lua index 0c15a2653..3ccaab5d6 100644 --- a/mods/CORE/mcl_damage_source/init.lua +++ b/mods/CORE/mcl_damage_source/init.lua @@ -1,11 +1,17 @@ MCLDamageSource = class() function MCLDamageSource:constructor(tbl) - for k, v in pairs(tbl or {}) do - self[k] = v + if tbl then + for k, v in pairs(tbl) do + self[k] = v + end end end +function MCLDamageSource:from_mt(reason) + +end + MCLDamageSource:__getter("direct_object", function(self) local obj = self.raw_source_object if not obj then diff --git a/mods/CORE/mcl_object/init.lua b/mods/CORE/mcl_object/init.lua index f84c19c00..2953662c4 100644 --- a/mods/CORE/mcl_object/init.lua +++ b/mods/CORE/mcl_object/init.lua @@ -8,15 +8,15 @@ end function MCLObject:on_punch(hitter, time_from_last_punch, tool_capabilities, dir, damage) local source = MCLDamageSource({is_punch = true, raw_source_object = hitter}) - damage = self:damage_modifier(damage, source) or damage - - self.damage_info = { - damage = damage, - source = source, - knockback = self:get_knockback(source, time_from_last_punch, tool_capabilities, dir, nil, damage), + local knockback = { + hitter = hitter, + time_from_last_punch = time_from_last_punch, + tool_capabilities = tool_capabilities, + dir = dir, } - return damage + self:damage(damage, source, knockback) + return true end -- use this function to deal regular damage to an object (do NOT use :punch() unless toolcaps need to be handled) @@ -24,11 +24,22 @@ function MCLObject:damage(damage, source, knockback) damage = self:damage_modifier(damage, source) or damage self:set_hp(self:get_hp() - damage) - self.damage_info = { + if type(knockback) == "table" then + knockback = self:calculate_knockback( + knockback.hitter, + knockback.time_from_last_punch, + knockback.tool_capabilities, + knockback.dir or vector.direction(knockback.hitter:get_pos(), self.object:get_pos(), + knockback.distance or vector.distance(knockback.hitter:get_pos(), self.object:get_pos(), + damage, + source) + end + + table.insert(self.damage_info, { damage = damage, source = source, knockback = knockback, - } + }) return damage end @@ -75,22 +86,12 @@ end function MCLObject:on_damage(damage, source, knockback) end -function MCLObject:get_knockback(source, time_from_last_punch, tool_capabilities, dir, distance, damage) - local direct_object = source:direct_object() - - return self:calculate_knockback( - self.object, - direct_object, - time_from_last_punch or 1.0, - tool_capabilities or {fleshy = damage}, - dir or vector.direction(direct_object:get_pos(), self.object:get_pos()), - distance or vector.distance(direct_object:get_pos(), self.object:get_pos()), - damage = damage, - ) -end - MCLObject.calculate_knockback = minetest.calculate_knockback +function minetest.calculate_knockback() + return 0 +end + function MCLObject:on_step() local damage_info = self.damage_info if damage_info then diff --git a/mods/CORE/mcl_object_mgr/init.lua b/mods/CORE/mcl_object_mgr/init.lua index 623ac341e..85a3d5718 100644 --- a/mods/CORE/mcl_object_mgr/init.lua +++ b/mods/CORE/mcl_object_mgr/init.lua @@ -70,6 +70,7 @@ function mcl_object_mgr.register_entity(name, initial_properties, base_class) add_entity_wrapper(def, "on_deactivate") add_entity_wrapper(def, "on_step") + add_entity_wrapper(def, "on_punch") add_entity_wrapper(def, "on_death") add_entity_wrapper(def, "on_rightclick") add_entity_wrapper(def, "on_attach_child") @@ -109,4 +110,12 @@ add_player_wrapper("on_rightclick") add_player_wrapper("on_death", "register_on_dieplayer") add_player_wrapper("on_respawn") -minetest.register_on_player_hpchange(function(player, hp_change, reason)) +minetest.register_on_player_hpchange(function(player, hp_change, reason) + if not reason.auto then + local mclplayer = mcl_object_mgr.get(player) + local source = MCLDamageSource():from_mt(reason) + + mclplayer:damage(hp_change, source) + return true + end +end, true) diff --git a/mods/CORE/mcl_player/init.lua b/mods/CORE/mcl_player/init.lua index c8dc0f721..0894a742d 100644 --- a/mods/CORE/mcl_player/init.lua +++ b/mods/CORE/mcl_player/init.lua @@ -14,12 +14,6 @@ MCLPlayer:__override_pipe("death_drop", function(self, inventory, listname, inde end end) -function MCLPlayer:get_knockback(source, ...) - if not source.is_punch then - return MCLObject.get_knockback(self, source, ...) - end -end - function MCLPlayer:on_damage(damage, source, knockback) MCLObject.on_damage(self, damage, source, knockback) end diff --git a/mods/ITEMS/mcl_armor/damage.lua b/mods/ITEMS/mcl_armor/damage.lua index 9c4528925..379c83da2 100644 --- a/mods/ITEMS/mcl_armor/damage.lua +++ b/mods/ITEMS/mcl_armor/damage.lua @@ -87,7 +87,7 @@ function MCLObject:damage_modifier(damage, source) if thorns_damage > 0 and source_object ~= self then local thorns_damage_source = MCLDamageSource({direct_object = self, source_object = source_object, is_thorns = true}) - local thorns_knockback = source_object:get_knockback(thorns_damage_source, nil, nil, nil, nil, thorns_damage) + local thorns_knockback = {hitter = self} source_object:damage(thorns_damage, thorns_damage_source, thorns_knockback)