-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
116 lines (98 loc) · 3.36 KB
/
app.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
var WALLCOLOR = "#B31B1B";
var ROBOTCOLOR = "#6EB43F";
function isAncestor(descendant: Node, ancestor: Node): boolean {
if (descendant == null) {
return false;
}
if (descendant == ancestor) {
return true;
}
else {
return isAncestor(descendant.parentNode, ancestor);
}
}
class Pico {
mapcanvas: HTMLCanvasElement;
guidiv: HTMLDivElement;
controldiv: HTMLDivElement;
levels: Array<ILevel>;
header: HTMLDivElement;
mapspan: HTMLSpanElement;
currentLevel: ILevel;
static createCanvas(): HTMLCanvasElement {
var canvas = document.createElement("canvas");
canvas.width = 600;
canvas.height = 600;
return canvas;
}
constructor() {
this.levels = [];
this.levels.push(new StartLevel());
this.levels.push(new StupidLevel());
this.levels.push(new SpiralLevel());
this.levels.push(new SnakeLevel());
this.levels.push(new MazeLevel());
this.levels.push(new SquareRoomLevel());
this.levels.push(new DiamondRoomLevel());
this.levels.push(new HardMazeLevel());
}
clear(): void {
for (var level of this.levels) {
level.clear();
}
location.reload();
}
static instance: Pico = new Pico();
static getInstance(): Pico { return Pico.instance; }
init(): void {
this.mapcanvas = Pico.createCanvas();
document.getElementById("mapcanvas").appendChild(this.mapcanvas);
this.guidiv = document.createElement("div");
this.guidiv.classList.add("guicontainer");
document.getElementById("gui").appendChild(this.guidiv);
this.controldiv = document.createElement("div");
document.getElementById("mapdata").appendChild(this.controldiv);
this.header = <HTMLDivElement>document.getElementById("header");
var leftbutton = document.createElement("button");
leftbutton.classList.add("mapleft");
leftbutton.addEventListener("click", () => {
this.previousLevel();
})
this.header.appendChild(leftbutton);
var mapselectdiv = document.createElement("div");
mapselectdiv.classList.add("mapselect");
this.mapspan = document.createElement("span");
mapselectdiv.appendChild(this.mapspan);
this.header.appendChild(mapselectdiv);
var rightbutton = document.createElement("button");
rightbutton.classList.add("mapright");
rightbutton.addEventListener("click", () => {
this.nextLevel();
})
this.header.appendChild(rightbutton);
this.changeLevel(this.levels[0]);
}
changeLevel(level: ILevel) {
if (this.currentLevel) {
this.currentLevel.toBackground();
}
this.currentLevel = level;
this.mapspan.innerText = level.getName();
level.toForeground(this.mapcanvas, this.controldiv, this.guidiv);
}
nextLevel() {
var index = this.levels.indexOf(this.currentLevel) + 1;
while (index >= this.levels.length) {
index -= this.levels.length;
}
this.changeLevel(this.levels[index]);
}
previousLevel() {
var index = this.levels.indexOf(this.currentLevel) - 1;
while (index < 0) {
index += this.levels.length;
}
this.changeLevel(this.levels[index]);
}
}