jsonIOmpNode

by sirfizx

HTML

<script src="http://cdn.socket.io/socket.io-1.3.5.js"></script>

JavaScript

/*
	

	PLAYER OBJECT DEFINITION
	                                                                                                                 

*/

var Player = function(playerData){
	this.playerData = playerData;
	/*  The gameName property enables use
	    of the server for multiple games which
	    are blind to each other.
		We can filter on the gameName when
		players data is sent from server.
	*/
	this.playerData.gameName = 'foo';
	/*  Some additional player properties 
	    to give you some ideas.
	*/
	this.playerData.timePlayed = 0;
	this.playerData.score = 0;
	/* 

	   AVATAR

	   Here I use a DIV with a child H4. You may choose any visual
	   representation for your player you like such
	   as a 3D Entity in WebGL.
	*/
	var e = document.createElement("DIV");
	e.style.display = 'inline-block';
	e.style.width = '140px';
	e.style.height = '50px';
	e.style.backgroundColor = 'yellow';
	var n = document.createElement("H4");
	n.innerHTML = this.playerData.name;
	e.appendChild(n);
	this.e = e;
	document.body.appendChild(this.e);


}

Player.prototype ={
	update: function(dt){
     	this.playerData.timePlayed += dt;
     	// raise the score by one in roughly 10% of the loops
     	if(10*Math.random() > 9){
     		this.playerData.score++;
     		this.e.children[0].innerHTML = this.playerData.name + ' : ' + this.playerData.score + ' pts.' ;
     	}
	},
	destroy: function(){
		if(isNode(this.e)){
			document.body.removeChild(this.e);
			this.e = 0;
		}
	}
}

//HELPER FUNCTION
//Returns true if it is a DOM node
function isNode(o){
  return (
    typeof Node === "object" ? o instanceof Node : 
    o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName==="string"
  );
}






/*
	

	ADDITIONAL GLOBAL VARIABLES
	                                                                                                                 

*/

var otherPlayers = [];

var myName;
var myPlayer;





/*
	

	SOCKET.IO
	                                        ...