godot-shooter / files / scenes / HUD.gd
HUD.gd
5b18ed86 2023-11-01 Pavel Safronov | 1 | extends Control |
| 2 | ||
16938031 2023-11-17 Pavel Safronov | 3 | const Utils = preload("res://utils/Utils.gd") |
5b18ed86 2023-11-01 Pavel Safronov | 4 | export var score = 0 |
16938031 2023-11-17 Pavel Safronov | 5 | export var objective = "0/5" |
636e5224 2023-12-01 Pavel Safronov | 6 | export var is_game_over = false |
641b63b5 2023-11-10 Pavel Safronov | 7 | var is_dragging = false |
fc64e166 2023-11-08 Pavel Safronov | 8 | signal paused |
210ccd64 2023-11-09 Pavel Safronov | 9 | signal debug_signal |
641b63b5 2023-11-10 Pavel Safronov | 10 | signal position_set(position) |
fa5168b2 2023-11-08 Pavel Safronov | 11 | |
212b7b25 2023-11-08 Pavel Safronov | 12 | onready var label = $CanvasLayer/MessageLabel |
ddae17d7 2023-11-08 Pavel Safronov | 13 | onready var animation_player = $AnimationPlayer |
c2827327 2023-11-10 Pavel Safronov | 14 | onready var score_label = $MarginContainer2/HBoxContainer/Score |
fa5168b2 2023-11-08 Pavel Safronov | 15 | onready var objective_label = $MarginContainer/HBoxContainer2/Objective |
c2827327 2023-11-10 Pavel Safronov | 16 | onready var weapon_label = $MarginContainer2/HBoxContainer/Weapon |
| 17 | onready var ammo_label = $MarginContainer2/HBoxContainer/Ammo | |
fa5168b2 2023-11-08 Pavel Safronov | 18 | |
641b63b5 2023-11-10 Pavel Safronov | 19 | func _input(event: InputEvent) -> void: |
636e5224 2023-12-01 Pavel Safronov | 20 | if is_game_over: |
| 21 | return | |
c3ac3ece 2023-11-11 Pavel Safronov | 22 | var nodes = get_tree().get_nodes_in_group("pause_menu") |
cac0fb4f 2023-11-11 Pavel Safronov | 23 | var is_pause_menu_open = false |
| 24 | for n in nodes: | |
| 25 | var node: Control = n | |
| 26 | is_pause_menu_open = is_pause_menu_open or node.visible | |
c3ac3ece 2023-11-11 Pavel Safronov | 27 | if is_pause_menu_open: |
| 28 | return | |
| 29 | ||
641b63b5 2023-11-10 Pavel Safronov | 30 | if event is InputEventScreenTouch and event.pressed: |
cbc328fb 2023-11-17 Pavel Safronov | 31 | set_position_event(event.position) |
641b63b5 2023-11-10 Pavel Safronov | 32 | if event is InputEventScreenDrag: |
cbc328fb 2023-11-17 Pavel Safronov | 33 | set_position_event(event.position) |
641b63b5 2023-11-10 Pavel Safronov | 34 | if event is InputEventMouseButton: |
| 35 | if not is_dragging and event.pressed: | |
| 36 | is_dragging = true | |
| 37 | get_tree().paused = true | |
ede9acdd 2023-11-17 Pavel Safronov | 38 | set_position_event(event.position) |
641b63b5 2023-11-10 Pavel Safronov | 39 | if is_dragging and not event.pressed: |
| 40 | is_dragging = false | |
| 41 | get_tree().paused = false | |
| 42 | if event is InputEventMouseMotion and is_dragging: | |
cbc328fb 2023-11-17 Pavel Safronov | 43 | set_position_event(event.position) |
641b63b5 2023-11-10 Pavel Safronov | 44 | |
cbc328fb 2023-11-17 Pavel Safronov | 45 | func set_position_event(event_pos: Vector2) -> void: |
| 46 | emit_signal("position_set", event_pos) | |
641b63b5 2023-11-10 Pavel Safronov | 47 | |
5b18ed86 2023-11-01 Pavel Safronov | 48 | func _process(_delta: float) -> void: |
fa5168b2 2023-11-08 Pavel Safronov | 49 | score_label.text = "%s" % score |
| 50 | objective_label.text = "%s" % objective | |
f3c75ae9 2023-11-15 Pavel Safronov | 51 | if Input.is_action_just_pressed("pause"): |
| 52 | emit_signal("paused") | |
52220002 2023-11-06 Pavel Safronov | 53 | if Input.is_action_just_pressed("exit"): |
| 54 | if OS.get_name() == "OSX": | |
| 55 | get_tree().quit() | |
| 56 | else: | |
fc64e166 2023-11-08 Pavel Safronov | 57 | emit_signal("paused") |
| 58 | # restart() | |
52220002 2023-11-06 Pavel Safronov | 59 | if Input.is_action_just_pressed("restart"): |
| 60 | restart() | |
210ccd64 2023-11-09 Pavel Safronov | 61 | if Input.is_action_just_pressed("debug_action"): |
| 62 | emit_signal("debug_signal") | |
52220002 2023-11-06 Pavel Safronov | 63 | |
| 64 | func restart(): | |
| 65 | var _error = get_tree().reload_current_scene() | |
| 66 | get_tree().paused = false | |
ddae17d7 2023-11-08 Pavel Safronov | 67 | |
| 68 | func show_message(message: String, duration_seconds: int = 2) -> void: | |
9dfa0893 2023-11-08 Pavel Safronov | 69 | var now_msecs = OS.get_system_time_msecs() |
| 70 | # var stack = Utils.get_stack_string() | |
| 71 | # Utils.log("showing message:", message, "at:", now_msecs, "in stack:", stack) | |
44475e04 2023-11-08 Pavel Safronov | 72 | var fade_in = animation_player.get_animation("message fade in") |
| 73 | fade_in.track_set_key_value(1, 0, message) | |
| 74 | var fade_out = animation_player.get_animation("message fade out") | |
| 75 | fade_out.track_set_key_time(0, 1, duration_seconds) | |
| 76 | ||
9dfa0893 2023-11-08 Pavel Safronov | 77 | var new_fade_in = "fade in %s" % now_msecs |
44475e04 2023-11-08 Pavel Safronov | 78 | animation_player.add_animation(new_fade_in, fade_in) |
9dfa0893 2023-11-08 Pavel Safronov | 79 | var new_fade_out = "fade out %s" % now_msecs |
44475e04 2023-11-08 Pavel Safronov | 80 | animation_player.add_animation(new_fade_out, fade_out) |
| 81 | ||
| 82 | if animation_player.is_playing(): | |
| 83 | animation_player.queue(new_fade_in) | |
| 84 | else: | |
| 85 | animation_player.play(new_fade_in) | |
| 86 | animation_player.queue(new_fade_out) | |
0005bbcc 2023-11-10 Pavel Safronov | 87 | |
| 88 | func _on_PauseButton_pressed() -> void: | |
| 89 | emit_signal("paused") | |
c2827327 2023-11-10 Pavel Safronov | 90 | |
| 91 | func set_current_weapon(weapon: Weapon) -> void: | |
a5758228 2023-11-30 Pavel Safronov | 92 | var is_non_weapon = weapon.name == "" |
| 93 | if is_non_weapon: | |
| 94 | weapon_label.text = "" | |
| 95 | ammo_label.text = "" | |
| 96 | else: | |
| 97 | weapon_label.text = "%s:" % weapon.name | |
| 98 | ammo_label.text = "%s" % weapon.ammo |