/
game.wren
60 lines (50 loc) · 1.47 KB
/
game.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
import "dome" for Process
import "graphics" for Canvas, Color, Point
import "./api" for CanvasString, Fading, Scene, Sprite
import "./player" for Player
import "./enemies" for EnemySpawner
class GameLevel is Scene {
construct init(parent) {
_parent = parent
// Load in the background and HUD
setupDrawLoop()
_background = Sprite.new("res/level_bg.jpg")
_bgOffset = 0
addTempCanvasItem(_background, _bgOffset, 0)
addCanvasItem(CanvasString.new("Current Score:"), 10, 10)
_score = 0
_prefs = _parent.loadPrefs()
// Set up audio
if (!_prefs["mute"]) {
_channelID = Fading.play("cyberrunner")
}
// Add the player and a list to track the enemies
_player = Player.new()
addTempCanvasItem(_player, _player.x, _player.y)
_spawn = EnemySpawner.new(_prefs["difficulty"])
draw(0)
}
update() {
super()
// Background and Score
_bgOffset = _bgOffset - 2
if (_bgOffset + _background.width < 0) { _bgOffset = _bgOffset + _background.width }
addTempCanvasItem(_background, _bgOffset, 0)
addTempCanvasItem(_background, _bgOffset+_background.width, 0)
_score = _score + 1
addTempCanvasItem(CanvasString.new("%(_score)"), 125, 10)
// Update the player
_player.update(this)
_spawn.update(this)
// Check for game over
if (_player.isDead(_spawn.enemyList)) {
Fading.stop(_channelID)
_parent.loadScene("gameover", [_score])
}
}
mouseHandler() { _player.mouseHandler() }
// Boilerplate + functions
static run(parent) {
return init(parent)
}
}