1
0
Fork 0

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, gravity = GRAVITY,
swim = def.swim, swim = def.swim,
swim_in = def.swim_in or {mobs_mc.items.water_source, "mcl_core:water_flowing", mobs_mc.items.river_water_source}, 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, --set_animation = mobs.set_animation,
--end j4i stuff --end j4i stuff

View File

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

View File

@ -1,4 +1,19 @@
local math_pi = math.pi 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 -- set defined animation
mobs.set_mob_animation = function(self, anim, fixed_frame) mobs.set_mob_animation = function(self, anim, fixed_frame)
@ -67,8 +82,8 @@ mobs.death_effect = function(pos, yaw, collisionbox, rotate)
time = 0.001, time = 0.001,
minpos = vector.add(pos, min), minpos = vector.add(pos, min),
maxpos = vector.add(pos, max), maxpos = vector.add(pos, max),
minvel = vector.new(-5,-5,-5), minvel = vector_new(-5,-5,-5),
maxvel = vector.new(5,5,5), maxvel = vector_new(5,5,5),
minexptime = 1.1, minexptime = 1.1,
maxexptime = 1.5, maxexptime = 1.5,
minsize = 1, minsize = 1,
@ -102,4 +117,49 @@ mobs.movement_rotation_lock = function(self)
elseif math.abs(current_engine_yaw - current_lua_yaw) > 0.05 and self.object:get_properties().automatic_face_movement_dir == false then elseif math.abs(current_engine_yaw - current_lua_yaw) > 0.05 and self.object:get_properties().automatic_face_movement_dir == false then
self.object:set_properties{automatic_face_movement_dir = self.rotate} self.object:set_properties{automatic_face_movement_dir = self.rotate}
end 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 end