diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-09-29 22:31:03 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-09-29 22:31:03 +0100 |
commit | a10570d865106dcaf1037979cad1788da03e989d (patch) | |
tree | 7f49d98cbeb8f85e32351a428e2d898cc8dcfd2a | |
parent | 0b14efee60765701f439b5174e56c0cf0639d20c (diff) |
ui: Add UI to disply ammo and info toasts
-rw-r--r-- | prefabs/test_arena.tscn | 37 | ||||
-rw-r--r-- | scripts/player.gd | 19 | ||||
-rw-r--r-- | scripts/ui.gd | 31 |
3 files changed, 76 insertions, 11 deletions
diff --git a/prefabs/test_arena.tscn b/prefabs/test_arena.tscn index c84ae4b..f75e038 100644 --- a/prefabs/test_arena.tscn +++ b/prefabs/test_arena.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=10 format=3 uid="uid://dlevowk0jrhlg"] +[gd_scene load_steps=11 format=3 uid="uid://dlevowk0jrhlg"] [ext_resource type="Script" path="res://scripts/dungeon_manager.gd" id="1_dbsm0"] [ext_resource type="PackedScene" uid="uid://cc1m2a1obsyn4" path="res://addons/fpc/character.tscn" id="1_vc6b5"] [ext_resource type="Script" path="res://scripts/player.gd" id="2_puqns"] +[ext_resource type="Script" path="res://scripts/ui.gd" id="4_mhgxx"] [sub_resource type="Environment" id="Environment_2poci"] background_color = Color(0.130548, 0.170599, 0.433834, 1) @@ -93,10 +94,11 @@ in_air_momentum = false sprint_enabled = false crouch_enabled = false -[node name="PlayerCam" type="Node3D" parent="Player"] +[node name="PlayerCam" type="Node3D" parent="Player" node_paths=PackedStringArray("ui_control")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0) script = ExtResource("2_puqns") grapple_distance = 50.0 +ui_control = NodePath("../../CanvasLayer") [node name="Camera3D" type="Camera3D" parent="Player/PlayerCam"] current = true @@ -120,5 +122,36 @@ shape = SubResource("BoxShape3D_r46wa") [node name="LevelContainer" type="Node3D" parent="."] +[node name="CanvasLayer" type="CanvasLayer" parent="." node_paths=PackedStringArray("player")] +script = ExtResource("4_mhgxx") +player = NodePath("../Player/PlayerCam") + +[node name="Ammo" type="Label" parent="CanvasLayer"] +anchors_preset = 3 +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -125.0 +offset_top = -71.0 +offset_right = -56.0 +offset_bottom = -48.0 +grow_horizontal = 0 +grow_vertical = 0 +text = "Ammo: 0" + +[node name="Toast" type="Label" parent="CanvasLayer"] +anchors_preset = 7 +anchor_left = 0.5 +anchor_top = 1.0 +anchor_right = 0.5 +anchor_bottom = 1.0 +offset_left = -20.0 +offset_top = -134.0 +offset_right = 20.0 +offset_bottom = -111.0 +grow_horizontal = 2 +grow_vertical = 0 + [connection signal="area_entered" from="Player/PlayerCam/Area3D" to="Player/PlayerCam" method="_on_interactable_enter"] [connection signal="area_exited" from="Player/PlayerCam/Area3D" to="Player/PlayerCam" method="_on_interactable_exit"] diff --git a/scripts/player.gd b/scripts/player.gd index 3b1fb6a..bbcc4f4 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -6,6 +6,8 @@ extends Node3D @export var fire_rate: float = 0.1 +@export var ui_control: CanvasLayer + enum State { NO_GRAPPLE, FREE, @@ -24,7 +26,7 @@ var interactable: Node3D = null # Called when the node enters the scene tree for the first time. func _ready() -> void: - pass # Replace with function body. + assert(ui_control != null) func do_grapple() -> void: if Input.is_action_just_pressed("attack") and current_state == State.FREE: @@ -61,12 +63,9 @@ func fire_weapon(delta: float) -> void: var end = ending_pos if result: - print("Hit! ") if result.collider.has_method("on_hit"): result.collider.on_hit(result.position) end = result.position - else: - print("Miss") var bullet: Node3D = bullet_inst.instantiate() bullet.position = starting_pos @@ -78,20 +77,24 @@ func fire_weapon(delta: float) -> void: bullet.look_at(end, up) ammo -= 1 - print("Ammo: ", ammo) fire_timer = 0 else: fire_timer += delta func do_interact() -> void: + if interactable != null and not ui_control.have_toast(): + ui_control.set_toast("Press E to interact", 0) + elif interactable == null and not ui_control.have_toast(): + ui_control.set_toast("", 0) + if Input.is_action_just_pressed("interact") and interactable != null: if interactable.has_method("get_contents"): var contents = interactable.get_contents() if contents > 0: ammo += contents - print("Got ", contents, " ammo!") + ui_control.set_toast("Got " + str(contents) + " ammo!", 1) else: - print("Empty container") + ui_control.set_toast("Empty container", 1) func _physics_process(delta: float) -> void: do_grapple() @@ -102,10 +105,8 @@ func _physics_process(delta: float) -> void: func _process(_delta: float) -> void: pass - func _on_interactable_enter(body: Node3D) -> void: interactable = body - print("Got interactable") func _on_interactable_exit(body: Node3D) -> void: if interactable == body: diff --git a/scripts/ui.gd b/scripts/ui.gd new file mode 100644 index 0000000..eaf2d0c --- /dev/null +++ b/scripts/ui.gd @@ -0,0 +1,31 @@ +extends CanvasLayer + +@export var player: Node3D + +var timeout: float = 0 +var timeout_counter: float = 0 + +# Called when the node enters the scene tree for the first time. +#func _ready() -> void: +# pass # Replace with function body. + +func have_toast() -> bool: + return timeout > 0 + +func set_toast(text: String, p_timeout: float) -> void: + $Toast.text = text + + if p_timeout != 0: + timeout = p_timeout + timeout_counter = 0 + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + $Ammo.text = "Ammo " + str(player.ammo) + + if timeout > 0: + if timeout_counter < timeout: + timeout_counter += delta + else: + $Toast.text = "" + timeout = 0 |