Skip to content

Commit

Permalink
Merge pull request #59 from miketvo/ssssss/feature/game-sounds
Browse files Browse the repository at this point in the history
[ssssss] Game Sounds
  • Loading branch information
miketvo authored Aug 28, 2024
2 parents d03fcf6 + dcaf76b commit d3d567a
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 31 deletions.
7 changes: 5 additions & 2 deletions p0ng/scripts/core/sfx_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ func play_sound(audio_stream_name: StringName) -> void:


## Plays a child [AudioStreamPlayer2D].
func play_sound2d(audio_stream_name: StringName, position: Vector2) -> void:
func play_sound2d(audio_stream_name: StringName, position: Vector2, global: bool = true) -> void:
var audio_stream_player: AudioStreamPlayer2D = get_node("%s" % audio_stream_name)
audio_stream_player.global_position = position
if global:
audio_stream_player.global_position = position
else:
audio_stream_player.position = position
audio_stream_player.play()

#endregion
Expand Down
5 changes: 3 additions & 2 deletions ssssss/assets/default_bus_layout.tres
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hipass = 0.08
wet = 0.25

[resource]
bus/0/volume_db = 0.00948906
bus/0/effect/0/effect = SubResource("AudioEffectLimiter_js1wn")
bus/0/effect/0/enabled = true
bus/1/name = &"GlobalEffects"
Expand All @@ -30,11 +31,11 @@ bus/2/name = &"UI"
bus/2/solo = false
bus/2/mute = false
bus/2/bypass_fx = false
bus/2/volume_db = -16.0
bus/2/volume_db = -16.106
bus/2/send = &"GlobalEffects"
bus/3/name = &"Gameplay"
bus/3/solo = false
bus/3/mute = false
bus/3/bypass_fx = false
bus/3/volume_db = 0.0
bus/3/volume_db = 2.2426
bus/3/send = &"UI"
Binary file added ssssss/assets/sfx/food_eaten.wav
Binary file not shown.
24 changes: 24 additions & 0 deletions ssssss/assets/sfx/food_eaten.wav.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://bxffji76hmx4e"
path="res://.godot/imported/food_eaten.wav-5976631f301990fbb393e893a9c06aa6.sample"

[deps]

source_file="res://assets/sfx/food_eaten.wav"
dest_files=["res://.godot/imported/food_eaten.wav-5976631f301990fbb393e893a9c06aa6.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 added ssssss/assets/sfx/game_beaten.wav
Binary file not shown.
24 changes: 24 additions & 0 deletions ssssss/assets/sfx/game_beaten.wav.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://jujqj345ygj8"
path="res://.godot/imported/game_beaten.wav-9e08073be99c4f038f581fb76927f3c4.sample"

[deps]

source_file="res://assets/sfx/game_beaten.wav"
dest_files=["res://.godot/imported/game_beaten.wav-9e08073be99c4f038f581fb76927f3c4.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 added ssssss/assets/sfx/level_lost.wav
Binary file not shown.
24 changes: 24 additions & 0 deletions ssssss/assets/sfx/level_lost.wav.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://di5o2v7rwwtf3"
path="res://.godot/imported/level_lost.wav-9fa2bb8f8fe3f1b6fe84de4ddf02aaa1.sample"

[deps]

source_file="res://assets/sfx/level_lost.wav"
dest_files=["res://.godot/imported/level_lost.wav-9fa2bb8f8fe3f1b6fe84de4ddf02aaa1.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 added ssssss/assets/sfx/level_won.wav
Binary file not shown.
24 changes: 24 additions & 0 deletions ssssss/assets/sfx/level_won.wav.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://cfwma0y7vnnlu"
path="res://.godot/imported/level_won.wav-7da469e8d6fc80a44195b142a811bb59.sample"

[deps]

source_file="res://assets/sfx/level_won.wav"
dest_files=["res://.godot/imported/level_won.wav-7da469e8d6fc80a44195b142a811bb59.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 added ssssss/assets/sfx/snake_collide.wav
Binary file not shown.
24 changes: 24 additions & 0 deletions ssssss/assets/sfx/snake_collide.wav.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://hj33qaf21w5"
path="res://.godot/imported/snake_collide.wav-d9e74059279601b51335be68afaf552c.sample"

[deps]

source_file="res://assets/sfx/snake_collide.wav"
dest_files=["res://.godot/imported/snake_collide.wav-d9e74059279601b51335be68afaf552c.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
2 changes: 1 addition & 1 deletion ssssss/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ config_version=5
[application]

config/name="ssssss"
config/version="0.5.3"
config/version="0.5.4"
run/main_scene="res://scenes/main.tscn"
config/use_custom_user_dir=true
config/custom_user_dir_name="ambientlamp/ssssss"
Expand Down
34 changes: 33 additions & 1 deletion ssssss/scenes/game/game.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=3 uid="uid://ii1krswp2yx6"]
[gd_scene load_steps=15 format=3 uid="uid://ii1krswp2yx6"]

[ext_resource type="Script" path="res://scripts/scenes/game/game.gd" id="1_y2h2x"]
[ext_resource type="PackedScene" uid="uid://b8yj1ahfqm5yo" path="res://scenes/game/game_ui.tscn" id="2_t11qt"]
Expand All @@ -8,6 +8,12 @@
[ext_resource type="Script" path="res://scripts/core/state_machine/state.gd" id="6_li0jf"]
[ext_resource type="Script" path="res://scripts/scenes/game/game_state_controller/classic_state.gd" id="6_svpid"]
[ext_resource type="Script" path="res://scripts/scenes/game/game_state_controller/stop_state.gd" id="8_x3kth"]
[ext_resource type="Script" path="res://scripts/core/sfx_controller.gd" id="9_62386"]
[ext_resource type="AudioStream" uid="uid://di5o2v7rwwtf3" path="res://assets/sfx/level_lost.wav" id="10_141y4"]
[ext_resource type="AudioStream" uid="uid://cfwma0y7vnnlu" path="res://assets/sfx/level_won.wav" id="11_2d5jk"]
[ext_resource type="AudioStream" uid="uid://jujqj345ygj8" path="res://assets/sfx/game_beaten.wav" id="12_vfwgr"]
[ext_resource type="AudioStream" uid="uid://bxffji76hmx4e" path="res://assets/sfx/food_eaten.wav" id="13_lgpxk"]
[ext_resource type="AudioStream" uid="uid://hj33qaf21w5" path="res://assets/sfx/snake_collide.wav" id="14_4wm4w"]

[node name="Game" type="Node2D"]
script = ExtResource("1_y2h2x")
Expand Down Expand Up @@ -46,3 +52,29 @@ script = ExtResource("6_li0jf")

[node name="StopState" type="Node" parent="GameStateController"]
script = ExtResource("8_x3kth")

[node name="SfxController" type="Node" parent="."]
script = ExtResource("9_62386")

[node name="LevelLostSfx" type="AudioStreamPlayer" parent="SfxController"]
stream = ExtResource("10_141y4")
volume_db = 2.0
bus = &"Gameplay"

[node name="LevelWonSfx" type="AudioStreamPlayer" parent="SfxController"]
stream = ExtResource("11_2d5jk")
bus = &"Gameplay"

[node name="GameBeatenSfx" type="AudioStreamPlayer" parent="SfxController"]
stream = ExtResource("12_vfwgr")
bus = &"Gameplay"

[node name="FoodEatenSfx" type="AudioStreamPlayer2D" parent="SfxController"]
stream = ExtResource("13_lgpxk")
volume_db = 6.0
bus = &"Gameplay"

[node name="SnakeCollideSfx" type="AudioStreamPlayer2D" parent="SfxController"]
stream = ExtResource("14_4wm4w")
volume_db = 16.0
bus = &"Gameplay"
7 changes: 5 additions & 2 deletions ssssss/scripts/core/sfx_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ func play_sound(audio_stream_name: StringName) -> void:


## Plays a child [AudioStreamPlayer2D].
func play_sound2d(audio_stream_name: StringName, position: Vector2) -> void:
func play_sound2d(audio_stream_name: StringName, position: Vector2, global: bool = true) -> void:
var audio_stream_player: AudioStreamPlayer2D = get_node("%s" % audio_stream_name)
audio_stream_player.global_position = position
if global:
audio_stream_player.global_position = position
else:
audio_stream_player.position = position
audio_stream_player.play()

#endregion
Expand Down
38 changes: 27 additions & 11 deletions ssssss/scripts/scenes/game/game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var _level: Dictionary
var _level_scene: PackedScene
var _paused: bool

@onready var _game_ui: UI = $UI/GameUI
@onready var _game_stop_state: State = $GameStateController/StopState
@onready var _game_ui: UI = $UI/GameUI
@onready var _sfx_controller: SfxController = $SfxController


# ============================================================================ #
Expand All @@ -42,7 +43,7 @@ func _ready() -> void:

game_world = _level_scene.instantiate()
game_world.initial_step_duration = Global.INITIAL_STEP_DURATION
game_world.food_eaten.connect(_on_game_world_food_eaten.unbind(1))
game_world.food_eaten.connect(_on_game_world_food_eaten)
game_world.snake_collided.connect(_on_game_world_snake_collided)
add_child(game_world)
level_loaded.emit()
Expand Down Expand Up @@ -76,14 +77,19 @@ func is_paused() -> bool:
# Listens to $GameStateController/StopState.stopped().
func _on_game_ended() -> void:
game_ended.emit()
if game_result == GameResult.GAME_LOST:
game_world.play_tile_map_animation("game_lost")

# Give the player a bit of time to register the game result.
await get_tree().create_timer(GAME_END_DELAY).timeout
match game_result:
GameResult.GAME_WON:
if Global.current_game_mode < Global.levels.size():
_sfx_controller.play_sound("LevelWonSfx")
else:
_sfx_controller.play_sound("GameBeatenSfx")
await get_tree().create_timer(GAME_END_DELAY).timeout
game_world.play_tile_map_animation("game_won")
GameResult.GAME_LOST:
_sfx_controller.play_sound("LevelLostSfx")
game_world.play_tile_map_animation("game_lost")
await get_tree().create_timer(GAME_END_DELAY).timeout

if game_result == GameResult.GAME_WON:
game_world.play_tile_map_animation("game_won")
_game_ui.end_game()


Expand All @@ -103,14 +109,24 @@ func _on_game_ui_acted(action: StringName) -> void:
scene_finished.emit(SceneKey.MAIN_MENU)


# Listens to _game_world.food_eaten(snake_id: int, food_coords: Vector2i).unbind(1).
func _on_game_world_food_eaten(snake_id: int) -> void:
# Listens to _game_world.food_eaten(snake_id: int, food_coords: Vector2i).
func _on_game_world_food_eaten(snake_id: int, food_coords: Vector2i) -> void:
var food_global_position: Vector2 = transform * (
game_world.transform *
game_world.get_cell_position(food_coords, true, true)
)
_sfx_controller.play_sound2d("FoodEatenSfx", food_global_position)
if snake_id == 0:
player_ate_food.emit()


# Listens to _game_world.snake_collided(snake_id: int, food_coords: Vector2i).
func _on_game_world_snake_collided(snake_id: int, collide_coords: Vector2i) -> void:
var collide_global_position: Vector2 = transform * (
game_world.transform *
game_world.get_cell_position(collide_coords, true, true)
)
_sfx_controller.play_sound2d("SnakeCollideSfx", collide_global_position)
if (
snake_id != 0 and
collide_coords in game_world.snakes[0]
Expand Down
9 changes: 1 addition & 8 deletions ssssss/scripts/scenes/world/render_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
extends Node


signal snake_death_animation_finished

const SnakeDeath: PackedScene = preload("res://scenes/world/snake_death.tscn")

@export var world: World:
Expand Down Expand Up @@ -270,19 +268,14 @@ func _set_cells_terrain_path_wrapped(

func _play_snake_death_animation(snake_id: int) -> void:
var snake_cell_coords: Array[Vector2i] = world.snakes[snake_id]
var tile_size: Vector2i = world.get_tile_size()
for cell_coords in snake_cell_coords:
var snake_death_sprite: Sprite2D = SnakeDeath.instantiate()
snake_death_sprite.modulate =\
Global.COLOR_PALETTE["fg_1"] if snake_id == 0\
else Global.COLOR_PALETTE["fg_0"]
snake_death_sprite.position = Vector2i(
cell_coords.x * tile_size.x,
cell_coords.y * tile_size.y
)
snake_death_sprite.position = world.get_cell_position(cell_coords)
tile_map.add_child(snake_death_sprite)
await get_tree().create_timer(0.03).timeout
snake_death_animation_finished.emit()

#endregion
# ============================================================================ #
13 changes: 11 additions & 2 deletions ssssss/scripts/scenes/world/world.gd
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ var food_grid: WorldGrid2D


@onready var _tile_map: Node2D = $TileMap
@onready var _render_controller: Node = $RenderController
@onready var _build_state: State = $WorldStateController/BuildState
@onready var _run_state: State = $WorldStateController/RunState

Expand Down Expand Up @@ -151,6 +150,17 @@ func _ready() -> void:

# ============================================================================ #
#region Public methods
func get_cell_position(
cell_coords: Vector2i,
center: bool = false,
include_tile_map_transform: bool = false
) -> Vector2:
var result_position: Vector2 = Vector2(get_tile_size() * cell_coords)
if center: result_position += Vector2(get_tile_size()) / 2
if include_tile_map_transform: result_position = _tile_map.transform * result_position
return result_position


func spawn_snake(
spawn_coords: Vector2i, direction: Vector2i, length: int,
debug: bool = false
Expand Down Expand Up @@ -294,7 +304,6 @@ func _on_step() -> void:
# Listens to _run_state.snake_collided(snake_id: int, collide_coords: Vector2i).
func _on_snake_collided(snake_id: int, collide_coords: Vector2i) -> void:
snake_collided.emit(snake_id, collide_coords)
await _render_controller.snake_death_animation_finished
if snake_id > 0:
despawn_snake(snake_id)

Expand Down
7 changes: 5 additions & 2 deletions template/scripts/core/sfx_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ func play_sound(audio_stream_name: StringName) -> void:


## Plays a child [AudioStreamPlayer2D].
func play_sound2d(audio_stream_name: StringName, position: Vector2) -> void:
func play_sound2d(audio_stream_name: StringName, position: Vector2, global: bool = true) -> void:
var audio_stream_player: AudioStreamPlayer2D = get_node("%s" % audio_stream_name)
audio_stream_player.global_position = position
if global:
audio_stream_player.global_position = position
else:
audio_stream_player.position = position
audio_stream_player.play()

#endregion
Expand Down

0 comments on commit d3d567a

Please sign in to comment.