some more cleanups, from code review

This commit is contained in:
kno10 2024-09-18 14:25:34 +02:00
parent b473d55644
commit ddd180f5be
3 changed files with 14 additions and 18 deletions

View File

@ -224,12 +224,12 @@ function mob_class:mob_activate(staticdata, def, dtime)
self._current_animation = nil self._current_animation = nil
self:set_animation("stand") self:set_animation("stand")
if self.riden_by_jock then --- Keep this function before self.on_spawn() is run. if self.riden_by_jock then --- Keep this function before self:on_spawn()
self.object:remove() self.object:remove()
return return
end end
if self.on_spawn and not self.on_spawn_run and self.on_spawn(self) then self.on_spawn_run = true end if self.on_spawn and not self.on_spawn_run and self:on_spawn() then self.on_spawn_run = true end
if not self.wears_armor and self.armor_list then self.armor_list = nil end if not self.wears_armor and self.armor_list then self.armor_list = nil end

View File

@ -401,10 +401,7 @@ end
-- dogshoot attack switch and counter function -- dogshoot attack switch and counter function
function mob_class:dogswitch(dtime) function mob_class:dogswitch(dtime)
-- switch mode not activated -- switch mode not activated
if not self.dogshoot_switch if not self.dogshoot_switch or not dtime then return 0 end
or not dtime then
return 0
end
self.dogshoot_count = self.dogshoot_count + dtime self.dogshoot_count = self.dogshoot_count + dtime
@ -604,8 +601,7 @@ function mob_class:on_punch(hitter, tflp, tool_capabilities, dir)
end end
end end
-- knock back effect (only on full punch) -- knock back effect (only on full punch)
if self.knock_back if self.knock_back and tflp >= punch_interval then
and tflp >= punch_interval then
-- direction error check -- direction error check
dir = dir or vector_zero() dir = dir or vector_zero()
@ -645,7 +641,7 @@ function mob_class:on_punch(hitter, tflp, tool_capabilities, dir)
kb = kb + luaentity._knockback * 0.25 kb = kb + luaentity._knockback * 0.25
end end
self._kb_turn = true self._kb_turn = true
self._turn_to=self.object:get_yaw()-1.57 self:turn_by(HALFPI, .1) -- knockback turn
self.frame_speed_multiplier=2.3 self.frame_speed_multiplier=2.3
if self.animation.run_end then if self.animation.run_end then
self:set_animation("run") self:set_animation("run")
@ -770,7 +766,7 @@ local function clear_aggro(self)
self.path.way = nil self.path.way = nil
end end
function mob_class:do_states_attack (dtime) function mob_class:do_states_attack(dtime)
self.timer = self.timer + dtime self.timer = self.timer + dtime
if self.timer > 100 then self.timer = 1 end if self.timer > 100 then self.timer = 1 end
@ -813,7 +809,7 @@ function mob_class:do_states_attack (dtime)
if self.attack_type == "explode" then if self.attack_type == "explode" then
if target_line_of_sight then if target_line_of_sight then
self:turn_in_direction(p.x - s.x, p.z - s.z, 1, dtime) self:turn_in_direction(p.x - s.x, p.z - s.z, 1)
end end
local node_break_radius = self.explosion_radius or 1 local node_break_radius = self.explosion_radius or 1
@ -932,7 +928,7 @@ function mob_class:do_states_attack (dtime)
p = vector_copy(p1) p = vector_copy(p1)
end end
self:turn_in_direction(p.x - s.x, p.z - s.z, 1, dtime) self:turn_in_direction(p.x - s.x, p.z - s.z, 1)
-- move towards enemy if beyond mob reach -- move towards enemy if beyond mob reach
if dist > self.reach then if dist > self.reach then
@ -1004,7 +1000,7 @@ function mob_class:do_states_attack (dtime)
or (self.attack_type == "dogshoot" and (dist > self.reach or dist < self.avoid_distance and self.shooter_avoid_enemy) and self:dogswitch() == 0) then or (self.attack_type == "dogshoot" and (dist > self.reach or dist < self.avoid_distance and self.shooter_avoid_enemy) and self:dogswitch() == 0) then
local vec = vector_new(p.x - s.x, p.y - s.y - 1, p.z - s.z) local vec = vector_new(p.x - s.x, p.y - s.y - 1, p.z - s.z)
local dist = (vec.x*vec.x + vec.y*vec.y + vec.z*vec.z)^0.5 local dist = (vec.x*vec.x + vec.y*vec.y + vec.z*vec.z)^0.5
self:turn_in_direction(vec.x, vec.z, 1, dtime) self:turn_in_direction(vec.x, vec.z, 1)
if self.strafes then if self.strafes then
if not self.strafe_direction then self.strafe_direction = HALFPI end if not self.strafe_direction then self.strafe_direction = HALFPI end

View File

@ -247,17 +247,21 @@ function mob_class:update_roll()
end end
-- Relative turn, primarily for random turning -- Relative turn, primarily for random turning
-- @param dtime deprecated: ignored now, because of smooth rotations
function mob_class:turn_by(angle, delay, dtime) function mob_class:turn_by(angle, delay, dtime)
return self:set_yaw((self.object:get_yaw() or 0) + angle, delay, dtime) return self:set_yaw((self.object:get_yaw() or 0) + angle, delay, dtime)
end end
-- Turn into a direction (e.g., to the player, or away) -- Turn into a direction (e.g., to the player, or away)
-- @param dtime deprecated: ignored now, because of smooth rotations
function mob_class:turn_in_direction(dx, dz, delay, dtime) function mob_class:turn_in_direction(dx, dz, delay, dtime)
if abs(dx) == 0 and abs(dz) == 0 then return self.object:get_yaw() + self.rotate end if abs(dx) == 0 and abs(dz) == 0 then return self.object:get_yaw() + self.rotate end
return self:set_yaw(-atan2(dx, dz) - self.rotate, delay, dtime) return self:set_yaw(-atan2(dx, dz) - self.rotate, delay, dtime)
end end
-- set and return valid yaw -- set and return valid yaw
function mob_class:set_yaw(yaw, delay, dtime) -- FIXME: dtime unused -- @param dtime deprecated: ignored now, because of smooth rotations
function mob_class:set_yaw(yaw, delay, dtime)
if self.noyaw then return end if self.noyaw then return end
if self._kb_turn then return yaw end -- knockback in effect
if not self.object:get_yaw() or not self.object:get_pos() then return end if not self.object:get_yaw() or not self.object:get_pos() then return end
self.delay = delay or 0 self.delay = delay or 0
self.target_yaw = yaw % TWOPI self.target_yaw = yaw % TWOPI
@ -266,10 +270,6 @@ end
-- improved smooth rotation -- improved smooth rotation
function mob_class:check_smooth_rotation(dtime) function mob_class:check_smooth_rotation(dtime)
if self._turn_to then
self:set_yaw(self._turn_to, .1)
self._turn_to = nil
end
if not self.target_yaw then return end if not self.target_yaw then return end
local delay = self.delay local delay = self.delay