-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseUnit.cs
210 lines (195 loc) · 5.79 KB
/
BaseUnit.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* this class goes to every game unit, contains basics for unity mechanics including health, selection info, and unittype
*/
public class BaseUnit : MonoBehaviour {
public Weapon hullMountedWeapon = null;
public int maxHP, HP;
public int owner = 0; //0 is neutral
public bool selected;
public bool isArmed = true; //does this have a weapon or turret
private int weaponRange = 0; //range of weapon(s)
private string unitName = "Unnamed Unit";
private Pathfinder myPathfinder = null; //this is to be set on start
public List<Turret> turrets = new List<Turret>();
public GameObject deathExplosion = null; //spawns on death
public GameObject selectionCircle = null; //the green thing that is around the unit when selected
private GameObject spawnedSelectionCircle = null; //the actual circle thing that is currently around the unit
private Classification classification = Classification.Simple;
public Pathfinder getMyPathfinder()
{
return myPathfinder;
}
public void setMyPathfinder(Pathfinder p)
{
myPathfinder = p;
}
public Classification GetClassification()
{
return classification;
}
public void setClassification(Classification c)
{
classification = c;
}
public string getUnitName()
{
return unitName;
}
public void setUnitName(string s)
{
unitName = s;
}
void Start()
{
init();
recordUnit();
}
public int getWeaponRnage()
{
return weaponRange;
}
public void setWeaponRange(int i )
{
weaponRange = i;
}
//adds unit to list of that player
public void recordUnit()
{
try
{
if (PlayerControl.playerUnits[owner] == null)
{
PlayerControl.playerUnits.Add(owner, new HashSet<BaseUnit>());
}
PlayerControl.playerUnits[owner].Add(this);
}
catch (KeyNotFoundException knfe)
{
PlayerControl.playerUnits.Add(owner, new HashSet<BaseUnit>());
PlayerControl.playerUnits[owner].Add(this);
print("added key " + owner);
}
}
//removes unit from playerlist
public void recordUnitDeath()
{
if (PlayerControl.playerUnits[owner].Contains(this))
{
PlayerControl.playerUnits[owner].Remove(this);
}
}
public void setupWeapons()
{
int lowestRange = 99999;
Turret[] tur = this.gameObject.GetComponentsInChildren<Turret>();
if (tur != null && tur.Length > 0)
{
foreach (Turret t in tur)
{
if (t == null) continue;
if (!turrets.Contains(t)) turrets.Add(t);
if (t.getWeapon().range < lowestRange)
{
lowestRange = t.getWeapon().range;
}
}
}
if (hullMountedWeapon != null && hullMountedWeapon.range < lowestRange) lowestRange = hullMountedWeapon.range;
weaponRange = lowestRange;
if (lowestRange > 9999)
{
//no guns
isArmed = false;
}
}
//should be overwritten by each unit, sets up initial params
public virtual void init()
{
unitName = "unnamed Unit";
maxHP = 100;
HP = maxHP;
selected = false;
classification = Classification.Simple;
myPathfinder = gameObject.GetComponent<Pathfinder>();
owner = 1;
}
/*
* dont just modify the raw HP, use this method
*/
public void TakeDamage(int amount)
{
this.HP -= amount;
if (HP <= 0)
{
Kill();
}
}
/*
* removes this unit from the game
*/
public void Kill()
{
recordUnitDeath();
OnDeath();
if (gameObject.transform.parent != null)
{
Destroy(gameObject.transform.parent.gameObject);
return;
}
Destroy(this.gameObject);
}
//what happens when this dies
public virtual void OnDeath()
{
if (deathExplosion != null)
{
Instantiate(deathExplosion, gameObject.transform.position, gameObject.transform.rotation);
}
}
//used to modify where bullets should aim.
public virtual Vector3 getHitOffset()
{
return new Vector3(0, 0, 0);
}
public void SetSelected(bool b)
{
if (b)
{
selected = true;
if (selectionCircle != null && spawnedSelectionCircle==null)
{
spawnedSelectionCircle = Instantiate(this.selectionCircle, new Vector3(transform.parent.transform.position.x, -.35f, transform.parent.transform.position.z), new Quaternion(0, 0, 0, 0), this.transform);
}
//add here code that make a torus appear around the unit BROM ADD IT, your selection method sucks
}
else
{
selected = false;
if (spawnedSelectionCircle != null)
{
Destroy(spawnedSelectionCircle);
}
//add here code that makes the torus invisable BROM ADD IT
}
}
public bool IsSelected()
{
return selected;
}
}
/*
* used in the backend for determining which pathfinder to use and other mechaincal things
* simple is basic look at target then shoot
* tank should support a rotating turret
* heli floats in air and looks at target
* plane flies in lines around target
* structure doesnt move
*/
public enum Classification { Simple, Tank, Plane, Heli, Structure };
/*
* used for in-game systems like dealing extra damage to armored units
*/
public enum Attribute { Light, Armored, Flier, Structure, Biological }