comparison game/js/map.js @ 6:910b26feca76

full game flow
author ark
date 2026-10-19 01:05 +0200
parents 5492efd60e2c
children 22b604680c5a
comparison
equal deleted inserted replaced
5:67bf3c505145 6:910b26feca76
46 46
47 MM.GameMap = (function () { 47 MM.GameMap = (function () {
48 "use strict"; 48 "use strict";
49 49
50 var cfg = MM.config; 50 var cfg = MM.config;
51 // spike sides: distance between route samples, and the share of the
52 // samples in reach a side needs to count as facing the path
53 var SIDE_STEP = 4;
54 var SIDE_SHARE = 0.2;
51 55
52 function GameMap(index) { 56 function GameMap(index) {
53 var d = MM.maps[index]; 57 var d = MM.maps[index];
54 this.index = index; 58 this.index = index;
55 this.data = d; 59 this.data = d;
92 } 96 }
93 } 97 }
94 return best; 98 return best;
95 }; 99 };
96 100
101 // which sides of a tower at x, y face the path, as four booleans: up,
102 // right, down, left. each side covers a quarter of the directions. a side
103 // faces the path when it holds at least SIDE_SHARE of the route points
104 // within reach. with no route point in reach, only the side towards the
105 // nearest one does.
106 GameMap.prototype.facingSides = function (x, y, reach) {
107 var count = [0, 0, 0, 0], total = 0, nearest = Infinity, nearSide = 0;
108 var pt = {};
109 this.routes.forEach(function (r) {
110 pt.seg = 1;
111 for (var s = 0; s <= r.length; s += SIDE_STEP) {
112 r.point(s, pt);
113 var dx = pt.x - x, dy = pt.y - y, d = Math.hypot(dx, dy);
114 var side = Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);
115 if (d < nearest) {
116 nearest = d;
117 nearSide = side;
118 }
119 if (d <= reach) {
120 count[side]++;
121 total++;
122 }
123 }
124 });
125 return count.map(function (c, i) {
126 return total ? c >= SIDE_SHARE * total : i === nearSide;
127 });
128 };
129
97 // route weights for a monster of the given speed (balance.md, route 130 // route weights for a monster of the given speed (balance.md, route
98 // preference). fixed weights from routes.toml take precedence. 131 // preference). fixed weights from routes.toml take precedence.
99 GameMap.prototype.routeWeights = function (speed) { 132 GameMap.prototype.routeWeights = function (speed) {
100 var rp = cfg.route_preference; 133 var rp = cfg.route_preference;
101 var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1); 134 var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1);