-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameManager.ts
81 lines (74 loc) · 2.81 KB
/
gameManager.ts
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
class GameManager {
cursor: Sprite
field: Sprite[] = []
found: String[] = ["Air", "Water", "Fire", "Earth"]
constructor() {
this.cursor = sprites.create(img`
. f f f .
f f 1 f f
f 1 1 1 f
f f 1 f f
. f f f .
`, SpriteKind.Player)
controller.moveSprite(this.cursor)
this.cursor.z = 100
this.field[0] = elementList.Air.toSprite()
this.field[0].left = 20
this.field[0].top = 30
this.field[1] = elementList.Water.toSprite()
this.field[1].right = 80
this.field[1].top = 30
this.field[2] = elementList.Earth.toSprite()
this.field[2].left = 20
this.field[2].bottom = 90
this.field[3] = elementList.Fire.toSprite()
this.field[3].right = 80
this.field[3].bottom = 90
this.registerControls();
}
private registerControls() {
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
sprites.allOfKind(SpriteKind.Element).forEach(function (i) {
if (this.cursor.overlapsWith(i)) {
controller.moveSprite(i, 100, 100)
return
}
})
})
controller.A.onEvent(ControllerButtonEvent.Released, function () {
this.mergeOverlappingSprites();
});
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
this.createNewElement();
})
}
private mergeOverlappingSprites() {
for (let i of sprites.allOfKind(SpriteKind.Element)) {
controller.moveSprite(i, 0, 0)
for (let j of sprites.allOfKind(SpriteKind.Element)) {
if (i.overlapsWith(j)) {
let merge = i.data.merge(i, j)
if (merge) {
i.destroy()
j.destroy()
if (this.found.indexOf(merge.data.name) == -1) {
game.showLongText("You found a new element: " + merge.data.name, DialogLayout.Bottom)
this.found.push(merge.data.name)
}
this.field.push(merge)
} } } } }
private createNewElement() {
let element = game.askForString("Choose an element")
if (element === undefined) { return }
element = toLowerFirstUpper(element)
if (this.found.indexOf(element) != -1) {
this.field.push(elementList[element].toSprite())
this.field[this.field.length-1].x = this.cursor.x
this.field[this.field.length-1].y = this.cursor.y
} else if (elementList[element]) {
game.splash("Element has not been discovered")
} else {
game.splash("Element does not exist")
}
}
}