From e82e8c859d848d9b89fa6c184614110306d76605 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sat, 31 Jan 2015 16:12:36 +0000 Subject: [PATCH] Initial version source code --- index.html | 25 ++++++ js/game.js | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++ js/player.js | 0 server.js | 95 ++++++++++++++++++++ 4 files changed, 359 insertions(+) create mode 100644 index.html create mode 100644 js/game.js create mode 100644 js/player.js create mode 100644 server.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..4d89169 --- /dev/null +++ b/index.html @@ -0,0 +1,25 @@ + + + + + MyGame + + + + + + + + +WTFPL + + + \ No newline at end of file diff --git a/js/game.js b/js/game.js new file mode 100644 index 0000000..8f45c20 --- /dev/null +++ b/js/game.js @@ -0,0 +1,239 @@ +// 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(600, 400, + 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'); + this.sprite.frame = 4; + + // 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.jpg"); + + // Load the dude + game.load.spritesheet('dude', 'assets/dude.png', 32, 48); +} + +// 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(); + +} \ No newline at end of file diff --git a/js/player.js b/js/player.js new file mode 100644 index 0000000..e69de29 diff --git a/server.js b/server.js new file mode 100644 index 0000000..34e79a9 --- /dev/null +++ b/server.js @@ -0,0 +1,95 @@ +// Setup: +// npm install express +// npm install engine.io +// npm install eureca.io + +// Pull in express +var express = require('express'); + +// Our app and server objects +var app = express(app); +var server = require('http').createServer(app); + +// serve static files from the current directory +app.use(express.static(__dirname)); + +// get a eureca server +var EurecaServer = require('eureca.io').EurecaServer; + +//create an instance of EurecaServer +var eurecaServer = new EurecaServer({ + // Allow out communication functions + allow: ['handshake', 'spawnPlayer', 'kill', 'updateState'] +}); + +// attach eurica to our server +eurecaServer.attach(server); + +// Our list of clients +var clients = {}; + +// When a client connects +eurecaServer.onConnect(function(conn) { + + console.log('Connection %s from ', conn.id, conn.remoteAddress); + + // Grab the client proxy to call functions easily + var remote = eurecaServer.getClient(conn.id); + + // Handshake with the client sending it it's new ID + remote.handshake(conn.id); + + // Inform the client of all other players to spawn + for (var c in clients) { + var x = clients[c].state ? clients[c].state.x : 0; + var y = clients[c].state ? clients[c].state.y : 0; + + remote.spawnPlayer(clients[c].id, x, y); + } + + // Inform other clients to spawn this player + for (var c in clients) { + var x = remote.state ? remote.state.x : 0; + var y = remote.state ? remote.state.y : 0; + + clients[c].remote.spawnPlayer(conn.id, x, y); + } + + // Add the client to our list + clients[conn.id] = { + id: conn.id, + remote: remote, + } +}); + +// When a client disconnects +eurecaServer.onDisconnect(function(conn) { + + console.log('Disconnection ', conn.id); + + // delete this client + delete clients[conn.id]; + + // Notify other clients of disconnection + for (var c in clients) { + clients[c].remote.kill(conn.id); + } +}); + +// When a client asks to update it's state +eurecaServer.exports.updateMe = function(state) { + + // Get conncection info + var conn = this.connection; + + // Send new state to all other clients + for (var c in clients) { + clients[c].remote.updateState(conn.id, state); + } + + // store the last known state + clients[conn.id].state = state; +} + +// Listen on port 80 +server.listen(8000); \ No newline at end of file