-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerTank.cs
214 lines (134 loc) · 4.39 KB
/
PlayerTank.cs
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
207
208
209
210
211
212
213
214
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTank : TankBase {
[Header("PlayerControls")]
public KeyCode left;
public KeyCode right;
public KeyCode forward;
public KeyCode backward;
public KeyCode fireWeapon;
public KeyCode weaponSelect1;
public KeyCode weaponSelect2;
public KeyCode weaponSelect3;
public KeyCode OverHeadCamera;
public KeyCode DriveCamera;
public GunData activegun;
private float XMovement;
private float ZMovement;
private bool moved;
//*** Index of last fired shell
public int shellIndex;
private Projectile lastshell;
private float firingRateTimer;
private float firingRate;
private Vector3 shootingOffset = new Vector3(0f,0f,0f);
// Use this for initialization
void Start () {
base.Start ();
//*** Set pistol as default weapon
SetActiveweapon (0);
CameraController.instance.SetCameraTarget (this.gameObject,
CameraController.CameraOptions.DriveView);
GameplayManager.instance.SetPlayerTransform (this.gameObject.transform);
}
// Update is called once per frame
void Update () {
base.Update ();
//*** Reset Movement
XMovement = 0f;
ZMovement = 0f;
moved = false;
//*** Move from left to right
if(Input.GetKey(left)){
moved = true;
transform.Rotate((Vector3.up * Time.deltaTime * Attributes.rotationSpeed) * -1f);
}else if(Input.GetKey(right)){
moved = true;
transform.Rotate(Vector3.up * Time.deltaTime *Attributes.rotationSpeed);
}
//*** Move forward and Back
if(Input.GetKey(forward)){
moved = true;
ZMovement = Attributes.speed;
}else if(Input.GetKey(backward)){
moved = true;
ZMovement = Attributes.speed * -1f;
}
//*** Increment timer
firingRateTimer += Time.deltaTime;
//*** Check against firing rate
if (Input.GetKey(fireWeapon) && firingRateTimer >= firingRate) {
//** Reset Firing Rate timer
firingRateTimer = 0f;
//*** Reset shell Index
shellIndex = 0;
//*** Begin Firing Weapons
FireWeapon ();
}
//*** Weapon Switch Checks
if (Input.GetKeyDown (weaponSelect1)) {
SetActiveweapon (0);
}
if (Input.GetKeyDown (weaponSelect2)) {
SetActiveweapon (1);
}
if (Input.GetKeyDown (weaponSelect3)) {
SetActiveweapon (2);
}
if (Input.GetKeyDown (OverHeadCamera)) {
CameraController.instance.SetCamera (CameraController.CameraOptions.TopDown);
}
if (Input.GetKeyDown (DriveCamera)) {
CameraController.instance.SetCamera (CameraController.CameraOptions.DriveView);
}
//*** Move Tank
if(moved)
transform.Translate(new Vector3(XMovement,0f,ZMovement));
}
public void FireWeapon(){
StartCoroutine (FireNextShell (activegun.projectileInfo[shellIndex].firingDelay));
}
IEnumerator FireNextShell( float fireDelay){
//*** Get Players Current rotation
Vector3 rotation = transform.rotation.eulerAngles;
//*** Add Rotation angle of the Shell to players Rotation Angle
rotation = rotation + activegun.projectileInfo[shellIndex].firingAngle;
//*** Get firing position and offset
Vector3 firingPosition = transform.position + shootingOffset;
yield return new WaitForSeconds(fireDelay);
//*** Instantiate shell withcalculated firing angle ( players angle + firing angle of projectile itself)
lastshell = Instantiate (activegun.projectileInfo[shellIndex].projectilePrefab,
firingPosition , Quaternion.Euler(rotation)).GetComponent<Projectile>();
//*** Set Projectile info
lastshell.SetProjectile(activegun.ProjectileFlightSpeed,
activegun.ammoLifeSpan,
activegun.damageOnHit);
//*** Increment index
shellIndex++;
if(shellIndex < activegun.projectileInfo.Count )
FireWeapon();
}
public void SetActiveweapon(int pWeaponNum){
Debug.Log ("Setting Weapon");
switch (pWeaponNum) {
case 0:
activegun = GameplayManager.instance.GetWeapon (GunData.gunType.pistol);
firingRate = activegun.firingRate;
UIController.instance.RenderWeaponSelection (activegun.nickName);
break;
case 1:
activegun = GameplayManager.instance.GetWeapon (GunData.gunType.machineGun);
firingRate = activegun.firingRate;
UIController.instance.RenderWeaponSelection (activegun.nickName);
break;
case 2:
activegun = GameplayManager.instance.GetWeapon (GunData.gunType.ShotGun);
firingRate = activegun.firingRate;
UIController.instance.RenderWeaponSelection (activegun.nickName);
break;
default:
break;
}
}
}