sprite: add resolution, optimize matches()

This commit is contained in:
anna 2022-10-26 22:09:22 +02:00
parent 8447313106
commit 446a052832
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 32 additions and 12 deletions

1
dist/palette.json vendored
View file

@ -1,6 +1,7 @@
{ {
"baseurl": "/sprites", "baseurl": "/sprites",
"sections": 4, "sections": 4,
"resolution": 128,
"sprites": [ "sprites": [
{ {
"url": "/01.png", "url": "/01.png",

View file

@ -42,6 +42,8 @@ export interface IPaletteData {
* (as in the four edges of a quadratic tile). * (as in the four edges of a quadratic tile).
*/ */
sections: number, sections: number,
/** horizontal and vertical resolution of all sprites, in pixels */
resolution: number,
/** Array of all tile prototypes. */ /** Array of all tile prototypes. */
sprites: ISpriteData[], sprites: ISpriteData[],
} }
@ -66,19 +68,33 @@ export class Sprite {
.catch(e => console.error(e)); .catch(e => console.error(e));
} }
public matches(ownDir: Direction, other: Sprite): boolean { public matches(ownEdge: Direction, other: Sprite, otherEdge: Direction): boolean {
if (other.sections !== this.sections) { const sections = this.sections;
if (other.sections !== sections) {
return false; return false;
} }
const ownStart = ownDir * this.sections; const getStart = (edge: Direction) => {
let start = edge * sections;
if (edge >= 2) {
start += sections - 1;
}
return start;
};
let ownPos = getStart(ownEdge);
let otherPos = getStart(otherEdge);
const otherDir: Direction = (ownDir + 2) % 4; const ownDir = ownEdge < 2 ? 1 : -1;
const otherStart = otherDir * other.sections; const otherDir = otherEdge < 2 ? 1 : -1;
for (let n = 0; n < sections; n++) {
if (this.edges[ownPos] !== other.edges[otherPos]) {
return false;
}
ownPos += ownDir;
otherPos += otherDir;
}
let ownEdge = this.edges.slice(ownStart, ownStart + this.sections); return true;
let otherEdge = other.edges.slice(otherStart, otherStart + other.sections);
return !ownEdge.some((v, i) => otherEdge[other.sections - i - 1] !== v);
} }
/** /**
@ -126,20 +142,23 @@ export const enum Direction {
export class Palette { export class Palette {
private readonly sections: number; private readonly sections: number;
public readonly sprites: Sprite[]; public readonly sprites: Sprite[];
public readonly resolution: number;
public constructor(proto: IPaletteData) { public constructor(proto: IPaletteData) {
this.sections = proto.sections; this.sections = proto.sections;
const tiles = []; const sprites = [];
for (let tile of proto.sprites) { for (let s of proto.sprites) {
tiles.push(new Sprite(proto.sections, proto.baseurl, tile)); sprites.push(new Sprite(proto.sections, proto.baseurl, s));
} }
this.sprites = tiles; this.sprites = sprites;
this.resolution = proto.resolution;
} }
} }
const PALETTE_SCHEMA: JSONSchema = { const PALETTE_SCHEMA: JSONSchema = {
baseurl: "string", baseurl: "string",
sections: "number", sections: "number",
resolution: "number",
sprites: [{ sprites: [{
url: "string", url: "string",
edges: ["number"], edges: ["number"],