1
0
Fork 0

Make flying mobs float in water and lava

This commit is contained in:
jordan4ibanez 2021-04-17 13:04:55 -04:00
parent 8783912938
commit 0794bc5437
2 changed files with 199 additions and 4 deletions

View File

@ -29,7 +29,7 @@ end
| | / _` | '_ \ / _` | | | / _` | '_ \ / _` |
| |___| (_| | | | | (_| | | |___| (_| | | | | (_| |
\_____/\__,_|_| |_|\__,_| \_____/\__,_|_| |_|\__,_|
]] ]]--
--this is basically reverse jump_check --this is basically reverse jump_check
local cliff_check = function(self,dtime) local cliff_check = function(self,dtime)
@ -297,6 +297,130 @@ local swim_state_execution = function(self,dtime)
end end
--[[
______ _
| ___| |
| |_ | |_ _
| _| | | | | |
| | | | |_| |
\_| |_|\__, |
__/ |
|___/
]]--
-- state switching logic (stand, walk, run, attacks)
local fly_state_list_wandering = {"stand", "fly"}
local fly_state_switch = function(self, dtime)
self.state_timer = self.state_timer - dtime
if self.wandering and self.state_timer <= 0 then
self.state_timer = math.random(4,10) + math.random()
self.state = fly_state_list_wandering[math.random(1,#fly_state_list_wandering)]
end
end
--check if a mob needs to turn while flyming
local fly_turn_check = function(self,dtime)
local pos = self.object:get_pos()
pos.y = pos.y + 0.1
local dir = minetest_yaw_to_dir(self.yaw)
local collisionbox = self.object:get_properties().collisionbox
local radius = collisionbox[4] + 0.5
vector_multiply(dir, radius)
local test_dir = vector.add(pos,dir)
local green_flag_1 = minetest_get_item_group(minetest_get_node(test_dir).name, "solid") ~= 0
return(green_flag_1)
end
--this is to swap the built in engine acceleration modifier
local fly_physics_swapper = function(self,inside_fly_node)
--should be flyming, gravity is applied, switch to floating
if inside_fly_node and self.object:get_acceleration().y ~= 0 then
self.object:set_acceleration(vector_new(0,0,0))
--not be fly, gravity isn't applied, switch to falling
elseif not inside_fly_node and self.object:get_acceleration().y == 0 then
self.pitch = 0
self.object:set_acceleration(vector_new(0,-self.gravity,0))
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 = minetest_get_item_group(current_node, "solid") == 0
--recheck if in water or lava
if minetest_get_item_group(current_node, "water") ~= 0 or minetest_get_item_group(current_node, "lava") ~= 0 then
inside_fly_node = false
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
mobs.set_mob_animation(self, "stand")
mobs.set_fly_velocity(self,0)
elseif self.state == "fly" then
self.walk_timer = self.walk_timer - dtime
--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
--do animation
mobs.set_mob_animation(self, "walk")
--do a quick turn to make mob continuously move
--if in a bird cage or something
if fly_turn_check(self,dtime) then
quick_rotate(self,dtime)
end
mobs.set_fly_velocity(self,self.walk_velocity)
end
else
--make the mob float
if self.floats and minetest_get_item_group(current_node, "water") ~= 0 then
mobs.float(self)
end
end
end
--[[ --[[
___ ___ _ _ _ ___ ___ _ _ _
| \/ | (_) | | (_) | \/ | (_) | | (_)
@ -306,7 +430,7 @@ ___ ___ _ _ _
\_| |_/\__,_|_|_| |_| \_____/\___/ \__, |_|\___| \_| |_/\__,_|_|_| |_| \_____/\___/ \__, |_|\___|
__/ | __/ |
|___/ |___/
]] ]]--
--the main loop --the main loop
mobs.mob_step = function(self, dtime) mobs.mob_step = function(self, dtime)
@ -322,7 +446,8 @@ mobs.mob_step = function(self, dtime)
swim_state_execution(self, dtime) swim_state_execution(self, dtime)
--flying --flying
elseif self.fly then elseif self.fly then
print("I probably should be flying >:(") fly_state_switch(self, dtime)
fly_state_execution(self,dtime)
--regular mobs that walk around --regular mobs that walk around
else else
land_state_switch(self, dtime) land_state_switch(self, dtime)

View File

@ -12,6 +12,31 @@ local vector_multiply = vector.multiply
local minetest_yaw_to_dir = minetest.yaw_to_dir local minetest_yaw_to_dir = minetest.yaw_to_dir
--this is a generic float function
mobs.float = function(self)
local current_velocity = self.object:get_velocity()
local goal_velocity = {
x = 0,
y = 5,
z = 0,
}
local new_velocity_addition = vector.subtract(goal_velocity,current_velocity)
new_velocity_addition.x = 0
new_velocity_addition.z = 0
--smooths out mobs a bit
if vector_length(new_velocity_addition) >= 0.0001 then
self.object:add_velocity(new_velocity_addition)
end
end
--[[ --[[
_ _ _ _
| | | | | | | |
@ -151,6 +176,51 @@ mobs.set_swim_velocity = function(self, 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
--[[
______ _
| ___| |
| |_ | |_ _
| _| | | | | |
| | | | |_| |
\_| |_|\__, |
__/ |
|___/
]]--
-- 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)
if v == 0 then
pitch = 0
end
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) local new_velocity_addition = vector.subtract(goal_velocity,current_velocity)
if vector_length(new_velocity_addition) > vector_length(goal_velocity) then if vector_length(new_velocity_addition) > vector_length(goal_velocity) then