forked from domeengine/dome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.wren
378 lines (324 loc) · 7.17 KB
/
main.wren
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import "input" for Keyboard
import "graphics" for Canvas, Color, ImageData, Point
import "audio" for AudioEngine
import "random" for Random
import "./test"
import "io" for FileSystem
// Consider moving Box to "graphics"
class Box {
construct new(x1, y1, x2, y2) {
_p1 = Point.new(x1, y1)
_p2 = Point.new(x2, y2)
}
x1 { _p1.x }
y1 { _p1.y }
x2 { _p2.x }
y2 { _p2.y }
}
// -------------------------
// ------- GAME CODE -------
// -------------------------
class Game {
static init() {
__state = MainGame
__state.init()
__done = false
__loadSettingsOp = FileSystem.load("setup.sh")
System.print(__loadSettingsOp.result.length)
}
static update() {
if (__loadSettingsOp.complete && !__done) {
__settingsFile = __loadSettingsOp.result
__done = true
System.print("loaded")
System.print(__settingsFile.data)
}
__state.update()
if (__state.next) {
__state = __state.next
__state.init()
}
}
static draw(dt) {
__state.draw(dt)
}
}
class Explosion {
construct new(x, y) {
_x = x + OurRandom.int(6)-3
_y = y + OurRandom.int(6)-3
_c = [Color.red, Color.orange][OurRandom.int(2)]
_t = 0
}
x { _x }
y { _y }
done { _t > 5 }
update() {
_t = _t + 1
}
draw() {
Canvas.circlefill(_x, _y, _t, _c)
}
}
class Star {
construct new() {
_x = OurRandom.int(Canvas.width)
_y = OurRandom.int(Canvas.height)
_s = OurRandom.float()
}
x { _x }
y { _y }
update() {
_y = _y + 0.25 + _s
if (_y > Canvas.height) {
_x = OurRandom.int(Canvas.width)
_y = 0
}
}
draw() {
Canvas.pset(_x, _y, Color.lightgray)
}
}
class Bullet {
construct fire(x, y) {
_x = x
_y = y
_alive = true
}
x { _x }
y { _y }
h { 2 }
w { 2 }
alive { _alive }
kill() {
_alive = false
}
update() {
_y = _y - 3
}
draw() {
var color = Color.white
Canvas.rectfill(_x, _y, 2, 2, Color.white)
Canvas.rectfill(_x, _y+2, 2, 4, Color.darkgray)
}
}
class Enemy {
construct new(x, y) {
_x = x
_y = y
_alive = true
_image = ImageData.loadFromFile("res/enemy.png")
}
alive { _alive }
x { _x }
y { _y }
h { 8 }
w { 6 }
kill() {
_alive = false
}
update() {
_y = _y + 1
}
draw() {
if (alive) {
Canvas.draw(_image, x, y)
}
}
}
class Ship {
construct new() {
_x = Canvas.width / 2
_y = Canvas.height - 20
_imm = false
_health = 3
_t = 0
_ship = [
ImageData.loadFromFile("res/ship1.png"),
ImageData.loadFromFile("res/ship2.png")
]
}
x { _x }
y { _y }
h { 8 }
w { 6 }
health { _health }
imm { _imm }
damage() {
if (!_imm) {
_health = _health - 1
_imm = true
_t = 0
}
}
move(x, y) {
_x = _x + x
_y = _y + y
_t = _t + 1
if (_imm && _t > 30) {
_imm = false
}
}
draw(t) {
var frame = (t / 5).floor % 2
if (_health > 0 && !_imm || (_t/4).floor % 2 == 0) {
Canvas.draw(_ship[frame], _x, _y)
}
}
}
var OurRandom = Random.new(12345)
class MainGame {
static next { __next}
static init() {
__next = null
__w = 5
__h = 5
__t = 0
__points = 0
__ship = Ship.new()
__bullets = []
__enemies = []
__explosions = []
__stars = []
for (i in 0...30) {
__stars.add(Star.new())
}
for (i in 0...5) {
__enemies.add(Enemy.new(OurRandom.int(Canvas.width), -OurRandom.int(30)))
}
__lastFire = 0
__heart = ImageData.loadFromFile("res/heart-full.png")
__heartEmpty = ImageData.loadFromFile("res/heart-empty.png")
AudioEngine.load("fire", "res/Laser_Shoot.wav")
AudioEngine.load("explosion", "res/Explosion.wav")
AudioEngine.load("music", "res/around-the-corner.ogg")
// AudioEngine.load("music", "res/music.wav")
__channel = AudioEngine.play("music", 1, true, -0.5)
}
static update() {
__t = __t + 1
var x = 0
var y = 0
AudioEngine.setChannelPan(__channel, (((__t / 60) % 20) * 0.1) - 1 )
if (__ship.health > 0) {
if (Keyboard.isKeyDown("left")) {
x = -1
}
if (Keyboard.isKeyDown("right")) {
x = 1
}
if (Keyboard.isKeyDown("up")) {
y = -1
}
if (Keyboard.isKeyDown("down")) {
y = 1
}
if (Keyboard.isKeyDown("space")) {
if ((__t - __lastFire) > 10) {
__bullets.add(Bullet.fire(__ship.x+2, __ship.y))
__lastFire = __t
AudioEngine.play("fire")
}
}
}
// TODO: Remove this and embed it in main engine
// AudioEngine.update()
__ship.move(x, y)
for (enemy in __enemies) {
enemy.update()
if (!__ship.imm && colliding(__ship, enemy)) {
__ship.damage()
enemy.kill()
AudioEngine.play("explosion")
for (i in 1..5) {
__explosions.add(Explosion.new(enemy.x, enemy.y))
}
}
}
if (__ship.health == 0 && __explosions.count == 0) {
__next = GameOverState
AudioEngine.stopAllChannels()
}
var bulletCount = 0
for (bullet in __bullets) {
bullet.update()
// check if we hit something
for (enemy in __enemies) {
if (enemy.alive && colliding(bullet, enemy)) {
bullet.kill()
enemy.kill()
AudioEngine.play("explosion")
for (i in 1..5) {
__explosions.add(Explosion.new(enemy.x, enemy.y))
}
__points = __points + 1
}
}
}
__bullets = __bullets.where {|bullet|
return bullet.alive && bullet.y > 0
}.toList
__enemies = __enemies.where {|enemy|
var isAlive = enemy.alive && enemy.y < Canvas.height
if (!isAlive) {
__enemies.add(Enemy.new(OurRandom.int(Canvas.width), 0))
}
return isAlive
}.toList
__explosions = __explosions.where {|explosion|
explosion.update()
return !explosion.done
}.toList
for (star in __stars) {
star.update()
}
}
static colliding(o1, o2) {
var box1 = Box.new(o1.x, o1.y, o1.x + o1.w, o1.y+o1.h)
var box2 = Box.new(o2.x, o2.y, o2.x + o2.w, o2.y+o2.h)
return box1.x1 < box2.x2 &&
box1.x2 > box2.x1 &&
box1.y1 < box2.y2 &&
box1.y2 > box2.y1
}
static draw(dt) {
Canvas.cls()
__stars.each {|star| star.draw() }
__enemies.each {|enemy| enemy.draw() }
__bullets.each {|bullet| bullet.draw() }
__ship.draw(__t)
__explosions.each {|explosion| explosion.draw() }
Canvas.rectfill(0,0, 320,10, Color.black)
// Draw UI
for (i in 1..3) {
if (i <= __ship.health) {
Canvas.draw(__heart, 292+6*i, 3)
} else {
Canvas.draw(__heartEmpty, 292+6*i, 3)
}
}
Canvas.print("Score: %(__points)", 3, 3, Color.white)
}
}
// State displays a "Game Over" message and allows a restart
class GameOverState {
static next { __next}
static init() {
__next = null
__hold = 0
}
static update() {
if (Keyboard.isKeyDown("space")) {
__hold = __hold + 1
if (__hold > 4) {
__next = MainGame
}
} else {
__hold = 0
}
}
static draw(dt) {
Canvas.cls()
Canvas.print("Game Over", 160-27, 120-3, Color.white)
}
}