35 lines
1.1 KiB
GDScript
35 lines
1.1 KiB
GDScript
extends RefCounted
|
|
class_name WeaponAudio
|
|
|
|
const SOUND_PATHS := {
|
|
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 = {}
|
|
|
|
static func play(parent: Node, source_position: Vector2, weapon_kind: int, volume_db: float = -6.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] = AudioStreamMP3.load_from_buffer(FileAccess.get_file_as_bytes(sound_path))
|
|
var stream := _streams[weapon_kind] as AudioStreamMP3
|
|
if stream == null:
|
|
return
|
|
|
|
var player := AudioStreamPlayer2D.new()
|
|
player.stream = stream
|
|
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()
|