99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
import { COLOR } from "../data/colors.ts";
|
|
import { fillRect, setPixelColor } from "../io/window.ts";
|
|
|
|
export const inRect = (x: number, y: number, rectX: number, rectY: number, rectW: number, rectH: number) => {
|
|
return (
|
|
x >= rectX &&
|
|
x < rectX+rectW &&
|
|
y >= rectY &&
|
|
y < rectY+rectH
|
|
)
|
|
}
|
|
|
|
export function reGridWithGap (x: number, y: number, gridX: number, gridY: number, cellW: number, cellH: number, gapX: number, gapY: number): {x: number, y: number} | null {
|
|
const gx = Math.floor((x-gridX)/(cellW+gapX));
|
|
const gy = Math.floor((y-gridY)/(cellH+gapY));
|
|
if (x >= gridX+(cellW+gapX)*gx+cellW || y >= gridY+(cellH+gapY)*gy+cellH) {
|
|
return null;
|
|
}
|
|
return {
|
|
x: Math.floor((x-gridX)/(cellW+gapX)),
|
|
y: Math.floor((y-gridY)/(cellH+gapY)),
|
|
}
|
|
}
|
|
|
|
export function reGrid (x: number, y: number, gridX: number, gridY: number, cellW: number, cellH: number): {x: number, y: number} {
|
|
const gx = Math.floor((x-gridX)/(cellW));
|
|
const gy = Math.floor((y-gridY)/(cellH));
|
|
return {
|
|
x: gx,
|
|
y: gy,
|
|
}
|
|
}
|
|
|
|
export const outlineRect = (x: number, y: number, w: number, h: number, color: number) => {
|
|
fillRect(x, y, w, 1, color);
|
|
fillRect(x, y, 1, h, color);
|
|
fillRect(x+w-1, y, 1, h, color);
|
|
fillRect(x, y+h-1, w, 1, color);
|
|
}
|
|
|
|
export const drawTransparentRect = (x: number, y: number, w: number, h: number) => {
|
|
Array(w*h).fill(0).map((_z, j) => {
|
|
const jx = j%w;
|
|
const jy = Math.floor(j/w);
|
|
setPixelColor(x+jx, y+jy, (jx+jy)%2 ? COLOR.BLACK : COLOR.DARKGRAY);
|
|
})
|
|
}
|
|
|
|
export const drawVoidRect = (x: number, y: number, w: number, h: number) => {
|
|
Array(w*h).fill(0).map((_z, j) => {
|
|
const jx = j%w;
|
|
const jy = Math.floor(j/w);
|
|
setPixelColor(x+jx, y+jy, (jx+jy)%2 ? COLOR.BLACK : COLOR.DARKERBLUE);
|
|
})
|
|
}
|
|
|
|
export const subgrid = <T>(array: Array<T>, gridW: number, x: number, y: number, w: number, h: number): Array<T|undefined> => {
|
|
return Array(h).fill(0).flatMap((_, i) => {
|
|
if (y+i < 0 || y+i > array.length/gridW) {
|
|
return Array(w).fill(undefined);
|
|
}
|
|
const x0 = Math.max(0, x);
|
|
const x1 = Math.min(x+w, gridW);
|
|
const start = (y+i)*gridW+x0;
|
|
const end = (y+i)*gridW+x1;
|
|
const before = Array(x0 - x).fill(undefined);
|
|
const after = Array((x+w) - x1).fill(undefined);
|
|
const middle = array.slice(start, end);
|
|
return [...before, ...middle, ...after];
|
|
})
|
|
}
|
|
|
|
export const LinearGrid = <T>(array: Array<T>, gridW: number) => {
|
|
return {
|
|
get(x: number, y: number) {
|
|
return array[this.coordsToIndex(x, y)]
|
|
},
|
|
getIndex(i: number) {
|
|
return array[i];
|
|
},
|
|
set(x: number, y: number, value: T) {
|
|
array[this.coordsToIndex(x, y)] = value;
|
|
},
|
|
setIndex(i: number, value: T) {
|
|
array[i] = value;
|
|
},
|
|
values: array,
|
|
indexToCoords: (i: number) => {
|
|
return [i%gridW, Math.floor(i/gridW)];
|
|
},
|
|
coordsToIndex: (x: number, y: number) => {
|
|
return y*gridW+x;
|
|
},
|
|
// TODO: make alterations to subgrid affect parent grid
|
|
subgrid: (x: number, y: number, w: number, h: number) => LinearGrid(subgrid(array, gridW, x, y, w, h), w),
|
|
}
|
|
}
|
|
|