Add winning text
This commit is contained in:
parent
d24c3291db
commit
2a153b7c7c
1 changed files with 43 additions and 11 deletions
48
js/game.js
48
js/game.js
|
@ -15,13 +15,14 @@ var destinations = [{
|
||||||
name: "PIZZA",
|
name: "PIZZA",
|
||||||
num: 0,
|
num: 0,
|
||||||
}, {
|
}, {
|
||||||
name: "PACKED LUNCH",
|
name: "KITCHEN",
|
||||||
num: 0,
|
num: 0,
|
||||||
}, ];
|
}, ];
|
||||||
|
|
||||||
var title = "FOODICATOR: RELOADED";
|
var title = "FOODICATOR: RELOADED";
|
||||||
var titlefont = "40px Arial";
|
var titlefont = "40px Arial";
|
||||||
var destfont = "25px Arial";
|
var destfont = "25px Arial";
|
||||||
|
var winningfont = "30px Arial";
|
||||||
|
|
||||||
// to block until ready
|
// to block until ready
|
||||||
var ready = false;
|
var ready = false;
|
||||||
|
@ -124,15 +125,16 @@ var game = new Phaser.Game(width, height,
|
||||||
|
|
||||||
// get the length of a vector
|
// get the length of a vector
|
||||||
function lengthV(vec) {
|
function lengthV(vec) {
|
||||||
return length(vec.x,vec.y);
|
return length(vec.x, vec.y);
|
||||||
}
|
}
|
||||||
function length(x,y) {
|
|
||||||
|
function length(x, y) {
|
||||||
return Math.sqrt((x * x) + (y * y));
|
return Math.sqrt((x * x) + (y * y));
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the length of a vector
|
// get the length of a vector
|
||||||
function distance(x1, y1, x2, y2) {
|
function distance(x1, y1, x2, y2) {
|
||||||
return length( x1 - x2, y1 - y2 );
|
return length(x1 - x2, y1 - y2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalise a vector
|
// Normalise a vector
|
||||||
|
@ -303,9 +305,8 @@ function updateDests() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add for the current player
|
// Add for the current player
|
||||||
var dist = distance(player.sprite.x,player.sprite.y, dest.text.x, dest.text.y);
|
var dist = distance(player.sprite.x, player.sprite.y, dest.text.x, dest.text.y);
|
||||||
if (dist < votingRange)
|
if (dist < votingRange) {
|
||||||
{
|
|
||||||
dest.num++;
|
dest.num++;
|
||||||
dest.vote = true;
|
dest.vote = true;
|
||||||
}
|
}
|
||||||
|
@ -314,10 +315,35 @@ function updateDests() {
|
||||||
|
|
||||||
|
|
||||||
// Set the texts
|
// Set the texts
|
||||||
|
var winners = [];
|
||||||
for (var i = 0; i < len; ++i) {
|
for (var i = 0; i < len; ++i) {
|
||||||
var dest = destinations[i];
|
var dest = destinations[i];
|
||||||
dest.text.setText( dest.name + ":" + dest.num);
|
dest.text.setText(dest.name + " HAS " + dest.num);
|
||||||
|
|
||||||
|
// add to the array
|
||||||
|
if ((winners.length === 0) || (dest.num == winners[0].num)) {
|
||||||
|
winners.push(dest);
|
||||||
|
} else if ((dest.num > winners[0].num)) // clear array and add
|
||||||
|
{
|
||||||
|
winners = [];
|
||||||
|
winners.push(dest);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wintext;
|
||||||
|
if (winners.length === 0) {
|
||||||
|
wintext = "NO WINNER";
|
||||||
|
} else if (winners.length === 1) {
|
||||||
|
wintext = winners[0].name + " IS WINNINGS";
|
||||||
|
} else {
|
||||||
|
wintext = "DRAW BETWEEN " + winners[0].name;
|
||||||
|
var left = winners.length;
|
||||||
|
for (var i = 1; i < left; ++i) {
|
||||||
|
wintext += " AND " + winners[i].name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
winningText.setText(wintext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Phaser functions
|
// Phaser functions
|
||||||
|
@ -362,6 +388,12 @@ function create() {
|
||||||
textGroup.add(titleText);
|
textGroup.add(titleText);
|
||||||
|
|
||||||
createDests();
|
createDests();
|
||||||
|
|
||||||
|
winningText = game.add.text(game.world.centerX, height - 60, "NO WINNER", {
|
||||||
|
font: winningfont,
|
||||||
|
fill: "#FFFFFF"
|
||||||
|
});
|
||||||
|
winningText.anchor.x = Math.round(winningText.width * 0.5) / winningText.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
// On update
|
// On update
|
||||||
|
|
Loading…
Add table
Reference in a new issue