diff --git a/js/game.js b/js/game.js index 551a90a..c202ffa 100644 --- a/js/game.js +++ b/js/game.js @@ -1,4 +1,29 @@ -// If we're ready +// list of destinations +var destinations = [{ + name: "SOUTHSIDE", + num: 0, +}, { + name: "FRANCE", + num: 0, +}, { + name: "TADS", + num: 0, +}, { + name: "SAVOYS", + num: 0, +}, { + name: "PIZZA", + num: 0, +}, { + name: "PACKED LUNCH", + num: 0, +}, ]; + +var title = "FOODICATOR: RELOADED"; +var titlefont = "40px Arial"; +var destfont = "25px Arial"; + +// to block until ready var ready = false; // My ID @@ -53,7 +78,7 @@ var eurecaClientSetup = function() { // Spawn another player eurecaClient.exports.spawnPlayer = function(i, x, y) { - if (i == myId) return; // do nothing for me + if (i === myId) return; // do nothing for me // log and create the player console.log(i, " connected"); @@ -65,18 +90,30 @@ var eurecaClientSetup = function() { // Update a player state eurecaClient.exports.updateState = function(id, state) { + if (id === myId) return; // do nothing for me + var pl = playerList[id]; // if we have this player update it's pos if (pl) { pl.sprite.x = state.x; pl.sprite.y = state.y; + } else { + // If we don't have it, we probably should so add it + var player = new Player(id, state.x, state.y); + + // add to our list + playerList[i] = player; } + } } +var width = 800; +var height = 540; + // Create the game -var game = new Phaser.Game(800, 540, +var game = new Phaser.Game(width, height, Phaser.AUTO, '', { preload: preload, @@ -86,13 +123,21 @@ var game = new Phaser.Game(800, 540, // get the length of a vector -function length(vec) { - return Math.sqrt((vec.x * vec.x) + (vec.y * vec.y)); +function lengthV(vec) { + return length(vec.x,vec.y); +} +function length(x,y) { + return Math.sqrt((x * x) + (y * y)); +} + +// get the length of a vector +function distance(x1, y1, x2, y2) { + return length( x1 - x2, y1 - y2 ); } // Normalise a vector -function normalise(vec) { - var len = length(vec); +function normaliseV(vec) { + var len = lengthV(vec); vec.x = (vec.x / len); vec.y = (vec.y / len); } @@ -158,8 +203,8 @@ Player.prototype.update = function() { } // Normalise out force - if (length(force) > 0) { - normalise(force) + if (lengthV(force) > 0) { + normaliseV(force) // Limit to pos velocity.x = Math.min(velocity.x + force.x, topSpeed); @@ -177,7 +222,7 @@ Player.prototype.update = function() { } // if we have a speed, then update pos and send info to server - if (length(velocity) > 1) { + if (lengthV(velocity) > 1) { sprite.position.x = sprite.position.x + velocity.x; sprite.position.y = sprite.position.y + velocity.y; @@ -197,6 +242,84 @@ Player.prototype.kill = function() { this.alive = false; } +function createDests() { + var minX = 0; + var maxX = width - minX; + var minY = 150; + var maxY = height; + + var len = destinations.length; + + // we need 3 columns max + var numCols = 3; + var numRows = len / numCols; + + var colsep = ((maxX - minX) * (1 / numCols)); + var rowsep = ((maxY - minY) * (1 / numRows)); + + var num = 0; + var row = 0; + var col = 0; + for (var i = 0; i < len; ++i) { + var dest = destinations[i]; + + var posX = minX + (colsep * col) + (colsep * 0.5); + var posY = minY + (rowsep * row); + + // Create our destination text + var destText = game.add.text(posX, posY, dest.name, { + font: destfont, + fill: "#FFFFFF" + }); + destText.anchor.x = Math.round(destText.width * 0.5) / destText.width; + + // Add to our group + textGroup.add(destText); + dest.text = destText; + + // increment num, rows and cols + num++; + col = num % numCols; + if (col === 0) row++; + } +} + + +function updateDests() { + + var votingRange = 100; + + // Get number of voters + var len = destinations.length + for (var i = 0; i < len; ++i) { + var dest = destinations[i]; + dest.num = 0; + dest.vote = false; + + // For each external player + for (var p in playerList) { + var dist = distance(playerList[p].sprite.x,playerList[p].sprite.y, dest.text.x, dest.text.y); + if (dist < votingRange) dest.num++; + } + + // Add for the current player + var dist = distance(player.sprite.x,player.sprite.y, dest.text.x, dest.text.y); + if (dist < votingRange) + { + dest.num++; + dest.vote = true; + } + } + + + + // Set the texts + for (var i = 0; i < len; ++i) { + var dest = destinations[i]; + dest.text.setText( dest.name + ":" + dest.num); + } +} + // Phaser functions // preload all assets @@ -209,9 +332,6 @@ function preload() { // load the background game.load.image("bg", "assets/bg.png"); - // load the background - game.load.image("options", "assets/options.png"); - // Load the dude game.load.image('dude', 'assets/dude.png', 50, 50); } @@ -223,7 +343,7 @@ function create() { var background = game.add.sprite(0, 0, "bg"); // create our player - player = new Player(myId, game.world.centerX + (25 - Math.random() * 50),game.world.centerY + (25 - Math.random() * 50) ); + player = new Player(myId, game.world.centerX + (25 - Math.random() * 50), game.world.centerY + (25 - Math.random() * 50)); // Get controls controls = game.input.keyboard.createCursorKeys(); @@ -232,11 +352,16 @@ function create() { textGroup = game.add.group(); // Create our title text - var titleText= game.add.text(game.world.centerX,10,"FOODICATOR", {font: "65px Arial", fill: "#FFFFFF" }); + var titleText = game.add.text(game.world.centerX, 20, title, { + font: titlefont, + fill: "#FFFFFF" + }); titleText.anchor.x = Math.round(titleText.width * 0.5) / titleText.width; // Add to our group textGroup.add(titleText); + + createDests(); } // On update @@ -250,4 +375,7 @@ function update() { // Bring the text group to the top game.world.bringToTop(textGroup); + + // Update our destinations + updateDests(); } \ No newline at end of file