Pathfinding and walking the path
This commit is contained in:
59
js/grid.js
59
js/grid.js
@@ -5,16 +5,18 @@ export class Grid {
|
||||
this.layers = new Map();
|
||||
this.masks = new Map();
|
||||
this.prnt = prnt;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.prnt.style.display = 'grid';
|
||||
this.prnt.style.gridTemplateColumns = `repeat(${width}, 1fr)`;
|
||||
this.prnt.style.gridTemplateRows = `repeat(${height}, 1fr)`;
|
||||
this.prnt.style.gridTemplateColumns = `repeat(${this.width}, 1fr)`;
|
||||
this.prnt.style.gridTemplateRows = `repeat(${this.height}, 1fr)`;
|
||||
this.prnt.style.backgroundImage = `url("images/${tileset}/land.svg")`;
|
||||
for (let i = 0; i <= layers.length; i++) {
|
||||
const name = layers[i];
|
||||
this.layers.set(name, new Layer(i * height, tileset));
|
||||
this.layers.set(name, new Layer(i * this.height, tileset));
|
||||
}
|
||||
for (const name of masks) {
|
||||
this.masks.set(name, new Mask(width, height));
|
||||
this.masks.set(name, new Mask(this.width, this.height));
|
||||
}
|
||||
}
|
||||
add_tile(tile_factory, x, y) {
|
||||
@@ -28,5 +30,54 @@ export class Grid {
|
||||
}
|
||||
return tile;
|
||||
}
|
||||
add_debug_tile(x, y, color) {
|
||||
const div = document.createElement('div');
|
||||
this.prnt.appendChild(div);
|
||||
div.style.gridColumnStart = `${x + 1}`;
|
||||
div.style.gridRowStart = `${y + 1}`;
|
||||
div.style.gridColumnEnd = 'span 1';
|
||||
div.style.gridRowEnd = 'span 1';
|
||||
div.style.backgroundColor = color;
|
||||
div.style.opacity = '0.3';
|
||||
div.style.zIndex = '1000000';
|
||||
}
|
||||
get_path(mask_name, src, dst) {
|
||||
const mask = this.masks.get(mask_name);
|
||||
const next = [];
|
||||
for (let x = 0; x < this.width; x++) {
|
||||
next.push(Array(this.height).fill(undefined));
|
||||
}
|
||||
// BFS
|
||||
const queue = [dst];
|
||||
while (queue.length) {
|
||||
const point = queue.shift();
|
||||
// Horizontal / vertical has to come before diagonal
|
||||
for (const [dx, dy] of [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]]) {
|
||||
const check = [point[0] + dx, point[1] + dy];
|
||||
if (next[check[0]][check[1]]) {
|
||||
// Already visited
|
||||
continue;
|
||||
}
|
||||
if (!mask.mask[check[0]][check[1]]) {
|
||||
// Not allowed
|
||||
continue;
|
||||
}
|
||||
next[check[0]][check[1]] = point;
|
||||
if (check[0] == src[0] && check[1] == src[1]) {
|
||||
const path = [];
|
||||
let cur = src;
|
||||
while (cur[0] != dst[0] || cur[1] != dst[1]) {
|
||||
path.push(cur);
|
||||
cur = next[cur[0]][cur[1]];
|
||||
}
|
||||
path.push(dst);
|
||||
return path;
|
||||
}
|
||||
queue.push(check);
|
||||
}
|
||||
}
|
||||
// No valid path
|
||||
return [];
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=grid.js.map
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"grid.js","sourceRoot":"","sources":["../ts/grid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAIjC,MAAM,OAAO,IAAI;IAKf,YAAY,IAAiB,EAAE,KAAa,EAAE,MAAc,EAAE,OAAe,EAAE,MAAgB,EAAE,KAAe;QAHhH,WAAM,GAAuB,IAAI,GAAG,EAAiB,CAAC;QACtD,UAAK,GAAsB,IAAI,GAAG,EAAgB,CAAC;QAGjD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;QACjC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,mBAAmB,GAAG,UAAU,KAAK,QAAQ,CAAC;QAC9D,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,gBAAgB,GAAG,UAAU,MAAM,QAAQ,CAAC;QAC5D,IAAI,CAAC,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K,CAAC,CAAC,CAAC,CAAC,EAAE;oBAC5B,kBAAkB;oBAClB,SAAS;iBACV;gBAED,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;oBAClC,cAAc;oBACd,SAAS;iBACV;gBAED,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;gBACjC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE;oBAC5C,MAAM,IAAI,GAAY,EAAE,CAAC;oBACzB,IAAI,GAAG,GAAG,GAAG,CAAC;oBACd,OAAO,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE;wBAC3C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;wBACf,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;qBAC5B;oBACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBACf,OAAO,IAAI,CAAC;iBACb;gBACD,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aACrB;SACF;QAED,gBAAgB;QAChB,OAAO,EAAE,CAAC;IACZ,CAAC;CACF"}
|
||||
47
js/tower.js
47
js/tower.js
@@ -71,30 +71,6 @@ export function main() {
|
||||
grid.add_tile(tiles.BRIDGE_LR, 92, 52);
|
||||
grid.add_tile(tiles.BRIDGE_LR, 92, 36);
|
||||
grid.add_tile(tiles.BRIDGE_LR, 92, 20);
|
||||
function add_debug_tile(x, y, color) {
|
||||
const div = document.createElement('div');
|
||||
real.appendChild(div);
|
||||
div.style.gridColumnStart = `${x + 1}`;
|
||||
div.style.gridRowStart = `${y + 1}`;
|
||||
div.style.gridColumnEnd = 'span 1';
|
||||
div.style.gridRowEnd = 'span 1';
|
||||
div.style.backgroundColor = color;
|
||||
div.style.opacity = '0.3';
|
||||
div.style.zIndex = '1000000';
|
||||
}
|
||||
{
|
||||
const m = grid.masks.get('walkable').mask;
|
||||
for (let x = 0; x < m.length; x++) {
|
||||
for (let y = 0; y < m[x].length; y++) {
|
||||
if (m[x][y]) {
|
||||
add_debug_tile(x, y, 'orange');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
add_debug_tile(118, 23, 'blue');
|
||||
add_debug_tile(44, 94, 'red');
|
||||
function rand(min, max) {
|
||||
return Math.round(Math.random() * (max - min) + min);
|
||||
}
|
||||
@@ -109,15 +85,30 @@ export function main() {
|
||||
grid.add_tile(tiles.FIREBALL_IMPACT, 62 + target_relative_x, 54 + target_relative_y);
|
||||
}, factory.duration);
|
||||
}, 3250);
|
||||
const greenaxe = grid.add_tile(tiles.GREENAXE, 60, 32);
|
||||
const path = grid.get_path('walkable', [118, 23], [44, 94]);
|
||||
const greenaxe = grid.add_tile(tiles.GREENAXE, 115, 18);
|
||||
greenaxe.play('walk');
|
||||
greenaxe.elem.style.transitionProperty = 'top,left';
|
||||
greenaxe.elem.style.transitionDuration = '25s';
|
||||
greenaxe.elem.style.transitionDuration = '0.5s';
|
||||
greenaxe.elem.style.transitionTimingFunction = 'linear';
|
||||
greenaxe.elem.style.top = '0';
|
||||
greenaxe.elem.style.left = '0';
|
||||
greenaxe.elem.style.transform = 'scaleX(-1)';
|
||||
setTimeout(() => greenaxe.elem.style.left = '-500%', 100);
|
||||
let prev = [118, 23];
|
||||
setInterval(() => {
|
||||
const next = path.shift();
|
||||
if (!next) {
|
||||
return;
|
||||
}
|
||||
if (next[0] - prev[0] < 0) {
|
||||
greenaxe.elem.style.transform = 'scaleX(-1)';
|
||||
}
|
||||
else if (next[0] - prev[0] > 0) {
|
||||
greenaxe.elem.style.transform = 'scaleX(1)';
|
||||
}
|
||||
prev = next;
|
||||
greenaxe.elem.style.left = `${(next[0] - 118) / 6 * 100}%`;
|
||||
greenaxe.elem.style.top = `${(next[1] - 23) / 6 * 100}%`;
|
||||
}, 500);
|
||||
}
|
||||
;
|
||||
main();
|
||||
|
||||
File diff suppressed because one or more lines are too long
72
ts/grid.ts
72
ts/grid.ts
@@ -3,26 +3,32 @@ import { Mask } from './mask.js';
|
||||
import { Tile } from './tile.js';
|
||||
import { TileFactory } from './tile_factory.js';
|
||||
|
||||
type Point = [number, number]
|
||||
|
||||
export class Grid {
|
||||
prnt: HTMLElement;
|
||||
width: number;
|
||||
height: number;
|
||||
layers: Map<string, Layer> = new Map<string, Layer>();
|
||||
masks: Map<string, Mask> = new Map<string, Mask>();
|
||||
|
||||
constructor(prnt: HTMLElement, width: number, height: number, tileset: string, layers: string[], masks: string[]) {
|
||||
this.prnt = prnt;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
this.prnt.style.display = 'grid';
|
||||
this.prnt.style.gridTemplateColumns = `repeat(${width}, 1fr)`;
|
||||
this.prnt.style.gridTemplateRows = `repeat(${height}, 1fr)`;
|
||||
this.prnt.style.gridTemplateColumns = `repeat(${this.width}, 1fr)`;
|
||||
this.prnt.style.gridTemplateRows = `repeat(${this.height}, 1fr)`;
|
||||
this.prnt.style.backgroundImage = `url("images/${tileset}/land.svg")`;
|
||||
|
||||
for (let i = 0; i <= layers.length; i++) {
|
||||
const name = layers[i];
|
||||
this.layers.set(name, new Layer(i * height, tileset));
|
||||
this.layers.set(name, new Layer(i * this.height, tileset));
|
||||
}
|
||||
|
||||
for (const name of masks) {
|
||||
this.masks.set(name, new Mask(width, height));
|
||||
this.masks.set(name, new Mask(this.width, this.height));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,4 +46,62 @@ export class Grid {
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
add_debug_tile(x: number, y: number, color: string) {
|
||||
const div = document.createElement('div');
|
||||
this.prnt.appendChild(div);
|
||||
div.style.gridColumnStart = `${x + 1}`;
|
||||
div.style.gridRowStart = `${y + 1}`;
|
||||
div.style.gridColumnEnd = 'span 1';
|
||||
div.style.gridRowEnd = 'span 1';
|
||||
div.style.backgroundColor = color;
|
||||
div.style.opacity = '0.3';
|
||||
div.style.zIndex = '1000000';
|
||||
}
|
||||
|
||||
get_path(mask_name: string, src: Point, dst: Point): Point[] {
|
||||
const mask = this.masks.get(mask_name)!;
|
||||
|
||||
const next = [];
|
||||
for (let x = 0; x < this.width; x++) {
|
||||
next.push(Array(this.height).fill(undefined));
|
||||
}
|
||||
|
||||
// BFS
|
||||
const queue: Point[] = [dst];
|
||||
while (queue.length) {
|
||||
const point = queue.shift()!;
|
||||
|
||||
// Horizontal / vertical has to come before diagonal
|
||||
for (const [dx, dy] of [[-1,0], [1,0], [0,-1], [0,1], [-1,-1], [-1,1], [1,-1], [1,1]]) {
|
||||
const check: Point = [point[0] + dx, point[1] + dy];
|
||||
|
||||
if (next[check[0]][check[1]]) {
|
||||
// Already visited
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!mask.mask[check[0]][check[1]]) {
|
||||
// Not allowed
|
||||
continue;
|
||||
}
|
||||
|
||||
next[check[0]][check[1]] = point;
|
||||
if (check[0] == src[0] && check[1] == src[1]) {
|
||||
const path: Point[] = [];
|
||||
let cur = src;
|
||||
while (cur[0] != dst[0] || cur[1] != dst[1]) {
|
||||
path.push(cur);
|
||||
cur = next[cur[0]][cur[1]];
|
||||
}
|
||||
path.push(dst);
|
||||
return path;
|
||||
}
|
||||
queue.push(check);
|
||||
}
|
||||
}
|
||||
|
||||
// No valid path
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
51
ts/tower.ts
51
ts/tower.ts
@@ -85,33 +85,6 @@ export function main() {
|
||||
grid.add_tile(tiles.BRIDGE_LR, 92, 36);
|
||||
grid.add_tile(tiles.BRIDGE_LR, 92, 20);
|
||||
|
||||
function add_debug_tile(x: number, y: number, color: string) {
|
||||
const div = document.createElement('div');
|
||||
real.appendChild(div);
|
||||
div.style.gridColumnStart = `${x + 1}`;
|
||||
div.style.gridRowStart = `${y + 1}`;
|
||||
div.style.gridColumnEnd = 'span 1';
|
||||
div.style.gridRowEnd = 'span 1';
|
||||
div.style.backgroundColor = color;
|
||||
div.style.opacity = '0.3';
|
||||
div.style.zIndex = '1000000';
|
||||
}
|
||||
|
||||
{
|
||||
const m = grid.masks.get('walkable')!.mask;
|
||||
for (let x = 0; x < m.length; x++) {
|
||||
for (let y = 0; y < m[x].length; y++) {
|
||||
if (m[x][y]) {
|
||||
add_debug_tile(x, y, 'orange');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
add_debug_tile(118, 23, 'blue');
|
||||
add_debug_tile(44, 94, 'red');
|
||||
|
||||
function rand(min: number, max: number): number {
|
||||
return Math.round(Math.random() * (max - min) + min);
|
||||
}
|
||||
@@ -128,15 +101,31 @@ export function main() {
|
||||
}, factory.duration);
|
||||
}, 3250);
|
||||
|
||||
const greenaxe = grid.add_tile(tiles.GREENAXE, 60, 32);
|
||||
const path = grid.get_path('walkable', [118, 23], [44, 94]);
|
||||
|
||||
const greenaxe = grid.add_tile(tiles.GREENAXE, 115, 18);
|
||||
greenaxe.play('walk');
|
||||
greenaxe.elem.style.transitionProperty = 'top,left';
|
||||
greenaxe.elem.style.transitionDuration = '25s';
|
||||
greenaxe.elem.style.transitionDuration = '0.5s';
|
||||
greenaxe.elem.style.transitionTimingFunction = 'linear';
|
||||
greenaxe.elem.style.top = '0';
|
||||
greenaxe.elem.style.left = '0';
|
||||
greenaxe.elem.style.transform = 'scaleX(-1)';
|
||||
setTimeout(() => greenaxe.elem.style.left = '-500%', 100);
|
||||
|
||||
let prev = [118, 23];
|
||||
setInterval(() => {
|
||||
const next = path.shift();
|
||||
if (!next) {
|
||||
return;
|
||||
}
|
||||
if (next[0] - prev[0] < 0) {
|
||||
greenaxe.elem.style.transform = 'scaleX(-1)';
|
||||
} else if (next[0] - prev[0] > 0) {
|
||||
greenaxe.elem.style.transform = 'scaleX(1)';
|
||||
}
|
||||
prev = next;
|
||||
greenaxe.elem.style.left = `${(next[0] - 118) / 6 * 100}%`;
|
||||
greenaxe.elem.style.top = `${(next[1] - 23) / 6 * 100}%`;
|
||||
}, 500);
|
||||
};
|
||||
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user