Skip to content

Commit

Permalink
got unit backgrounds and defaultmessage to trigger properly on turn c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
lmartel committed Sep 5, 2013
1 parent c4991e6 commit 9e15e5c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
77 changes: 47 additions & 30 deletions client.js
Expand Up @@ -35,7 +35,7 @@ if (Meteor.isClient) {
};

Template.message.text = function(){
var msg = Session.get("message");
var msg = getMessage();
// msg = "a really long status message just so I can test exactly how this should be rendered in dat dere sidebar";
if(!msg) return undefined;
msg = msg.split(" ");
Expand Down Expand Up @@ -236,31 +236,21 @@ if (Meteor.isClient) {
}
break;
default:
throw "board has no active game";
throw "can't handle this phase";
}
return false;
}
}

//TODO change to template-style
/**
* Selects the given unit. If the unit is already selected,
* de-selects it.
*/
function toggleUnitSelection(unit){
if(unit.used) return;
var old = getUnit();
var hex = getBoard().get(unit.location);
if(old){
var oldHex = getBoard().get(old.location);
if(old._id === unit._id){
undoBackground(oldHex);
undoAllBackgrounds(CAN_MOVE_TO);
setUnit(undefined);
} else {
undoBackground(oldHex);
undoAllBackgrounds(CAN_MOVE_TO);
temporaryBackground(hex, UNIT_SELECTED);
setUnit(unit);
}
if(old && old._id === unit._id){
setUnit(undefined);
} else {
temporaryBackground(hex, UNIT_SELECTED);
setUnit(unit);
}
}
Expand All @@ -278,9 +268,31 @@ if (Meteor.isClient) {
hex._bgBackup = undefined;
}

function undoAllBackgrounds(color){
/**
* Helper method. Clears any temporary backgrounds that match {colors}.
* @param colors a single css color, or an array of them
*/
function undoAllBackgrounds(colors){
var check;
if(colors.length){
check = function(hex){
for(var i = 0; i < colors.length; i++){
if(hex.fill === colors[i]) return true;
}
return false;
};
} else if(colors){
check = function(hex){
return hex.fill === color;
};
} else {
check = function(){
return true;
}
}

getBoard().forEach(function(hex){
if(!color || hex.fill === color) undoBackground(hex);
if(check(hex)) undoBackground(hex);
});
}

Expand Down Expand Up @@ -373,10 +385,7 @@ if (Meteor.isClient) {
}

Template.movement.rendered = function(){
if(!this.rendered){
defaultMessage();
this.rendered = true;
}
defaultMessage(false);
};

Template.movement.unitsLeft = unusedUnits;
Expand All @@ -394,23 +403,31 @@ if (Meteor.isClient) {
});
return unused;
}

return undefined;
}

Template.movement.range = function(){
var active = getUnit();
Template.movement.unitStatus = function(){
var board = getBoard();
if(!active || !board) return;
var valid = board.get(active.location).getMovementRange(getCard(active).speed);
if(!board) return;

var bgs = [UNIT_SELECTED, CAN_MOVE_TO];
if(notYourTurn()) bgs.push(UNIT_USED);
undoAllBackgrounds(bgs);
var active = getUnit();
if(!active) return;

var start = board.get(active.location);
temporaryBackground(start, UNIT_SELECTED);
var valid = start.getMovementRange(getCard(active).speed);

// Filter list to empty hexes only
valid = valid.reduce(function(array, hex){
if(hex.getPayloadData() === null) array.push(hex);
return array;
}, []);
for(var i = 0; i < valid.length; i++){
var hex = valid[i];
temporaryBackground(hex, CAN_MOVE_TO);
temporaryBackground(valid[i], CAN_MOVE_TO);
}
};

Expand Down
2 changes: 1 addition & 1 deletion client/app.html
Expand Up @@ -134,7 +134,7 @@ <h2>Your turn to move!<br>
</h2>
</div>
<button class="army-ready">Done Moving</button>
{{ range }}
{{ unitStatus }}
</template>
<template name="assault">
<div class="controls-header">
Expand Down
19 changes: 12 additions & 7 deletions client/session.js
Expand Up @@ -204,6 +204,11 @@ function _message(text, flicker){
Session.set("message", text);
}

getMessage = _getMessage;
function _getMessage(){
return Session.get("message");
}

/**
* Triggers a pause in gameplay and a "Wait for turn" message in the following situations:
* 1. The game is still in the DRAFT phase but your army is ready
Expand Down Expand Up @@ -234,26 +239,26 @@ function _defaultMessage(flicker){
switch(getGame().phase){
case Phase.DRAFT:
if(notYourTurn()){
message("Waiting for your opponent to finish drafting.");
message("Waiting for your opponent to finish drafting.", flicker);
} else {
message("Draft phase: choose your units.");
message("Draft phase: choose your units.", flicker);
}
break;
case Phase.DEPLOY:
deployMessage(flicker);
break;
case Phase.MOVEMENT:
if(notYourTurn()){
message("Other player's turn to move.");
message("Other player's turn to move.", flicker);
} else {
message("Your turn to move.");
message("Your turn to move.", flicker);
}
break;
case Phase.ASSAULT:
if(notYourTurn()){
message("Other player's assault phase.");
message("Other player's assault phase.", flicker);
} else {
message("Assault phase: move or declare attacks!");
message("Assault phase: move or declare attacks!", flicker);
}
break;
default:
Expand All @@ -266,7 +271,7 @@ function _defaultMessage(flicker){

var player = Meteor.userId();
if(notYourTurn()){
message("It's the other player's turn to deploy.");
message("It's the other player's turn to deploy.", flicker);
return;
}
var game = getGame();
Expand Down

0 comments on commit 9e15e5c

Please sign in to comment.