Skip to content

Commit

Permalink
Extract unit voice events handler, closes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jan 3, 2025
1 parent de367b7 commit bf752c8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
8 changes: 7 additions & 1 deletion source/match/players/human/Human.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=5 format=3 uid="uid://dh8dedilkhuoy"]
[gd_scene load_steps=6 format=3 uid="uid://dh8dedilkhuoy"]

[ext_resource type="Script" path="res://source/match/players/human/Human.gd" id="1_7iu1j"]
[ext_resource type="Script" path="res://source/match/players/human/UnitActionsController.gd" id="1_m624e"]
[ext_resource type="PackedScene" uid="uid://q5w474dvts3f" path="res://source/match/players/human/StructurePlacementHandler.tscn" id="2_bbqrb"]
[ext_resource type="Script" path="res://source/match/players/human/VoiceNarratorController.gd" id="4_ljr74"]
[ext_resource type="Script" path="res://source/match/players/human/UnitVoicesController.gd" id="5_i4ssw"]

[node name="Human" type="Node3D"]
script = ExtResource("1_7iu1j")
Expand All @@ -20,3 +21,8 @@ script = ExtResource("4_ljr74")

[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="VoiceNarratorController"]
process_mode = 3

[node name="UnitVoicesController" type="Node" parent="."]
script = ExtResource("5_i4ssw")

[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="UnitVoicesController"]
41 changes: 41 additions & 0 deletions source/match/players/human/UnitVoicesController.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extends Node

const Structure = preload("res://source/match/units/Structure.gd")

var _last_ack_event = 0

@onready var _audio_player = find_child("AudioStreamPlayer")
@onready var _player = get_parent()


func _ready() -> void:
MatchSignals.unit_selected.connect(_on_unit_selected)
MatchSignals.unit_targeted.connect(_on_unit_action_requsted)
MatchSignals.terrain_targeted.connect(_on_unit_action_requsted)


func _handle_event(event):
if _audio_player.playing:
return
_audio_player.stream = Constants.Match.VoiceNarrator.EVENT_TO_ASSET_MAPPING[event]
_audio_player.play()


func _on_unit_selected(unit):
if not unit is Structure and unit.player == _player:
_handle_event(Constants.Match.VoiceNarrator.Events.UNIT_HELLO)
# TODO: handle building - perhaps with some sound instead of voice


func _on_unit_action_requsted(_ignore):
if get_tree().get_nodes_in_group("selected_units").any(
func(unit): return not unit is Structure and unit.player == _player
):
_handle_event(
(
Constants.Match.VoiceNarrator.Events.UNIT_ACK_1
if _last_ack_event == 0
else Constants.Match.VoiceNarrator.Events.UNIT_ACK_2
)
)
_last_ack_event = (_last_ack_event + 1) % 2
28 changes: 0 additions & 28 deletions source/match/players/human/VoiceNarratorController.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
extends Node

# TODO: consider using 2 players - 1 for voice narrator and 1 for units
# ...perhaps extract unit voices to other controller
# TODO: avoid spamming unit voices

const Structure = preload("res://source/match/units/Structure.gd")

const UNDER_ATTACK_NOTIFICATION_THRESHOLD_MS = 10 * 1000

var _last_ack_event = 0
var _last_event_handled = null
var _last_under_attack_notification_timestamp = 0

Expand All @@ -35,9 +30,6 @@ func _ready():
MatchSignals.unit_production_finished.connect(_on_production_finished)
MatchSignals.not_enough_resources_for_production.connect(_on_not_enough_resources)
MatchSignals.not_enough_resources_for_construction.connect(_on_not_enough_resources)
MatchSignals.unit_selected.connect(_on_unit_selected)
MatchSignals.unit_targeted.connect(_on_unit_action_requsted)
MatchSignals.terrain_targeted.connect(_on_unit_action_requsted)
MatchSignals.unit_construction_finished.connect(_on_construction_finished)


Expand Down Expand Up @@ -99,23 +91,3 @@ func _on_construction_finished(unit):
func _on_not_enough_resources(player):
if player == get_parent():
_handle_event(Constants.Match.VoiceNarrator.Events.NOT_ENOUGH_RESOURCES)


func _on_unit_selected(unit):
if not unit is Structure and unit.player == _player:
_handle_event(Constants.Match.VoiceNarrator.Events.UNIT_HELLO)
# TODO: handle building (add some sound?)


func _on_unit_action_requsted(_ignore):
if get_tree().get_nodes_in_group("selected_units").any(
func(unit): return not unit is Structure and unit.player == _player
):
_handle_event(
(
Constants.Match.VoiceNarrator.Events.UNIT_ACK_1
if _last_ack_event == 0
else Constants.Match.VoiceNarrator.Events.UNIT_ACK_2
)
)
_last_ack_event = (_last_ack_event + 1) % 2

0 comments on commit bf752c8

Please sign in to comment.