diff game/js/tower.js @ 6:910b26feca76

full game flow
author ark
date 2026-10-19 01:05 +0200
parents 2d46729a022e
children 8df95050fc8b
line wrap: on
line diff
--- a/game/js/tower.js	Sun Oct 18 22:50:24 2026 +0200
+++ b/game/js/tower.js	Mon Oct 19 01:05:29 2026 +0200
@@ -1,4 +1,4 @@
-// towers: targeting and attacks
+// towers: targeting, attacks, upgrade, sell
 
 window.MM = window.MM || {};
 
@@ -9,19 +9,44 @@
     // how long a melee attack shows its spikes pushed out
     var BURST_TIME = 0.3;
 
-    function create(type, x, y) {
+    // sides: for t0, which sides draw spikes (map.facingSides); all four
+    // when left out
+    function create(type, x, y, sides) {
         return {
             type: type,
             def: cfg.tower[type],
             level: 1,
             x: x,
             y: y,
+            sides: sides || [true, true, true, true],
+            // purchases, {cost, pause}: pause is the number of the pause
+            // the purchase was made in, 0 while the game ran
+            paid: [],
             angle: -Math.PI / 2,
             cooldown: 0,
             burst: 0
         };
     }
 
+    // cost of the next level, null at the top level. level n costs the
+    // base cost x n.
+    function upgradeCost(t) {
+        return t.level < t.def.damage.length ? t.def.cost * (t.level + 1) : null;
+    }
+
+    // coins back for selling the tower: half of everything paid for it,
+    // rounded down, except purchases made during the current pause, which
+    // come back in full. pause is the number of the current pause, 0 while
+    // the game runs.
+    function refund(t, pause) {
+        var rest = 0, full = 0;
+        t.paid.forEach(function (p) {
+            if (pause && p.pause === pause) full += p.cost;
+            else rest += p.cost;
+        });
+        return Math.floor(rest * cfg.tower_rules.sell_refund) + full;
+    }
+
     function sprite(t) {
         return t.level === 1 ? t.type : t.type + "_" + t.level;
     }
@@ -78,5 +103,5 @@
     }
 
     return { create: create, update: update, sprite: sprite, range: range,
-             BURST_TIME: BURST_TIME };
+             upgradeCost: upgradeCost, refund: refund, BURST_TIME: BURST_TIME };
 })();