Skip to content

Commit

Permalink
add hexagon type hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jan 10, 2015
1 parent 9e09a5c commit 5678b68
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 29 deletions.
39 changes: 35 additions & 4 deletions www/js/catan/js/hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,46 @@

// 2 3 4 5 6 7 8 9 10 11 12
// 1 2 3 4 5 6 5 4 3 2 1
Catan.Hexagon = function (land, pos) {
this.land = land;
Catan.Hexagon = function (pos, land) {
this.position = pos;
this.number = undefined;
this.circle = undefined;
this.land = land;
};

Catan.Hexagon.prototype.isEmpty = function () {
return this.land == Catan.T.Empty;
};
Catan.Hexagon.prototype.isCoast = function () {
return false;
};
Catan.Hexagon.prototype.isHarbor = function () {
return false;
};
Catan.Hexagon.prototype.isLand = function () {
return false;
};


Catan.Hexagon.Land = function (pos, land) {
Catan.Hexagon.call(this, pos, land);
this.number = undefined;
};
Catan.Hexagon.Land.prototype = new Catan.Hexagon();

Catan.Hexagon.Land.prototype.isLand = function () {
return false;
};


Catan.Hexagon.Coast = function (pos, land) {
Catan.Hexagon.call(this, pos, land);
};
Catan.Hexagon.Coast.prototype = new Catan.Hexagon();

Catan.Hexagon.prototype.isCoast = function () {
return true;
};
Catan.Hexagon.Coast.prototype.isHarbor = function () {
return this.land !== Catan.T.Empty;
};

})(Catan);
73 changes: 48 additions & 25 deletions www/js/catan/js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,48 @@
"use strict";

Catan.Map = function (height, width, center) {
// @todo: dynamically process the coast coordinates based on map topology
this.coastOrderedCoordinates = [
[center.column - 1, center.line - 3],
[center.column, center.line - 3],
[center.column + 1, center.line - 3],
[center.column + 2, center.line - 3],

[center.column + 2, center.line - 2],
[center.column + 3, center.line - 1],
[center.column + 3, center.line],
[center.column + 3, center.line + 1],
[center.column + 2, center.line + 2],

[center.column + 2, center.line + 3],
[center.column + 1, center.line + 3],
[center.column , center.line + 3],
[center.column - 1, center.line + 3],

[center.column - 2, center.line + 2],
[center.column - 2, center.line + 1],
[center.column - 3, center.line],
[center.column - 2, center.line - 1],
[center.column - 2, center.line - 2]
];
// Make these coordinates immutable
Object.freeze(this.coastOrderedCoordinates);

this.board = [];
for (var i = 0; i < width; i++) {
this.board[i] = [];
for (var j = 0; j < height; j++) {
this.board[i][j] = new Catan.Hexagon(Catan.T.Empty, new Catan.Position(i, j));
var hexagon, land = Catan.T.Empty, position = new Catan.Position(i, j);
if (this.isCoastPosition(i, j)) {
hexagon = new Catan.Hexagon.Coast(position, land);
} else {
hexagon = new Catan.Hexagon.Land(position, land);
}
this.board[i][j] = hexagon;
}
}
this.center = center;

};


Expand All @@ -26,43 +60,32 @@
callback(this.center.column - 2, this.center.line);
};


Catan.Map.prototype.eachCoast = function (callback) {
for (var col = this.center.column - 1; col <= this.center.column + 2; col++) {
callback(col, this.center.line - 3);
}
var orderedCoordinates = [
[this.center.column + 2, this.center.line - 2],
[this.center.column + 3, this.center.line - 1],
[this.center.column + 3, this.center.line],
[this.center.column + 3, this.center.line + 1],
[this.center.column + 2, this.center.line + 2]
];
for (var i = 0; i < orderedCoordinates.length; i++) {
callback(orderedCoordinates[i][0], orderedCoordinates[i][1]);
}
for (col = this.center.column + 2; col >= this.center.column - 1; col--) {
callback(col, this.center.line + 3);
for (var i = 0; i < this.coastOrderedCoordinates.length; i++) {
callback(this.coastOrderedCoordinates[i][0], this.coastOrderedCoordinates[i][1]);
}
};

orderedCoordinates = [
[this.center.column - 2, this.center.line + 2],
[this.center.column - 2, this.center.line + 1],
[this.center.column - 3, this.center.line],
[this.center.column - 2, this.center.line - 1],
[this.center.column - 2, this.center.line - 2]
];
for (i = 0; i < orderedCoordinates.length; i++) {
callback(orderedCoordinates[i][0], orderedCoordinates[i][1]);

Catan.Map.prototype.isCoastPosition = function (column, line) {
for (var i = 0; i < this.coastOrderedCoordinates.length; i++) {
if (this.coastOrderedCoordinates[i][0] === column && this.coastOrderedCoordinates[i][1] === line) {
return true;
}
}
return false;
};


Catan.Map.prototype.get = function (column, line) {
if (this.board[column] === undefined) {
return undefined;
}
return this.board[column][line];
};


Catan.Map.prototype.eachNeighbour = function (col, line, callback) {
var orderedNeighbourCoordinates =
(0 === line % 2) ?
Expand Down

0 comments on commit 5678b68

Please sign in to comment.