-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.adept
More file actions
215 lines (193 loc) · 6.48 KB
/
Unit.adept
File metadata and controls
215 lines (193 loc) · 6.48 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
207
208
209
210
211
212
213
214
215
enum UnitKind (WORKER, MARINE, CANNON, LING)
struct Unit (
kind UnitKind,
x, y float,
texture CaptTexture,
health, armor int,
id usize,
target <Vector2f> Optional,
timer int,
selectable, enemy bool
) {
constructor(kind UnitKind, x, y float) {
this.kind = kind
this.x = x
this.y = y
this.armor = 0
this.id = gamedata.next_id++
this.target = none() ~> <Vector2f> Optional
this.timer = 0
this.enemy = false
exhaustive switch this.kind {
case UnitKind::WORKER
this.texture = textures.worker
this.health = 40
case UnitKind::MARINE
this.texture = textures.marine
this.health = 40
case UnitKind::CANNON
this.texture = textures.cannon
this.health = 100
this.armor = 2
case UnitKind::LING
this.texture = textures.ling
this.health = 60
this.enemy = true
}
this.selectable = !this.enemy
}
func update {
exhaustive switch this.kind {
case UnitKind::WORKER
if this.timer <= 0 {
each Resources in gamedata.resources {
if it.left > 0 && this.getAABB().intersecting(it.getAABB()) {
it.mine()
this.timer = 60
break
}
}
} else {
this.timer--
}
case UnitKind::MARINE
if this.timer <= 0 {
each Unit in static gamedata.units {
if it.enemy and it.health > 0 and distance(it.x, it.y, this.x, this.y) < 128.0f {
it.damage(10)
gamedata.playSound(sfx.gun)
this.timer = 60
gamedata.particles.add(HitParticle(it.x, it.y))
break
}
}
} else {
this.timer--
}
case UnitKind::CANNON
if this.timer <= 0 {
each Unit in static gamedata.units {
if it.enemy and it.health > 0 and distance(it.x, it.y, this.x, this.y) < 192.0f {
it.damage(60)
gamedata.playSound(sfx.cannon)
this.timer = 180
break
}
}
} else {
this.timer--
}
case UnitKind::LING
target Vector2f = Vector2f(captViewWidth() / 2.0f, captViewHeight() / 2.0f)
closest *Unit = null
closest_distance float = 100000000.0f
each Unit in static gamedata.units {
unless it.enemy {
closeness float = distance(it.x, it.y, this.x, this.y)
if closeness < closest_distance {
closest_distance = closeness
closest = &it
}
}
}
if closest != null {
target = Vector2f(closest.x, closest.y)
}
this.target = some(target)
if this.timer <= 0 {
each Unit in gamedata.units {
if !it.enemy and it.health > 0 and it.getAABB().intersecting(this.getAABB()) {
it.damage(10)
this.timer = 30
gamedata.playSound(sfx.ling_bite)
break
}
}
} else {
this.timer -= 1
}
}
}
func damage(amount int) {
this.health -= max(amount - this.armor, 0)
}
func playDeathSound {
exhaustive switch this.kind {
case UnitKind::WORKER
gamedata.playSound(sfx.worker_death)
case UnitKind::MARINE
gamedata.playSound(sfx.marine_death)
case UnitKind::CANNON
gamedata.playSound(sfx.cannon_death)
case UnitKind::LING
gamedata.playSound(sfx.ling_death)
}
}
func tryPlaceBuilding(kind BuildingKind, ore_cost, gas_cost int) successful {
if gamedata.ore < ore_cost or gamedata.gas < gas_cost {
gamedata.playSound(sfx.error)
return false
}
new_building Building = Building(kind, this.x, this.y)
each Building in static gamedata.buildings {
if it.getAABB().intersecting(new_building.getAABB()) {
gamedata.playSound(sfx.error)
gamedata.next_id--
return false
}
}
gamedata.buildings.add(new_building)
gamedata.ore -= ore_cost
gamedata.gas -= gas_cost
return true
}
func pickOption(index usize) successful {
exhaustive switch this.kind {
case UnitKind::WORKER
switch index {
case 0
return this.tryPlaceBuilding(BuildingKind::CENTER, 400, 0)
case 1
return this.tryPlaceBuilding(BuildingKind::DEPOT, 150, 0)
case 2
return this.tryPlaceBuilding(BuildingKind::FACTORY, 150, 50)
}
case UnitKind::MARINE
case UnitKind::CANNON
case UnitKind::LING
}
return false
}
func draw {
const width float = 32.0f
const height float = 32.0f
reversed bool = false
if this.kind == UnitKind::LING && this.target.has && this.target.getPointer().x < this.x {
reversed = true
}
if reversed {
captDrawTexture(this.texture, this.x + width/2.0f, this.y - height/2.0f, -width, height)
} else {
captDrawTexture(this.texture, this.x - width/2.0f, this.y - height/2.0f, width, height)
}
}
func drawSelectionCircle {
const width float = 32.0f
const height float = 32.0f
captDrawTexture(textures.selection, this.x - width/2.0f, this.y - height/2.0f, width, height)
}
func drawOptions {
exhaustive switch this.kind {
case UnitKind::WORKER
drawSingleOption(0, textures.center)
drawSingleOption(1, textures.depot)
drawSingleOption(2, textures.factory)
case UnitKind::MARINE
case UnitKind::CANNON
case UnitKind::LING
}
}
func getAABB <float> AABB {
return AABB(this.x - 16.0f, this.y - 16.0f, 32.0f, 32.0f)
}
}