Added require_all method to replace explicitly requiring every source file, added SoundManager and sound effects, added sound for shield/health regen

This commit is contained in:
2020-05-08 19:03:45 -05:00
parent bc695df4a1
commit e3a2c9abe0
13 changed files with 197 additions and 106 deletions

View File

@@ -0,0 +1,53 @@
class IMICFPS
module SoundManager
extend CyberarmEngine::Common
MASTERS = {}
EFFECTS = []
PLAYLISTS = {}
def self.master_volume
1.0
end
def self.music_volume
0.25 * master_volume
end
def self.sfx_volume
0.5 * master_volume
end
def self.load_master(package)
return if MASTERS.dig(package)
yaml = YAML.load_file( "#{IMICFPS.assets_path}/#{package}/shared/sound/master.yaml" )
MASTERS[package] = yaml
end
def self.sound(package, name)
if data = sound_data(package, name.to_s)
get_sample("#{IMICFPS.assets_path}/#{package}/shared/sound/#{data["path"]}")
else
raise "Missing sound: '#{name}' in package '#{package}'"
end
end
def self.sound_data(package, name)
load_master(package)
if master = MASTERS.dig(package)
return master["sounds"].find { |s| s["name"] == name }
end
return nil
end
def self.sound_effect(klass, options)
EFFECTS << klass.new(options)
end
def self.update
EFFECTS.each { |e| e.update; EFFECTS.delete(e) if e.done? }
end
end
end

View File

@@ -29,7 +29,7 @@ class IMICFPS
if window.config.get(:options, :fps)
create_slot "FPS: #{Gosu.fps}"
create_slot "Frame time: #{Gosu.milliseconds - window.delta_time}ms" if window.config.get(:debug_options, :stats)
create_slot "Frame time: #{(Gosu.milliseconds - window.delta_time).to_s.rjust(3, "0")}ms" if window.config.get(:debug_options, :stats)
end
if window.config.get(:debug_options, :stats)

19
lib/sound_effect.rb Normal file
View File

@@ -0,0 +1,19 @@
class IMICFPS
class SoundEffect
attr_reader :sound, :options
def initialize(options = {})
raise "expected Hash, got #{options.class}" unless options.is_a?(Hash)
@options = options
raise "sound not specified!" unless @options[:sound]
setup
end
def setup
end
def update
end
end
end

View File

@@ -0,0 +1,32 @@
class IMICFPS
class SoundEffect
class FadeIn < SoundEffect
def setup
@start_time = Gosu.milliseconds
@duration = @options[:duration] # in milliseconds
@initial_volume = @options[:volume] ? @options[:volume] : 0.0
@sound = @options[:sound]
raise "duration not specified!" unless @duration
@channel = @sound.play(calculate_volume)
end
def ratio
(Gosu.milliseconds - @start_time.to_f) / @duration
end
def calculate_volume
volume = (SoundManager.sfx_volume - @initial_volume) * ratio
end
def update
@channel.volume = calculate_volume
end
def done?
(Gosu.milliseconds - @start_time.to_f) / @duration >= 1.0
end
end
end
end

View File

@@ -0,0 +1,22 @@
class IMICFPS
class SoundEffect
class FadeInAndOut < FadeIn
def setup
@hang_time = @options[:hang_time] ? @options[:hang_time] : 0.0
super
end
# TODO: Handle hang time
def ratio
r = super
if r < 0.5
r * 2
else
2.0 - (r * 2)
end
end
end
end
end

View File

@@ -0,0 +1,9 @@
class IMICFPS
class SoundEffect
class FadeOut < FadeIn
def ratio
1.0 - super
end
end
end
end

View File

@@ -0,0 +1,25 @@
class IMICFPS
class SoundEffect
class ShieldRegen < SoundEffect
def setup
@sound = SoundManager.sound("base", :shield_regen)
@player = @options[:player]
@channel = @sound.play(0.0, 0.0, true)
end
def ratio
@player.health
end
def update
@channel.speed = 0.5 + ratio / 2
@channel.volume = 1.0 - ratio / 2
end
def done?
ratio >= 1.0
end
end
end
end

View File

@@ -7,6 +7,7 @@ class IMICFPS
@start_time = Gosu.milliseconds
@time_to_live = 3_000
# SoundManager.sound_effect(SoundEffect::FadeIn, sound: SoundManager.sound("base", :shield_regen), duration: 3_000.0)
window.needs_cursor = false
end
@@ -32,6 +33,7 @@ class IMICFPS
@title.draw
fill(Gosu::Color.rgba(0,0,0, 255 * (1.2 - fraction_left)))
end
end

View File

@@ -68,6 +68,7 @@ class IMICFPS
@console.update if @show_console
@overlay.update
SoundManager.update
@number_of_vertices = 0
@delta_time = Gosu.milliseconds