From dda7839d8c4c2292e9c8d6472faf38372654d886 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Fri, 16 Apr 2021 21:43:02 -0400 Subject: [PATCH] Add in prototype swimming --- .../mcl_mobs/api/mob_functions/ai.lua | 44 ++++++++++++++++++- .../mcl_mobs/api/mob_functions/movement.lua | 32 ++++++++++++++ mods/ENTITIES/mobs_mc/squid.lua | 1 + 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index c77e0b1a59..4a19d00670 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -2,6 +2,7 @@ local math_random = math.random local vector_multiply = vector.multiply local vector_add = vector.add +local vector_new = vector.new local minetest_yaw_to_dir = minetest.yaw_to_dir local minetest_get_item_group = minetest.get_item_group @@ -201,11 +202,32 @@ local flop = function(self,dtime) mobs.flop(self) end +--this is to swap the built in engine acceleration modifier +local fly_physics_swapper = function(self,inside_fly_node) + --push non-existing physics switch to new mobs + self.fly_physics_switch = self.fly_physics_switch or not inside_fly_node + + --should be flying, gravity is applied, switch to floating + if inside_fly_node and not self.fly_physics_switch then + self.object:set_acceleration(vector_new(0,0,0)) + self.fly_physics_switch = true + --not be flying, gravity isn't applied, switch to falling + elseif not inside_fly_node and self.fly_physics_switch then + self.pitch = 0 + self.object:set_acceleration(vector_new(0,-self.gravity,0)) + self.fly_physics_switch = false + end +end + + +local random_pitch_multiplier = {-1,1} -- states are executed here local fly_state_execution = function(self,dtime) local pos = self.object:get_pos() + + pos.y = pos.y + self.object:get_properties().collisionbox[5] local current_node = minetest_get_node(pos).name local inside_fly_node = false @@ -217,10 +239,12 @@ local fly_state_execution = function(self,dtime) end end - + --turn gravity on or off + fly_physics_swapper(self,inside_fly_node) --fly properly if inside fly node if inside_fly_node then + if self.state == "stand" then --do animation @@ -231,11 +255,27 @@ local fly_state_execution = function(self,dtime) --print("standing") + mobs.set_fly_velocity(self,0) + elseif self.state == "fly" then + self.walk_timer = self.walk_timer - dtime - --print("flying") + --reset the walk timer + if self.walk_timer <= 0 then + + --re-randomize the walk timer + self.walk_timer = math.random(1,6) + math.random() + + --set the mob into a random direction + self.yaw = (math_random() * (math.pi * 2)) + --create a truly random pitch, since there is no easy access to pitch math that I can find + self.pitch = math_random() * random_pitch_multiplier[math_random(1,2)] + end + + + mobs.set_fly_velocity(self,self.walk_velocity) end --flop around if not inside fly node else diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua index 09e21f4707..681d97a1e0 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua @@ -98,4 +98,36 @@ mobs.flop = function(self, velocity) self.object:add_velocity(final_additional_force) return true +end + + + +-- move mob in facing direction +--this has been modified to be internal +--internal = lua (self.yaw) +--engine = c++ (self.object:get_yaw()) +mobs.set_fly_velocity = function(self, v) + + local yaw = (self.yaw or 0) + local pitch = (self.pitch or 0) + + local current_velocity = self.object:get_velocity() + + local goal_velocity = { + x = (math_sin(yaw) * -v), + y = pitch, + z = (math_cos(yaw) * v), + } + + + local new_velocity_addition = vector.subtract(goal_velocity,current_velocity) + + if vector_length(new_velocity_addition) > vector_length(goal_velocity) then + vector.multiply(new_velocity_addition, (vector_length(goal_velocity) / vector_length(new_velocity_addition))) + end + + --smooths out mobs a bit + if vector_length(new_velocity_addition) >= 0.0001 then + self.object:add_velocity(new_velocity_addition) + end end \ No newline at end of file diff --git a/mods/ENTITIES/mobs_mc/squid.lua b/mods/ENTITIES/mobs_mc/squid.lua index fbec650ddb..f650472fc7 100644 --- a/mods/ENTITIES/mobs_mc/squid.lua +++ b/mods/ENTITIES/mobs_mc/squid.lua @@ -16,6 +16,7 @@ mobs:register_mob("mobs_mc:squid", { xp_min = 1, xp_max = 3, armor = 100, + rotate = 270, -- FIXME: If the squid is near the floor, it turns black collisionbox = {-0.4, 0.0, -0.4, 0.4, 0.9, 0.4}, visual = "mesh",