From f1141aed9fa52bf57e8867fdb3ffb520793dab07 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Fri, 16 Apr 2021 21:08:54 -0400 Subject: [PATCH] Make mobs flop when outside of flying node --- .../mcl_mobs/api/mob_functions/ai.lua | 37 ++++++++++++------ .../mcl_mobs/api/mob_functions/movement.lua | 39 ++++++++++++++++++- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index 4313c29ce0..c77e0b1a59 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -193,41 +193,54 @@ local fly_state_switch = function(self, dtime) self.state_timer = math.random(4,10) + math.random() self.state = fly_state_list_wandering[math.random(1,#fly_state_list_wandering)] end - end +--this is going to need some more logic gates because birds can flop around +local flop = function(self,dtime) + mobs.flop(self) +end + +-- states are executed here local fly_state_execution = function(self,dtime) local pos = self.object:get_pos() local current_node = minetest_get_node(pos).name - local inside_swim_node = false + local inside_fly_node = false + --quick scan everything to see if inside fly node for _,id in pairs(self.fly_in) do if id == current_node then - inside_swim_node = true + inside_fly_node = true break end end - print(inside_swim_node) + - if self.state == "stand" then + --fly properly if inside fly node + if inside_fly_node then + if self.state == "stand" then - --do animation - --mobs.set_mob_animation(self, "stand") + --do animation + --mobs.set_mob_animation(self, "stand") - --set the velocity of the mob - --mobs.set_velocity(self,0) + --set the velocity of the mob + --mobs.set_velocity(self,0) - --print("standing") + --print("standing") - elseif self.state == "fly" then + elseif self.state == "fly" then - --print("flying") + --print("flying") + end + --flop around if not inside fly node + else + --print("flopping") + flop(self,dtime) end end diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua index 390a867789..09e21f4707 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua @@ -1,10 +1,15 @@ local math_pi = math.pi local math_sin = math.sin local math_cos = math.cos +local math_random = math.random +local DOUBLE_PI = math_pi * 2 -- localize vector functions -local vector_new = vector.new -local vector_length = vector.length +local vector_new = vector.new +local vector_length = vector.length +local vector_multiply = vector.multiply + +local minetest_yaw_to_dir = minetest.yaw_to_dir -- move mob in facing direction --this has been modified to be internal @@ -60,7 +65,37 @@ mobs.jump = function(self, velocity) return end + --fallback velocity to allow modularity velocity = velocity or 8 self.object:add_velocity(vector_new(0,velocity,0)) +end + +--make mobs flop +mobs.flop = function(self, velocity) + + if self.object:get_velocity().y ~= 0 or not self.old_velocity or (self.old_velocity and self.old_velocity.y > 0) then + return false + end + + mobs.set_velocity(self, 0) + + --fallback velocity to allow modularity + velocity = velocity or 8 + + --create a random direction (2d yaw) + local dir = DOUBLE_PI * math_random() + + --create a random force value + local force = math_random(0,3) + math_random() + + --convert the yaw to a direction vector then multiply it times the force + local final_additional_force = vector_multiply(minetest_yaw_to_dir(dir), force) + + --place in the "flop" velocity to make the mob flop + final_additional_force.y = velocity + + self.object:add_velocity(final_additional_force) + + return true end \ No newline at end of file