Reload and player HUD

This commit is contained in:
2023-08-04 14:18:28 -07:00
parent 339cb374b8
commit e396a34d1b
5 changed files with 81 additions and 25 deletions

61
Scripts/Gun.cs Normal file
View 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;
}
}

View File

@@ -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)

View File

@@ -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";
}
}