Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 1 | import {FT_TO_M, FIELD_WIDTH} from './constants'; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 2 | import {drawField, drawTarget} from './field'; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 3 | import {drawRobot} from './robot'; |
| 4 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 5 | function main(): void { |
| 6 | const vis = new Visualiser(); |
| 7 | } |
| 8 | |
| 9 | class Visualiser { |
| 10 | private x = 3; |
| 11 | private y = 0; |
| 12 | private theta = 0; |
| 13 | |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 14 | private drawLocked = false; |
Alex Perry | b243346 | 2019-03-26 21:45:26 -0700 | [diff] [blame^] | 15 | private targetLocked = false; |
| 16 | private targetX = 0; |
| 17 | private targetY = 0; |
| 18 | private targetTheta = 0; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 19 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 20 | constructor() { |
| 21 | const canvas = <HTMLCanvasElement>document.getElementById('field'); |
| 22 | const ctx = canvas.getContext('2d'); |
| 23 | |
| 24 | const server = location.host; |
| 25 | const socket = new WebSocket(`ws://${server}/ws`); |
| 26 | const reader = new FileReader(); |
| 27 | reader.addEventListener('loadend', (e) => { |
| 28 | const text = e.srcElement.result; |
| 29 | const j = JSON.parse(text); |
| 30 | this.x = j.robot.x; |
| 31 | this.y = j.robot.y; |
| 32 | this.theta = j.robot.theta; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 33 | |
| 34 | if(j.target) { |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 35 | this.targetLocked = j.target.frozen && j.target.have_target; |
| 36 | this.targetX = j.target.x; |
| 37 | this.targetY = j.target.y; |
| 38 | this.targetTheta = j.target.theta; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 39 | } |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 40 | }); |
| 41 | socket.addEventListener('message', (event) => { |
| 42 | reader.readAsText(event.data); |
| 43 | }); |
| 44 | window.requestAnimationFrame(() => this.draw(ctx)); |
| 45 | } |
| 46 | |
| 47 | reset(ctx : CanvasRenderingContext2D) : void { |
| 48 | ctx.setTransform(1,0,0,1,0,0); |
| 49 | const size = Math.min(window.innerHeight, window.innerWidth) * 0.98; |
| 50 | ctx.canvas.height = size; |
| 51 | ctx.canvas.width = size; |
| 52 | ctx.clearRect(0,0,size,size); |
| 53 | |
| 54 | ctx.translate(size/2, size); |
| 55 | ctx.rotate(-Math.PI / 2); |
| 56 | ctx.scale(1, -1); |
| 57 | const M_TO_PX = size / FIELD_WIDTH |
| 58 | ctx.scale(M_TO_PX, M_TO_PX); |
| 59 | ctx.lineWidth = 1 / M_TO_PX; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | draw(ctx : CanvasRenderingContext2D) : void { |
| 63 | this.reset(ctx); |
| 64 | |
| 65 | drawField(ctx); |
| 66 | drawRobot(ctx, this.x, this.y, this.theta); |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 67 | ctx.save(); |
| 68 | ctx.lineWidth = 2.0 * ctx.lineWidth; |
| 69 | if (this.targetLocked) { |
| 70 | ctx.strokeStyle = 'blue'; |
| 71 | } else { |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 72 | ctx.strokeStyle = 'red'; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 73 | } |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 74 | drawTarget(ctx, this.targetX, this.targetY, this.targetTheta); |
| 75 | ctx.restore(); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 76 | window.requestAnimationFrame(() => this.draw(ctx)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | main(); |