-
Notifications
You must be signed in to change notification settings - Fork 18
/
scene.ts
162 lines (139 loc) · 4.75 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
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
namespace microcode {
const INPUT_PRIORITY = 10
const UPDATE_PRIORITY = 20
const RENDER_PRIORITY = 30
const SCREEN_PRIORITY = 100
export abstract class Scene implements IComponent {
private xfrm_: Affine
private color_: number
private backgroundCaptured_ = false
//% 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() {
if (Options.menuProfiling) {
control.onEvent(
ControllerButtonEvent.Pressed,
controller.menu.id,
() => {
control.heapSnapshot()
}
)
}
}
/* abstract */ shutdown() {}
/* override */ activate() {
pointerevents.pushContext(
(x, y) => this.handleClick(x, y),
(x, y) => this.handleMove(x, y),
(dx, dy) => this.handleWheel(dx, dy)
)
profile()
}
/* override */ deactivate() {
pointerevents.popContext()
profile()
}
/* abstract */ update() {}
/* abstract */ draw() {}
protected handleClick(x: number, y: number) {}
protected handleMove(x: number, y: number) {}
protected handleWheel(dx: number, dy: number) {}
get backgroundCaptured() {
return !!this.backgroundCaptured_
}
/**
* Captures the current screen image as background image. You must call releaseBackground to resume usual rendering.
*/
captureBackground() {
control.assert(
!this.backgroundCaptured_,
ERROR_DOUBLE_BACKGROUND_CAPTURE
)
this.backgroundCaptured_ = true
}
releaseBackground() {
this.backgroundCaptured_ = false
}
__init() {
control.eventContext().registerFrameHandler(INPUT_PRIORITY, () => {
control.enablePerfCounter()
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, () => {
control.enablePerfCounter()
this.update()
})
control.eventContext().registerFrameHandler(RENDER_PRIORITY, () => {
control.enablePerfCounter()
// perf: render directly on the background image buffer
this.draw()
if (Options.fps)
Screen.image.print(control.EventContext.lastStats, 1, 1, 15)
if (screen !== Screen.image)
screen.drawImage(Screen.image, 0, 0)
})
control.eventContext().registerFrameHandler(SCREEN_PRIORITY, () => {
control.enablePerfCounter()
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
}
}
}
// this is needed for compat with most recent version of arcade
namespace game {
export function addScenePushHandler(handler: (oldScene: any) => void) {}
export function addScenePopHandler(handler: (oldScene: any) => void) {}
}