added simple smooth rotation (thanks ThomasMonroe)

This commit is contained in:
TenPlus1 2018-04-08 12:25:06 +01:00
parent 5889886f3c
commit d05bf9b489

79
api.lua
View File

@ -145,17 +145,29 @@ end
-- set and return valid yaw -- set and return valid yaw
local set_yaw = function(self, yaw) local set_yaw = function(self, yaw, delay)
if not yaw or yaw ~= yaw then if not yaw or yaw ~= yaw then
yaw = 0 yaw = 0
end end
self:setyaw(yaw) delay = delay or 0
if delay == 0 then
self.object:set_yaw(yaw)
return yaw return yaw
end end
self.target_yaw = yaw
self.delay = delay
return target_yaw
end
-- global function to set mob yaw
function mobs:yaw(self, yaw, delay)
set_yaw(self, yaw, delay)
end
-- set defined animation -- set defined animation
local set_animation = function(self, anim) local set_animation = function(self, anim)
@ -1457,7 +1469,7 @@ local runaway_from = function(self)
yaw = yaw + pi yaw = yaw + pi
end end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 4)
self.state = "runaway" self.state = "runaway"
self.runaway_timer = 3 self.runaway_timer = 3
self.following = nil self.following = nil
@ -1543,7 +1555,7 @@ local follow_flop = function(self)
if p.x > s.x then yaw = yaw + pi end if p.x > s.x then yaw = yaw + pi end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 6)
-- anyone but standing npc's can move along -- anyone but standing npc's can move along
if dist > self.reach if dist > self.reach
@ -1647,7 +1659,7 @@ local do_states = function(self, dtime)
yaw = yaw + random(-0.5, 0.5) yaw = yaw + random(-0.5, 0.5)
end end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 8)
end end
set_velocity(self, 0) set_velocity(self, 0)
@ -1721,7 +1733,7 @@ local do_states = function(self, dtime)
if lp.x > s.x then yaw = yaw + pi end if lp.x > s.x then yaw = yaw + pi end
-- look towards land and jump/move in that direction -- look towards land and jump/move in that direction
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 6)
do_jump(self) do_jump(self)
set_velocity(self, self.walk_velocity) set_velocity(self, self.walk_velocity)
else else
@ -1740,14 +1752,14 @@ local do_states = function(self, dtime)
if lp.x > s.x then yaw = yaw + pi end if lp.x > s.x then yaw = yaw + pi end
end end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 8)
-- otherwise randomly turn -- otherwise randomly turn
elseif random(1, 100) <= 30 then elseif random(1, 100) <= 30 then
yaw = yaw + random(-0.5, 0.5) yaw = yaw + random(-0.5, 0.5)
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 8)
end end
-- stand for great fall in front -- stand for great fall in front
@ -1813,6 +1825,7 @@ local do_states = function(self, dtime)
self.v_start = false self.v_start = false
self.timer = 0 self.timer = 0
self.blinktimer = 0 self.blinktimer = 0
self.path.way = nil
return return
end end
@ -1828,7 +1841,7 @@ local do_states = function(self, dtime)
if p.x > s.x then yaw = yaw + pi end if p.x > s.x then yaw = yaw + pi end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw)
local node_break_radius = self.explosion_radius or 1 local node_break_radius = self.explosion_radius or 1
local entity_damage_radius = self.explosion_damage_radius local entity_damage_radius = self.explosion_damage_radius
@ -2016,7 +2029,7 @@ local do_states = function(self, dtime)
if p.x > s.x then yaw = yaw + pi end if p.x > s.x then yaw = yaw + pi end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw)
-- move towards enemy if beyond mob reach -- move towards enemy if beyond mob reach
if dist > self.reach then if dist > self.reach then
@ -2119,7 +2132,7 @@ local do_states = function(self, dtime)
if p.x > s.x then yaw = yaw + pi end if p.x > s.x then yaw = yaw + pi end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw)
set_velocity(self, 0) set_velocity(self, 0)
@ -2445,7 +2458,7 @@ local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
yaw = yaw + pi yaw = yaw + pi
end end
yaw = set_yaw(self.object, yaw) yaw = set_yaw(self, yaw, 6)
self.state = "runaway" self.state = "runaway"
self.runaway_timer = 0 self.runaway_timer = 0
self.following = nil self.following = nil
@ -2665,7 +2678,7 @@ local mob_activate = function(self, staticdata, def, dtime)
-- set anything changed above -- set anything changed above
self.object:set_properties(self) self.object:set_properties(self)
set_yaw(self.object, (random(0, 360) - 180) / 180 * pi) set_yaw(self, (random(0, 360) - 180) / 180 * pi, 6)
update_tag(self) update_tag(self)
set_animation(self, "stand") set_animation(self, "stand")
@ -2735,6 +2748,46 @@ local mob_step = function(self, dtime)
falling(self, pos) falling(self, pos)
-- smooth rotation by ThomasMonroe314
if self.delay and self.delay > 0 then
local yaw = self.object:get_yaw()
if self.delay == 1 then
yaw = self.target_yaw
else
local dif = abs(yaw - self.target_yaw)
if yaw > self.target_yaw then
if dif > pi then
dif = 2 * pi - dif -- need to add
yaw = yaw + dif / self.delay
else
yaw = yaw - dif / self.delay -- need to subtract
end
elseif yaw < self.target_yaw then
if dif > pi then
dif = 2 * pi - dif
yaw = yaw - dif / self.delay -- need to subtract
else
yaw = yaw + dif / self.delay -- need to add
end
end
if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
if yaw < 0 then yaw = yaw + (pi * 2) end
end
self.delay = self.delay - 1
self.object:set_yaw(yaw)
end
-- end rotation
-- knockback timer -- knockback timer
if self.pause_timer > 0 then if self.pause_timer > 0 then