Reload and player HUD
This commit is contained in:
@@ -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"]
|
||||
|
||||
61
Scripts/Gun.cs
Normal file
61
Scripts/Gun.cs
Normal file
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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<Gun>("/root/Player/Gun");
|
||||
var ammoLabel = GetChildren().Single(x => x.Name == "AmmoLabel") as Label;
|
||||
ammoLabel.Text = $"{--_currentAmmo}/30";
|
||||
ammoLabel.Text = $"{gun.CurrentAmmo}/30";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user