45 lines
1.5 KiB
GDScript
45 lines
1.5 KiB
GDScript
extends RefCounted
|
|
class_name WeaponAudio
|
|
|
|
const SOUND_PATHS := {
|
|
ItemDefinition.WeaponKind.GAMMA: "res://assets/sounds/effects/radiation.mp3",
|
|
ItemDefinition.WeaponKind.PLASMA: "res://assets/sounds/effects/plasma.mp3",
|
|
ItemDefinition.WeaponKind.MACHINE_GUN: "res://assets/sounds/effects/machine_gun.mp3",
|
|
ItemDefinition.WeaponKind.LASER: "res://assets/sounds/effects/laser.mp3",
|
|
ItemDefinition.WeaponKind.MISSILE: "res://assets/sounds/effects/rocket.mp3",
|
|
}
|
|
|
|
static var _streams: Dictionary = {}
|
|
|
|
const BUS_NAMES := {
|
|
ItemDefinition.WeaponKind.GAMMA: &"Plasma",
|
|
ItemDefinition.WeaponKind.PLASMA: &"Plasma",
|
|
ItemDefinition.WeaponKind.MACHINE_GUN: &"MachineGun",
|
|
ItemDefinition.WeaponKind.LASER: &"Laser",
|
|
ItemDefinition.WeaponKind.MISSILE: &"Missile",
|
|
}
|
|
|
|
static func play(parent: Node, source_position: Vector2, weapon_kind: int, volume_db: float = 0.0) -> void:
|
|
if not is_instance_valid(parent):
|
|
return
|
|
|
|
if not SOUND_PATHS.has(weapon_kind):
|
|
return
|
|
if not _streams.has(weapon_kind):
|
|
var sound_path: String = SOUND_PATHS[weapon_kind]
|
|
_streams[weapon_kind] = load(sound_path) as AudioStreamMP3
|
|
var stream := _streams[weapon_kind] as AudioStreamMP3
|
|
if stream == null:
|
|
return
|
|
|
|
var player := AudioStreamPlayer2D.new()
|
|
player.stream = stream
|
|
player.bus = BUS_NAMES[weapon_kind]
|
|
player.volume_db = volume_db
|
|
player.max_distance = 1500.0
|
|
player.attenuation = 1.0
|
|
parent.add_child(player)
|
|
player.global_position = source_position
|
|
player.finished.connect(player.queue_free)
|
|
player.play()
|