// If we're ready var ready = false; // My ID var myId; // List of other players var playerList = {}; // Set up the eureca server var eurecaServer; // Client setup function var eurecaClientSetup = function() { // create an instance of eureca.io client var eurecaClient = new Eureca.Client(); // Ready function for getting the server eurecaClient.ready(function(proxy) { eurecaServer = proxy; }); // export functions can be called on the server side // Handshake function to hand over ID eurecaClient.exports.handshake = function(id) { //create() is moved here to make sure nothing is created before uniq id assignation myId = id; create(); state = { x: player.sprite.x, y: player.sprite.y } eurecaServer.updateMe(state); ready = true; } // close a connection eurecaClient.exports.kill = function(id) { var pl = playerList[id]; // if we have this player if (pl) { // Kill the player and log pl.kill(); console.log(id, " disconnected ", pl); } } // Spawn another player eurecaClient.exports.spawnPlayer = function(i, x, y) { if (i == myId) return; // do nothing for me // log and create the player console.log(i, " connected"); var player = new Player(i, x, y); // add to our list playerList[i] = player; } // Update a player state eurecaClient.exports.updateState = function(id, state) { 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; } } } // Create the game var game = new Phaser.Game(800, 540, Phaser.AUTO, '', { preload: preload, create: eurecaClientSetup, update: update }); // get the length of a vector function length(vec) { return Math.sqrt((vec.x * vec.x) + (vec.y * vec.y)); } // Normalise a vector function normalise(vec) { var len = length(vec); vec.x = (vec.x / len); vec.y = (vec.y / len); } // Player object Player = function(id, x, y) { // Constants this.id = id; this.topSpeed = 5; this.acceleration = 1.5; this.friction = 0.6; this.alive = false; // varying this.velocity = { x: 0, y: 0 }; // Create a sprite this.sprite = game.add.sprite(x, y, 'dude'); // set alive this.alive = true; } // Update a player Player.prototype.update = function() { // early out if dead if (!this.alive) return; // control force var force = { x: 0, y: 0 } // Grab the members, javascript is weird? var sprite = this.sprite; var velocity = this.velocity; var topSpeed = this.topSpeed; var acceleration = this.acceleration; var friction = this.friction; // Left and right if (controls.left.isDown) { // Move to the left force.x = -acceleration; } else if (controls.right.isDown) { // Move to the right force.x = acceleration; } // Up and down if (controls.up.isDown) { // Move up force.y = -acceleration; } else if (controls.down.isDown) { // Move down force.y = acceleration; } // Normalise out force if (length(force) > 0) { normalise(force) // Limit to pos velocity.x = Math.min(velocity.x + force.x, topSpeed); velocity.y = Math.min(velocity.y + force.y, topSpeed); // Limit to neg velocity.x = Math.max(velocity.x + force.x, -topSpeed); velocity.y = Math.max(velocity.y + force.y, -topSpeed); } else { // if no force, then apply friction velocity.x = velocity.x * friction; velocity.y = velocity.y * friction; } // if we have a speed, then update pos and send info to server if (length(velocity) > 1) { sprite.position.x = sprite.position.x + velocity.x; sprite.position.y = sprite.position.y + velocity.y; var state = { x: sprite.position.x, y: sprite.position.y } eurecaServer.updateMe(state) } } // Kill a player Player.prototype.kill = function() { // The player and its settings this.sprite.kill(); this.alive = false; } // Phaser functions // preload all assets function preload() { // Set centered game.scale.pageAlignHorizontally = true; game.scale.pageAlignVertically = true; // 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); } // Create the game function create() { // Add the background sprite background = game.add.sprite(0, 0, "bg"); // create our player player = new Player(myId, Math.random() * 100, Math.random() * 100); // Get controls controls = game.input.keyboard.createCursorKeys(); } // On update function update() { // do nothing if not ready yet if (!ready) return; // update our player player.update(); }