Skip to content

Commit

Permalink
Update renderer background logic
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Nov 6, 2024
1 parent 7b3976a commit f71caaf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 66 deletions.
Binary file modified public/tile-map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 60 additions & 66 deletions renderer/Background.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,89 @@
import { canvasToImage } from "../lib/canvasToImage";
import { createCanvas } from "../lib/createCanvas";
import { ZombieSurvival } from "../simulator";
import { assets } from "./Assets";
import { canvasToImage } from "@/lib/canvasToImage";
import { createCanvas } from "@/lib/createCanvas";
import { ZombieSurvival } from "@/simulator";

export const BITMASK_TO_TILE_INDEX = {
export const BITMASK_TO_TILE_INDEX: Record<number, number> = {
0: 0,
4: 1,
92: 2,
124: 3,
116: 4,
80: 5,
// no index 6: tile not used
16: 7,
20: 8,
87: 9,
223: 10,
241: 11,
21: 12,
64: 13,
29: 14,
117: 15,
85: 16,
71: 17,
221: 18,
125: 19,
112: 20,
31: 21,
253: 22,
113: 23,
28: 24,
127: 25,
247: 26,
209: 27,
23: 28,
199: 29,
213: 30,
95: 31,
255: 32,
245: 33,
81: 34,
5: 35,
84: 36,
93: 37,
119: 38,
215: 39,
193: 40,
17: 41,
// no index 42: tile not used
1: 43,
7: 44,
197: 45,
69: 46,
68: 47,
65: 48,
16: 1,
88: 2,
216: 3,
248: 4,
120: 5,
72: 6,
64: 7,
208: 8,
122: 9,
222: 10,
255: 11,
251: 12,
106: 13,
82: 14,
94: 15,
219: 16,
254: 17,
// 18 not used
127: 19,
75: 20,
210: 21,
250: 22,
126: 23,
31: 24,
95: 25,
91: 26,
74: 27,
214: 28,
// 29 not used
107: 30,
80: 31,
10: 32,
66: 33,
2: 34,
86: 35,
223: 36,
123: 37,
90: 38,
24: 39,
218: 40,
104: 41,
18: 42,
30: 43,
27: 44,
26: 45,
8: 46,
22: 47,
11: 48,
} as const;

const neighborOffsets = [
[-1, -1],
[0, -1],
[1, -1],
[-1, 0],
[1, 0],
[1, 1],
[0, 1],
[-1, 1],
[-1, 0],
[-1, -1],
[0, 1],
[1, 1],
] as const;

export function getBitMask(
map: string[][],
x: number,
y: number,
outOfBound = 1,
outOfBoundToken = " ",
) {
const width = ZombieSurvival.boardWidth(map);
const height = ZombieSurvival.boardHeight(map);

let bitmask = 0;

for (let i = 0; i < neighborOffsets.length; i++) {
const [dx, dy] = neighborOffsets[i];
const nx = x + dx;
const ny = y + dy;
const token = map[y + dy]?.[x + dx] ?? outOfBoundToken;

if (ny < 0 || ny >= height || nx < 0 || nx >= width) {
bitmask |= outOfBound << i;
} else if (map[ny][nx] === "R") {
if (token === "R") {
bitmask |= 1 << i;
}
}

return bitmask as keyof typeof BITMASK_TO_TILE_INDEX;
return bitmask;
}

export async function generateBg(
Expand Down

0 comments on commit f71caaf

Please sign in to comment.