laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
b2605246eaabc242a12d7ad5117c9dee1735785b
parent
e29a5847e0c84fff2cdfe94b0ad0b9a923700f1e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-21 16:30
basic field of vision

Diffstat

M static/main.js 42 +++++++++++++++++++++++++++++++++++++++---

1 files changed, 39 insertions, 3 deletions


diff --git a/static/main.js b/static/main.js

@@ -1,4 +1,5 @@
    1     1 var $pre = document.querySelector('pre');
   -1     2 var radius = 5;
    2     3 
    3     4 var params = new URLSearchParams(location.search);
    4     5 var gameId = params.get('game');
@@ -12,23 +13,51 @@ var send = function(data) {
   12    13 var game = {
   13    14     id: -1,
   14    15     rects: [],
   -1    16     seen: {},
   15    17     objects: {},
   16    18 
   -1    19     inView(a, b) {
   -1    20         var dx = a.x - b.x;
   -1    21         var dy = a.y - b.y;
   -1    22         return dx * dx + dy * dy < radius * radius;
   -1    23     },
   -1    24 
   17    25     getChar(x, y) {
   -1    26         if (!this.seen[[x, y]]) {
   -1    27             return [' ', -1];
   -1    28         }
   -1    29 
   -1    30         var inView = Object.values(this.objects).some(
   -1    31             obj => obj.type === 'player' && this.inView(obj.pos, {x, y})
   -1    32         );
   -1    33         var white = inView ? -1 : 0;
   -1    34 
   18    35         if (x === this.ladder.x && y === this.ladder.y) {
   19    -1             return ['>', -1];
   -1    36             return ['>', white];
   20    37         }
   21    38         if (Object.values(this.objects).some(obj => x === obj.pos.x && y === obj.pos.y)) {
   22    39             return ['@', 4];
   23    40         }
   24    41         if (this.rects.some(rect => x >= rect.x1 && x <= rect.x2 && y >= rect.y1 && y <= rect.y2)) {
   25    -1             return ['.', -1];
   -1    42             return ['.', white];
   26    43         }
   27    44         if (this.rects.some(rect => x >= rect.x1 - 1 && x <= rect.x2 + 1 && y >= rect.y1 - 1 && y <= rect.y2 + 1)) {
   28    -1             return ['#', -1];
   -1    45             return ['#', white];
   29    46         }
   30    47         return [' ', -1];
   31    48     },
   -1    49 
   -1    50     updateSeen(pos) {
   -1    51         for (let dy = -radius; dy <= radius; dy++) {
   -1    52             const y = pos.y + dy;
   -1    53             for (let dx = -radius; dx <= radius; dx++) {
   -1    54                 const x = pos.x + dx;
   -1    55                 if (!this.seen[[x, y]] && this.inView(pos, {x, y})) {
   -1    56                     this.seen[[x, y]] = true;
   -1    57                 }
   -1    58             }
   -1    59         }
   -1    60     },
   32    61 };
   33    62 
   34    63 var getSize = function() {
@@ -100,13 +129,20 @@ socket.onmessage = function(event) {
  100   129             game.ladder = msg.ladder;
  101   130             game.horizontal = msg.horizontal;
  102   131             game.vertical = msg.vertical;
   -1   132             game.seen = {};
  103   133         } else if (msg.action === 'create') {
  104   134             game.objects[msg.id] = {
  105   135                 type: msg.type,
  106   136                 pos: msg.pos,
  107   137             };
   -1   138             if (msg.type === 'player') {
   -1   139                 game.updateSeen(msg.pos);
   -1   140             }
  108   141         } else if (msg.action === 'setPosition') {
  109   142             game.objects[msg.id].pos = msg.pos;
   -1   143             if (game.objects[msg.id].type === 'player') {
   -1   144                 game.updateSeen(msg.pos);
   -1   145             }
  110   146         } else if (msg.action === 'remove') {
  111   147             delete game.objects[msg.id];
  112   148         }