|
1 |
| -import { Draw } from './draw'; |
2 |
| -import { Rect } from '../comets'; |
| 1 | +import { Rect } from "../comets"; |
3 | 2 | import { HEIGHT, OBJECT_SCALE, SHIP_RECT, WIDTH } from "./constants";
|
| 3 | +import { Draw } from "./draw"; |
| 4 | +import { random } from "./util"; |
4 | 5 |
|
5 | 6 | export class Screen implements Rect {
|
| 7 | + canvas: HTMLCanvasElement; |
| 8 | + ctx: CanvasRenderingContext2D; |
| 9 | + x: number = 0; |
| 10 | + y: number = 0; |
| 11 | + width: number; |
| 12 | + height: number; |
| 13 | + draw: Draw; |
| 14 | + width2: number; |
| 15 | + height2: number; |
6 | 16 |
|
7 |
| - canvas: HTMLCanvasElement; |
8 |
| - ctx: CanvasRenderingContext2D; |
9 |
| - x: number = 0; |
10 |
| - y: number = 0; |
11 |
| - width: number; |
12 |
| - height: number; |
13 |
| - draw: Draw; |
14 |
| - width2: number; |
15 |
| - height2: number; |
16 |
| - |
17 |
| - private _fontXL: number; |
18 |
| - private _fontL: number; |
19 |
| - private _fontM: number; |
20 |
| - private _fontS: number; |
21 |
| - private _objectScale: number; |
22 |
| - private _shipRect: Rect; |
23 |
| - private _pointSize: number; |
| 17 | + private _fontXL: number; |
| 18 | + private _fontL: number; |
| 19 | + private _fontM: number; |
| 20 | + private _fontS: number; |
| 21 | + private _objectScale: number; |
| 22 | + private _shipRect: Rect; |
| 23 | + private _pointSize: number; |
24 | 24 |
|
25 |
| - constructor() { |
26 |
| - this.canvas = document.getElementById('canvas') as HTMLCanvasElement; |
27 |
| - this.ctx = this.canvas.getContext('2d'); |
28 |
| - this.draw = new Draw(this.ctx, this); |
29 |
| - this.init(); |
| 25 | + constructor() { |
| 26 | + this.canvas = document.getElementById("canvas") as HTMLCanvasElement; |
| 27 | + this.ctx = this.canvas.getContext("2d"); |
| 28 | + this.draw = new Draw(this.ctx, this); |
| 29 | + this.init(); |
30 | 30 |
|
31 |
| - window.addEventListener('resize', () => { |
32 |
| - console.log("resizing"); |
33 |
| - this.init(); |
34 |
| - }); |
35 |
| - } |
| 31 | + window.addEventListener("resize", () => { |
| 32 | + console.log("resizing"); |
| 33 | + this.init(); |
| 34 | + }); |
| 35 | + } |
36 | 36 |
|
37 |
| - init() { |
38 |
| - this.canvas.width = WIDTH; |
39 |
| - this.canvas.height = HEIGHT; |
40 |
| - this.width = this.canvas.width; |
41 |
| - this.height = this.canvas.height; |
42 |
| - this.width2 = this.width / 2; |
43 |
| - this.height2 = this.height / 2; |
| 37 | + init() { |
| 38 | + this.canvas.width = WIDTH; |
| 39 | + this.canvas.height = HEIGHT; |
| 40 | + this.width = this.canvas.width; |
| 41 | + this.height = this.canvas.height; |
| 42 | + this.width2 = this.width / 2; |
| 43 | + this.height2 = this.height / 2; |
44 | 44 |
|
45 |
| - this._fontXL = 48; |
46 |
| - this._fontL = 24; |
47 |
| - this._fontM = 18; |
48 |
| - this._fontS = 10; |
49 |
| - this._objectScale = OBJECT_SCALE; |
| 45 | + this._fontXL = 48; |
| 46 | + this._fontL = 24; |
| 47 | + this._fontM = 18; |
| 48 | + this._fontS = 10; |
| 49 | + this._objectScale = OBJECT_SCALE; |
50 | 50 |
|
51 |
| - this._pointSize = 4 * this._objectScale; |
| 51 | + this._pointSize = 4 * this._objectScale; |
52 | 52 |
|
53 |
| - this._shipRect = SHIP_RECT; |
54 |
| - } |
| 53 | + this._shipRect = SHIP_RECT; |
| 54 | + } |
55 | 55 |
|
56 |
| - get font() { |
57 |
| - let self = this; |
58 |
| - return { |
59 |
| - get xlarge() { |
60 |
| - return self._fontXL; |
61 |
| - }, |
62 |
| - get large() { |
63 |
| - return self._fontL; |
64 |
| - }, |
65 |
| - get medium() { |
66 |
| - return self._fontM; |
67 |
| - }, |
68 |
| - get small() { |
69 |
| - return self._fontS; |
70 |
| - } |
71 |
| - } |
72 |
| - } |
| 56 | + get font() { |
| 57 | + let self = this; |
| 58 | + return { |
| 59 | + get xlarge() { |
| 60 | + return self._fontXL; |
| 61 | + }, |
| 62 | + get large() { |
| 63 | + return self._fontL; |
| 64 | + }, |
| 65 | + get medium() { |
| 66 | + return self._fontM; |
| 67 | + }, |
| 68 | + get small() { |
| 69 | + return self._fontS; |
| 70 | + }, |
| 71 | + }; |
| 72 | + } |
73 | 73 |
|
74 |
| - get objectScale() { |
75 |
| - return this._objectScale; |
76 |
| - } |
77 |
| - |
78 |
| - get pointSize() { |
79 |
| - return this._pointSize; |
80 |
| - } |
81 |
| - |
82 |
| - get shipRect() { |
83 |
| - return this._shipRect; |
84 |
| - } |
| 74 | + get objectScale() { |
| 75 | + return this._objectScale; |
| 76 | + } |
85 | 77 |
|
86 |
| - preShake() { |
87 |
| - this.ctx.save(); |
88 |
| - var dx = Math.random() * 10; |
89 |
| - var dy = Math.random() * 10; |
90 |
| - this.ctx.translate(dx, dy); |
91 |
| - } |
| 78 | + get pointSize() { |
| 79 | + return this._pointSize; |
| 80 | + } |
92 | 81 |
|
93 |
| - postShake() { |
94 |
| - this.ctx.restore(); |
95 |
| - } |
96 |
| -} |
| 82 | + get shipRect() { |
| 83 | + return this._shipRect; |
| 84 | + } |
| 85 | + |
| 86 | + preShake() { |
| 87 | + this.ctx.save(); |
| 88 | + var dx = random(0, 1) * 10; |
| 89 | + var dy = random(0, 1) * 10; |
| 90 | + this.ctx.translate(dx, dy); |
| 91 | + } |
97 | 92 |
|
| 93 | + postShake() { |
| 94 | + this.ctx.restore(); |
| 95 | + } |
| 96 | +} |
0 commit comments