Skip to content

Commit

Permalink
refactor to remove deprecated Harbor tile type
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jan 10, 2015
1 parent 5678b68 commit cbf4a4f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
5 changes: 1 addition & 4 deletions www/js/catan/js/catan.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ Catan = (function () {
"use strict";

var Catan = {
debug: false,

T: {
Hills: 1,
Pasture: 2,
Expand All @@ -13,8 +11,7 @@ Catan = (function () {
Forest: 5,
Desert: 6,
Ocean: 7,
Harbor: 8,
Empty: 9
Empty: 8
}
};

Expand Down
15 changes: 7 additions & 8 deletions www/js/catan/js/generator/generator.harbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
if (n++ % 2 != mod) {
map.get(i, j).land = Catan.T.Ocean;
} else {
map.get(i, j).land = Catan.T.Harbor;
map.get(i, j).circle = harbors.pop();
map.get(i, j).land = harbors.pop();

if (map.get(i, j).circle != Catan.T.Empty) {
if (map.get(i, j).land != Catan.T.Empty) {
harborCoords.push(new Catan.Position(i, j));
}
}
Expand All @@ -57,17 +56,17 @@
var allowedLands = lands;
allowedLands = map.getAllowedLands(harborCoords[h].column, harborCoords[h].line, allowedLands);

if (!Catan.Tools.contains(allowedLands, currentHarbor.circle)) {
if (!Catan.Tools.contains(allowedLands, currentHarbor.land)) {
// find a place to go
for (var hh = 0; hh < harborCoords.length; hh++) {
allowedLands = lands;
allowedLands = map.getAllowedLands(harborCoords[hh].column, harborCoords[hh].line, allowedLands);
if (Catan.Tools.contains(allowedLands, currentHarbor.circle)) {
if (Catan.Tools.contains(allowedLands, currentHarbor.land)) {
// do swap
var swapHarbor = map.get(harborCoords[hh].column, harborCoords[hh].line);
var cTmp = swapHarbor.circle;
swapHarbor.circle = currentHarbor.circle;
currentHarbor.circle = cTmp;
var cTmp = swapHarbor.land;
swapHarbor.land = currentHarbor.land;
currentHarbor.land = cTmp;
swapped = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions www/js/catan/js/hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
};
Catan.Hexagon.Coast.prototype = new Catan.Hexagon();

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

})(Catan);
47 changes: 36 additions & 11 deletions www/js/catan/js/ui.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(function(Catan){
"use strict";

Catan.UI = {};
Catan.UI = {
// if set to true, displays the coordinates system
debug: false
};

Catan.UI.drawMap = function (map, canvas) {
var ctx = canvas.getContext('2d');
Expand All @@ -24,9 +27,7 @@

for (var i = 0; i < map.board.length; i++) {
for (var j = 0; j < map.board[i].length; j++) {
if (map.get(i, j).land != Catan.T.Empty) {
Catan.UI.drawHexagon(map.get(i, j), ctx, size, dist);
}
Catan.UI.drawHexagon(map.get(i, j), ctx, size, dist);
}
}
};
Expand Down Expand Up @@ -54,13 +55,37 @@
case Catan.T.Desert:
color = "rgb(255, 255, 117)";
break;
case Catan.T.Harbor:
case Catan.T.Ocean:
color = "rgb(69, 91, 217)";
case Catan.T.Empty:
color = "rgb(255, 255, 255)";
break;
default:
console.log("Can't find color for non-coast with this land type", land);
}
return color;
};
var getColorFromCoast = function (land) {
var color;
switch (land) {
case Catan.T.Hills:
color = "rgb(224, 129, 27)";
break;
case Catan.T.Pasture:
color = "rgb(43, 224, 27)";
break;
case Catan.T.Mountains:
color = "rgb(145, 145, 145)";
break;
case Catan.T.Fields:
color = "rgb(229, 255, 0)";
break;
case Catan.T.Forest:
color = "rgb(8, 150, 34)";
break;
case Catan.T.Empty:
color = "rgb(255, 255, 255)";
break;
default:
console.log("Can't find color for coast with this land type", land);
}
return color;
};
Expand All @@ -72,7 +97,7 @@
cx = hexagon.position.column * (width + dist) - ((hexagon.position.line + 1) % 2) * (width + dist) / 2 + width/2 + size.canvasWidth/2 - mapWidth/2,
cy = hexagon.position.line * (3 / 4 * height + dist) + height/2 + size.canvasHeight/2 - mapHeight/2;

ctx.fillStyle = getColorFromLand(hexagon.land);
ctx.fillStyle = hexagon.isCoast() ? "rgb(69, 91, 217)" : getColorFromLand(hexagon.land);
ctx.beginPath();
ctx.moveTo(cx, cy - height / 2);
ctx.lineTo(cx + width / 2, cy - height / 4);
Expand All @@ -92,12 +117,12 @@
ctx.fill();
};

if (hexagon.land == Catan.T.Harbor) {
fillCircle(cx, cy, 11, getColorFromLand(hexagon.circle));
if (hexagon.isHarbor()) {
fillCircle(cx, cy, 11, getColorFromCoast(hexagon.land));
ctx.stroke();
}

if (Catan.debug) {
if (Catan.UI.debug) {
// draw coordinates
ctx.fillStyle = "rgb(0, 0, 0)";
ctx.font = "8pt Arial";
Expand Down

0 comments on commit cbf4a4f

Please sign in to comment.