wobblyhead hg > minor_monsters
diff game/js/map.js @ 10:22b604680c5a
adjusted monster movement to be less uniform
| author | ark |
|---|---|
| date | 2026-10-19 16:56 +0200 |
| parents | 910b26feca76 |
| children | 7fda634d15ad |
line wrap: on
line diff
--- a/game/js/map.js Mon Oct 19 15:55:37 2026 +0200 +++ b/game/js/map.js Mon Oct 19 16:56:38 2026 +0200 @@ -5,9 +5,16 @@ MM.Route = (function () { "use strict"; + // sideways offsets on the inside of a bend: share of the distance at + // which the offset point would stop moving forward, and how fast the + // limit may change per pixel along the route + var BEND_SHARE = 0.7; + var LIMIT_SLOPE = 0.5; + // a route from the build: a polyline with cumulative distances, so a - // point can be found by the distance walked - function Route(data) { + // point can be found by the distance walked. maxOffset is the largest + // sideways offset that shifted() will be asked for. + function Route(data, maxOffset) { var p = data.points; this.xs = []; this.ys = []; @@ -22,8 +29,60 @@ this.length = d; this.value = data.value; this.weight = data.weight; + this.sideways(maxOffset); } + // set up the sideways direction at each point (nx, ny) and how far an + // offset may go to the left and right there (left, right) + Route.prototype.sideways = function (maxOffset) { + var xs = this.xs, ys = this.ys, at = this.at, n = xs.length, i; + // direction of travel of segment i, from point i - 1 to point i + var tx = [0], ty = [0]; + for (i = 1; i < n; i++) { + var len = at[i] - at[i - 1]; + tx.push((xs[i] - xs[i - 1]) / len); + ty.push((ys[i] - ys[i - 1]) / len); + } + // at each point, the left side as drawn, averaged over the two + // segments that meet there + this.nx = []; + this.ny = []; + for (i = 0; i < n; i++) { + var a = Math.max(i, 1), b = Math.min(i + 1, n - 1); + var sx = ty[a] + ty[b], sy = -tx[a] - tx[b], sl = Math.hypot(sx, sy); + if (sl < 1e-6) { sx = ty[a]; sy = -tx[a]; sl = 1; } + this.nx.push(sx / sl); + this.ny.push(sy / sl); + } + // along a segment the sideways direction turns. an offset on the + // inside of the turn moves forward at 1 + offset * k of the walking + // speed, k being negative on that side. limit the offset at both + // ends of the segment so this stays above 1 - BEND_SHARE + var left = [], right = []; + for (i = 0; i < n; i++) { + left.push(maxOffset); + right.push(maxOffset); + } + for (i = 1; i < n; i++) { + var k = ((this.nx[i] - this.nx[i - 1]) * tx[i] + + (this.ny[i] - this.ny[i - 1]) * ty[i]) / (at[i] - at[i - 1]); + var lim = BEND_SHARE / Math.abs(k); + var side = k < 0 ? left : right; + side[i - 1] = Math.min(side[i - 1], lim); + side[i] = Math.min(side[i], lim); + } + // let the limits change gradually, so monsters drift towards the + // centre line before a tight bend rather than at it + [left, right].forEach(function (lim) { + for (i = 1; i < n; i++) + lim[i] = Math.min(lim[i], lim[i - 1] + LIMIT_SLOPE * (at[i] - at[i - 1])); + for (i = n - 2; i >= 0; i--) + lim[i] = Math.min(lim[i], lim[i + 1] + LIMIT_SLOPE * (at[i + 1] - at[i])); + }); + this.left = left; + this.right = right; + }; + // 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 @@ -41,6 +100,23 @@ return out; }; + // as point(), moved sideways by d: positive to the left of the direction + // of travel as drawn, negative to the right. on the inside of bends d is + // reduced, so the point keeps moving forward as s grows + Route.prototype.shifted = function (s, d, out) { + this.point(s, out); + var at = this.at, n = at.length, i, t; + if (s <= 0) { i = 1; t = 0; } + else if (s >= this.length) { i = n - 1; t = 1; } + else { i = out.seg; t = (s - at[i - 1]) / (at[i] - at[i - 1]); } + var lim = d > 0 ? this.left : this.right; + var m = lim[i - 1] + (lim[i] - lim[i - 1]) * t; + if (Math.abs(d) > m) d = d > 0 ? m : -m; + out.x += d * (this.nx[i - 1] + (this.nx[i] - this.nx[i - 1]) * t); + out.y += d * (this.ny[i - 1] + (this.ny[i] - this.ny[i - 1]) * t); + return out; + }; + return Route; })(); @@ -60,7 +136,10 @@ this.name = d.name; this.width = d.width; this.height = d.height; - this.routes = d.routes.map(function (r) { return new MM.Route(r); }); + // largest sideways offset of a monster from the centre line + this.maxOffset = cfg.movement.offset * cfg.maps[index].path_half_width; + var maxOffset = this.maxOffset; + this.routes = d.routes.map(function (r) { return new MM.Route(r, maxOffset); }); } GameMap.prototype.legalCell = function (cx, cy) {
