mirror of
https://github.com/t-affeldt/climate_api.git
synced 2024-11-17 08:00:25 +01:00
Implement sounds, enable start and stop events
This commit is contained in:
parent
2090aff6bd
commit
299d520af6
7
TODO.md
7
TODO.md
@ -2,21 +2,18 @@
|
||||
|
||||
## Required for MVP
|
||||
- Test non-random heat and humidity values
|
||||
- Re-implement sounds into new engine
|
||||
- Implement start/end events
|
||||
- Make sure all weather presets are working
|
||||
- Implement chat commands
|
||||
|
||||
## Planned for first release
|
||||
- Improve test_condition function
|
||||
- Implement ABM system for additional effects
|
||||
- Add light level to possible conditions
|
||||
- Implement fallback for sky changes without skylayer
|
||||
- Configurable number of days in a year
|
||||
- Add license information to source files
|
||||
- Write helpful README
|
||||
- Find good values for weather conditions
|
||||
- Set effects on player join
|
||||
- Improve value structures of particle and skybox effects
|
||||
- Make sounds adjust to changes by weather presets
|
||||
|
||||
## Nice to have
|
||||
- Write documentation on how to add weathers and effects
|
||||
|
@ -4,7 +4,7 @@ if not minetest.get_modpath("skylayer") then return end
|
||||
local SKYBOX_NAME = "climate_api:clouds"
|
||||
|
||||
local function set_clouds(player, clouds)
|
||||
sky = { name = SKYBOX_NAME, cloud_data = clouds }
|
||||
local sky = { name = SKYBOX_NAME, cloud_data = clouds }
|
||||
skylayer.add_layer(player:get_player_name(), sky)
|
||||
end
|
||||
|
||||
@ -44,5 +44,5 @@ local function remove_effect(player_data)
|
||||
end
|
||||
|
||||
climate_api.register_effect("climate_api:clouds", handle_effect, "tick")
|
||||
climate_api.register_effect("climate_api:clouds", remove_effect, "end")
|
||||
climate_api.register_effect("climate_api:clouds", remove_effect, "stop")
|
||||
climate_api.set_effect_cycle("climate_api:clouds", climate_api.LONG_CYCLE)
|
@ -31,5 +31,5 @@ local function remove_effect(player_data)
|
||||
end
|
||||
|
||||
climate_api.register_effect("climate_api:skybox", handle_effect, "tick")
|
||||
climate_api.register_effect("climate_api:skybox", remove_effect, "end")
|
||||
climate_api.register_effect("climate_api:skybox", remove_effect, "stop")
|
||||
climate_api.set_effect_cycle("climate_api:skybox", climate_api.LONG_CYCLE)
|
@ -1,14 +1,59 @@
|
||||
if not climate_mod.settings.sound then return end
|
||||
|
||||
return
|
||||
local handles = {}
|
||||
local function start_sound(pname, weather, sound)
|
||||
if handles[pname] == nil then handles[pname] = {} end
|
||||
if handles[pname][weather] ~= nil then return end
|
||||
local handle = minetest.sound_play(sound, {
|
||||
to_player = pname,
|
||||
loop = true
|
||||
})
|
||||
handles[pname][weather] = handle
|
||||
end
|
||||
|
||||
local function update_effect(player_data)
|
||||
local function stop_sound(pname, weather, sound)
|
||||
if handles[pname] == nil or handles[pname][weather] == nil then return end
|
||||
local handle = handles[pname][weather]
|
||||
minetest.sound_stop(handle)
|
||||
handles[pname][weather] = nil
|
||||
end
|
||||
|
||||
local function start_effect(player_data)
|
||||
for playername, data in pairs(player_data) do
|
||||
for weather, value in pairs(data) do
|
||||
climate_mod.effects.play_sound(player, value)
|
||||
start_sound(playername, weather, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
climate_api.register_effect("climate_api:sound", update_effect, "change")
|
||||
climate_api.set_effect_cycle("climate_api:skybox", climate_api.MEDIUM_CYCLE)
|
||||
local function handle_effect(player_data, prev_data)
|
||||
for playername, data in pairs(player_data) do
|
||||
for weather, value in pairs(data) do
|
||||
if prev_data[playername][weather] == nil then
|
||||
start_sound(playername, weather, sound)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for playername, data in pairs(prev_data) do
|
||||
for weather, value in pairs(data) do
|
||||
if player_data[playername][weather] == nil then
|
||||
stop_sound(playername, weather, sound)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function stop_effect(prev_data)
|
||||
minetest.log(dump2(prev_data, "stop_effect"))
|
||||
for playername, data in pairs(prev_data) do
|
||||
for weather, value in pairs(data) do
|
||||
stop_sound(playername, weather, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
climate_api.register_effect("climate_api:sound", start_effect, "start")
|
||||
climate_api.register_effect("climate_api:sound", handle_effect, "tick")
|
||||
climate_api.register_effect("climate_api:sound", stop_effect, "stop")
|
||||
climate_api.set_effect_cycle("climate_api:sound", 0)
|
5
init.lua
5
init.lua
@ -38,18 +38,21 @@ climate_mod.current_effects = {}
|
||||
climate_mod.weathers = {}
|
||||
climate_mod.effects = {}
|
||||
climate_mod.cycles = {}
|
||||
climate_mod.influences = {}
|
||||
|
||||
-- import core API
|
||||
climate_mod.state = dofile(modpath .. "/lib/datastorage.lua")
|
||||
climate_api = dofile(modpath .. "/lib/api.lua")
|
||||
climate_api.utility = dofile(modpath .. "/lib/api_utility.lua")
|
||||
climate_api.environment = dofile(modpath .. "/lib/environment.lua")
|
||||
--climate_api = dofile(modpath .. "/lib/influences.lua")
|
||||
climate_mod.world = dofile(modpath .. "/lib/world.lua")
|
||||
climate_mod.trigger = dofile(modpath .. "/lib/trigger.lua")
|
||||
dofile(modpath.."/lib/main.lua")
|
||||
dofile(modpath.."/lib/commands.lua")
|
||||
|
||||
-- import predefined environment effects
|
||||
dofile(modpath .. "/ca_effects/clouds.lua")
|
||||
dofile(modpath .. "/ca_effects/particles.lua")
|
||||
dofile(modpath .. "/ca_effects/skybox.lua")
|
||||
dofile(modpath .. "/ca_effects/clouds.lua")
|
||||
dofile(modpath .. "/ca_effects/sound.lua")
|
||||
|
10
lib/api.lua
10
lib/api.lua
@ -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
30
lib/influences.lua
Normal 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)
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user