Last active
December 19, 2016 16:34
-
-
Save Kuzcoo/b8d0bd74b04eadfeca2640b2384a69c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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