Add mob collision detection

This commit is contained in:
oilboi 2020-04-30 18:49:19 -04:00
parent 7bc80989b2
commit 202ac4ae2f
2 changed files with 33 additions and 0 deletions

View File

@ -83,6 +83,10 @@ mob_register.item_amount = def.item_amount
mob_register.die_in_light = def.die_in_light
mob_register.die_in_light_level = def.die_in_light_level
mob_register.mob = true
mob_register.collision_boundary = def.collision_boundary or 1
mobs.create_movement_functions(def,mob_register)
mobs.create_interaction_functions(def,mob_register)
@ -99,6 +103,9 @@ mob_register.on_step = function(self, dtime)
if self.custom_function_begin then
self.custom_function_begin(self,dtime)
end
self.collision_detection(self)
if self.dead == false and self.death_animation_timer == 0 then
self.move(self,dtime)
self.set_animation(self)

View File

@ -35,6 +35,32 @@ mobs.create_interaction_functions = function(def,mob_register)
texture = "critical.png",
})
end
mob_register.collision_detection = function(self)
local pos = self.object:get_pos()
--do collision detection from the base of the mob
pos.y = pos.y - self.object:get_properties().collisionbox[2]
for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.collision_boundary)) do
if object:is_player() or object:get_luaentity().mob == true then
local pos2 = object:get_pos()
local dir = vector.direction(pos,pos2)
dir.y = 0
local velocity = vector.multiply(dir,1.1)
vel1 = vector.multiply(velocity, -1)
vel2 = velocity
self.object:add_velocity(vel1)
if object:is_player() then
object:add_player_velocity(vel2)
else
object:add_velocity(vel2)
end
end
end
end
--this controls what happens when the mob gets punched
mob_register.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)