-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
101 lines (80 loc) · 2.46 KB
/
index.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
import domready = require("domready");
import Game from "./game";
import {version} from "./package.json";
console.log("You Are Now In Space v" + version);
domready(() => {
const game = new Game();
const engine = game.engine;
// Work around Firefox not supporting image-rendering: pixelated
// See https://github.com/excaliburjs/Excalibur/issues/1676
if (engine.canvas.style.imageRendering === "") {
engine.canvas.style.imageRendering = "crisp-edges";
}
engine.canvas.setAttribute("tabindex", "-1");
function scale(): void {
const scaleFactor = Math.floor(Math.min(
window.innerWidth / game.width,
window.innerHeight / game.height
));
engine.screen.viewport = {
width: game.width * scaleFactor,
height: game.height * scaleFactor
};
engine.screen.applyResolutionAndViewport();
}
function onKey(event: KeyboardEvent): void {
engine.canvas.focus();
switch (event.code) {
case "ArrowUp":
case "ArrowDown":
case "ArrowLeft":
case "ArrowRight":
case "KeyX":
event.preventDefault();
}
}
let clicked = false;
function onClick(): void {
clicked = true;
hidePointer();
game.active = true;
}
let pointerTimeout = 0;
function onMouseMove(): void {
showPointer();
if (pointerTimeout) {
clearTimeout(pointerTimeout);
}
pointerTimeout = window.setTimeout(() => {
if (game.active) {
hidePointer();
}
}, 500);
}
function onFocus(): void {
if (clicked) {
hidePointer();
game.active = true;
}
}
function onBlur(): void {
showPointer();
game.active = false;
}
function hidePointer(): void {
engine.canvas.style.cursor = "none";
}
function showPointer(): void {
engine.canvas.style.cursor = "auto";
}
scale();
window.addEventListener("resize", scale);
window.addEventListener("keydown", onKey);
window.addEventListener("keypress", onKey);
window.addEventListener("keyup", onKey);
window.addEventListener("click", onClick, true);
window.addEventListener("mousemove", onMouseMove, true);
window.addEventListener("focus", onFocus, true);
window.addEventListener("blur", onBlur, true);
game.start();
});