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

122 lines
2.9 KiB
Lua
Raw Permalink 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-18 14:05:08 +00:00
local knockback = {
hitter = hitter,
time_from_last_punch = time_from_last_punch,
tool_capabilities = tool_capabilities,
dir = dir,
2021-03-17 10:08:14 +00:00
}
2021-03-18 14:05:08 +00:00
self:damage(damage, source, knockback)
return true
2021-03-17 10:08:14 +00:00
end
2021-03-19 08:11:12 +00:00
function MCLObject:apply_knockback(strength, dir)
local oldvel = self.object:get_velocity()
local vel = vector.multiply(vector.normalize(vector.new(dir.x, 0, dir.z)), strength)
if self:is_on_ground() then
local old_y = oldvel.y / 2
y = math.min(0.4, old_y / 2 + strenth)
vel.y
end
vel = vector.subtract(vel, vector.divide(vector.new(oldvel.x, 0, oldvel.z), 2))
self.object:add_velocity(vel)
end
2021-03-17 10:08:14 +00:00
-- 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
2021-03-18 14:05:08 +00: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 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-18 14:05:08 +00:00
})
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
2021-03-18 14:05:08 +00:00
MCLObject.calculate_knockback = minetest.calculate_knockback
2021-03-17 14:21:27 +00:00
2021-03-18 14:05:08 +00:00
function minetest.calculate_knockback()
return 0
2021-03-17 10:08:14 +00:00
end
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