1
0
mirror of https://github.com/mt-mods/hangglider.git synced 2024-11-16 07:20:39 +01:00
This commit is contained in:
Niklp09 2023-01-05 21:08:44 +01:00
parent cd67eb2d19
commit b8d380668d
2 changed files with 27 additions and 92 deletions

View File

@ -9,7 +9,5 @@ read_globals = {
"vector", "ItemStack",
-- Mod deps
"wardzones",
"minetestd",
"player_monoids"
}

117
init.lua
View File

@ -55,54 +55,14 @@
local HUD_Overlay = true --show glider struts as overlay on HUD
local debug = false --show debug info in top-center of hud
local moveModelUp = false
if tonumber(string.sub(minetest.get_version().string, 1, 1)) and
tonumber(string.sub(minetest.get_version().string, 1, 1)) > 4 then
moveModelUp = true
end
hangglider = {} --Make this global, so other mods can tell if hangglider exists.
hangglider.use = {}
if HUD_Overlay then
hangglider.id = {} -- hud id for displaying overlay with struts
end
if debug then hangglider.debug = {} end -- hud id for debug data
--hangglider.airbreak = {} -- true if falling fast when equip
--[[
minetest.register_entity("hangglider:airstopper", { --A one-instant entity that catches the player and stops them.
is_visible = false,
physical = false,
immortal = true,
attach = nil,
on_step = function(self, _)
local canExist = false
if self.attach then
local player = self.attach
if player:is_player() then
local pname = player:get_player_name()
canExist = true
if player:get_player_velocity().y < 0.5 and player:get_player_velocity().y > -0.5 then
--Let go when the player actually stops, as that's the whole point.
if hangglider.use[pname] then
if moveModelUp then
minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
else
minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
end
end
canExist = false
end
end
if not canExist then
player:set_detach()
end
end
if not canExist then
self.object:remove()
end
end
})]]
if minetest.global_exists("areas") then
if minetest.get_modpath("areas") then
hangglider.flak = true
-- chat command definition essentially copied from areas mod.
minetest.register_chatcommand("area_flak",{
@ -115,8 +75,7 @@ if minetest.global_exists("areas") then
end
if not areas:isAreaOwner(id, name) then
return false, "Area "..id.." does not exist"
.." or is not owned by you."
return false, "Area "..id.." does not exist".." or is not owned by you."
end
local open = not areas.areas[id].flak
-- Save false as nil to avoid inflating the DB.
@ -150,12 +109,14 @@ hangglider.shot_sound = function (pos)
pos = pos,
max_hear_distance = 30,
gain = 10.0,
})
}, true)
end
local has_player_monoids = minetest.get_modpath("player_monoids")
local physics_attrs = {"jump", "speed", "gravity"}
local function apply_physics_override(player, overrides)
if minetest.get_modpath("player_monoids") then
if has_player_monoids then
for _, attr in pairs(physics_attrs) do
if overrides[attr] then
player_monoids[attr]:add_change(player, overrides[attr], "hangglider:glider")
@ -169,7 +130,7 @@ end
local function remove_physics_override(player, overrides)
for _, attr in pairs(physics_attrs) do
if overrides[attr] then
if minetest.global_exists("player_monoids") then
if has_player_monoids then
player_monoids[attr]:del_change(player, "hangglider:glider")
else
player:set_physics_override({[attr] = 1})
@ -186,7 +147,7 @@ minetest.register_entity("hangglider:glider", {
mesh = "glider.obj",
immortal = true,
static_save = false,
textures = {"wool_white.png","default_wood.png"},
textures = {"wool_white.png", "default_wood.png"},
on_step = function(self, dtime)
local canExist = false
if self.object:get_attach() then
@ -199,20 +160,19 @@ minetest.register_entity("hangglider:glider", {
if mrn_name then
if not (mrn_name.walkable or mrn_name.liquidtype ~= "none") then
canExist = true
step_v = player:get_velocity().y
if step_v < 0 and step_v > -3 then
apply_physics_override(player, {speed=math.abs(step_v/2) + 0.75})
elseif step_v <= -3 then --Cap our gliding movement speed.
apply_physics_override(player, {speed=2.25})
else
remove_physics_override(player, {speed=1})
end
if debug then
player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..
player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname]))
end
apply_physics_override(player, {gravity=((step_v + 3)/20)})
step_v = player:get_velocity().y
if step_v < 0 and step_v > -3 then
apply_physics_override(player, {speed=math.abs(step_v/2) + 0.75})
elseif step_v <= -3 then --Cap our gliding movement speed.
apply_physics_override(player, {speed=2.25})
else
remove_physics_override(player, {speed=1})
end
if debug then
player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..
player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname]))
end
apply_physics_override(player, {gravity=((step_v + 3)/20)})
end
end
end
@ -231,13 +191,11 @@ minetest.register_entity("hangglider:glider", {
end
end
if not canExist then
remove_physics_override(player, { gravity=1, jump = 1, speed = 1,})
remove_physics_override(player, {gravity = 1, jump = 1, speed = 1})
hangglider.use[pname] = false
if HUD_Overlay then
player:hud_change(hangglider.id[pname], "text", "blank.png")
player:hud_change(hangglider.id[pname], "text", "blank.png")
end
--hangglider.airbreak[pname] = false
end
end
end
@ -256,7 +214,6 @@ minetest.register_on_dieplayer(function(player)
hangglider.use[player:get_player_name()] = false
end)
minetest.register_on_joinplayer(function(player)
local pname = player:get_player_name()
remove_physics_override(player, {
@ -283,7 +240,6 @@ minetest.register_on_joinplayer(function(player)
-- ht = {50,50,50},
}
end
--hangglider.airbreak[pname] = false
end)
minetest.register_on_leaveplayer(function(player)
@ -291,13 +247,12 @@ minetest.register_on_leaveplayer(function(player)
hangglider.use[pname] = nil
if HUD_Overlay then hangglider.id[pname] = nil end
if debug then hangglider.debug[pname] = nil end
--hangglider.airbreak[pname] = nil
end)
minetest.register_tool("hangglider:hangglider", {
description = "Glider",
inventory_image = "glider_item.png",
stack_max=1,
stack_max = 1,
on_use = function(itemstack, player)
if not player or player.is_fake_player then
-- player does not exist or is created from an automated machine (fake_player)
@ -306,32 +261,14 @@ minetest.register_tool("hangglider:hangglider", {
local pos = player:get_pos()
local pname = player:get_player_name()
if not hangglider.use[pname] then --Equip
minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0})
minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0}, true)
if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end
local airbreak = false
--[[if vel < -1.5 then -- engage mid-air, falling fast, so stop but ramp velocity more quickly
--hangglider.airbreak[pname] = true
airbreak = true
local stopper = minetest.add_entity(pos, "hangglider:airstopper")
minetest.after(0, function(stopper, player) --"Extreme Measures"
stopper:set_pos(player:get_pos())
stopper:get_luaentity().attach = player
player:set_attach( stopper, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
end, stopper, player)
end]]
if not airbreak then
if moveModelUp then
minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
else
minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
end
minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
end
hangglider.use[pname] = true
apply_physics_override(player, {jump = 0})
-- if minetest 0.4.x use this:
-- if minetest 5.x use this:
-- minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
itemstack:set_wear(itemstack:get_wear() + 255)
return itemstack
elseif hangglider.use[pname] then --Unequip