From 3cf263d292f9fc5a7a18fafa2aa1fbc8e1840a0a Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Sat, 17 Apr 2021 16:23:38 -0400 Subject: [PATCH] Add in dynamic pitch in flying/swimming mobs --- mods/ENTITIES/mcl_mobs/api/api.lua | 1 + .../mcl_mobs/api/mob_functions/ai.lua | 16 +++++ .../mcl_mobs/api/mob_functions/animation.lua | 64 ++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api/api.lua b/mods/ENTITIES/mcl_mobs/api/api.lua index 47804b0d0..1c3ca79b8 100644 --- a/mods/ENTITIES/mcl_mobs/api/api.lua +++ b/mods/ENTITIES/mcl_mobs/api/api.lua @@ -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 diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index 2c9467278..fe7b0309f 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -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 diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/animation.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/animation.lua index 2f89333da..8d37b7c09 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/animation.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/animation.lua @@ -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, @@ -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 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 \ No newline at end of file