- commit
- be26c24296a82bbf4621b9179eb66995695f0c75
- parent
- 163941f3e437f31ee45c6d3769b4e91465d0748f
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-22 07:31
advanced field of vision
Diffstat
| M | static/main.js | 52 | +++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 files changed, 51 insertions, 1 deletions
diff --git a/static/main.js b/static/main.js
@@ -36,9 +36,59 @@ var game = {
36 36 },
37 37
38 38 inView(a, b) {
-1 39 // check radius
39 40 var dx = a.x - b.x;
40 41 var dy = a.y - b.y;
41 -1 return dx * dx + dy * dy < radius * radius;
-1 42 if (dx * dx + dy * dy >= radius * radius) {
-1 43 return false;
-1 44 }
-1 45
-1 46 // perf: shortcut if in same rect
-1 47 for (const rect of this.rects) {
-1 48 if (
-1 49 inRect(a, rect, true) && inRect(b, rect, true)
-1 50 && (inRect(a, rect) || inRect(b, rect))
-1 51 ) {
-1 52 return true;
-1 53 }
-1 54 }
-1 55
-1 56 // ray casting
-1 57 if (Math.abs(dx) > Math.abs(dy)) {
-1 58 const [c, d] = a.x > b.x ? [b, a] : [a, b];
-1 59 return [
-1 60 [c.y + 0.4, d.y + 0.4],
-1 61 [c.y + 0.4, d.y - 0.4],
-1 62 [c.y - 0.4, d.y + 0.4],
-1 63 [c.y - 0.4, d.y - 0.4],
-1 64 ].some(([y1, y2]) => {
-1 65 const f = (y2 - y1) / (d.x - c.x);
-1 66 for (let x = c.x + 1; x < d.x; x++) {
-1 67 const y = Math.round((x - c.x) * f + y1);
-1 68 if (!this.getRect({x, y})) {
-1 69 return false;
-1 70 }
-1 71 }
-1 72 return true;
-1 73 });
-1 74 } else {
-1 75 const [c, d] = a.y > b.y ? [b, a] : [a, b];
-1 76 return [
-1 77 [c.x + 0.4, d.x + 0.4],
-1 78 [c.x + 0.4, d.x - 0.4],
-1 79 [c.x - 0.4, d.x + 0.4],
-1 80 [c.x - 0.4, d.x - 0.4],
-1 81 ].some(([x1, x2]) => {
-1 82 const f = (x2 - x1) / (d.y - c.y);
-1 83 for (let y = c.y + 1; y < d.y; y++) {
-1 84 const x = Math.round((y - c.y) * f + x1);
-1 85 if (!this.getRect({x, y})) {
-1 86 return false;
-1 87 }
-1 88 }
-1 89 return true;
-1 90 });
-1 91 }
42 92 },
43 93
44 94 getChar(x, y) {