reduce code duplication, add mob:stand()

This commit is contained in:
kno10 2024-07-13 16:04:04 +02:00
parent 8c2289686d
commit 5eda59dc13

@ -26,6 +26,13 @@ local PIQUARTER = 0.25 * math.pi
local registered_fallback_node = minetest.registered_nodes[mcl_mobs.fallback_node] local registered_fallback_node = minetest.registered_nodes[mcl_mobs.fallback_node]
-- Stop movement and stand
function mob_class:stand()
self:set_velocity(0)
self.state = "stand"
self:set_animation("stand")
end
-- get node but use fallback for nil or unknown -- get node but use fallback for nil or unknown
local node_ok = function(pos, fallback) local node_ok = function(pos, fallback)
local node = minetest.get_node_or_nil(pos) local node = minetest.get_node_or_nil(pos)
@ -351,9 +358,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range)
end end
if random() <= 0.8 then if random() <= 0.8 then
if self.state ~= "stand" then if self.state ~= "stand" then
self:set_velocity(0) self:stand()
self.state = "stand"
self:set_animation("stand")
end end
local yaw = self.object:get_yaw() or 0 local yaw = self.object:get_yaw() or 0
self:set_yaw(yaw + PIHALF * (random() - 0.5), 10) self:set_yaw(yaw + PIHALF * (random() - 0.5), 10)
@ -366,9 +371,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range)
end end
if random() <= 0.99 then if random() <= 0.99 then
if self.state ~= "stand" then if self.state ~= "stand" then
self:set_velocity(0) self:stand()
self.state = "stand"
self:set_animation("stand")
end end
local yaw = self.object:get_yaw() or 0 local yaw = self.object:get_yaw() or 0
yaw = self:set_yaw(yaw + PI * (random() - 0.5), 10) yaw = self:set_yaw(yaw + PI * (random() - 0.5), 10)
@ -871,9 +874,7 @@ function mob_class:do_states_walk()
yaw = yaw + random() - 0.5 yaw = yaw + random() - 0.5
self:set_yaw(yaw, 8) self:set_yaw(yaw, 8)
end end
self:set_velocity(0) self:stand()
self.state = "stand"
self:set_animation("stand")
yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 6) yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 6)
return return
elseif logging then elseif logging then
@ -905,9 +906,7 @@ function mob_class:do_states_walk()
end end
-- stop at fences or randomly -- stop at fences or randomly
if self.facing_fence == true or random() <= 0.3 then if self.facing_fence == true or random() <= 0.3 then
self:set_velocity(0) self:stand()
self.state = "stand"
self:set_animation("stand")
return return
end end
-- facing wall? then turn -- facing wall? then turn
@ -998,9 +997,7 @@ function mob_class:do_states_runaway()
if self.runaway_timer > 5 if self.runaway_timer > 5
or self:is_at_cliff_or_danger() then or self:is_at_cliff_or_danger() then
self.runaway_timer = 0 self.runaway_timer = 0
self:set_velocity(0) self:stand()
self.state = "stand"
self:set_animation("stand")
yaw = self:set_yaw(yaw + PI * (random() + 0.5), 8) yaw = self:set_yaw(yaw + PI * (random() + 0.5), 8)
else else
self:set_velocity( self.run_velocity) self:set_velocity( self.run_velocity)