Skip to content

Instantly share code, notes, and snippets.

@Shigetorum635
Last active October 15, 2021 17:42
Show Gist options
  • Save Shigetorum635/d08588da798763424237ef4b801dbab4 to your computer and use it in GitHub Desktop.
Save Shigetorum635/d08588da798763424237ef4b801dbab4 to your computer and use it in GitHub Desktop.
Array Grid for Node-Hill.
class Grid {
constructor(width, height, cellSize, baseLine){
this.height = height;
this.width = width;
this.cellSize = cellSize;
this.baseLine = baseLine;
this.gridArray = this.generateArray(width, height);
for (let x = 0; x < this.gridArray[0].length; x++) {
for (let y = 0; y < this.gridArray[1].length; y++) {
// Generates a block per array value
let brick = new Brick(this.getWorldPosition(x, y), new Vector3(cellSize, cellSize, cellSize), '#fffff00');
brick.visibility = 1;
Game.newBrick(brick);
brick.touching((player) => this.debugFunction(brick))
}
}
}
debugFunction = function (brick){
Game.topPrintAll(`Brick real Position#X: ${brick.position.x} Y: ${brick.position.y}`, 1);
console.log(brick.position)
let WorldPos = this.getXY(brick.position)
let GridXY = this.getXY(WorldPos)
Game.bottomPrintAll(`Grid Position#X: ${GridXY.x} Y: ${GridXY.y}`)
}
generateArray = function (rows, columns, value) {
if (value === void 0) { value = function (x, y) { return 0; }; }
let array = new Array(rows);
for (let i = 0; i < rows; i++) {
array[i] = new Array(columns);
for (let j = 0; j < columns; j++) {
array[i][j] = value(i, j);
}
}
return array;
};
displayArray = function () {
console.table(this.gridArray);
};
setValue = function (x, y, value) {
if (x >= 0 && y >= 0 && x < this.width && y < this.height)
this.gridArray[x][y] = value;
};
setWorldValue = function (worldPosition, value) {
let _a = this.getXY(worldPosition), x = _a.x, y = _a.y;
this.setValue(x, y, value);
};
getWorldPosition = function (x, y) {
return new Vector3(x * this.cellSize, y * this.cellSize, this.baseLine);
};
getXY = function (worldPosition) {
let xPos = Math.floor(worldPosition.x / this.cellSize);
let yPos = Math.floor(worldPosition.y / this.cellSize);
return { x: xPos, y: yPos };
};
}
module.exports = Grid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment