hexcraft
ARCHIVED - browser-based 3D hexagonal tile editor built with Three.js
git clone https://git.vogt.world/hexcraft.git
Log | Files | README.md | LICENSE
← All files
name: js/board.js
-rw-r--r--
1683
 1Board = function (width, depth, height) {
 2
 3	this.height = height;
 4  this.width = width;
 5  this.depth = depth;
 6  
 7  this.Board = new Array(width);
 8  for (var x = 0; x < this.Board.length; x++) {
 9    this.Board[x] = new Array(depth);
10    for (var z = 0; z < this.Board[x].length; z++) {
11      this.Board[x][z] = new Array(height);
12      for (var y = 0; y < this.Board[x][z].length; y++) {
13        this.Board[x][z][y] = null;
14      }
15    }
16  }
17  
18  this.getHexagon = function(coordinates) {
19    return this.Board[coordinates.x][coordinates.z][coordinates.y];
20  }
21  
22  this.setHexagon = function(coordinates, color) {
23    this.Board[coordinates.x][coordinates.z][coordinates.y] = color;
24  }
25  
26  this.unsetHexagon = function(coordinates) {
27    this.Board[coordinates.x][coordinates.z][coordinates.y] = null;
28  }
29  
30  this.topMostHexagon = function(coordinates) {
31    for (var i = coordinates.y; i < height; i++) {
32      if (this.getHexagon({x:coordinates.x, z:coordinates.z, y:i}) == null) {
33        return i;
34      }
35    }
36  }
37  
38  this.clear = function() {
39    for (var z = 0; z < this.height; z++) {
40      for (var x = 0; x < this.width; x++) {
41        for (var y = 0; y < this.depth; y++) {
42          this.unsetHexagon({x:x, y:y, z:z});
43        }
44      }
45    }
46  }
47
48  this.getActiveTiles = function() {
49    var active = new Array();
50    var temp;
51    for (var z = 0; z < this.height; z++) {
52      for (var x = 0; x < this.width; x++) {
53        for (var y = 1; y < this.depth; y++) {
54          temp = this.getHexagon({x:x, y:y, z:z});
55          if (temp !== null) {
56            active.push({x:x, y:y, z:z, color: temp});
57          }
58        }
59      }
60    }
61    return active;
62  }
63  
64}