laneya2

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

commit
e418bee4f469e12763fc06e3e4404232713581c1
parent
c944e64295bfd86157f95ad9f660e61dbd8229a9
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-29 10:20
display stats in menu

Diffstat

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

1 files changed, 44 insertions, 1 deletions


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

@@ -18,6 +18,10 @@ var COLORS = {
   18    18     'pile': 3,
   19    19 };
   20    20 
   -1    21 var ITEMS = {
   -1    22     'potion': {health: 10},
   -1    23 };
   -1    24 
   21    25 var inRect = function(pos, rect, withWalls) {
   22    26     if (withWalls) {
   23    27         return pos.x >= rect.x1 - 1 && pos.x <= rect.x2 + 1
@@ -208,6 +212,35 @@ var screen = {
  208   212         }
  209   213     },
  210   214 
   -1   215     table(items, cols) {
   -1   216         var c = Math.floor(cols / 3);
   -1   217         var item = ITEMS[this.menuSelected] || {};
   -1   218         var rows = items.map(([label, key]) => {
   -1   219             return [label, '' + game.stats[key], '' + (item[key] || ''), item[key]];
   -1   220         });
   -1   221         var l1 = Math.max(...rows.map(row => row[0].length));
   -1   222         var l2 = Math.max(...rows.map(row => row[1].length));
   -1   223         var l3 = Math.max(...rows.map(row => row[2].length));
   -1   224 
   -1   225         if ((l1 + 2) + l2 + (l3 ? l3 + 3 : 0) + 2 > c) {
   -1   226             l1 = c - (2 + l2 + (l3 ? l3 + 3 : 0) + 2);
   -1   227         }
   -1   228         rows.forEach((row, i) => {
   -1   229             this.commitSpan((row[0].substr(0, l1) + ':').padEnd(l1 + 2), -1);
   -1   230             this.commitSpan(row[1].padStart(l2), -1);
   -1   231             var l = (l1 + 2) + l2;
   -1   232             if (row[3]) {
   -1   233                 this.commitSpan(' → ', 7);
   -1   234                 this.commitSpan(row[2].padStart(l3), row[3] < 0 ? 1 : 2);
   -1   235                 l += 3 + l3;
   -1   236             }
   -1   237             this.commitSpan(' '.repeat(c - l));
   -1   238             if ((i + 1) % 3 === 0) {
   -1   239                 $pre.append('\n');
   -1   240             }
   -1   241         });
   -1   242     },
   -1   243 
  211   244     renderHealth() {
  212   245         var health = Math.round(game.stats.health / game.stats.healthTotal * this.cols);
  213   246         this.commitSpan('='.repeat(health), 1);
@@ -216,7 +249,7 @@ var screen = {
  216   249     },
  217   250 
  218   251     renderMenu() {
  219    -1         var rows = this.rows - 3;
   -1   252         var rows = this.rows - 4;
  220   253         var items = Object.entries(game.inventory);
  221   254         items.sort();
  222   255 
@@ -236,6 +269,16 @@ var screen = {
  236   269 
  237   270         this.menuSelected = items.length ? items[this.menuCursor][0] : null;
  238   271 
   -1   272         this.table([
   -1   273             ['Health', 'health'],
   -1   274             ['Attack', 'attack'],
   -1   275             ['Sight', 'lineOfSight'],
   -1   276             ['Max Health', 'healthTotal'],
   -1   277             ['Defense', 'defense'],
   -1   278             ['Speed', 'speed'],
   -1   279         ], this.cols);
   -1   280         $pre.append('\n');
   -1   281 
  239   282         for (let i = 0; i < rows; i++) {
  240   283             if (i + this.menuOffset < items.length) {
  241   284                 var [name, count] = items[i + this.menuOffset];