GameOver sound

pull/26/head
Sascha 2023-10-30 09:21:05 +07:00
parent e873873a78
commit fd2c37f30b
11 changed files with 141 additions and 26 deletions

@ -1,6 +1,6 @@
<Project Sdk="Godot.NET.Sdk/4.2.0-beta.3">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
</Project>

@ -1,15 +1,17 @@
[gd_scene load_steps=14 format=3 uid="uid://det8556rpxhbv"]
[gd_scene load_steps=16 format=3 uid="uid://det8556rpxhbv"]
[ext_resource type="PackedScene" uid="uid://tlr55u0gn20l" path="res://Packed-Scenes/PlayerShip.tscn" id="1_njb5h"]
[ext_resource type="Texture2D" uid="uid://cg6n1hh3lj7rn" path="res://Textures/tile_nebula_green_dff.png" id="2_43vix"]
[ext_resource type="Script" path="res://Scripts/Background.cs" id="3_imgbw"]
[ext_resource type="PackedScene" uid="uid://6dn1gjqffnt" path="res://Packed-Scenes/asteroid_01.tscn" id="5_do6ba"]
[ext_resource type="AudioStream" uid="uid://cyleigtlbv5o8" path="res://Sounds/explosion_player.wav" id="6_h2mtt"]
[ext_resource type="PackedScene" uid="uid://cvlxm2yrohsca" path="res://Packed-Scenes/asteroid_02.tscn" id="6_tqoe7"]
[ext_resource type="PackedScene" uid="uid://brqqgidqchi88" path="res://Packed-Scenes/asteroid_03.tscn" id="7_v6ul2"]
[ext_resource type="Script" path="res://Scripts/SoundManager.cs" id="8_4ksf0"]
[ext_resource type="AudioStream" uid="uid://bthqgu8sulv77" path="res://Sounds/music_background.wav" id="8_4ms0p"]
[ext_resource type="AudioStream" uid="uid://t0y8yw2ceskq" path="res://Sounds/explosion_asteroid.wav" id="9_2dqf6"]
[ext_resource type="Script" path="res://Scripts/GameManager.cs" id="9_rsrr5"]
[ext_resource type="AudioStream" uid="uid://bspwjmgsoxkbn" path="res://Sounds/gameover2.wav" id="9_rtdps"]
[ext_resource type="AudioStream" uid="uid://dlml6o3xscxqf" path="res://Sounds/weapon_player.wav" id="11_bbedd"]
[ext_resource type="Script" path="res://Scripts/GameArea.cs" id="11_p0rh7"]
@ -69,19 +71,26 @@ freeze_mode = 1
transform = Transform3D(15, 0, 0, 0, 15, 0, 0, 0, 15, 0, 0, 0)
shape = SubResource("BoxShape3D_bw288")
[node name="SoundManager" type="Node" parent="." node_paths=PackedStringArray("backgroundMusic", "explosionSound", "laserSound")]
[node name="SoundManager" type="Node" parent="." node_paths=PackedStringArray("backgroundMusic", "gameOverMusic", "asteroidExplosionSound", "playerExplosionSound", "laserSound")]
script = ExtResource("8_4ksf0")
backgroundMusic = NodePath("BackgroundMusic")
explosionSound = NodePath("ExplosionSound")
gameOverMusic = NodePath("GameOverMusic")
asteroidExplosionSound = NodePath("AsteroidExplosionSound")
playerExplosionSound = NodePath("PlayerExplosionSound")
laserSound = NodePath("LaserSound")
[node name="ExplosionSound" type="AudioStreamPlayer3D" parent="SoundManager"]
[node name="AsteroidExplosionSound" type="AudioStreamPlayer3D" parent="SoundManager"]
stream = ExtResource("9_2dqf6")
[node name="PlayerExplosionSound" type="AudioStreamPlayer3D" parent="SoundManager"]
stream = ExtResource("6_h2mtt")
[node name="BackgroundMusic" type="AudioStreamPlayer" parent="SoundManager"]
stream = ExtResource("8_4ms0p")
volume_db = -20.0
autoplay = true
[node name="GameOverMusic" type="AudioStreamPlayer" parent="SoundManager"]
stream = ExtResource("9_rtdps")
[node name="LaserSound" type="AudioStreamPlayer3D" parent="SoundManager"]
stream = ExtResource("11_bbedd")
@ -169,6 +178,7 @@ scroll_active = false
shortcut_keys_enabled = false
[node name="LabelGameOver" type="RichTextLabel" parent="Control"]
visible = false
layout_mode = 2
offset_left = 12.0
offset_top = 437.0

@ -44,7 +44,6 @@ public partial class Asteroid : Node3D {
// // Explode(collisionPosition);
// break;
case PlayerShip:
GameManager.Instance.Lives--;
PlayerShip.Instance.Explode(collisionPosition);
Explode(collisionPosition);
break;
@ -65,7 +64,7 @@ public partial class Asteroid : Node3D {
public void Explode(Vector3 exPosition) {
if (exPosition != Vector3.Zero) {
SoundManager.Instance.PlayExplosion(exPosition);
SoundManager.Instance.Play(SoundManager.Sound.AsteroidExplosion, exPosition);
if (explosion?.Instantiate() is GpuParticles3D ex) {
GetParent().AddChild(ex);
ex.Position = exPosition;

@ -40,7 +40,7 @@ public partial class GameManager : Node {
get => lives;
set {
lives = value;
if (lives <= 0) GameOver = true;
if (lives <= 0) SetGameOver();
RefreshUi();
}
}
@ -84,6 +84,7 @@ public partial class GameManager : Node {
public override void _Ready() {
Instance = this;
spawnTimer = spawnRate;
SoundManager.Instance.Play(SoundManager.Sound.BackgroundMusic);
RefreshUi();
}
@ -125,4 +126,10 @@ public partial class GameManager : Node {
public void OnGameAreaBodyExited(Node body) {
GD.Print($"{body.Name} leaved GameArea!");
}
private void SetGameOver() {
GameOver = true;
SoundManager.Instance.Stop(SoundManager.Sound.BackgroundMusic);
SoundManager.Instance.Play(SoundManager.Sound.GameOver);
}
}

@ -58,7 +58,7 @@ public partial class PlayerShip : Node3D {
if (laser.Instantiate() is Node3D shot) {
shot.Position = PlayerRb.Position + new Vector3(0f, 0f, 0.74f);
shots.AddChild(shot);
SoundManager.Instance.PlayLaserSound(PlayerRb.Position);
SoundManager.Instance.Play(SoundManager.Sound.Laser, PlayerRb.Position);
}
}
@ -68,7 +68,6 @@ public partial class PlayerShip : Node3D {
jet.Visible = true;
KinematicCollision3D collision = PlayerRb.MoveAndCollide(moveDir * (float)delta);
if (collision?.GetCollider() is Node3D collider) {
Vector3 collisionPosition = collision.GetPosition();
Node3D parent = collider.GetParent<Node3D>();
switch (parent) {
case GameArea:
@ -91,7 +90,7 @@ public partial class PlayerShip : Node3D {
ex.Emitting = true;
}
SoundManager.Instance.PlayExplosion(collisionPosition);
SoundManager.Instance.Play(SoundManager.Sound.PlayerExplosion, collisionPosition);
GameManager.Instance.Lives--;
}
}

@ -1,26 +1,78 @@
using System;
using System.Runtime.InteropServices;
using Godot;
namespace Scripts;
public partial class SoundManager : Node {
[Export] private AudioStreamPlayer backgroundMusic;
[Export] private AudioStreamPlayer3D explosionSound;
[Export] private AudioStreamPlayer gameOverMusic;
[Export] private AudioStreamPlayer3D asteroidExplosionSound;
[Export] private AudioStreamPlayer3D playerExplosionSound;
[Export] private AudioStreamPlayer3D laserSound;
public static SoundManager Instance { get; private set; }
public override void _Ready() {
Instance = this;
public enum Sound {
BackgroundMusic,
AsteroidExplosion,
PlayerExplosion,
Laser,
GameOver
}
public void PlayExplosion(Vector3 position) {
explosionSound.Position = position;
explosionSound.PitchScale = (float)GD.RandRange(0.25f, 1.75f);
explosionSound.Play();
public override void _Ready() => Instance = this;
public void Play(Sound sound, [Optional] Vector3 position) {
float pitchScale = 0;
if (sound is Sound.Laser or Sound.AsteroidExplosion or Sound.PlayerExplosion) pitchScale = (float)GD.RandRange(0.25f, 1.75f);
switch (sound) {
case Sound.AsteroidExplosion:
asteroidExplosionSound.Position = position;
asteroidExplosionSound.PitchScale = pitchScale;
asteroidExplosionSound.Play();
break;
case Sound.Laser:
laserSound.Position = position;
laserSound.PitchScale = pitchScale;
laserSound.Play();
break;
case Sound.PlayerExplosion:
playerExplosionSound.Position = position;
playerExplosionSound.PitchScale = pitchScale;
playerExplosionSound.Play();
break;
case Sound.BackgroundMusic:
backgroundMusic.Play();
break;
case Sound.GameOver:
gameOverMusic.Play();
break;
default:
throw new ArgumentOutOfRangeException(nameof(sound), sound, null);
}
}
public void PlayLaserSound(Vector3 position) {
laserSound.Position = position;
laserSound.PitchScale = (float)GD.RandRange(0.25f, 1.75f);
laserSound.Play();
public void Stop(Sound sound) {
switch (sound) {
case Sound.AsteroidExplosion:
asteroidExplosionSound.Stop();
break;
case Sound.Laser:
laserSound.Stop();
break;
case Sound.PlayerExplosion:
playerExplosionSound.Stop();
break;
case Sound.BackgroundMusic:
backgroundMusic.Stop();
break;
case Sound.GameOver:
gameOverMusic.Stop();
break;
default:
throw new ArgumentOutOfRangeException(nameof(sound), sound, null);
}
}
}

Binary file not shown.

@ -0,0 +1,24 @@
[remap]
importer="wav"
type="AudioStreamWAV"
uid="uid://boo3vdjp3i3if"
path="res://.godot/imported/gameover.wav-f2ebf5eb7584291f79759c2ab9f5cabc.sample"
[deps]
source_file="res://Sounds/gameover.wav"
dest_files=["res://.godot/imported/gameover.wav-f2ebf5eb7584291f79759c2ab9f5cabc.sample"]
[params]
force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop_mode=0
edit/loop_begin=0
edit/loop_end=-1
compress/mode=0

Binary file not shown.

Binary file not shown.

@ -0,0 +1,24 @@
[remap]
importer="wav"
type="AudioStreamWAV"
uid="uid://bspwjmgsoxkbn"
path="res://.godot/imported/gameover2.wav-5f9b1f4fc6807498ae9eb6738b6d0ddf.sample"
[deps]
source_file="res://Sounds/gameover2.wav"
dest_files=["res://.godot/imported/gameover2.wav-5f9b1f4fc6807498ae9eb6738b6d0ddf.sample"]
[params]
force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop_mode=0
edit/loop_begin=0
edit/loop_end=-1
compress/mode=0