VoxeLibre/mods/CORE/mcl_object/init.lua

106 lines
2.5 KiB
Lua
Raw Normal View History

2021-03-17 11:08:14 +01:00
MCLObject = class()
function MCLObject:constructor(obj)
self.object = obj.object or obj
self.IS_MCL_OBJECT = true
end
2021-03-17 15:21:27 +01: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 11:08:14 +01:00
2021-03-18 15:05:08 +01:00
local knockback = {
hitter = hitter,
time_from_last_punch = time_from_last_punch,
tool_capabilities = tool_capabilities,
dir = dir,
2021-03-17 11:08:14 +01:00
}
2021-03-18 15:05:08 +01:00
self:damage(damage, source, knockback)
return true
2021-03-17 11:08:14 +01: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 15:21:27 +01: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 11:08:14 +01:00
2021-03-18 15:05:08 +01:00
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, {
2021-03-17 15:21:27 +01:00
damage = damage,
2021-03-17 11:08:14 +01:00
source = source,
2021-03-17 15:21:27 +01:00
knockback = knockback,
2021-03-18 15:05:08 +01:00
})
2021-03-17 11:08:14 +01:00
2021-03-17 15:21:27 +01:00
return damage
2021-03-17 11:08:14 +01: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 15:21:27 +01:00
function MCLObject:damage_modifier(damage, source)
2021-03-17 11:08:14 +01:00
if self.invulnerable and not source.bypasses_invulnerability then
return 0
end
end
2021-03-17 15:21:27 +01:00
function MCLObject:on_damage(damage, source, knockback)
end
2021-03-18 15:05:08 +01:00
MCLObject.calculate_knockback = minetest.calculate_knockback
2021-03-17 15:21:27 +01:00
2021-03-18 15:05:08 +01:00
function minetest.calculate_knockback()
return 0
2021-03-17 11:08:14 +01:00
end
function MCLObject:on_step()
local damage_info = self.damage_info
if damage_info then
self.damage_info = nil
2021-03-17 15:21:27 +01: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 11:08:14 +01:00
end
end