wobblyhead hg > minor_monsters
diff game/js/map.js @ 1:5492efd60e2c
first prototype + updated art sheet
| author | ark |
|---|---|
| date | 2026-10-18 14:50 +0200 |
| parents | |
| children | 910b26feca76 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/js/map.js Sun Oct 18 14:50:20 2026 +0200 @@ -0,0 +1,109 @@ +// placement lookup, snapping, route sampling + +window.MM = window.MM || {}; + +MM.Route = (function () { + "use strict"; + + // a route from the build: a polyline with cumulative distances, so a + // point can be found by the distance walked + function Route(data) { + var p = data.points; + this.xs = []; + this.ys = []; + this.at = []; + var d = 0; + for (var i = 0; i < p.length; i += 2) { + if (i > 0) d += Math.hypot(p[i] - p[i - 2], p[i + 1] - p[i - 1]); + this.xs.push(p[i]); + this.ys.push(p[i + 1]); + this.at.push(d); + } + this.length = d; + this.value = data.value; + this.weight = data.weight; + } + + // write the point at distance s along the route into out.x, out.y. + // out.seg remembers the segment, so the next search for a point + // further along starts there + Route.prototype.point = function (s, out) { + var at = this.at, n = at.length; + if (s <= 0) { out.x = this.xs[0]; out.y = this.ys[0]; return out; } + if (s >= this.length) { out.x = this.xs[n - 1]; out.y = this.ys[n - 1]; return out; } + var i = out.seg || 1; + if (i >= n || at[i - 1] > s) i = 1; + while (at[i] < s) i++; + out.seg = i; + var t = (s - at[i - 1]) / (at[i] - at[i - 1]); + out.x = this.xs[i - 1] + (this.xs[i] - this.xs[i - 1]) * t; + out.y = this.ys[i - 1] + (this.ys[i] - this.ys[i - 1]) * t; + return out; + }; + + return Route; +})(); + +MM.GameMap = (function () { + "use strict"; + + var cfg = MM.config; + + function GameMap(index) { + var d = MM.maps[index]; + this.index = index; + this.data = d; + this.name = d.name; + this.width = d.width; + this.height = d.height; + this.routes = d.routes.map(function (r) { return new MM.Route(r); }); + } + + GameMap.prototype.legalCell = function (cx, cy) { + var d = this.data; + if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false; + return d.grid.charCodeAt(cy * d.cols + cx) === 49; + }; + + // nearest legal tower position to x, y within the snap radius that keeps + // the minimum distance to the towers in the list. returns {x, y} or null. + GameMap.prototype.snap = function (x, y, towers) { + var p = cfg.placement, cell = this.data.cell; + var reach = Math.ceil(p.snap_radius / cell); + var cx0 = Math.floor(x / cell), cy0 = Math.floor(y / cell); + var best = null, bestD = p.snap_radius; + for (var cy = cy0 - reach; cy <= cy0 + reach; cy++) { + for (var cx = cx0 - reach; cx <= cx0 + reach; cx++) { + if (!this.legalCell(cx, cy)) continue; + var px = cx * cell + cell / 2, py = cy * cell + cell / 2; + var d = Math.hypot(px - x, py - y); + if (d > bestD) continue; + var free = true; + for (var i = 0; i < towers.length; i++) { + if (Math.hypot(towers[i].x - px, towers[i].y - py) < p.min_tower_distance) { + free = false; + break; + } + } + if (free) { + best = { x: px, y: py }; + bestD = d; + } + } + } + return best; + }; + + // route weights for a monster of the given speed (balance.md, route + // preference). fixed weights from routes.toml take precedence. + GameMap.prototype.routeWeights = function (speed) { + var rp = cfg.route_preference; + var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1); + return this.routes.map(function (r) { + if (r.weight !== null) return r.weight; + return Math.max(rp.min_weight, 1 + rp.bias * f * (2 * r.value - 1)); + }); + }; + + return GameMap; +})();
