Add in dynamic pitch in flying/swimming mobs

This commit is contained in:
jordan4ibanez 2021-04-17 16:23:38 -04:00
parent 5ade34115c
commit 3cf263d292
3 changed files with 79 additions and 2 deletions

View file

@ -313,6 +313,7 @@ function mobs:register_mob(name, def)
gravity = GRAVITY,
swim = def.swim,
swim_in = def.swim_in or {mobs_mc.items.water_source, "mcl_core:water_flowing", mobs_mc.items.river_water_source},
pitch_switch = "static",
--set_animation = mobs.set_animation,
--end j4i stuff

View file

@ -1,4 +1,5 @@
local math_random = math.random
local math_pi = math.pi
local vector_multiply = vector.multiply
local vector_add = vector.add
@ -273,6 +274,8 @@ local swim_state_execution = function(self,dtime)
mobs.set_swim_velocity(self,0)
mobs.set_static_pitch(self)
elseif self.state == "swim" then
self.walk_timer = self.walk_timer - dtime
@ -300,6 +303,8 @@ local swim_state_execution = function(self,dtime)
end
mobs.set_swim_velocity(self,self.walk_velocity)
mobs.set_dynamic_pitch(self)
end
--flop around if not inside swim node
else
@ -307,6 +312,8 @@ local swim_state_execution = function(self,dtime)
mobs.set_mob_animation(self, "stand")
mobs.flop(self)
mobs.set_static_pitch(self)
end
end
@ -394,6 +401,8 @@ local fly_state_execution = function(self,dtime)
mobs.set_fly_velocity(self,0)
mobs.set_static_pitch(self)
elseif self.state == "fly" then
self.walk_timer = self.walk_timer - dtime
@ -420,12 +429,18 @@ local fly_state_execution = function(self,dtime)
quick_rotate(self,dtime)
end
mobs.set_dynamic_pitch(self)
mobs.set_fly_velocity(self,self.walk_velocity)
end
else
--make the mob float
if self.floats and float_now then
mobs.set_velocity(self, 0)
mobs.float(self)
mobs.set_static_pitch(self)
end
end
end
@ -476,4 +491,5 @@ mobs.mob_step = function(self, dtime)
end
self.old_velocity = self.object:get_velocity()
self.old_pos = self.object:get_pos()
end

View file

@ -1,4 +1,19 @@
local math_pi = math.pi
local math_floor = math.floor
local HALF_PI = math_pi/2
local vector_distance = vector.distance
local vector_new = vector.new
local minetest_dir_to_yaw = minetest.dir_to_yaw
-- simple degrees calculation
local degrees = function(yaw)
return(yaw*180.0/math_pi)
end
-- set defined animation
mobs.set_mob_animation = function(self, anim, fixed_frame)
@ -67,8 +82,8 @@ mobs.death_effect = function(pos, yaw, collisionbox, rotate)
time = 0.001,
minpos = vector.add(pos, min),
maxpos = vector.add(pos, max),
minvel = vector.new(-5,-5,-5),
maxvel = vector.new(5,5,5),
minvel = vector_new(-5,-5,-5),
maxvel = vector_new(5,5,5),
minexptime = 1.1,
maxexptime = 1.5,
minsize = 1,
@ -103,3 +118,48 @@ mobs.movement_rotation_lock = function(self)
self.object:set_properties{automatic_face_movement_dir = self.rotate}
end
end
local calculate_pitch = function(self)
local pos = self.object:get_pos()
local pos2 = self.old_pos
if pos == nil or pos2 == nil then
return false
end
return(minetest_dir_to_yaw(vector_new(vector_distance(vector_new(pos.x,0,pos.z),vector_new(pos2.x,0,pos2.z)),0,pos.y - pos2.y)) + HALF_PI)
end
--this is a helper function used to make mobs pitch rotation dynamically flow when flying/swimming
mobs.set_dynamic_pitch = function(self)
local pitch = calculate_pitch(self)
if not pitch then
return
end
local current_rotation = self.object:get_rotation()
current_rotation.x = pitch
self.object:set_rotation(current_rotation)
self.pitch_switch = "dynamic"
end
--this is a helper function used to make mobs pitch rotation reset when flying/swimming
mobs.set_static_pitch = function(self)
if self.pitch_switch == "static" then
return
end
local current_rotation = self.object:get_rotation()
current_rotation.x = 0
current_rotation.z = 0
self.object:set_rotation(current_rotation)
self.pitch_switchfdas = "static"
end