Some slight refactoring
This commit is contained in:
parent
04f206814c
commit
f8c1cebedb
@ -104,7 +104,7 @@ const faux = {
|
||||
pset: setPixelColor,
|
||||
map: (mapSheet: number, tileX: number, tileY: number, screenX: number, screenY: number, tileW: number, tileH: number) => {
|
||||
const originalSpritesheet = getSpritesheet() ?? 0;
|
||||
getMapSheet(mapSheet).forEach(([sprSheet, spr], i) => {
|
||||
getMapSheet(mapSheet).values.forEach(([sprSheet, spr], i) => {
|
||||
const x = i%64;
|
||||
const y = Math.floor(i/64);
|
||||
if (x >= tileX && y >= tileY && x < tileX + tileW && y < tileY + tileH) {
|
||||
@ -119,19 +119,19 @@ const faux = {
|
||||
if (x < 0 || x >= 64 || y < 0 || y >= 64) {
|
||||
return undefined;
|
||||
}
|
||||
return getMapSheet(mapSheet)[y*64+x][0];
|
||||
return getMapSheet(mapSheet).get(x, y)[0];
|
||||
},
|
||||
mgetspr: (mapSheet: number, x: number, y: number) => {
|
||||
if (x < 0 || x >= 64 || y < 0 || y >= 64) {
|
||||
return undefined;
|
||||
}
|
||||
return getMapSheet(mapSheet)[y*64+x][1];
|
||||
return getMapSheet(mapSheet).get(x, y)[1];
|
||||
},
|
||||
mset: (mapSheet: number, x: number, y: number, sprSheet: number, spr: number) => {
|
||||
if (x < 0 || x >= 64 || y < 0 || y >= 64) {
|
||||
return;
|
||||
}
|
||||
getMapSheet(mapSheet)[y*64+x] = [sprSheet, spr];
|
||||
getMapSheet(mapSheet).set(x, y, [sprSheet, spr]);
|
||||
},
|
||||
// Input
|
||||
[CHAR.UP]: K.ARROW_UP,
|
||||
|
26
maptab.ts
26
maptab.ts
@ -30,18 +30,22 @@ const state = {
|
||||
setSheet(page.activeSheet, "map", val);
|
||||
},
|
||||
setInPatch(i: number, sprsheet: number, sprite: number) {
|
||||
const xx = this.selectedPatch%overviewW;
|
||||
const yy = Math.floor(this.selectedPatch/overviewW);
|
||||
const cell = (yy*patchH+Math.floor(i/patchW))*overviewW*patchW+xx*patchW+i%patchW;
|
||||
this.map[cell] = [sprsheet, sprite];
|
||||
const xx = patchW*(this.selectedPatch%overviewW);
|
||||
const yy = patchH*Math.floor(this.selectedPatch/overviewW);
|
||||
const cellVal = this.map.subgrid(xx, yy, patchW, patchH).values[i];
|
||||
cellVal[0] = sprsheet
|
||||
cellVal[1] = sprite;
|
||||
// const cell = (yy*patchH+Math.floor(i/patchW))*overviewW*patchW+xx*patchW+i%patchW;
|
||||
// this.map[cell] = [sprsheet, sprite];
|
||||
},
|
||||
get patch() {
|
||||
const xx = this.selectedPatch%overviewW;
|
||||
const yy = Math.floor(this.selectedPatch/overviewW);
|
||||
return Array(overviewH).fill(0).flatMap((_, i) => {
|
||||
const start = (yy*patchH+i)*overviewW*patchW+xx*patchW;
|
||||
return this.map.slice(start, start+patchW);
|
||||
})
|
||||
const xx = patchW*(this.selectedPatch%overviewW);
|
||||
const yy = patchH*Math.floor(this.selectedPatch/overviewW);
|
||||
return this.map.subgrid(xx, yy, patchW, patchH);
|
||||
// return Array(overviewH).fill(0).flatMap((_, i) => {
|
||||
// const start = (yy*patchH+i)*overviewW*patchW+xx*patchW;
|
||||
// return this.map.slice(start, start+patchW);
|
||||
// })
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,7 +143,7 @@ const draw = () => {
|
||||
|
||||
// Draw the current patch
|
||||
fillRect(patchX-1, patchY-1, (patchW*spriteW)+2, (patchH*spriteH)+2, COLOR.BLACK);
|
||||
state.patch.forEach(([sprsheet, sprite], i) => {
|
||||
state.patch.values.forEach(([sprsheet, sprite], i) => {
|
||||
const spriteX = patchX+spriteW*(i%patchW);
|
||||
const spriteY = patchY+spriteH*Math.floor(i/patchW);
|
||||
if (getSheet(sprsheet).sheet_type === "spritesheet") {
|
||||
|
3
sheet.ts
3
sheet.ts
@ -1,4 +1,5 @@
|
||||
import { getCart } from "./cart.ts";
|
||||
import { LinearGrid } from "./util.ts";
|
||||
// import { runCode, addToContext } from "./runcode.ts";
|
||||
|
||||
// "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts"
|
||||
@ -50,5 +51,5 @@ export const getMapSheet = (sheet: number) => {
|
||||
if (sheet_type !== "map") {
|
||||
throw "Trying to use a non-map sheet as a map."
|
||||
}
|
||||
return value;
|
||||
return LinearGrid(value, 64);
|
||||
}
|
36
util.ts
36
util.ts
@ -44,4 +44,38 @@ export const drawTransparentRect = (x: number, y: number, w: number, h: number)
|
||||
const jy = Math.floor(j/w);
|
||||
setPixelColor(x+jx, y+jy, (jx+jy)%2 ? COLOR.BLACK : COLOR.DARKGRAY);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const subgrid = <T>(array: Array<T>, gridW: number, x: number, y: number, w: number, h: number): Array<T> => {
|
||||
return Array(h).fill(0).flatMap((_, i) => {
|
||||
const start = (y+i)*gridW+x;
|
||||
return array.slice(start, start+w);
|
||||
})
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user