Skip to content

Instantly share code, notes, and snippets.

@Kuzcoo
Last active December 19, 2016 16:34
Show Gist options
  • Save Kuzcoo/b8d0bd74b04eadfeca2640b2384a69c0 to your computer and use it in GitHub Desktop.
Save Kuzcoo/b8d0bd74b04eadfeca2640b2384a69c0 to your computer and use it in GitHub Desktop.
class Grid {
constructor(w,h,cellSize) {
this.grid = [];
this.cellSize = cellSize;
//
this.createGrid(w,h);
}
createGrid(w,h) {
for (let row = 0; row < w/this.cellSize; row++) {
this.grid.push([]);
for (let col = 0; col < h/this.cellSize; col++) {
this.grid[row].push(null);
}
}
}
getCell(n) {
return Math.ceil(n/this.cellSize);
}
handleSomething(unit) {
}
add(unit) {
let pos = {
x: this.getCell(unit.pos.x),
y: this.getCell(unit.pos.y)
};
if (!this.grid[pos.x][pos.y]) {
return this.grid[pos.x][pos.y] = unit;
}
let current = this.grid[pos.x][pos.y];
while (current.next) {
current = current.next;
}
current.next = unit;
unit.prev = current;
}
}
class Unit {
constructor(grid,x,y) {
this.grid = grid;
this.pos = {x,y};
this.grid.add(this);
}
update() {}
draw() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment