Divergent/mods/Anomaly Radios Extended/gamedata/scripts/ph_sound.script

149 lines
4.1 KiB
Plaintext
Raw Permalink Blame History

class "snd_source"
function snd_source:__init (obj, storage)
self.object = obj
self.st = storage
self.destructed = false
end
function snd_source:reset_scheme(loading)
self.last_update = 0
self.st.signals = {}
self.played_sound = nil
self.first_sound = true
self.st.pause_time = 0
self.st.sound_set = true
if loading == false then
self.destructed = false
else
self.destructed = load_var (self.object, "destr")
end
end
function snd_source:save ()
save_var (self.object, "destr", self.destructed)
end
function snd_source:hit_callback(obj, amount, local_direction, who, bone_index)
if self.st.no_hit == true then return end
--printf ("SOUND SOURCE HAVE A HIT")
local who_name
if who then
who_name = who:name()
else
who_name = "nil"
end
--printf("_bp: snd_source:hit_callback obj='%s', amount=%d, who='%s'", obj:name(), amount, who_name)
if self.played_sound ~= nil then
self.played_sound:stop ()
self.played_sound = nil
end
self.destructed = true
end
function snd_source:update(delta)
if (axr_main and axr_main.config) and (ui_options.get("sound/radio/zone") ~= true) then
if self.played_sound ~= nil then
self.played_sound:stop()
self.played_sound = nil
end
return
end
if self.destructed == true then return end
if xr_logic.try_switch_to_another_section (self.object, self.st, db.actor) then
return
end
if self.st.pause_time - device ():time_global () > 0 then
return
end
self.st.pause_time = 0
if self.st.sound_set == true then
self.st.sound_set = false
if self.st.random then
self.played_sound = xr_sound.get_sound_object(self.st.theme, "random")
elseif self.st.looped then
self.played_sound = xr_sound.get_sound_object(self.st.theme, "looped")
else
self.played_sound = xr_sound.get_sound_object(self.st.theme, "seq")
end
if self.played_sound ~= nil then
self.played_sound:play_at_pos (self.object, self.object:position ())
else
self.st.signals["theme_end"] = true
end
self.first_sound = false
end
if self.last_update == 0 then
self.last_update = device ():time_global ()
else
if device ():time_global () - self.last_update > 50 then
self.last_update = 0
else
return
end
end
if self.played_sound ~= nil then
if self.played_sound:playing () == false then
if self.first_sound == false then
self.st.signals["sound_end"] = true
end
self.st.sound_set = true
if self.st.pause_min ~= 0 or self.st.pause_max ~= 0 then
local time = math.random (self.st.pause_min, self.st.pause_max)
self.st.pause_time = device ():time_global () + time
end
self.first_sound = false
else
self.played_sound:set_position (self.object:position ())
end
local volume = xr_logic.pick_section_from_condlist(db.actor, self.object, self.st.volume)
if (volume and volume ~= "") then
self.played_sound.volume = tonumber(volume)
end
end
end
function snd_source:deactivate ()
if self.played_sound ~= nil then
self.played_sound:stop ()
self.played_sound = nil
end
end
function add_to_binder (npc, ini, scheme, section, storage)
local new_action = snd_source (npc, storage)
-- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> actions, <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> reset_scheme <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>:
xr_logic.subscribe_action_for_events(npc, storage, new_action)
end
function set_scheme(npc, ini, scheme, section, gulag_name)
local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
st.logic = xr_logic.cfg_get_switch_conditions(ini, section, npc)
st.theme = ini:r_string_ex(section,"snd")
st.looped = ini:r_bool_ex(section,"looped",false)
st.random = ini:r_bool_ex(section,"random",true)
st.pause_min = ini:r_float_ex(section,"min_idle") or 0
st.pause_max = ini:r_float_ex(section,"max_idle") or 0
st.no_hit = ini:r_bool_ex(section,"no_hit",true)
st.volume = xr_logic.parse_condlist(npc, section, "volume",ini:r_string_ex(section,"volume") or "1")
if st.pause_max < st.pause_min then
abort ("PH_SOUND - invalid time range !!!")
end
end