forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.ts
103 lines (88 loc) · 2.84 KB
/
scene.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
namespace microcode {
const INPUT_PRIORITY = 10
const UPDATE_PRIORITY = 20
const RENDER_PRIORITY = 30
const SCREEN_PRIORITY = 100
export abstract class Scene {
private xfrm_: Affine
private color_: number
//% blockCombine block="xfrm" callInDebugger
public get xfrm() {
return this.xfrm_
}
//% blockCombine block="color" callInDebugger
public get color() {
return this.color_
}
public set color(v) {
this.color_ = v
}
constructor(public app: App, public name: string) {
this.xfrm_ = new Affine()
this.color_ = 12
}
/* abstract */ startup() {}
/* abstract */ shutdown() {}
/* abstract */ activate() {}
/* abstract */ deactivate() {}
/* abstract */ update() {}
/* abstract */ draw() {}
__init() {
control.eventContext().registerFrameHandler(INPUT_PRIORITY, () => {
const dtms = (control.eventContext().deltaTime * 1000) | 0
controller.left.__update(dtms)
controller.right.__update(dtms)
controller.up.__update(dtms)
controller.down.__update(dtms)
})
// Setup frame callbacks.
control.eventContext().registerFrameHandler(UPDATE_PRIORITY, () => {
this.update()
})
control.eventContext().registerFrameHandler(RENDER_PRIORITY, () => {
Screen.image.fill(0)
this.draw()
screen.fill(this.color_)
screen.drawTransparentImage(Screen.image, 0, 0)
})
control
.eventContext()
.registerFrameHandler(SCREEN_PRIORITY, control.__screen.update)
}
}
export class SceneManager {
scenes: Scene[]
constructor() {
this.scenes = []
}
public pushScene(scene: Scene) {
const currScene = this.currScene()
if (currScene) {
currScene.deactivate()
}
control.pushEventContext()
this.scenes.push(scene)
scene.startup()
scene.activate()
scene.__init()
}
public popScene() {
const prevScene = this.scenes.pop()
if (prevScene) {
prevScene.deactivate()
prevScene.shutdown()
control.popEventContext()
}
const currScene = this.currScene()
if (currScene) {
currScene.activate()
}
}
private currScene(): Scene {
if (this.scenes.length) {
return this.scenes[this.scenes.length - 1]
}
return undefined
}
}
}