comparison 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
comparison
equal deleted inserted replaced
9:53f015966118 10:22b604680c5a
3 window.MM = window.MM || {}; 3 window.MM = window.MM || {};
4 4
5 MM.Route = (function () { 5 MM.Route = (function () {
6 "use strict"; 6 "use strict";
7 7
8 // sideways offsets on the inside of a bend: share of the distance at
9 // which the offset point would stop moving forward, and how fast the
10 // limit may change per pixel along the route
11 var BEND_SHARE = 0.7;
12 var LIMIT_SLOPE = 0.5;
13
8 // a route from the build: a polyline with cumulative distances, so a 14 // a route from the build: a polyline with cumulative distances, so a
9 // point can be found by the distance walked 15 // point can be found by the distance walked. maxOffset is the largest
10 function Route(data) { 16 // sideways offset that shifted() will be asked for.
17 function Route(data, maxOffset) {
11 var p = data.points; 18 var p = data.points;
12 this.xs = []; 19 this.xs = [];
13 this.ys = []; 20 this.ys = [];
14 this.at = []; 21 this.at = [];
15 var d = 0; 22 var d = 0;
20 this.at.push(d); 27 this.at.push(d);
21 } 28 }
22 this.length = d; 29 this.length = d;
23 this.value = data.value; 30 this.value = data.value;
24 this.weight = data.weight; 31 this.weight = data.weight;
32 this.sideways(maxOffset);
25 } 33 }
34
35 // set up the sideways direction at each point (nx, ny) and how far an
36 // offset may go to the left and right there (left, right)
37 Route.prototype.sideways = function (maxOffset) {
38 var xs = this.xs, ys = this.ys, at = this.at, n = xs.length, i;
39 // direction of travel of segment i, from point i - 1 to point i
40 var tx = [0], ty = [0];
41 for (i = 1; i < n; i++) {
42 var len = at[i] - at[i - 1];
43 tx.push((xs[i] - xs[i - 1]) / len);
44 ty.push((ys[i] - ys[i - 1]) / len);
45 }
46 // at each point, the left side as drawn, averaged over the two
47 // segments that meet there
48 this.nx = [];
49 this.ny = [];
50 for (i = 0; i < n; i++) {
51 var a = Math.max(i, 1), b = Math.min(i + 1, n - 1);
52 var sx = ty[a] + ty[b], sy = -tx[a] - tx[b], sl = Math.hypot(sx, sy);
53 if (sl < 1e-6) { sx = ty[a]; sy = -tx[a]; sl = 1; }
54 this.nx.push(sx / sl);
55 this.ny.push(sy / sl);
56 }
57 // along a segment the sideways direction turns. an offset on the
58 // inside of the turn moves forward at 1 + offset * k of the walking
59 // speed, k being negative on that side. limit the offset at both
60 // ends of the segment so this stays above 1 - BEND_SHARE
61 var left = [], right = [];
62 for (i = 0; i < n; i++) {
63 left.push(maxOffset);
64 right.push(maxOffset);
65 }
66 for (i = 1; i < n; i++) {
67 var k = ((this.nx[i] - this.nx[i - 1]) * tx[i]
68 + (this.ny[i] - this.ny[i - 1]) * ty[i]) / (at[i] - at[i - 1]);
69 var lim = BEND_SHARE / Math.abs(k);
70 var side = k < 0 ? left : right;
71 side[i - 1] = Math.min(side[i - 1], lim);
72 side[i] = Math.min(side[i], lim);
73 }
74 // let the limits change gradually, so monsters drift towards the
75 // centre line before a tight bend rather than at it
76 [left, right].forEach(function (lim) {
77 for (i = 1; i < n; i++)
78 lim[i] = Math.min(lim[i], lim[i - 1] + LIMIT_SLOPE * (at[i] - at[i - 1]));
79 for (i = n - 2; i >= 0; i--)
80 lim[i] = Math.min(lim[i], lim[i + 1] + LIMIT_SLOPE * (at[i + 1] - at[i]));
81 });
82 this.left = left;
83 this.right = right;
84 };
26 85
27 // write the point at distance s along the route into out.x, out.y. 86 // 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 87 // out.seg remembers the segment, so the next search for a point
29 // further along starts there 88 // further along starts there
30 Route.prototype.point = function (s, out) { 89 Route.prototype.point = function (s, out) {
39 out.x = this.xs[i - 1] + (this.xs[i] - this.xs[i - 1]) * t; 98 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; 99 out.y = this.ys[i - 1] + (this.ys[i] - this.ys[i - 1]) * t;
41 return out; 100 return out;
42 }; 101 };
43 102
103 // as point(), moved sideways by d: positive to the left of the direction
104 // of travel as drawn, negative to the right. on the inside of bends d is
105 // reduced, so the point keeps moving forward as s grows
106 Route.prototype.shifted = function (s, d, out) {
107 this.point(s, out);
108 var at = this.at, n = at.length, i, t;
109 if (s <= 0) { i = 1; t = 0; }
110 else if (s >= this.length) { i = n - 1; t = 1; }
111 else { i = out.seg; t = (s - at[i - 1]) / (at[i] - at[i - 1]); }
112 var lim = d > 0 ? this.left : this.right;
113 var m = lim[i - 1] + (lim[i] - lim[i - 1]) * t;
114 if (Math.abs(d) > m) d = d > 0 ? m : -m;
115 out.x += d * (this.nx[i - 1] + (this.nx[i] - this.nx[i - 1]) * t);
116 out.y += d * (this.ny[i - 1] + (this.ny[i] - this.ny[i - 1]) * t);
117 return out;
118 };
119
44 return Route; 120 return Route;
45 })(); 121 })();
46 122
47 MM.GameMap = (function () { 123 MM.GameMap = (function () {
48 "use strict"; 124 "use strict";
58 this.index = index; 134 this.index = index;
59 this.data = d; 135 this.data = d;
60 this.name = d.name; 136 this.name = d.name;
61 this.width = d.width; 137 this.width = d.width;
62 this.height = d.height; 138 this.height = d.height;
63 this.routes = d.routes.map(function (r) { return new MM.Route(r); }); 139 // largest sideways offset of a monster from the centre line
140 this.maxOffset = cfg.movement.offset * cfg.maps[index].path_half_width;
141 var maxOffset = this.maxOffset;
142 this.routes = d.routes.map(function (r) { return new MM.Route(r, maxOffset); });
64 } 143 }
65 144
66 GameMap.prototype.legalCell = function (cx, cy) { 145 GameMap.prototype.legalCell = function (cx, cy) {
67 var d = this.data; 146 var d = this.data;
68 if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false; 147 if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false;