mirror of
https://github.com/mt-mods/hangglider.git
synced 2024-11-16 07:20:39 +01:00
Add API to register custom "can fly checks"
This commit is contained in:
parent
2c708abd88
commit
7e8832f523
|
@ -1,5 +1,6 @@
|
|||
globals = {
|
||||
"areas",
|
||||
"hangglider",
|
||||
}
|
||||
|
||||
read_globals = {
|
||||
|
|
12
README.md
12
README.md
|
@ -31,3 +31,15 @@ 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.
|
||||
|
||||
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, pos)
|
||||
-- Add your code here
|
||||
-- Must return `true` (can fly) or `false` (can't fly)
|
||||
return true
|
||||
end)
|
||||
```
|
||||
|
|
41
init.lua
41
init.lua
|
@ -1,3 +1,4 @@
|
|||
hangglider = {}
|
||||
|
||||
local has_player_monoids = minetest.get_modpath("player_monoids")
|
||||
local has_areas = minetest.get_modpath("areas")
|
||||
|
@ -73,20 +74,34 @@ local function remove_physics_overrides(player)
|
|||
end
|
||||
end
|
||||
|
||||
local function can_fly(pos, name)
|
||||
if not enable_flak then
|
||||
return true
|
||||
end
|
||||
local flak = false
|
||||
local owners = {}
|
||||
for _, area in pairs(areas:getAreasAtPos(pos)) do
|
||||
if area.flak then
|
||||
flak = true
|
||||
local fly_checks = {}
|
||||
|
||||
function hangglider.add_fly_check(func)
|
||||
table.insert(fly_checks, func)
|
||||
end
|
||||
|
||||
local function can_fly(name, pos)
|
||||
-- Area flak check
|
||||
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
|
||||
owners[area.owner] = true
|
||||
end
|
||||
if flak and not owners[name] then
|
||||
return false
|
||||
|
||||
-- Custom checks set by other mods
|
||||
for _, func in ipairs(fly_checks) do
|
||||
local ret = func(name, pos)
|
||||
if ret == false then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
@ -142,7 +157,7 @@ local function hangglider_step(self, dtime)
|
|||
})
|
||||
end
|
||||
end
|
||||
if not can_fly(pos, name) then
|
||||
if not can_fly(name, pos) then
|
||||
if not self.flak_timer then
|
||||
self.flak_timer = 0
|
||||
shoot_flak_sound(pos)
|
||||
|
|
Loading…
Reference in New Issue
Block a user