Class: Doom::Game::SoundEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/doom/game/sound_engine.rb

Overview

Plays sound effects for game events.

Constant Summary collapse

WEAPON_SOUNDS =

Weapon fire sounds

{
  PlayerState::WEAPON_FIST => 'DSPUNCH',
  PlayerState::WEAPON_PISTOL => 'DSPISTOL',
  PlayerState::WEAPON_SHOTGUN => 'DSSHOTGN',
  PlayerState::WEAPON_CHAINGUN => 'DSPISTOL',
  PlayerState::WEAPON_ROCKET => 'DSRLAUNC',
  PlayerState::WEAPON_PLASMA => 'DSPLASMA',
  PlayerState::WEAPON_BFG => 'DSBFG',
  PlayerState::WEAPON_CHAINSAW => 'DSSAWIDL'
}.freeze
MONSTER_SEE =

Monster see/activation sounds (from mobjinfo seesound)

{
  3004 => 'DSPOSIT1', # Zombieman
  9 => 'DSSGTSIT', # Shotgun Guy
  3001 => 'DSBGSIT1',  # Imp
  3002 => 'DSSGTSIT',  # Demon
  58 => 'DSSGTSIT', # Spectre
  3003 => 'DSBRSSIT', # Baron
  69 => 'DSBRSSIT', # Hell Knight
  3005 => 'DSCACSIT',  # Cacodemon
  3006 => 'DSSKLATK',  # Lost Soul
  65 => 'DSPOSIT2',  # Heavy Weapon Dude
  16 => 'DSCYBSIT',  # Cyberdemon
  7 => 'DSSPISIT' # Spider Mastermind
}.freeze
MONSTER_DEATH =

Monster death sounds (from mobjinfo deathsound)

{
  3004 => 'DSPODTH1', # Zombieman
  9 => 'DSSGTDTH', # Shotgun Guy
  3001 => 'DSBGDTH1',  # Imp
  3002 => 'DSSGTDTH',  # Demon
  58 => 'DSSGTDTH', # Spectre
  3003 => 'DSBRSDTH', # Baron
  69 => 'DSBRSDTH', # Hell Knight
  3005 => 'DSCACDTH',  # Cacodemon
  3006 => 'DSFIRXPL',  # Lost Soul
  65 => 'DSPODTH2',  # Heavy Weapon Dude
  16 => 'DSCYBDTH',  # Cyberdemon
  7 => 'DSSPIDTH' # Spider Mastermind
}.freeze
MONSTER_PAIN =

Monster pain sounds (from mobjinfo painsound)

{
  3004 => 'DSPOPAIN', # Zombieman
  9 => 'DSPOPAIN', # Shotgun Guy
  3001 => 'DSDMPAIN',  # Imp
  3002 => 'DSDMPAIN',  # Demon
  58 => 'DSDMPAIN', # Spectre
  3003 => 'DSDMPAIN', # Baron
  69 => 'DSDMPAIN', # Hell Knight
  3005 => 'DSDMPAIN',  # Cacodemon
  3006 => 'DSDMPAIN',  # Lost Soul
  65 => 'DSPOPAIN' # Heavy Weapon Dude
}.freeze
MONSTER_ATTACK =

Monster attack sounds (from mobjinfo attacksound)

{
  3004 => 'DSPISTOL', # Zombieman: pistol
  9 => 'DSSHOTGN', # Shotgun Guy: shotgun
  3001 => 'DSFIRSHT',  # Imp: fireball launch
  3002 => 'DSSGTATK',  # Demon: bite
  58 => 'DSSGTATK', # Spectre: bite
  3003 => 'DSFIRSHT', # Baron: fireball
  69 => 'DSFIRSHT', # Hell Knight: fireball
  3005 => 'DSFIRSHT', # Cacodemon: fireball
  65 => 'DSSHOTGN'  # Heavy Weapon Dude: chaingun burst
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(sound_manager) ⇒ SoundEngine

Returns a new instance of SoundEngine.



78
79
80
81
# File 'lib/doom/game/sound_engine.rb', line 78

def initialize(sound_manager)
  @sounds = sound_manager
  @last_played = {} # Throttle rapid repeats
end

Instance Method Details

#door_closeObject



137
138
139
# File 'lib/doom/game/sound_engine.rb', line 137

def door_close
  play('DSDORCLS', throttle: 0.2)
end

#door_openObject

--- Door/environment sounds ---



133
134
135
# File 'lib/doom/game/sound_engine.rb', line 133

def door_open
  play('DSDOROPN', throttle: 0.2)
end

#explosionObject

--- Explosions / impacts ---



171
172
173
# File 'lib/doom/game/sound_engine.rb', line 171

def explosion
  play('DSBAREXP')
end

#item_pickupObject



120
121
122
# File 'lib/doom/game/sound_engine.rb', line 120

def item_pickup
  play('DSITEMUP', throttle: 0.1)
end


101
102
103
# File 'lib/doom/game/sound_engine.rb', line 101

def menu_back
  play('DSSWTCHX')
end

--- Menu sounds ---



93
94
95
# File 'lib/doom/game/sound_engine.rb', line 93

def menu_move
  play('DSPSTOP', throttle: 0.05)
end


97
98
99
# File 'lib/doom/game/sound_engine.rb', line 97

def menu_select
  play('DSPISTOL')
end

#monster_attack(type) ⇒ Object



165
166
167
168
# File 'lib/doom/game/sound_engine.rb', line 165

def monster_attack(type)
  sound = MONSTER_ATTACK[type]
  play(sound, throttle: 0.1) if sound
end

#monster_death(type) ⇒ Object



155
156
157
158
# File 'lib/doom/game/sound_engine.rb', line 155

def monster_death(type)
  sound = MONSTER_DEATH[type]
  play(sound) if sound
end

#monster_pain(type) ⇒ Object



160
161
162
163
# File 'lib/doom/game/sound_engine.rb', line 160

def monster_pain(type)
  sound = MONSTER_PAIN[type]
  play(sound, throttle: 0.2) if sound
end

#monster_see(type) ⇒ Object

--- Monster sounds ---



150
151
152
153
# File 'lib/doom/game/sound_engine.rb', line 150

def monster_see(type)
  sound = MONSTER_SEE[type]
  play(sound, throttle: 0.5) if sound
end

#nowayObject



128
129
130
# File 'lib/doom/game/sound_engine.rb', line 128

def noway
  play('DSNOWAY', throttle: 0.3)
end

#platform_startObject



141
142
143
# File 'lib/doom/game/sound_engine.rb', line 141

def platform_start
  play('DSPSTART', throttle: 0.2)
end

#platform_stopObject



145
146
147
# File 'lib/doom/game/sound_engine.rb', line 145

def platform_stop
  play('DSPSTOP', throttle: 0.2)
end

#play(name, volume: 1.0, throttle: 0) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/doom/game/sound_engine.rb', line 83

def play(name, volume: 1.0, throttle: 0)
  now = Time.now.to_f
  return if (throttle > 0) && @last_played[name] && (now - @last_played[name]) < throttle

  sample = @sounds[name]
  sample&.play(volume)
  @last_played[name] = now
end

#player_deathObject



116
117
118
# File 'lib/doom/game/sound_engine.rb', line 116

def player_death
  play('DSPLDETH')
end

#player_painObject

--- Player events ---



112
113
114
# File 'lib/doom/game/sound_engine.rb', line 112

def player_pain
  play('DSPLPAIN', throttle: 0.3)
end

#weapon_fire(weapon) ⇒ Object

--- Weapon events ---



106
107
108
109
# File 'lib/doom/game/sound_engine.rb', line 106

def weapon_fire(weapon)
  sound = WEAPON_SOUNDS[weapon]
  play(sound, throttle: 0.05) if sound
end

#weapon_pickupObject



124
125
126
# File 'lib/doom/game/sound_engine.rb', line 124

def weapon_pickup
  play('DSWPNUP')
end