-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstage.ts
117 lines (101 loc) · 3.33 KB
/
stage.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
namespace kodu {
export class Stage {
components: Component[];
radar: KelpieGrid;
camera: Camera;
prevMs: number;
constructor(public app: App, public name: string) {
}
public get<T>(field: string): T { return undefined; }
public set<T>(field: string, value: T) { }
public update(dt: number) {
this.components.forEach(comp => comp.update(dt));
}
public remove(comp: Component) {
this.components = this.components.filter(c => c !== comp);
comp.stage = null;
}
public add(comp: Component) {
if (this.components.some(item => item === comp)) {
let fd = 0;
}
this.remove(comp);
this.components.push(comp);
comp.stage = this;
}
handleAPressed() {}
handleBPressed() {}
handleMenuPressed() {}
handleCursorCanvasClick(x: number, y: number) {}
handleCursorButtonClick(button: Button) {}
handleCursorCharacterClick(char: Character, x: number, y: number) {}
handleCursorCancel() {}
initScene() {
this.radar = new KelpieGrid();
this.components = [];
this.camera = new Camera(this);
controller.A.onEvent(ControllerButtonEvent.Pressed, () => {
this.handleAPressed();
});
controller.B.onEvent(ControllerButtonEvent.Pressed, () => {
this.handleBPressed();
});
controller.menu.onEvent(ControllerButtonEvent.Pressed, () => {
this.handleMenuPressed();
});
}
protected start() {
this.prevMs = control.millis();
game.onUpdate(() => {
const t = control.millis();
const dt = t - this.prevMs;
this.update(dt);
this.prevMs = t;
});
}
shutdownScene() {
const components = this.components;
components.forEach(comp => comp.destroy());
this.components = null;
}
notify(event: string, parm?: any) {
switch (event) {
case "cursor:canvasClick": {
const { x, y } = parm;
this.handleCursorCanvasClick(x, y);
break;
}
case "cursor:buttonClick": {
const { button } = parm;
this.handleCursorButtonClick(button);
break;
}
case "cursor:characterClick": {
const { char, x, y } = parm;
this.handleCursorCharacterClick(char, x, y);
break;
}
case "cursor:cancel": {
this.handleCursorCancel();
break;
}
}
}
}
export class StageManager {
stack: Stage[];
constructor() {
this.stack = [];
}
public push(stage: Stage) {
game.pushScene();
this.stack.push(stage);
stage.initScene();
}
public pop() {
const stage = this.stack.pop();
stage.shutdownScene();
game.popScene();
}
}
}