-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilding.adept
130 lines (113 loc) · 4.18 KB
/
Building.adept
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
enum BuildingKind (CENTER, DEPOT, FACTORY)
enum ProductionKind (UNIT)
struct Production (kind ProductionKind, id usize, progress, progress_speed float, done_sound Sound) {
constructor(unit_kind UnitKind, progress_speed float, done_sound Sound) {
this.kind = ProductionKind::UNIT
this.id = unit_kind as usize
this.progress = 0.0
this.progress_speed = progress_speed
this.done_sound = done_sound
}
}
struct Building (kind BuildingKind, x, y, w, h float, texture CaptTexture, id usize, name String, production <Production> Optional) {
constructor(kind BuildingKind, x, y float) {
this.kind = kind
this.x = x
this.y = y
this.id = gamedata.next_id++
this.production = none() ~> <Production> Optional
exhaustive switch kind {
case BuildingKind::CENTER
this.name = "Center"
this.w = 128.0f
this.h = 128.0f
this.texture = textures.center
case BuildingKind::DEPOT
this.name = "Shooting Range"
this.w = 64.0f
this.h = 64.0f
this.texture = textures.depot
case BuildingKind::FACTORY
this.name = "Factory"
this.w = 64.0f
this.h = 64.0f
this.texture = textures.factory
}
}
func update {
if this.production.isSome() {
production *Production = this.production.getPointer()
production.progress += production.progress_speed
if production.progress >= 1.0f {
exhaustive switch production.kind {
case ProductionKind::UNIT
gamedata.units.add(Unit(production.id as UnitKind, this.x, this.y + this.h / 2.0f))
gamedata.playSound(production.done_sound)
}
this.production = none() ~> <Production> Optional
}
}
}
func pickOption(index usize) successful {
if this.production.isSome(), return false
exhaustive switch this.kind {
case BuildingKind::CENTER
switch index {
case 0
if gamedata.ore < 50 {
gamedata.playSound(sfx.error)
return false
}
this.production = some(Production(UnitKind::WORKER, 0.005f, sfx.worker_ready))
gamedata.ore -= 50
}
case BuildingKind::DEPOT
switch index {
case 0
if gamedata.ore < 50 {
gamedata.playSound(sfx.error)
return false
}
this.production = some(Production(UnitKind::MARINE, 0.005f, sfx.marine_ready))
gamedata.ore -= 50
return true
}
case BuildingKind::FACTORY
switch index {
case 0
if gamedata.ore < 100 or gamedata.gas < 50 {
gamedata.playSound(sfx.error)
return false
}
this.production = some(Production(UnitKind::CANNON, 0.002f, sfx.cannon_ready))
gamedata.ore -= 100
gamedata.gas -= 50
return true
}
}
return false
}
func draw {
captDrawTexture(this.texture, this.x - this.w/2.0f, this.y - this.h/2.0f, this.w, this.h)
}
func drawOptions {
exhaustive switch this.kind {
case BuildingKind::CENTER
drawSingleOption(0, textures.worker)
case BuildingKind::DEPOT
drawSingleOption(0, textures.marine)
case BuildingKind::FACTORY
drawSingleOption(0, textures.cannon)
}
}
func drawProgress {
if this.production.isSome() {
progress float = this.production.getPointer().progress
captDrawTexture(textures.grey, this.x - this.w/2.0f, this.y - 3.0f*this.h/8.0f, this.w, 8.0f)
captDrawTexture(textures.white, this.x - this.w/2.0f, this.y - 3.0f*this.h/8.0f, this.w * progress, 8.0f)
}
}
func getAABB <float> AABB {
return AABB(this.x - this.w/2.0f, this.y - this.h/2.0f, this.w, this.h)
}
}