From e396a34d1bd724e2c30c635a81a77d6827b775d6 Mon Sep 17 00:00:00 2001 From: Zenny Date: Fri, 4 Aug 2023 14:18:28 -0700 Subject: [PATCH] Reload and player HUD --- Scenes/Player.tscn | 17 +++++++----- Scripts/Gun.cs | 61 ++++++++++++++++++++++++++++++++++++++++++++ Scripts/Player.cs | 14 ---------- Scripts/PlayerHUD.cs | 9 +++---- project.godot | 5 ++++ 5 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 Scripts/Gun.cs diff --git a/Scenes/Player.tscn b/Scenes/Player.tscn index 4bbaa86..2833e84 100644 --- a/Scenes/Player.tscn +++ b/Scenes/Player.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=10 format=3 uid="uid://dae1bs2yqqt5l"] +[gd_scene load_steps=11 format=3 uid="uid://dae1bs2yqqt5l"] [ext_resource type="Script" path="res://Scripts/Player.cs" id="1_jsq3s"] [ext_resource type="Environment" uid="uid://c1i78mwq3ug2f" path="res://Scenes/Enivornment.tres" id="2_t03qe"] [ext_resource type="AudioStream" uid="uid://cupjwk5q538g2" path="res://Audio/SFX/gunshot.mp3" id="3_5mglu"] [ext_resource type="Script" path="res://Scripts/SoundEffects.cs" id="4_fmvkw"] [ext_resource type="PackedScene" uid="uid://cc5emorildoar" path="res://Scenes/Gun.tscn" id="5_sb1dv"] +[ext_resource type="Script" path="res://Scripts/Gun.cs" id="6_48ohb"] [ext_resource type="Script" path="res://Scripts/PlayerHUD.cs" id="6_pawgr"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_cxbex"] @@ -42,17 +43,20 @@ target_position = Vector3(0, 0, -1000) [node name="AudioStreamPlayer3D" type="AudioStreamPlayer3D" parent="Pivot/Camera3D"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.10748, -0.0718701) stream = ExtResource("3_5mglu") -volume_db = -28.0 +volume_db = -13.823 max_db = -5.0 max_polyphony = 8 script = ExtResource("4_fmvkw") [node name="MeshInstance3D" type="MeshInstance3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) -visible = false mesh = SubResource("CapsuleMesh_7dxig") -[node name="Gun" parent="." instance=ExtResource("5_sb1dv")] +[node name="Gun" parent="." node_paths=PackedStringArray("_aimCast") instance=ExtResource("5_sb1dv")] +script = ExtResource("6_48ohb") +MaxAmmo = 30 +DamagePerHit = 8.0 +_aimCast = NodePath("../Pivot/Camera3D/AimCast") [node name="CanvasLayer" type="CanvasLayer" parent="."] script = ExtResource("6_pawgr") @@ -74,5 +78,6 @@ label_settings = SubResource("LabelSettings_3d4s1") horizontal_alignment = 1 vertical_alignment = 1 -[connection signal="FireGun" from="." to="Pivot/Camera3D/AudioStreamPlayer3D" method="OnFireGun"] -[connection signal="FireGun" from="." to="CanvasLayer" method="OnFireGun"] +[connection signal="FireGun" from="Gun" to="Pivot/Camera3D/AudioStreamPlayer3D" method="OnFireGun"] +[connection signal="FireGun" from="Gun" to="CanvasLayer" method="UpdateCurrentAmmo"] +[connection signal="ReloadGun" from="Gun" to="CanvasLayer" method="UpdateCurrentAmmo"] diff --git a/Scripts/Gun.cs b/Scripts/Gun.cs new file mode 100644 index 0000000..06763b0 --- /dev/null +++ b/Scripts/Gun.cs @@ -0,0 +1,61 @@ +using System.Threading.Tasks; +using Godot; + +public partial class Gun : Node3D +{ + [Export] + public int MaxAmmo; + + [Export] + public double DamagePerHit; + + [Signal] + public delegate void OnHitEventHandler(); + [Signal] + public delegate void FireGunEventHandler(); + [Signal] + public delegate void ReloadGunEventHandler(); + + [Export] + private RayCast3D _aimCast; + + private bool _reloading = false; + + public int CurrentAmmo { get; private set; } + + public override void _Ready() + { + CurrentAmmo = MaxAmmo; + } + + public override async void _Input(InputEvent @event) + { + if (CurrentAmmo > 0 && @event.IsActionPressed("fire") && !_reloading) + { + CurrentAmmo -= 1; + EmitSignal(SignalName.FireGun); + + if (_aimCast.IsColliding()) + { + EmitSignal(SignalName.OnHit); + _aimCast.GetCollider().Call("OnHitEvent", DamagePerHit); + } + } + + if (CurrentAmmo != MaxAmmo && @event.IsActionPressed("reload")) + { + CurrentAmmo = MaxAmmo; + await Reload(); + EmitSignal(SignalName.ReloadGun); + } + } + + + private async Task Reload() + { + _reloading = true; + await ToSignal(GetTree().CreateTimer(0.3f), "timeout"); + _reloading = false; + + } +} diff --git a/Scripts/Player.cs b/Scripts/Player.cs index 50cf452..56657c4 100644 --- a/Scripts/Player.cs +++ b/Scripts/Player.cs @@ -2,11 +2,6 @@ using Godot; public partial class Player : CharacterBody3D { - [Signal] - public delegate void OnHitEventHandler(); - [Signal] - public delegate void FireGunEventHandler(); - public const float _speed = 5.0f; public const float _jumpVelocity = 4.5f; [Export] @@ -64,15 +59,6 @@ public partial class Player : CharacterBody3D _pivot.RotateX(Mathf.DegToRad(-inputMouseEvent.Relative.Y * _mouseSensitivity)); _pivot.Rotation = new Vector3(Mathf.Clamp(_pivot.Rotation.X, Mathf.DegToRad(-80), Mathf.DegToRad(80)), _pivot.Rotation.Y, _pivot.Rotation.Z); } - - if (@event.IsActionPressed("fire")) - EmitSignal(SignalName.FireGun); - - if (@event.IsActionPressed("fire") && _aimCast.IsColliding()) - { - EmitSignal(SignalName.OnHit); - _aimCast.GetCollider().Call("OnHitEvent", 10); - } } private static Vector3 MovePlayer(Vector3 velocity, Basis basis, float delta, bool isOnFloor) diff --git a/Scripts/PlayerHUD.cs b/Scripts/PlayerHUD.cs index fd20037..b6e2bc3 100644 --- a/Scripts/PlayerHUD.cs +++ b/Scripts/PlayerHUD.cs @@ -3,16 +3,15 @@ using Godot; public partial class PlayerHUD : CanvasLayer { - private int _currentAmmo = 30; public override void _Ready() { - var ammoLabel = GetChildren().Single(x => x.Name == "AmmoLabel") as Label; - ammoLabel.Text = $"{_currentAmmo}/30"; + UpdateCurrentAmmo(); } - public void OnFireGun() + public void UpdateCurrentAmmo() { + var gun = GetNode("/root/Player/Gun"); var ammoLabel = GetChildren().Single(x => x.Name == "AmmoLabel") as Label; - ammoLabel.Text = $"{--_currentAmmo}/30"; + ammoLabel.Text = $"{gun.CurrentAmmo}/30"; } } diff --git a/project.godot b/project.godot index e3e20d7..872f0e0 100644 --- a/project.godot +++ b/project.godot @@ -67,6 +67,11 @@ quit={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +reload={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null) +] +} [physics]