-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeaponSystem.cs
More file actions
90 lines (76 loc) · 2.52 KB
/
Copy pathWeaponSystem.cs
File metadata and controls
90 lines (76 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using UnityEngine;
using UnityEngine.InputSystem;
namespace FPS3D
{
public class WeaponSystem : MonoBehaviour
{
[Header("Weapon Stats")]
[SerializeField] private float _fireRate = 10f;
[SerializeField] private float _damage = 25f;
[SerializeField] private float _range = 100f;
[SerializeField] private int _maxAmmo = 30;
[SerializeField] private float _reloadTime = 1.5f;
[Header("References")]
[SerializeField] private Transform _muzzlePoint;
[SerializeField] private LayerMask _hitLayers;
[SerializeField] private ParticleSystem _muzzleFlash;
private int _currentAmmo;
private float _nextFireTime;
private bool _isReloading;
private bool _fireHeld;
private Camera _mainCam;
public int CurrentAmmo => _currentAmmo;
public int MaxAmmo => _maxAmmo;
public bool IsReloading => _isReloading;
private void Awake()
{
_mainCam = Camera.main;
_currentAmmo = _maxAmmo;
}
private void Update()
{
if (_fireHeld && !_isReloading && Time.time >= _nextFireTime && _currentAmmo > 0)
{
Fire();
}
}
public void OnFire(InputValue value) => _fireHeld = value.isPressed;
public async void OnReload(InputValue value)
{
if (value.isPressed && !_isReloading && _currentAmmo < _maxAmmo)
{
await ReloadAsync();
}
}
private void Fire()
{
_nextFireTime = Time.time + 1f / _fireRate;
_currentAmmo--;
if (_muzzleFlash)
_muzzleFlash.Play();
Ray ray = _mainCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
if (Physics.Raycast(ray, out RaycastHit hit, _range, _hitLayers))
{
if (hit.collider.TryGetComponent<IDamageable>(out var target))
{
target.TakeDamage(Mathf.RoundToInt(_damage));
}
}
if (_currentAmmo <= 0)
{
_ = ReloadAsync();
}
}
private async Awaitable ReloadAsync()
{
_isReloading = true;
await Awaitable.WaitForSecondsAsync(_reloadTime, destroyCancellationToken);
_currentAmmo = _maxAmmo;
_isReloading = false;
}
}
public interface IDamageable
{
void TakeDamage(int amount);
}
}