Files
muuhd/frontend/TrainingCell.js
Kim Ravn Hansen b1d667d7cb vyyy
2025-09-17 15:00:32 +02:00

89 lines
4.1 KiB
JavaScript
Executable File

import { Direction } from "./WfcConstants.js";
/**
* Represents a 3x3 grid of values (sub-cells), that are used as building blocks for procedurally
* generated grids. In reality, only the center value will be included in the outputted WfcGrid;
* the 8 surrounding colors are there to establish which TrainingCells can live next to each other.
*/
export class TrainingCell {
/** @param {Uint8Array} values The 9 sub cells that make up this TrainingCell */
constructor() {
this.values = new Uint8Array(9);
}
/** @returns {string} The actual value of this Trainin gCell is represented by its center value */
get value() {
return this.values[Direction.C];
}
/**
* @param {TrainingCell} other
* @param {number} direction
*
* @returns {boolean}
*/
potentialNeighbours(other, direction) {
// sadly, we're not allowed to be friends with ourselves.
if (this === other) {
console.log("WTF were checking to be friends with ourselves!", this, other, direction);
return false;
}
return (
//
// if they want to live to my east,
// their two westmost columns must match
// my two eastmost columns
(direction == Direction.E &&
// My center col must match their west col
this.values[Direction.N] === other.values[Direction.NW] &&
this.values[Direction.C] === other.values[Direction.W] &&
this.values[Direction.S] === other.values[Direction.SW] &&
// My east col must match their center col
this.values[Direction.NE] === other.values[Direction.N] &&
this.values[Direction.E] === other.values[Direction.C] &&
this.values[Direction.SE] === other.values[Direction.S]) ||
//
// if they want to live to my west,
// their two eastmost columns must match
// my two westmost columns
(direction == Direction.W &&
// My center col must match their east col
this.values[Direction.N] === other.values[Direction.NE] &&
this.values[Direction.C] === other.values[Direction.E] &&
this.values[Direction.S] === other.values[Direction.SE] &&
// My west col must match their center col
this.values[Direction.NW] === other.values[Direction.N] &&
this.values[Direction.W] === other.values[Direction.C] &&
this.values[Direction.SW] === other.values[Direction.S]) ||
//
// if they want to live to my north,
// their two souther rows must match
// my two northern rows
(direction == Direction.N &&
// my middle row must match their south row
this.values[Direction.W] === other.values[Direction.SW] &&
this.values[Direction.C] === other.values[Direction.S] &&
this.values[Direction.E] === other.values[Direction.SE] &&
// my north row must match their middle row
this.values[Direction.NW] === other.values[Direction.W] &&
this.values[Direction.N] === other.values[Direction.C] &&
this.values[Direction.Direction.NE] === other.values[Direction.E]) ||
//
// if they want to live to my south,
// their two northern rows must match
// my two southern rows
(direction == Direction.S &&
// my middle row must match their north row
this.values[Direction.W] === other.values[Direction.NW] &&
this.values[Direction.C] === other.values[Direction.N] &&
this.values[Direction.E] === other.values[Direction.SE] &&
// my south row must match their middle row
this.values[Direction.SW] === other.values[Direction.W] &&
this.values[Direction.S] === other.values[Direction.C] &&
this.values[Direction.SE] === other.values[Direction.E])
);
}
}