|
| 1 | +(function exposeGameLogic(root, factory) { |
| 2 | + const api = factory(); |
| 3 | + if (typeof module === 'object' && module.exports) module.exports = api; |
| 4 | + if (root) root.LittleAndroidLogic = api; |
| 5 | +})(typeof globalThis !== 'undefined' ? globalThis : this, function createGameLogic() { |
| 6 | + 'use strict'; |
| 7 | + |
| 8 | + const DIR = Object.freeze({ |
| 9 | + south: Object.freeze({ dx: 0, dy: 1 }), |
| 10 | + north: Object.freeze({ dx: 0, dy: -1 }), |
| 11 | + east: Object.freeze({ dx: 1, dy: 0 }), |
| 12 | + west: Object.freeze({ dx: -1, dy: 0 }), |
| 13 | + }); |
| 14 | + |
| 15 | + const OPPOSITE_DIR = Object.freeze({ |
| 16 | + south: 'north', |
| 17 | + north: 'south', |
| 18 | + east: 'west', |
| 19 | + west: 'east', |
| 20 | + }); |
| 21 | + |
| 22 | + function oppositeDir(direction) { |
| 23 | + return OPPOSITE_DIR[direction]; |
| 24 | + } |
| 25 | + |
| 26 | + function mulberry32(seed) { |
| 27 | + return function random() { |
| 28 | + let value = seed |= 0; |
| 29 | + seed = value + 0x6D2B79F5 | 0; |
| 30 | + value = Math.imul(seed ^ seed >>> 15, 1 | seed); |
| 31 | + value = value + Math.imul(value ^ value >>> 7, 61 | value) ^ value; |
| 32 | + return ((value ^ value >>> 14) >>> 0) / 4294967296; |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + function createWorldMap(seed = 77) { |
| 37 | + const width = 24; |
| 38 | + const height = 20; |
| 39 | + const map = Array.from({ length: height }, () => Array(width).fill(0)); |
| 40 | + const random = mulberry32(seed); |
| 41 | + |
| 42 | + for (let y = 1; y <= 5; y++) { |
| 43 | + for (let x = 1; x <= 8; x++) map[y][x] = y === 1 ? 9 : 8; |
| 44 | + } |
| 45 | + map[0][3] = 11; |
| 46 | + map[0][7] = 11; |
| 47 | + map[5][4] = 10; |
| 48 | + map[5][5] = 10; |
| 49 | + |
| 50 | + for (let y = 0; y < height; y++) { |
| 51 | + map[y][17] = 17; |
| 52 | + map[y][18] = 5; |
| 53 | + map[y][19] = 4; |
| 54 | + map[y][20] = 5; |
| 55 | + map[y][21] = 17; |
| 56 | + } |
| 57 | + |
| 58 | + for (let x = 17; x <= 21; x++) { |
| 59 | + map[9][x] = 13; |
| 60 | + map[10][x] = 12; |
| 61 | + map[11][x] = 12; |
| 62 | + map[12][x] = 13; |
| 63 | + } |
| 64 | + |
| 65 | + for (let y = 6; y <= 10; y++) { |
| 66 | + map[y][4] = 2; |
| 67 | + map[y][5] = 2; |
| 68 | + } |
| 69 | + for (let x = 5; x <= 17; x++) { |
| 70 | + map[10][x] = 2; |
| 71 | + map[11][x] = 2; |
| 72 | + } |
| 73 | + |
| 74 | + const grassPatches = [ |
| 75 | + [10, 2, 14, 5], |
| 76 | + [12, 6, 16, 9], |
| 77 | + [1, 13, 6, 17], |
| 78 | + [8, 14, 13, 18], |
| 79 | + ]; |
| 80 | + for (const [x1, y1, x2, y2] of grassPatches) { |
| 81 | + for (let y = y1; y <= Math.min(y2, height - 1); y++) { |
| 82 | + for (let x = x1; x <= Math.min(x2, width - 1); x++) { |
| 83 | + if (map[y][x] === 0 && random() < 0.6) map[y][x] = 1; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const trees = [ |
| 89 | + [15, 2], [16, 4], [15, 6], [16, 8], [15, 14], [16, 16], |
| 90 | + [13, 3], [14, 7], [13, 13], [14, 17], [3, 8], [7, 7], |
| 91 | + [10, 8], [1, 11], [8, 12], [11, 1], [12, 3], |
| 92 | + ]; |
| 93 | + for (const [x, y] of trees) { |
| 94 | + if (y >= 0 && y < height && x < width) { |
| 95 | + if (map[y][x] === 0 || map[y][x] === 1) map[y][x] = 6; |
| 96 | + if (y + 1 < height && (map[y + 1][x] === 0 || map[y + 1][x] === 1)) { |
| 97 | + map[y + 1][x] = 7; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + for (const [x, y] of [[12, 10], [2, 15], [9, 17], [16, 13]]) { |
| 103 | + if (map[y][x] === 0 || map[y][x] === 1) map[y][x] = 14; |
| 104 | + } |
| 105 | + if (map[9][6] === 0 || map[9][6] === 2) map[9][6] = 15; |
| 106 | + if (map[6][3] === 0) map[6][3] = 16; |
| 107 | + |
| 108 | + return { width, height, map }; |
| 109 | + } |
| 110 | + |
| 111 | + function findPath(startX, startY, endX, endY, isWalkable) { |
| 112 | + const sx = Math.round(startX); |
| 113 | + const sy = Math.round(startY); |
| 114 | + let ex = Math.round(endX); |
| 115 | + let ey = Math.round(endY); |
| 116 | + |
| 117 | + if (!isWalkable(ex, ey)) { |
| 118 | + let best = null; |
| 119 | + let bestDistance = Infinity; |
| 120 | + for (let dy = -3; dy <= 3; dy++) { |
| 121 | + for (let dx = -3; dx <= 3; dx++) { |
| 122 | + if (!isWalkable(ex + dx, ey + dy)) continue; |
| 123 | + const distance = dx * dx + dy * dy; |
| 124 | + if (distance < bestDistance) { |
| 125 | + bestDistance = distance; |
| 126 | + best = { x: ex + dx, y: ey + dy }; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + if (!best) return []; |
| 131 | + ex = best.x; |
| 132 | + ey = best.y; |
| 133 | + } |
| 134 | + |
| 135 | + const key = (x, y) => `${x},${y}`; |
| 136 | + const heuristic = (x, y) => Math.abs(x - ex) + Math.abs(y - ey); |
| 137 | + const open = [{ x: sx, y: sy, g: 0, f: heuristic(sx, sy), parent: null }]; |
| 138 | + const closed = new Set(); |
| 139 | + const bestCost = { [key(sx, sy)]: 0 }; |
| 140 | + let iterations = 0; |
| 141 | + |
| 142 | + while (open.length > 0 && iterations < 3000) { |
| 143 | + iterations++; |
| 144 | + open.sort((a, b) => a.f - b.f); |
| 145 | + const current = open.shift(); |
| 146 | + if (current.x === ex && current.y === ey) { |
| 147 | + const path = []; |
| 148 | + for (let node = current; node; node = node.parent) { |
| 149 | + path.unshift({ x: node.x, y: node.y }); |
| 150 | + } |
| 151 | + return path; |
| 152 | + } |
| 153 | + |
| 154 | + closed.add(key(current.x, current.y)); |
| 155 | + for (const [dx, dy] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) { |
| 156 | + const x = current.x + dx; |
| 157 | + const y = current.y + dy; |
| 158 | + const tileKey = key(x, y); |
| 159 | + if (closed.has(tileKey) || !isWalkable(x, y)) continue; |
| 160 | + const cost = current.g + 1; |
| 161 | + if (bestCost[tileKey] !== undefined && cost >= bestCost[tileKey]) continue; |
| 162 | + bestCost[tileKey] = cost; |
| 163 | + open.push({ x, y, g: cost, f: cost + heuristic(x, y), parent: current }); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return []; |
| 168 | + } |
| 169 | + |
| 170 | + return Object.freeze({ DIR, oppositeDir, mulberry32, createWorldMap, findPath }); |
| 171 | +}); |
0 commit comments