From 66cbb87eaa7e0200f1f286c4b98e52f322bbbe45 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Sun, 26 Jan 2025 23:51:44 +0100 Subject: [PATCH] WIP: Add basic damage --- scenes/characters/placeholder.tscn | 1 - scenes/maps/el_test.gd | 2 +- scenes/maps/el_test.tscn | 61 +---------------------- scenes/maps/hud.gd | 8 +-- scenes/utils/in_game_hud.gd | 11 ++++ scenes/utils/in_game_hud.tscn | 55 ++++++++++++++++++++ scenes/utils/menu.gd | 4 +- scenes/weapon/bullet.tscn | 4 +- scripts/game_server_manager.gd | 8 ++- scripts/player/player_input_controller.gd | 18 +++++-- 10 files changed, 97 insertions(+), 75 deletions(-) create mode 100644 scenes/utils/in_game_hud.gd create mode 100644 scenes/utils/in_game_hud.tscn diff --git a/scenes/characters/placeholder.tscn b/scenes/characters/placeholder.tscn index e3c026f..6e70e9d 100644 --- a/scenes/characters/placeholder.tscn +++ b/scenes/characters/placeholder.tscn @@ -116,7 +116,6 @@ libraries = { } [node name="HUD" type="Control" parent="FirstPersonCameraMount"] -visible = false layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 diff --git a/scenes/maps/el_test.gd b/scenes/maps/el_test.gd index 98a87aa..463f506 100644 --- a/scenes/maps/el_test.gd +++ b/scenes/maps/el_test.gd @@ -34,7 +34,7 @@ func spawn_bullet(position): node.transform.basis = position.global_transform.basis node.name = str(bullet_amount) bullet_amount += 1 - $BulletSpawner.spawn(node) + #$BulletSpawner.spawn(node) $Bullets.add_child(node) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: diff --git a/scenes/maps/el_test.tscn b/scenes/maps/el_test.tscn index 4d2e49c..46f1cfb 100644 --- a/scenes/maps/el_test.tscn +++ b/scenes/maps/el_test.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=97 format=4 uid="uid://c1v6kb00y77ij"] +[gd_scene load_steps=93 format=4 uid="uid://c1v6kb00y77ij"] [ext_resource type="Script" path="res://scenes/maps/el_test.gd" id="1_d5e7i"] [ext_resource type="Texture2D" uid="uid://60qg81svnxfd" path="res://assets/models/maps/el_test_Image_0.png" id="2_o6seo"] @@ -7,8 +7,6 @@ [ext_resource type="Script" path="res://scenes/maps/csg_box_3d.gd" id="5_3oqpw"] [ext_resource type="Script" path="res://scenes/characters/blue/dummy.gd" id="6_m5xfd"] [ext_resource type="Script" path="res://scenes/characters/blue/head_collision.gd" id="7_yqr12"] -[ext_resource type="Texture2D" uid="uid://oopj5mj1vdp0" path="res://assets/crosshairs/crosshair_default.png" id="8_h4lu1"] -[ext_resource type="Script" path="res://scenes/maps/hud.gd" id="8_h81ia"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_1on1b"] resource_name = "Orange_Playground_Base_Color" @@ -1711,13 +1709,6 @@ _data = { "Run Forward": SubResource("Animation_6g1lc") } -[sub_resource type="LabelSettings" id="LabelSettings_ue1xd"] -font_size = 70 - -[sub_resource type="LabelSettings" id="LabelSettings_ht855"] -font_size = 100 -font_color = Color(0.756874, 0, 0.223924, 1) - [node name="ElTest" type="Node3D"] script = ExtResource("1_d5e7i") @@ -2573,54 +2564,4 @@ libraries = { [node name="Bullets" type="Node3D" parent="."] -[node name="BulletSpawner" type="MultiplayerSpawner" parent="."] -_spawnable_scenes = PackedStringArray("res://scenes/weapon/bullet.tscn") -spawn_path = NodePath("../Bullets") -spawn_limit = 100 - -[node name="HUD" type="Control" parent="."] -layout_mode = 3 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -offset_left = 8.0 -offset_right = 8.0 -grow_horizontal = 2 -grow_vertical = 2 -mouse_filter = 2 -script = ExtResource("8_h81ia") - -[node name="TextureRect" type="TextureRect" parent="HUD"] -layout_mode = 1 -anchors_preset = 8 -anchor_left = 0.5 -anchor_top = 0.5 -anchor_right = 0.5 -anchor_bottom = 0.5 -offset_left = -36.0 -offset_top = -36.0 -offset_right = 36.0 -offset_bottom = 36.0 -grow_horizontal = 2 -grow_vertical = 2 -texture = ExtResource("8_h4lu1") - -[node name="HealthIndicator" type="Label" parent="HUD"] -layout_mode = 1 -anchors_preset = 2 -anchor_top = 1.0 -anchor_bottom = 1.0 -offset_top = -23.0 -offset_right = 40.0 -grow_vertical = 0 -text = "100" -label_settings = SubResource("LabelSettings_ue1xd") - -[node name="FPS" type="Label" parent="HUD" groups=["player_placeholder"]] -layout_mode = 1 -offset_right = 40.0 -offset_bottom = 23.0 -text = "0" -label_settings = SubResource("LabelSettings_ht855") - [connection signal="body_part_hit" from="Dummy/Body/Armature/Skeleton3D/BoneAttachment3D/HeadCollision" to="Dummy" method="_on_head_collision_body_part_hit"] diff --git a/scenes/maps/hud.gd b/scenes/maps/hud.gd index 0f1cb75..6d5a014 100644 --- a/scenes/maps/hud.gd +++ b/scenes/maps/hud.gd @@ -10,9 +10,9 @@ func _ready() -> void: # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: var root := get_tree().get_root() - var id := str(multiplayer.get_unique_id()) - var placeholder: Node3D = root.find_child("*_" + id) - if placeholder: - health_indicator.text = placeholder.health + var id := multiplayer.get_unique_id() + var player_data = GameServerManager.get_player_health.rpc_id(1, id) + print(player_data) + health_indicator.text = str(player_data["health"]) pass diff --git a/scenes/utils/in_game_hud.gd b/scenes/utils/in_game_hud.gd new file mode 100644 index 0000000..9b5ba02 --- /dev/null +++ b/scenes/utils/in_game_hud.gd @@ -0,0 +1,11 @@ +extends Control + +@onready = +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass diff --git a/scenes/utils/in_game_hud.tscn b/scenes/utils/in_game_hud.tscn new file mode 100644 index 0000000..a02e23c --- /dev/null +++ b/scenes/utils/in_game_hud.tscn @@ -0,0 +1,55 @@ +[gd_scene load_steps=5 format=3 uid="uid://c137wl6ls3knx"] + +[ext_resource type="Script" path="res://scenes/utils/in_game_hud.gd" id="1_sttf0"] +[ext_resource type="Texture2D" uid="uid://oopj5mj1vdp0" path="res://assets/crosshairs/crosshair_default.png" id="2_fjr6q"] + +[sub_resource type="LabelSettings" id="LabelSettings_ds4qb"] +font_size = 70 + +[sub_resource type="LabelSettings" id="LabelSettings_vhjwu"] +font_size = 100 +font_color = Color(0.756874, 0, 0.223924, 1) + +[node name="InGameHud" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_sttf0") + +[node name="TextureRect" type="TextureRect" parent="."] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -28.0 +offset_top = -36.0 +offset_right = 44.0 +offset_bottom = 36.0 +grow_horizontal = 2 +grow_vertical = 2 +texture = ExtResource("2_fjr6q") + +[node name="HealthIndicator" type="Label" parent="."] +layout_mode = 1 +anchors_preset = 2 +anchor_top = 1.0 +anchor_bottom = 1.0 +offset_left = 8.0 +offset_top = -96.0 +offset_right = 128.0 +grow_vertical = 0 +text = "100" +label_settings = SubResource("LabelSettings_ds4qb") + +[node name="FPS" type="Label" parent="." groups=["player_placeholder"]] +layout_mode = 1 +offset_left = 8.0 +offset_right = 65.0 +offset_bottom = 137.0 +text = "0" +label_settings = SubResource("LabelSettings_vhjwu") diff --git a/scenes/utils/menu.gd b/scenes/utils/menu.gd index 29dbb6a..0437529 100644 --- a/scenes/utils/menu.gd +++ b/scenes/utils/menu.gd @@ -65,7 +65,7 @@ func player_loaded(): # When a peer connects, send them my player info. # This allows transfer of all desired data for each player, not only the unique ID. func _on_player_connected(id): - player_info = {"name": multiplayer.get_unique_id()} + player_info = {"name": multiplayer.get_unique_id(), "health": 100} _register_player.rpc_id(1, multiplayer.get_unique_id(), player_info) @rpc("any_peer", "reliable") @@ -103,7 +103,7 @@ func _on_server_disconnected(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _on_host_pressed() -> void: - player_info = {"name": str(1)} + player_info = {"name": str(1), "health": 100} _register_player(1, player_info) create_game() diff --git a/scenes/weapon/bullet.tscn b/scenes/weapon/bullet.tscn index 487f359..f9258af 100644 --- a/scenes/weapon/bullet.tscn +++ b/scenes/weapon/bullet.tscn @@ -33,10 +33,10 @@ size = Vector3(0.05, 0.05, 0.05) [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_b8q4c"] properties/0/path = NodePath(".:position") properties/0/spawn = true -properties/0/replication_mode = 2 +properties/0/replication_mode = 0 properties/1/path = NodePath(".:rotation") properties/1/spawn = true -properties/1/replication_mode = 2 +properties/1/replication_mode = 0 [node name="Bullet" type="Node3D"] transform = Transform3D(20, 0, 0, 0, 20, 0, 0, 0, 20, 0, 0, 0) diff --git a/scripts/game_server_manager.gd b/scripts/game_server_manager.gd index 16f1dcb..91deada 100644 --- a/scripts/game_server_manager.gd +++ b/scripts/game_server_manager.gd @@ -1,11 +1,15 @@ extends Node var players = {} + +var local_player_health = 0 # Called when the node enters the scene tree for the first time. +@rpc("reliable", "call_local") +func get_player_health(id: int) -> int: + return players.get(id)["health"] func _ready() -> void: pass # Replace with function body. - - + # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass diff --git a/scripts/player/player_input_controller.gd b/scripts/player/player_input_controller.gd index 84847ec..a2077db 100644 --- a/scripts/player/player_input_controller.gd +++ b/scripts/player/player_input_controller.gd @@ -3,6 +3,7 @@ # --------------------------------------------------------------------- class_name PlayerInput extends CharacterBody3D + @export var health: int = 100 @export_category("PlayerInput") @@ -47,6 +48,7 @@ func _ready() -> void: var err := _add_world_model() if err != OK: print("Error occured: " + str(err)) + _hide_camera_mount() _capture_mouse() @@ -64,6 +66,10 @@ func _add_world_model() -> Error : model_mount.add_child(node) return OK +func _hide_camera_mount() : + first_view_camera_mount.visible = 0 + hud.visible = 0 + var current_gun: String = "ak" @onready var gun_mount: Node3D = $FirstPersonCameraMount/GunMount @onready var gun_mount_anim: AnimationPlayer = $FirstPersonCameraMount/GunMount/AnimationPlayer @@ -159,6 +165,7 @@ func _rotate_camera(sens_mod: float = 1.0) -> void: rotation.y -= look_dir.x * camera_sens * sens_mod first_view_camera_mount.rotation.x = clamp(first_view_camera_mount.rotation.x - look_dir.y * camera_sens * sens_mod, -1.5, 1.5) +@onready var hud = $FirstPersonCameraMount/HUD @onready var health_indicator = $FirstPersonCameraMount/HUD/HealthIndicator @onready var fps_indicator = $FirstPersonCameraMount/HUD/FPS func _process(delta: float) -> void: @@ -255,6 +262,11 @@ func _hit_scan_damage(Collider): print("damaged") func take_damage(dam: int): - print("damage") - health -= dam - print(health) + var new_health = health - dam + set_health(new_health) + if multiplayer.is_server(): + set_health.rpc(new_health) + +@rpc("call_local", "reliable") +func set_health(val: int): + health = val