Implement sounds, enable start and stop events

This commit is contained in:
Till Affeldt
2020-04-14 11:25:00 +02:00
parent 2090aff6bd
commit 299d520af6
9 changed files with 146 additions and 22 deletions

View File

@ -2,7 +2,7 @@ local api = {}
api.SHORT_CYCLE = 0.03 -- for particles and fast animations
api.DEFAULT_CYCLE = 0.1 -- for most effect types
api.MEDIUM_CYCKE = 2.0 -- for ressource intensive tasks
api.MEDIUM_CYCLE = 2.0 -- for ressource intensive tasks
api.LONG_CYCLE = 5.0 -- for write operations and skybox changes
function api.register_weather(name, conditions, effects)
@ -17,13 +17,13 @@ end
function api.register_effect(name, handler, htype)
-- check for valid handler types
if htype ~= "start" and htype ~= "tick" and htype ~= "stop" then
minetest.log("warn", "[Climate API] Invalid effect handler type: " .. htype)
minetest.log("warning", "[Climate API] Invalid effect handler type: " .. htype)
return
end
-- create effect handler registry if not existent yet
if type(climate_mod.effects[name]) == "nil" then
climate_mod.effects[name] = { start = {}, tick = {}, stop = {} }
climate_mod.cycles[name] = { timespan = DEFAULT_CYCLE, timer = 0 }
climate_mod.cycles[name] = { timespan = api.DEFAULT_CYCLE, timer = 0 }
end
-- store effect handler
table.insert(climate_mod.effects[name][htype], handler)
@ -33,4 +33,8 @@ function api.set_effect_cycle(name, cycle)
climate_mod.cycles[name].timespan = cycle
end
--[[function api.register_influence(name, func)
climate_mod.influences[name] = func
end]]
return api

30
lib/influences.lua Normal file
View File

@ -0,0 +1,30 @@
climate_api.register_influence("heat", function(player)
return climate_mod.get_heat(player:get_pos())
end)
climate_api.register_influence("humidity", function(player)
return climate_mod.get_humidity(player:get_pos())
end)
climate_api.register_influence("windspeed", function(player)
local wind_x = climate_mod.state:get_float("wind_x")
local wind_z = climate_mod.state:get_float("wind_z")
return vector.length({x = wind_x, y = 0, z = wind_z})
end)
climate_api.register_influence("wind_x", function(player)
return climate_mod.state:get_float("wind_x")
end)
climate_api.register_influence("wind_z", function(player)
return climate_mod.state:get_float("wind_z")
end)
climate_api.register_influence("height", function(player)
local ppos = player:get_pos()
return ppos.y
end)
climate_api.register_influence("light", function(player)
return minetest.env:get_node_light(player:get_pos(), 0.5)
end)

View File

@ -16,12 +16,13 @@ minetest.register_globalstep(function(dtime)
climate_mod.world.update_status(noise_timer)
end
local previous_effects = climate_mod.current_effects
climate_mod.current_effects = climate_mod.trigger.get_active_effects()
for name, effect in pairs(climate_mod.effects) do
if climate_mod.cycles[name].timespan < climate_mod.cycles[name].timer + dtime then
climate_mod.cycles[name].timer = 0
climate_mod.trigger.call_handlers(name, climate_mod.current_effects[name])
climate_mod.trigger.call_handlers(name, climate_mod.current_effects[name], previous_effects[name])
else
climate_mod.cycles[name].timer = climate_mod.cycles[name].timer + dtime
end

View File

@ -91,10 +91,54 @@ function trigger.get_active_effects()
return effects
end
function trigger.call_handlers(name, effect)
if type(effect) == "nil" then return end
for _, handler in ipairs(climate_mod.effects[name]["tick"]) do
handler(effect)
function trigger.call_handlers(name, effect, prev_effect)
if effect == nil then effect = {} end
if prev_effect == nil then prev_effect = {} end
local starts = {}
local has_starts = false
local ticks = {current = {}, prev = {}}
local has_ticks = false
local stops = {}
local has_stops = false
for player, sources in pairs(effect) do
if type(prev_effect[player]) ~= "nil" then
has_ticks = true
ticks.current[player] = sources
ticks.prev[player] = prev_effect[player]
--prev_effect[player] = nil -- remove any found entries
else
has_starts = true
starts[player] = sources
end
end
for player, sources in pairs(prev_effect) do
if type(effect[player]) == "nil" then
stops[player] = sources
has_stops = true
end
end
if has_starts then
for _, handler in ipairs(climate_mod.effects[name]["start"]) do
handler(starts)
end
end
if has_ticks then
for _, handler in ipairs(climate_mod.effects[name]["tick"]) do
handler(ticks.current, ticks.prev)
end
end
-- remaining table lists ending effects
if has_stops then
minetest.log(dump2(name, "AAAAAAAAAAA"))
for _, handler in ipairs(climate_mod.effects[name]["stop"]) do
handler(stops)
end
end
end