1
0
Fork 0
MineClone2/mods/CORE/mcl_object/init.lua

105 lines
2.6 KiB
Lua
Raw Normal View History

2021-03-17 10:08:14 +00:00
MCLObject = class()
function MCLObject:constructor(obj)
self.object = obj.object or obj
self.IS_MCL_OBJECT = true
end
2021-03-17 14:21:27 +00:00
function MCLObject:on_punch(hitter, time_from_last_punch, tool_capabilities, dir, damage)
local source = MCLDamageSource({is_punch = true, raw_source_object = hitter})
2021-03-17 10:08:14 +00:00
2021-03-17 14:21:27 +00:00
damage = self:damage_modifier(damage, source) or damage
2021-03-17 10:08:14 +00:00
self.damage_info = {
2021-03-17 14:21:27 +00:00
damage = damage,
2021-03-17 10:08:14 +00:00
source = source,
2021-03-17 14:21:27 +00:00
knockback = self:get_knockback(source, time_from_last_punch, tool_capabilities, dir, nil, damage),
2021-03-17 10:08:14 +00:00
}
2021-03-17 14:21:27 +00:00
return damage
2021-03-17 10:08:14 +00:00
end
-- use this function to deal regular damage to an object (do NOT use :punch() unless toolcaps need to be handled)
2021-03-17 14:21:27 +00:00
function MCLObject:damage(damage, source, knockback)
damage = self:damage_modifier(damage, source) or damage
self:set_hp(self:get_hp() - damage)
2021-03-17 10:08:14 +00:00
self.damage_info = {
2021-03-17 14:21:27 +00:00
damage = damage,
2021-03-17 10:08:14 +00:00
source = source,
2021-03-17 14:21:27 +00:00
knockback = knockback,
2021-03-17 10:08:14 +00:00
}
2021-03-17 14:21:27 +00:00
return damage
2021-03-17 10:08:14 +00:00
end
function MCLObject:wield_index()
end
MCLObject:__getter("equipment", function(self)
return MCLEquipment(self:inventory(), self:wield_index())
end)
function MCLObject:get_hp()
return self.object:get_hp()
end
function MCLObject:set_hp(hp)
self.object:set_hp(hp)
end
function MCLObject:death_drop(inventory, listname, index, stack)
minetest.add_item(self.object:get_pos(), stack)
inventory:set_stack(listname, index, nil)
end
function MCLObject:on_death(source)
local inventory = self:inventory()
if inventory then
for listname, list in pairs(inventory:get_lists()) do
for index, stack in pairs(list) do
if stack:get_name() ~= "" and then
self:death_drop(inventory, listname, index, stack)
end
end
end
end
end
2021-03-17 14:21:27 +00:00
function MCLObject:damage_modifier(damage, source)
2021-03-17 10:08:14 +00:00
if self.invulnerable and not source.bypasses_invulnerability then
return 0
end
end
2021-03-17 14:21:27 +00:00
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,
)
2021-03-17 10:08:14 +00:00
end
2021-03-17 14:21:27 +00:00
MCLObject.calculate_knockback = minetest.calculate_knockback
2021-03-17 10:08:14 +00:00
function MCLObject:on_step()
local damage_info = self.damage_info
if damage_info then
self.damage_info = nil
2021-03-17 14:21:27 +00:00
self:on_damage(damage_info.damage, damage_info.source)
if damage_info.knockback then
self.object:add_velocity(damage_info.knockback)
end
2021-03-17 10:08:14 +00:00
end
end