mirror of
https://github.com/mt-mods/hangglider.git
synced 2025-01-25 09:20:38 +01:00
Add API to register custom "can fly checks"
This commit is contained in:
parent
49a878aa03
commit
1c0270d7ec
14
README.md
14
README.md
@ -31,3 +31,17 @@ The hang glider will wear out every time you use it. The hang glider can be repa
|
|||||||
If the `areas` mod is installed, airspace restrictions can be added to areas using the `/area_flak` command.
|
If the `areas` mod is installed, airspace restrictions can be added to areas using the `/area_flak` command.
|
||||||
|
|
||||||
When using a hang glider in an area with flak enabled, you will get shot down a few seconds after entering the area, this reduces your HP to 1 and destroys your hang glider.
|
When using a hang glider in an area with flak enabled, you will get shot down a few seconds after entering the area, this reduces your HP to 1 and destroys your hang glider.
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
#### Custom "can fly" checks
|
||||||
|
|
||||||
|
```lua
|
||||||
|
hangglider.add_fly_check(function(name, player)
|
||||||
|
-- `name` is the playername
|
||||||
|
-- `player` is the PlayerRef
|
||||||
|
-- Add your code here
|
||||||
|
-- Must return `true` (can fly) or `false` (can't fly)
|
||||||
|
return true
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
46
init.lua
46
init.lua
@ -1,3 +1,4 @@
|
|||||||
|
hangglider = {}
|
||||||
|
|
||||||
hangglider = {
|
hangglider = {
|
||||||
translator = minetest.get_translator('hangglider'),
|
translator = minetest.get_translator('hangglider'),
|
||||||
@ -80,20 +81,36 @@ local function remove_physics_overrides(player)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function can_fly(pos, name)
|
local fly_checks = {}
|
||||||
if not enable_flak then
|
|
||||||
return true
|
function hangglider.add_fly_check(func)
|
||||||
end
|
table.insert(fly_checks, func)
|
||||||
local flak = false
|
end
|
||||||
local owners = {}
|
|
||||||
for _, area in pairs(areas:getAreasAtPos(pos)) do
|
local function can_fly_flak(name, pos)
|
||||||
if area.flak then
|
-- Area flak check
|
||||||
flak = true
|
if enable_flak then
|
||||||
|
local flak = false
|
||||||
|
local owners = {}
|
||||||
|
for _, area in pairs(areas:getAreasAtPos(pos)) do
|
||||||
|
if area.flak then
|
||||||
|
flak = true
|
||||||
|
end
|
||||||
|
owners[area.owner] = true
|
||||||
|
end
|
||||||
|
if flak and not owners[name] then
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
owners[area.owner] = true
|
|
||||||
end
|
end
|
||||||
if flak and not owners[name] then
|
end
|
||||||
return false
|
|
||||||
|
local function can_fly_custom(name, player)
|
||||||
|
-- Custom checks set by other mods
|
||||||
|
for _, func in ipairs(fly_checks) do
|
||||||
|
local ret = func(name, player)
|
||||||
|
if ret == false then
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -149,7 +166,7 @@ local function hangglider_step(self, dtime)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not can_fly(pos, name) then
|
if not can_fly_flak(name, pos) then
|
||||||
if not self.flak_timer then
|
if not self.flak_timer then
|
||||||
self.flak_timer = 0
|
self.flak_timer = 0
|
||||||
shoot_flak_sound(pos)
|
shoot_flak_sound(pos)
|
||||||
@ -164,6 +181,9 @@ local function hangglider_step(self, dtime)
|
|||||||
gliding = false
|
gliding = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if not can_fly_custom(name, player) then
|
||||||
|
gliding = false
|
||||||
|
end
|
||||||
if not gliding then
|
if not gliding then
|
||||||
remove_physics_overrides(player)
|
remove_physics_overrides(player)
|
||||||
hanggliding_players[name] = nil
|
hanggliding_players[name] = nil
|
||||||
|
Loading…
Reference in New Issue
Block a user