-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeaponManger.cs
More file actions
206 lines (178 loc) · 5.23 KB
/
WeaponManger.cs
File metadata and controls
206 lines (178 loc) · 5.23 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Assets.Scripts.Weapon;
public class WeaponManger : MonoBehaviour
{
public Gun[] weapons;
[SerializeField] int defaultWeapon = 8;
public GrenadeThrower grenadeThrower;
public CharacterController character;
private int currentWeapon = 0;
private PlayerMovement charcterConteroller;
public Camera fpsCamera;
public GameObject crosshair;
public PlayerUI playerUI;
private bool grenadeThrowing = false;
private bool isSwitching = false;
private bool weaponHidden = false;
private GearVest gearVest;
// Start is called before the first frame update
void Start()
{
charcterConteroller = character.GetComponent<PlayerMovement>();
gearVest = new GearVest();
InitWeaponsList();
charcterConteroller.WeaponPositionEvent += new PlayerMovement.WeaponPosition(WeaponPosition);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.H))
{
HideWeapon();
}
if (weaponHidden)
return;
if (Input.GetButton("Fire1"))
{
if (charcterConteroller.CanFire)
{
Weapon.Shoot();
}
}
if (Input.GetMouseButtonDown(1) && charcterConteroller.IsWalking && charcterConteroller.OnGround)
{
crosshair.SetActive(false);
Weapon.SightMode(true);
}
else if (Input.GetMouseButtonUp(1))
{
crosshair.SetActive(true);
Weapon.SightMode(false);
}
WeaponSwitch();
if (Input.GetKeyDown(KeyCode.R))
{
Weapon.Reload();
}
if (Input.GetKeyDown(KeyCode.F) && CanAnimate())
{
Weapon.MeleeAttack();
}
if (Input.GetKeyDown(KeyCode.G))
{
ThrowGrenade();
}
}
private void ThrowGrenade()
{
Weapon.Selection(false);
grenadeThrowing = true;
isSwitching = true;
}
private void HideWeapon()
{
weaponHidden = !weaponHidden;
Weapon.Selection(!weaponHidden);
}
private void WeaponSwitch()
{
if (Weapon.Type == WeaponType.SniperRifle && Weapon.IsOnSight || !charcterConteroller.IsWalking || Weapon.IsOnAction() || isSwitching)
return;
float mouseWheel = Input.GetAxis("Mouse ScrollWheel");
if (mouseWheel != 0)
{
weapons[currentWeapon].Selection(false);
isSwitching = true;
if (mouseWheel > 0f)
{
if (currentWeapon == weapons.Length - 1)
currentWeapon = 0;
else
currentWeapon++;
}
if (mouseWheel < 0f)
{
if (currentWeapon == 0)
currentWeapon = weapons.Length - 1;
else
currentWeapon--;
}
}
}
public void UpdateAmmoUI(int ammo)
{
playerUI.UpdateAmmo(ammo, gearVest.Ammo.Amount(Weapon.GetAmmotype));
}
public Gun Weapon
{
get
{
return weapons[currentWeapon];
}
}
private void InitWeaponsList()
{
currentWeapon = defaultWeapon;
for(int i=0; i<weapons.Length; i++)
{
weapons[i].updateWeaponUIEvent += new Gun.UpdateAmmo(UpdateAmmoUI);
weapons[i].updateAmmoEvent += new Gun.UpdateAmmo(UpdateAmmo);
weapons[i].getAmmoEvent += new Gun.AmmoData(GetAmmo);
weapons[i].selectionEvent += new Weapon.SetBool(WeaponSelection);
weapons[i].gameObject.SetActive((i == defaultWeapon));
}
UpdateAmmoUI(Weapon.Magazine);
playerUI.Weapon.SetWeaponIcon(Weapon.Icon);
}
public void UpdateAmmo(int amount)
{
gearVest.Ammo.SetAmount(Weapon.GetAmmotype, amount);
}
public int GetAmmo(AmmoType type)
{
return gearVest.Ammo.Amount(type);
}
public PlayerUI GetPlayerUI()
{
return playerUI;
}
public void WeaponPosition(string pos, bool state)
{
Weapon.SetPosition(pos, state);
}
public bool IsOnSight
{
get { return Weapon.IsOnSight; }
}
private bool CanAnimate()
{
return charcterConteroller.IsWalking && !Weapon.Reloading && !Weapon.IsOnSight;
}
private void WeaponSelection(bool state)
{
if(state)
isSwitching = false;
else if (!weaponHidden)
{
if (grenadeThrowing)
{
grenadeThrower.Throw();
grenadeThrowing = false;
StartCoroutine(SelectWeapon(grenadeThrower.throwingTime + 0.2f));
}
else
StartCoroutine(SelectWeapon());
}
}
private IEnumerator SelectWeapon(float waitingTime = 0.6f)
{
// delay weapon selection
yield return new WaitForSeconds(waitingTime);
weapons[currentWeapon].Selection(true);
playerUI.UpdateAmmo(Weapon.Magazine, gearVest.Ammo.Amount(Weapon.GetAmmotype));
playerUI.Weapon.SetWeaponIcon(Weapon.Icon);
}
}