wobblyhead hg > minor_monsters
comparison game/js/map.js @ 1:5492efd60e2c
first prototype + updated art sheet
| author | ark |
|---|---|
| date | 2026-10-18 14:50 +0200 |
| parents | |
| children | 910b26feca76 |
comparison
equal
deleted
inserted
replaced
| 0:7af9dd4e2595 | 1:5492efd60e2c |
|---|---|
| 1 // placement lookup, snapping, route sampling | |
| 2 | |
| 3 window.MM = window.MM || {}; | |
| 4 | |
| 5 MM.Route = (function () { | |
| 6 "use strict"; | |
| 7 | |
| 8 // a route from the build: a polyline with cumulative distances, so a | |
| 9 // point can be found by the distance walked | |
| 10 function Route(data) { | |
| 11 var p = data.points; | |
| 12 this.xs = []; | |
| 13 this.ys = []; | |
| 14 this.at = []; | |
| 15 var d = 0; | |
| 16 for (var i = 0; i < p.length; i += 2) { | |
| 17 if (i > 0) d += Math.hypot(p[i] - p[i - 2], p[i + 1] - p[i - 1]); | |
| 18 this.xs.push(p[i]); | |
| 19 this.ys.push(p[i + 1]); | |
| 20 this.at.push(d); | |
| 21 } | |
| 22 this.length = d; | |
| 23 this.value = data.value; | |
| 24 this.weight = data.weight; | |
| 25 } | |
| 26 | |
| 27 // write the point at distance s along the route into out.x, out.y. | |
| 28 // out.seg remembers the segment, so the next search for a point | |
| 29 // further along starts there | |
| 30 Route.prototype.point = function (s, out) { | |
| 31 var at = this.at, n = at.length; | |
| 32 if (s <= 0) { out.x = this.xs[0]; out.y = this.ys[0]; return out; } | |
| 33 if (s >= this.length) { out.x = this.xs[n - 1]; out.y = this.ys[n - 1]; return out; } | |
| 34 var i = out.seg || 1; | |
| 35 if (i >= n || at[i - 1] > s) i = 1; | |
| 36 while (at[i] < s) i++; | |
| 37 out.seg = i; | |
| 38 var t = (s - at[i - 1]) / (at[i] - at[i - 1]); | |
| 39 out.x = this.xs[i - 1] + (this.xs[i] - this.xs[i - 1]) * t; | |
| 40 out.y = this.ys[i - 1] + (this.ys[i] - this.ys[i - 1]) * t; | |
| 41 return out; | |
| 42 }; | |
| 43 | |
| 44 return Route; | |
| 45 })(); | |
| 46 | |
| 47 MM.GameMap = (function () { | |
| 48 "use strict"; | |
| 49 | |
| 50 var cfg = MM.config; | |
| 51 | |
| 52 function GameMap(index) { | |
| 53 var d = MM.maps[index]; | |
| 54 this.index = index; | |
| 55 this.data = d; | |
| 56 this.name = d.name; | |
| 57 this.width = d.width; | |
| 58 this.height = d.height; | |
| 59 this.routes = d.routes.map(function (r) { return new MM.Route(r); }); | |
| 60 } | |
| 61 | |
| 62 GameMap.prototype.legalCell = function (cx, cy) { | |
| 63 var d = this.data; | |
| 64 if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false; | |
| 65 return d.grid.charCodeAt(cy * d.cols + cx) === 49; | |
| 66 }; | |
| 67 | |
| 68 // nearest legal tower position to x, y within the snap radius that keeps | |
| 69 // the minimum distance to the towers in the list. returns {x, y} or null. | |
| 70 GameMap.prototype.snap = function (x, y, towers) { | |
| 71 var p = cfg.placement, cell = this.data.cell; | |
| 72 var reach = Math.ceil(p.snap_radius / cell); | |
| 73 var cx0 = Math.floor(x / cell), cy0 = Math.floor(y / cell); | |
| 74 var best = null, bestD = p.snap_radius; | |
| 75 for (var cy = cy0 - reach; cy <= cy0 + reach; cy++) { | |
| 76 for (var cx = cx0 - reach; cx <= cx0 + reach; cx++) { | |
| 77 if (!this.legalCell(cx, cy)) continue; | |
| 78 var px = cx * cell + cell / 2, py = cy * cell + cell / 2; | |
| 79 var d = Math.hypot(px - x, py - y); | |
| 80 if (d > bestD) continue; | |
| 81 var free = true; | |
| 82 for (var i = 0; i < towers.length; i++) { | |
| 83 if (Math.hypot(towers[i].x - px, towers[i].y - py) < p.min_tower_distance) { | |
| 84 free = false; | |
| 85 break; | |
| 86 } | |
| 87 } | |
| 88 if (free) { | |
| 89 best = { x: px, y: py }; | |
| 90 bestD = d; | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 return best; | |
| 95 }; | |
| 96 | |
| 97 // route weights for a monster of the given speed (balance.md, route | |
| 98 // preference). fixed weights from routes.toml take precedence. | |
| 99 GameMap.prototype.routeWeights = function (speed) { | |
| 100 var rp = cfg.route_preference; | |
| 101 var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1); | |
| 102 return this.routes.map(function (r) { | |
| 103 if (r.weight !== null) return r.weight; | |
| 104 return Math.max(rp.min_weight, 1 + rp.bias * f * (2 * r.value - 1)); | |
| 105 }); | |
| 106 }; | |
| 107 | |
| 108 return GameMap; | |
| 109 })(); |
