/
Weapon.cs
285 lines (249 loc) · 8.29 KB
/
Weapon.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum WeaponType {
MainCaliber,
Raycaster,
Autogun
}
public class Weapon : MonoBehaviour {
protected const float MIN_ANGLE_FOR_FIRE = 1;
protected const float REFRESH_TICK = 3;
public Transform[] guns;
public float trunkLength = 0;
public MaterialPurpose raysMaterialType;
public SpritePurpose startSpriteType;
public WeaponType weaponType;
public float maxDistance;
public float maxAngle;
public byte firingZones = 51; // 2(6) 1(5) - 1-4 is upper hemisphere, 5 - 8 is bottom, decart system of coordinates
// 3(7) 4(8)
public float reloadTime;
public float damagePerSecond;
public int penetration;
public float pointingSpeed;
public float shotTime;
public Vector3 weaponDirection;
Vector3 pointingVector;
protected bool firing = false;
protected bool ready = true;
protected bool allGunsPrepared = true;
protected bool authomatic = false;
protected bool working = false;
protected float firingTimeLeft = 0;
protected float reloadingTimeLeft = 0;
protected float updatingEnemy = 0;
public Destructible target;
protected Controller myController;
protected LineRenderer[] rays;
protected GameObject[] startSplashes;
protected GameObject[] endSplashes;
void Awake()
{
working = false;
int count = guns.Length;
if (count == 0) {Destroy(this);return;}
Calibrate();
if (weaponType == WeaponType.Autogun) authomatic = true;
rays = new LineRenderer[count];
startSplashes = new GameObject[count];
endSplashes = new GameObject[count];
Material raysMaterial = GameMaster.storage.GetMaterial(raysMaterialType);
Sprite startSprite = GameMaster.storage.GetSprite(startSpriteType);
for (int i = 0; i < count; i++)
{
rays[i] = guns[i].gameObject.AddComponent<LineRenderer>();
rays[i].material = raysMaterial;
rays[i].startWidth = damagePerSecond/15;
rays[i].positionCount = 2;
rays[i].receiveShadows = false;
rays[i].shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
startSplashes[i] =new GameObject("startSplash");
startSplashes[i].transform.parent = guns[i].transform;
startSplashes[i].transform.localPosition = Vector3.forward * trunkLength;
SpriteRenderer sr = startSplashes[i].AddComponent<SpriteRenderer>();
sr.sprite = startSprite;
SpriteController sc = startSplashes[i].AddComponent<SpriteController>();
sc.SetData(Vector3.one * damagePerSecond /25.0f, penetration, 0);
endSplashes[i] = new GameObject("endSplash");
endSplashes[i].transform.parent = guns[i].transform;
endSplashes[i].transform.localPosition = Vector3.zero;
sr = endSplashes[i].AddComponent<SpriteRenderer>();
sr.sprite = startSprite;
sc = endSplashes[i].AddComponent<SpriteController>();
sc.SetData(Vector3.one * damagePerSecond /25.0f, penetration, 0);
rays[i].enabled = false;
startSplashes[i].SetActive(false);
endSplashes[i].SetActive(false);
}
}
protected virtual void Calibrate() {
if (weaponDirection == Vector3.zero) weaponDirection = transform.root.InverseTransformDirection(guns[guns.Length/2].forward);
pointingVector = transform.root.TransformDirection(weaponDirection);
foreach (Transform gun in guns) gun.transform.forward = pointingVector;
}
void Update()
{
if (GameMaster.IsPaused() ) return;
if (!working) {
//print ("searching for my Controller");
myController = transform.root.gameObject.GetComponent<Controller>();
if (myController != null)
{
myController.AddWeapon(this);
working = true;
}
return;
}
if (target != null)
{
if (!target.gameObject.activeSelf || !InRange(target.transform.position)) {StopGuns();}
}
else
{
//print ("I have no target");
if (firing) StopGuns();
if (authomatic && ready) {
updatingEnemy -= Time.deltaTime;
if (updatingEnemy <= 0) {
//print("time to request a new target");
updatingEnemy = REFRESH_TICK;
target = myController.GetEnemy(this);
}
}
}
float t = Time.deltaTime;
int i;
if (firing)
{
if (firingTimeLeft > 0)
{
firingTimeLeft -= t;
if (firingTimeLeft <=0) StopGuns();
}
if (target != null && InRange(target.transform.position)) PointGunsOnDirection(target.transform.position - transform.position, t);
RaycastHit rh;
for (i = 0; i < guns.Length; i++)
{
rays[i].SetPosition(0, guns[i].position + guns[i].forward * trunkLength);
Destructible d = null;
if (Physics.Raycast(guns[i].position + guns[i].forward * trunkLength, guns[i].forward , out rh, maxDistance))
{
d = rh.collider.GetComponent<Destructible>();
}
Vector3 endPoint;
if (rh.collider != null) {
endPoint = rh.point;
rh.collider.SendMessage("ApplyDamage", new Damage(damagePerSecond*t, penetration*t, rh.point));
}
else endPoint= guns[i].position + guns[i].forward * maxDistance;
rays[i].SetPosition(1, endPoint);
rays[i].endWidth = Vector3.Distance(rays[i].GetPosition(0), endPoint) / maxDistance * rays[i].startWidth;
endSplashes[i].transform.position = endPoint;
}
}
else
{
if (target != null) //GUNS POINTING ON TARGET
{
if (PointGunsOnDirection(target.transform.position - transform.position, t) == true && Vector3.Distance(target.transform.position, transform.position) <= maxDistance) ActivateGuns();
}
else
{ //GUNS AFTER FIRE
if (reloadingTimeLeft <=0) // for one-shot events where shot time is zero
{
StopGuns();
}
else
{
reloadingTimeLeft -= t;
if (reloadingTimeLeft <= 0)
{
ready = true;
target = null;
}
}
if (!allGunsPrepared) allGunsPrepared = PointGunsOnDirection(transform.root.TransformDirection(weaponDirection), t);
}
}
}
public void Fire (Destructible t)
{
if (ready && t != null && InRange(t.transform.position)) target = t;
}
bool PointGunsOnDirection (Vector3 dir, float time)
{
pointingVector = Vector3.RotateTowards(pointingVector, dir, pointingSpeed * time, pointingVector.magnitude );
foreach (Transform gun in guns) gun.forward = pointingVector;
if (Vector3.Angle (pointingVector, dir) < MIN_ANGLE_FOR_FIRE) return true; else return false;
}
public bool IsReady () {return ready;}
public void ActivateGuns() ///raycasting
{
firingTimeLeft = shotTime;
Vector3 endPos;
for (int i =0; i < guns.Length; i++)
{
rays[i].SetPosition(0, guns[i].position + guns[i].forward * trunkLength );
RaycastHit rh;
if (Physics.Raycast(guns[i].transform.position + guns[i].forward * trunkLength, guns[i].transform.forward, out rh, maxDistance)) {
endPos = rh.point;
}
else endPos = guns[i].position + guns[i].forward * maxDistance;
rays[i].SetPosition(1, endPos);
rays[i].endWidth = Vector3.Distance(rays[i].GetPosition(0), endPos) / maxDistance * rays[i].startWidth;
endSplashes[i].transform.position = endPos;
startSplashes[i].SetActive(true);
rays[i].enabled = true;
endSplashes[i].SetActive(true);
}
firingTimeLeft = shotTime;
reloadingTimeLeft = 0;
firing = true;
ready = false;
allGunsPrepared = false;
}
public void StopGuns() {
target = null;
firing = false;
for (int i = 0; i < guns.Length; i++)
{
rays[i].enabled = false;
startSplashes[i].SetActive(false);
endSplashes[i].SetActive(false);
}
reloadingTimeLeft = reloadTime;
}
public void SetAuthomatic (bool x) {
//print ("authomatic set to "+x.ToString());
authomatic = x;
}
public bool InRange (Vector3 pos)
{
Vector3 inpos = transform.root.InverseTransformPoint(pos);
if (inpos.magnitude > maxDistance) return false;
byte zone = 0;
if (inpos.y > 0) {
if (inpos.x >= 0) {
if (inpos.z >= 0) zone = 1;
else zone = 8;
}
else {
if (inpos.z >= 0) zone = 2;
else zone = 4;
}
}
else {
if (inpos.x >= 0) {
if (inpos.z >= 0) zone = 16;
else zone = 128;
}
else {
if (inpos.z >= 0) zone = 32;
else zone = 64;
}
}
if ((firingZones & zone) == 0) return false;
if (Vector3.Angle(weaponDirection, inpos) <= maxAngle) return true; else return false;
}
}