Game Development Reference
In-Depth Information
Capturing the flag
We'd like to set off a celebratory fireworks-style display when the player captures
a flag, so if you haven't already done so, go ahead and add the mechanics of
capturing flags. The core logic should be in a function we'll call for each player
in our physics loop:
function checkHasFlag(unit) {
var otherFlag = unit.team === TEAMS.R ? TEAMS.B.flag : TEAMS.R.flag;
if (unit.hasFlag) {
var flag = unit.team === TEAMS.R ? TEAMS.R.flag : TEAMS.B.flag;
if (flag.mesh.visible && isPlayerInCell(flag.row, flag.col)) {
otherFlag.mesh.traverse(function(node) {
node.visible = true;
});
unit.hasFlag = false;
}
}
else if (otherFlag.mesh.visible && isPlayerInCell(otherFlag.row,
otherFlag.col)) {
otherFlag.mesh.traverse(function(node) {
node.visible = false;
});
unit.hasFlag = true;
}
}
If the player has the opponent's flag, we're checking if they're standing on their
own flag so that they can score; if the player doesn't have the flag, we're checking if
they're standing on the other flag so that they can steal it. When a flag is stolen it is
marked as invisible, and it is marked as visible again when it is returned. (The code
for this is not included in the preceding example, but flags also need to be returned
when a flag carrier dies.)
 
Search WWH ::




Custom Search