From 7e3afd6c6466ba1bd08e8a9bbb0338f53faa1b37 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 10 Aug 2025 20:53:27 +0200 Subject: [PATCH] feat: added HUD object --- modules/wave_survival/heads_up_display.cpp | 27 +++++++++++++++++++++- modules/wave_survival/heads_up_display.h | 4 ++++ modules/wave_survival/register_types.cpp | 2 ++ modules/wave_survival/weapons/rifle.cpp | 3 +++ project/guis/heads_up_display.tscn | 24 +++++++++++++++++++ project/{menus => guis}/main_menu.tscn | 0 project/{menus => guis}/pause_menu.tscn | 0 project/maps/game.tscn | 22 ++++-------------- project/maps/testmap.tscn | 2 +- 9 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 project/guis/heads_up_display.tscn rename project/{menus => guis}/main_menu.tscn (100%) rename project/{menus => guis}/pause_menu.tscn (100%) diff --git a/modules/wave_survival/heads_up_display.cpp b/modules/wave_survival/heads_up_display.cpp index 8cca11e5..fb99be68 100644 --- a/modules/wave_survival/heads_up_display.cpp +++ b/modules/wave_survival/heads_up_display.cpp @@ -1,9 +1,24 @@ #include "heads_up_display.h" +HeadsUpDisplay *HeadsUpDisplay::singleton_instance{ nullptr }; + void HeadsUpDisplay::_bind_methods() { + ClassDB::bind_static_method("HeadsUpDisplay", D_METHOD("get_singleton"), &self_type::get_singleton); +} + +void HeadsUpDisplay::on_child_entered(Node *child) { + if (child->is_unique_name_in_owner() && child->get_name() == "Reticle") { + this->reticle = cast_to(child); + } } void HeadsUpDisplay::enter_tree() { + connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered)); + singleton_instance = this; +} + +void HeadsUpDisplay::exit_tree() { + singleton_instance = nullptr; } void HeadsUpDisplay::_notification(int what) { @@ -16,8 +31,18 @@ void HeadsUpDisplay::_notification(int what) { case NOTIFICATION_ENTER_TREE: enter_tree(); return; + case NOTIFICATION_EXIT_TREE: + exit_tree(); + return; } } -void HeadsUpDisplay::set_reticle_visibility(bool visible) { +HeadsUpDisplay *HeadsUpDisplay::get_singleton() { + return singleton_instance; +} + +void HeadsUpDisplay::set_reticle_visibility(bool visible) { + if (this->reticle) { + this->reticle->set_visible(visible); + } } diff --git a/modules/wave_survival/heads_up_display.h b/modules/wave_survival/heads_up_display.h index edccc57d..a94dc8c4 100644 --- a/modules/wave_survival/heads_up_display.h +++ b/modules/wave_survival/heads_up_display.h @@ -6,12 +6,16 @@ class HeadsUpDisplay : public Control { GDCLASS(HeadsUpDisplay, Control); static void _bind_methods(); + static HeadsUpDisplay *singleton_instance; void on_child_entered(Node *node); void enter_tree(); + void exit_tree(); + protected: void _notification(int what); public: + static HeadsUpDisplay *get_singleton(); void set_reticle_visibility(bool visible); private: diff --git a/modules/wave_survival/register_types.cpp b/modules/wave_survival/register_types.cpp index 169e706e..440a1885 100644 --- a/modules/wave_survival/register_types.cpp +++ b/modules/wave_survival/register_types.cpp @@ -4,6 +4,7 @@ #include "wave_survival/damage_box.h" #include "wave_survival/enemies/enemy_wretched.h" #include "wave_survival/enemy_body.h" +#include "wave_survival/heads_up_display.h" #include "wave_survival/health_status.h" #include "wave_survival/hitbox.h" #include "wave_survival/hitscan_muzzle.h" @@ -55,6 +56,7 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) { GDREGISTER_CLASS(SoundEventPatchboard); GDREGISTER_CLASS(SoundEventNode); GDREGISTER_CLASS(MuzzleEffect); + GDREGISTER_RUNTIME_CLASS(HeadsUpDisplay); memnew(SoundEventPatchboard); Engine::get_singleton()->add_singleton(Engine::Singleton(SoundEventPatchboard::get_class_static(), SoundEventPatchboard::get_singleton())); diff --git a/modules/wave_survival/weapons/rifle.cpp b/modules/wave_survival/weapons/rifle.cpp index 51c1b275..25a836ec 100644 --- a/modules/wave_survival/weapons/rifle.cpp +++ b/modules/wave_survival/weapons/rifle.cpp @@ -1,5 +1,6 @@ #include "rifle.h" #include "scene/animation/animation_player.h" +#include "wave_survival/heads_up_display.h" #include "wave_survival/hitscan_muzzle.h" #include "wave_survival/macros.h" #include "wave_survival/player_body.h" @@ -17,11 +18,13 @@ void Rifle::_bind_methods() { void Rifle::queue_enter_alt() { get_anim()->queue("hip_to_aim"); get_anim()->queue("aim"); + HeadsUpDisplay::get_singleton()->set_reticle_visibility(false); } void Rifle::queue_exit_alt() { get_anim()->queue("aim_to_hip"); get_anim()->queue("hip"); + HeadsUpDisplay::get_singleton()->set_reticle_visibility(true); } void Rifle::queue_enter_run() { diff --git a/project/guis/heads_up_display.tscn b/project/guis/heads_up_display.tscn new file mode 100644 index 00000000..2e145b5a --- /dev/null +++ b/project/guis/heads_up_display.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=2 format=3 uid="uid://dqo1pfy3r00ai"] + +[ext_resource type="Texture2D" uid="uid://dc26l1ync7env" path="res://assets/gui/reticle.svg" id="1_1o2rr"] + +[node name="HeadsUpDisplay" type="HeadsUpDisplay"] +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +mouse_filter = 2 +mouse_behavior_recursive = 1 + +[node name="CenterContainer" type="CenterContainer" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="TextureRect" type="TextureRect" parent="CenterContainer"] +layout_mode = 2 +texture = ExtResource("1_1o2rr") diff --git a/project/menus/main_menu.tscn b/project/guis/main_menu.tscn similarity index 100% rename from project/menus/main_menu.tscn rename to project/guis/main_menu.tscn diff --git a/project/menus/pause_menu.tscn b/project/guis/pause_menu.tscn similarity index 100% rename from project/menus/pause_menu.tscn rename to project/guis/pause_menu.tscn diff --git a/project/maps/game.tscn b/project/maps/game.tscn index 63771d9e..01f1f067 100644 --- a/project/maps/game.tscn +++ b/project/maps/game.tscn @@ -2,8 +2,8 @@ [ext_resource type="PackedScene" uid="uid://snjgu4yp5swd" path="res://objects/player.tscn" id="1_7vohb"] [ext_resource type="PackedScene" uid="uid://dllho5nkq2smw" path="res://maps/industrial_area_map.tscn" id="2_hph0i"] -[ext_resource type="PackedScene" uid="uid://bt054d3ic71rf" path="res://menus/pause_menu.tscn" id="3_28e7h"] -[ext_resource type="Texture2D" uid="uid://bwfkmwx6bd5u4" path="res://icon.svg" id="4_buohg"] +[ext_resource type="PackedScene" uid="uid://dqo1pfy3r00ai" path="res://guis/heads_up_display.tscn" id="3_7vohb"] +[ext_resource type="PackedScene" uid="uid://bt054d3ic71rf" path="res://guis/pause_menu.tscn" id="3_28e7h"] [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_ien74"] sky_horizon_color = Color(0.66224277, 0.6717428, 0.6867428, 1) @@ -48,20 +48,6 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -20.417719, 1.340589, -8.0147 [node name="IndustrialAreaMap" parent="SubViewportContainer/SubViewport" instance=ExtResource("2_hph0i")] +[node name="HeadsUpDisplay" parent="." instance=ExtResource("3_7vohb")] + [node name="PauseMenu" parent="." instance=ExtResource("3_28e7h")] - -[node name="CenterContainer" type="CenterContainer" parent="."] -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 -focus_behavior_recursive = 1 -mouse_filter = 2 -mouse_behavior_recursive = 1 - -[node name="TextureRect" type="TextureRect" parent="CenterContainer"] -custom_minimum_size = Vector2(10, 10) -layout_mode = 2 -texture = ExtResource("4_buohg") -expand_mode = 4 diff --git a/project/maps/testmap.tscn b/project/maps/testmap.tscn index 99dfc00a..03e97327 100644 --- a/project/maps/testmap.tscn +++ b/project/maps/testmap.tscn @@ -3,7 +3,7 @@ [ext_resource type="PackedScene" uid="uid://snjgu4yp5swd" path="res://objects/player.tscn" id="1_6t4yh"] [ext_resource type="Material" uid="uid://b075rlo1f0e4u" path="res://assets/materials/greenish_grid.tres" id="1_ng1ul"] [ext_resource type="PackedScene" uid="uid://5hg5eirw7v8n" path="res://objects/units/unit_4_wretched.tscn" id="3_7ng1a"] -[ext_resource type="PackedScene" uid="uid://bt054d3ic71rf" path="res://menus/pause_menu.tscn" id="4_d7x8f"] +[ext_resource type="PackedScene" uid="uid://bt054d3ic71rf" path="res://guis/pause_menu.tscn" id="4_d7x8f"] [sub_resource type="NavigationMesh" id="NavigationMesh_7ng1a"] vertices = PackedVector3Array(-13.25, 0.24851608, -15, -11.5, 0.24851608, -15.25, -11.75, 0.24851608, -24.5, -24.5, 0.24851608, -24.5, -24.5, 0.24851608, -13.75, -13.25, 0.24851608, -13.5, -2, 0.24851608, -15.25, 2.25, 0.24851608, -15.5, 2.5, 0.24851608, -23.5, 12.25, 0.24851608, -23.5, 12.5, 0.24851608, -24.5, 14, 0.24851608, -23.25, 24.5, 0.24851608, -24.5, 14, 0.24851608, 19.5, 12.25, 0.24851608, 19.75, 12.5, 0.24851608, 24.5, 24.5, 0.24851608, 24.5, 3.25, 6.998516, -22.5, 3.25, 6.998516, 18.75, 13, 6.998516, 18.75, 13, 6.998516, -22.5, 2.25, 0.24851608, 6.75, 3, 0.24851608, 6.5, 3, 0.24851608, 6.25, 2.25, 0.24851608, 6, -1.5, 0.24851608, 5.25, -1.5, 0.24851608, 6, -1.5, 0.24851608, 11.25, 2.25, 0.24851608, 11.75, 2.25, 0.24851608, -8.75, 3, 0.24851608, -9, 3, 0.24851608, -9.25, 2.25, 0.24851608, -9.5, -1.5, 0.24851608, -10, -1.5, 0.24851608, -9.25, -1.5, 0.24851608, -15, -2.5, 0.24851608, -9.75, -2.5, 0.24851608, -9.5, -2.5, 0.24851608, 5.5, -2.5, 0.24851608, 5.75, -2, 0.24851608, 11.5, -12.25, 6.998516, -14.25, -12.25, 6.998516, 10.5, -2.5, 6.998516, 10.5, -2.5, 6.998516, -14.25, -24.5, 0.24851608, 10, -13.25, 0.24851608, 9.75, -10.75, 0.24851608, -6.5, -10.5, 0.24851608, -6.5, -10.25, 0.24851608, -7.25, -11, 0.24851608, -7.25, -3.25, 0.24851608, -10, -3.25, 0.24851608, -9.25, -11.75, 0.24851608, -13.5, -3.25, 0.24851608, -13.5, -3.25, 0.24851608, -7.25, -11.75, 0.24851608, -7.25, -11.75, 3.498516, -9.5, -12.25, 3.498516, -9, -11.75, 3.498516, -8.75, -10.75, 3.498516, -6.5, -10.5, 3.498516, -6.5, -10.25, 3.498516, -7.25, -11, 3.498516, -7.25, -11.75, 3.498516, -13.5, -3.25, 3.498516, -7.25, -3.25, 3.498516, -13.5, -11.75, 3.498516, -7.25, 3.75, 0.24851608, -8.75, 3.75, 0.24851608, -9.5, 3.75, 0.24851608, -13.25, 6, 0.24851608, -7, 12.5, 0.24851608, -7, 12.5, 0.24851608, -13.25, 5.25, 0.24851608, -7, 5.5, 0.24851608, -6.25, 5.75, 0.24851608, -6.25, 3.75, 0.24851608, -7, 5.5, 3.498516, -6.25, 5.75, 3.498516, -6.25, 6, 3.498516, -7, 5.25, 3.498516, -7, 12.5, 3.498516, -7, 12, 3.498516, -10.25, 7.75, 3.498516, -10.25, 12.5, 3.498516, -13.25, 7.5, 3.498516, -13.25, 3.75, 3.498516, -13.25, 3.75, 3.498516, -7, -10.25, 0.24851608, -6, -11, 0.24851608, -6, -11.75, 0.24851608, 2, -4.5, 0.24851608, 2, -3.25, 0.24851608, 2, -3.25, 0.24851608, -6, -5.25, 0.24851608, 2, -5, 0.24851608, 2.75, -4.75, 0.24851608, 2.75, -11.75, 0.24851608, -6, -10.25, 3.498516, -6, -11, 3.498516, -6, -11.75, 3.498516, 2, -4.5, 3.498516, 2, -3.25, 3.498516, 2, -3.25, 3.498516, -6, -5.25, 3.498516, 2, -5, 3.498516, 2.75, -4.75, 3.498516, 2.75, -11.75, 3.498516, -6, 11, 0.24851608, 2.5, 11.25, 0.24851608, 3.25, 11.5, 0.24851608, 3.25, 11.75, 0.24851608, 2.5, 12.5, 0.24851608, -5.5, 6, 0.24851608, -5.5, 3.75, 0.24851608, 2.5, 5.25, 0.24851608, -5.5, 3.75, 0.24851608, -5.5, 12.5, 0.24851608, 2.5, 10.75, 3.498516, 2.5, 11, 3.498516, 3.25, 11.25, 3.498516, 3.25, 11.5, 3.498516, 2.5, 12.5, 3.498516, -5.5, 6, 3.498516, -5.5, 3.75, 3.498516, 2.5, 5.25, 3.498516, -5.5, 3.75, 3.498516, -5.5, 12.5, 3.498516, 2.5, -3.25, 0.24851608, 5.25, -3.25, 0.24851608, 6, -4.5, 0.24851608, 3.75, -5.25, 0.24851608, 3.75, -3.25, 0.24851608, 10, -11.75, 0.24851608, 3.75, -11.75, 0.24851608, 10, -3.25, 0.24851608, 3.75, -5.25, 3.498516, 3.75, -4.5, 3.498516, 3.75, -11.75, 3.498516, 3.75, -11.75, 3.498516, 7.25, -7, 3.498516, 7, -6.75, 3.498516, 10, -3.25, 3.498516, 10, -3.25, 3.498516, 3.75, 12.5, 3.498516, 6, 13, 3.498516, 5.5, 12.5, 3.498516, 5.25, 10.75, 3.498516, 4, 11.5, 3.498516, 4, 12.5, 3.498516, 10.25, 3.75, 3.498516, 4, 3.75, 3.498516, 10.25, 12.5, 3.498516, 4, 3.75, 0.24851608, 6, 3.75, 0.24851608, 6.75, 11, 0.24851608, 4, 12.5, 0.24851608, 10.25, 11.75, 0.24851608, 4, 3.75, 0.24851608, 10.25, 3.75, 0.24851608, 4, 12.5, 0.24851608, 4, -13, 0.24851608, 11.5, -11, 0.24851608, 24.5, -11, 0.24851608, 11.5, -24.5, 0.24851608, 24.5, 3.75, 0.24851608, 24.5, 4, 0.24851608, 19.75, 2.25, 0.24851608, 19.5)