-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
103 lines (91 loc) · 2.48 KB
/
main.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
import { Config, Piece, Dimension, Board } from './ttt';
import { ClientBoard } from './client';
import * as ui from './ui';
import * as graphics from './graphics';
declare var config: Config;
declare const window: any;
window.config = {
dimension: Dimension.FOUR,
rot_speed: 0.01,
zoom_speed: 0.25,
camera_dist: 3,
project_dist: 3,
point_size: 10,
fov: 60,
code: '',
};
let board = new Board(config.dimension);
function init() {
const can = document.getElementById('canvas') as HTMLCanvasElement;
can.addEventListener('keydown', keydown);
can.addEventListener('contextmenu', e => {
e.preventDefault();
return false
});
ui.init();
graphics.init(can);
graphics.draw(board);
ui.update_status(board);
}
function keydown(e: KeyboardEvent) {
const camera = graphics.camera;
switch(e.key) {
case '[':
if(camera.dimension > Dimension.TWO)
camera.dimension -= 1;
break;
case ']':
if(camera.dimension < board.dimension)
camera.dimension += 1;
break;
case 't':
ui.toggle_menu();
break;
case ' ':
board.put_piece();
break;
case 'r':
board.reset(config.dimension);
break;
case 'c':
if(board instanceof ClientBoard)
board.disconnect();
if(config.code != '') {
board = new ClientBoard(config.dimension, config.code, (board: Board) => {
ui.update_status(board);
graphics.draw(board);
});
} else {
board = new Board(config.dimension);
}
break;
case 'ArrowRight':
board.move([1, 0, 0, 0]);
break;
case 'ArrowLeft':
board.move([-1, 0, 0, 0]);
break;
case 'ArrowUp':
board.move([0, 1, 0, 0]);
break;
case 'ArrowDown':
board.move([0, -1, 0, 0]);
break;
case 'w':
board.move([0, 0, 1, 0]);
break;
case 's':
board.move([0, 0, -1, 0]);
break;
case 'd':
board.move([0, 0, 0, 1]);
break;
case 'a':
board.move([0, 0, 0, -1]);
break;
}
board.check_winners();
graphics.draw(board);
ui.update_status(board);
}
window.onload = init;