4 canvas { border: 1px solid black; }
7 <canvas id="terminal" />
10 let websocket_location = "ws://localhost:8000"
16 initialize: function() {
17 this.ctx = document.getElementById("terminal").getContext("2d"),
19 this.charWidth = this.ctx.measureText("M").width;
20 this.ctx.canvas.height = this.charHeight * this.rows;
21 this.ctx.canvas.width = this.charWidth * this.cols;
22 this.set_font(); // ctx.font gets reset to default on canvas size change, so we have to re-set our own
23 this.ctx.textBaseline = "top";
25 set_font: function(type='normal') {
26 this.ctx.font = type + ' ' + this.charHeight + 'px monospace';
28 write: function(start_y, start_x, msg, foreground_color='black') {
29 this.ctx.fillStyle = foreground_color;
30 this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
31 this.charWidth*msg.length, this.charHeight);
32 if (foreground_color === 'black') {
33 this.ctx.fillStyle = 'white';
35 this.ctx.fillStyle = 'black';
37 this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight);
39 drawBox: function (start_y, start_x, height, width, color='white') {
40 this.ctx.fillStyle = color;
41 this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
42 this.charWidth*width, this.charHeight*height);
47 tokenize: function(str) {
52 for (let i = 0; i < str.length; i++) {
58 } else if (c == '\\') {
60 } else if (c == '"') {
65 } else if (c == '"') {
67 } else if (c === ' ') {
68 if (token.length > 0) {
76 if (token.length > 0) {
81 parse_position(position_string) {
82 let coordinate_strings = position_string.split(',')
83 let position = [0, 0];
84 position[0] = coordinate_strings[0].slice(2);
85 position[1] = coordinate_strings[1].slice(2);
91 log_msg: function(msg) {
92 chat.history.unshift(msg);
93 if (chat.history.length > terminal.rows - 2) {
96 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols);
98 for (let line of chat.history) {
99 terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
101 // if (i > terminal.rows - 3) {
106 draw_map: function() {
107 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
108 for (const t in game.things) {
109 terminal.write(game.things[t][0], game.things[t][1], '@');
112 draw_tick_line: function(n) {
113 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
114 terminal.write(0, terminal.cols / 2, 'tick: ' + game.tick);
116 draw_input_line: function() {
117 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2, 'black');
118 terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
132 terminal.initialize()
133 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols, 'black');
135 document.addEventListener('keydown', (event) => {
136 if (chat.input_line === '') {
137 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.rows, 'black');
139 if (event.key && event.key.length === 1) {
140 chat.input_line += event.key;
141 tui.draw_input_line();
142 } else if (event.key === 'Backspace') {
143 chat.input_line = chat.input_line.slice(0, -1);
144 tui.draw_input_line();
145 } else if (event.key === 'Enter') {
146 websocket.send(chat.input_line);
148 tui.draw_input_line();
149 } else if (event.key === 'ArrowLeft') {
150 websocket.send('TASK:MOVE LEFT');
151 } else if (event.key === 'ArrowRight') {
152 websocket.send('TASK:MOVE RIGHT');
153 } else if (event.key === 'ArrowUp') {
154 websocket.send('TASK:MOVE UP');
155 } else if (event.key === 'ArrowDown') {
156 websocket.send('TASK:MOVE DOWN');
158 console.log(event.key);
161 let websocket = new WebSocket(websocket_location);
162 websocket.onmessage = function (event) {
163 let tokens = parser.tokenize(event.data);
164 if (tokens[0] === 'TURN') {
166 game.tick = parseInt(tokens[1]);
167 tui.draw_tick_line();
168 } else if (tokens[0] === 'THING_POS') {
169 game.things[tokens[1]] = parser.parse_position(tokens[2]);
171 } else if (tokens[0] === 'LOG') {
172 tui.log_msg(' ' + tokens[1]);
173 } else if (tokens[0] === 'ARGUMENT_ERROR') {
174 tui.log_msg('syntax error: ' + tokens[1]);
175 } else if (tokens[0] === 'GAME_ERROR') {
176 tui.log_msg('game error: ' + tokens[1]);
178 tui.log_msg('unhandled input: ' + event.data);