- commit
- 772dff878b272610db2c8d31669d46dccf3ddea5
- parent
- b8e781a1016f4324ec221ec499b2ebde519dc8eb
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-22 06:17
refactor: client getRect()
Diffstat
| M | static/main.js | 22 | ++++++++++++++++++++-- |
1 files changed, 20 insertions, 2 deletions
diff --git a/static/main.js b/static/main.js
@@ -11,12 +11,30 @@ var send = function(data) {
11 11 socket.send(JSON.stringify(data));
12 12 };
13 13
-1 14 var inRect = function(pos, rect, withWalls) {
-1 15 if (withWalls) {
-1 16 return pos.x >= rect.x1 - 1 && pos.x <= rect.x2 + 1
-1 17 && pos.y >= rect.y1 - 1 && pos.y <= rect.y2 + 1;
-1 18 } else {
-1 19 return pos.x >= rect.x1 && pos.x <= rect.x2
-1 20 && pos.y >= rect.y1 && pos.y <= rect.y2;
-1 21 }
-1 22 };
-1 23
14 24 var game = {
15 25 id: -1,
16 26 rects: [],
17 27 seen: {},
18 28 objects: {},
19 29
-1 30 getRect(pos, withWalls) {
-1 31 for (const rect of this.rects) {
-1 32 if (inRect(pos, rect, withWalls)) {
-1 33 return rect;
-1 34 }
-1 35 }
-1 36 },
-1 37
20 38 inView(a, b) {
21 39 var dx = a.x - b.x;
22 40 var dy = a.y - b.y;
@@ -39,10 +57,10 @@ var game = {
39 57 if (Object.values(this.objects).some(obj => x === obj.pos.x && y === obj.pos.y)) {
40 58 return ['@', 4];
41 59 }
42 -1 if (this.rects.some(rect => x >= rect.x1 && x <= rect.x2 && y >= rect.y1 && y <= rect.y2)) {
-1 60 if (this.getRect({x, y})) {
43 61 return ['.', white];
44 62 }
45 -1 if (this.rects.some(rect => x >= rect.x1 - 1 && x <= rect.x2 + 1 && y >= rect.y1 - 1 && y <= rect.y2 + 1)) {
-1 63 if (this.getRect({x, y}, true)) {
46 64 return ['#', white];
47 65 }
48 66 return [' ', -1];