Skip to content

Commit

Permalink
Add 'under attack' voice events, #83
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jan 3, 2025
1 parent df658e1 commit de367b7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/match/MatchConstants.gd
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ class VoiceNarrator:
MATCH_ABORTED,
MATCH_FINISHED_WITH_VICTORY,
MATCH_FINISHED_WITH_DEFEAT,
BASE_UNDER_ATTACK,
UNIT_UNDER_ATTACK,
UNIT_LOST,
UNIT_PRODUCTION_STARTED,
UNIT_PRODUCTION_FINISHED,
Expand All @@ -242,6 +244,12 @@ class VoiceNarrator:
preload("res://assets/voice/english/ttsmaker-com-148-alayna-us/you_are_victorious.ogg"),
Events.MATCH_FINISHED_WITH_DEFEAT:
preload("res://assets/voice/english/ttsmaker-com-148-alayna-us/you_have_lost.ogg"),
Events.BASE_UNDER_ATTACK:
preload(
"res://assets/voice/english/ttsmaker-com-148-alayna-us/your_base_is_under_attack.ogg"
),
Events.UNIT_UNDER_ATTACK:
preload("res://assets/voice/english/ttsmaker-com-148-alayna-us/unit_under_attack.ogg"),
Events.UNIT_LOST:
preload("res://assets/voice/english/ttsmaker-com-148-alayna-us/unit_lost.ogg"),
Events.UNIT_PRODUCTION_STARTED:
Expand Down
1 change: 1 addition & 0 deletions source/match/MatchSignals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ signal unit_spawned(unit)
signal unit_targeted(unit)
signal unit_selected(unit)
signal unit_deselected(unit)
signal unit_damaged(unit)
signal unit_died(unit)
signal unit_production_started(unit_prototype, producer_unit)
signal unit_production_finished(unit, producer_unit)
Expand Down
22 changes: 22 additions & 0 deletions source/match/players/human/VoiceNarratorController.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ extends Node

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

@onready var _audio_player = find_child("AudioStreamPlayer")
@onready var _player = get_parent()
Expand All @@ -26,6 +29,7 @@ func _ready():
MatchSignals.match_finished_with_defeat.connect(
_handle_event.bind(Constants.Match.VoiceNarrator.Events.MATCH_FINISHED_WITH_DEFEAT)
)
MatchSignals.unit_damaged.connect(_on_unit_damaged)
MatchSignals.unit_died.connect(_on_unit_died)
MatchSignals.unit_production_started.connect(_on_production_started)
MatchSignals.unit_production_finished.connect(_on_production_finished)
Expand Down Expand Up @@ -54,6 +58,24 @@ func _handle_event(event):
_audio_player.play()


func _on_unit_damaged(unit):
if unit.player != _player:
return
var current_timestamp = Time.get_ticks_msec()
if (
current_timestamp - _last_under_attack_notification_timestamp
> UNDER_ATTACK_NOTIFICATION_THRESHOLD_MS
):
_handle_event(
(
Constants.Match.VoiceNarrator.Events.BASE_UNDER_ATTACK
if unit is Structure
else Constants.Match.VoiceNarrator.Events.UNIT_UNDER_ATTACK
)
)
_last_under_attack_notification_timestamp = current_timestamp


func _on_unit_died(unit):
if unit.is_in_group("controlled_units"):
_handle_event(Constants.Match.VoiceNarrator.Events.UNIT_LOST)
Expand Down
3 changes: 3 additions & 0 deletions source/match/units/Unit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func is_revealing():


func _set_hp(value):
var old_hp = hp
hp = max(0, value)
if old_hp != null and hp < old_hp:
MatchSignals.unit_damaged.emit(self)
hp_changed.emit()
if hp == 0:
_handle_unit_death()
Expand Down

0 comments on commit de367b7

Please sign in to comment.