Skip to content

Framework for developing networked action-based games using Node.js and Socket.io

Notifications You must be signed in to change notification settings

natemcdowel/socketObject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  • Framework for developing action-based games using Node.js and Socket.io.
  • Extend any game object with socketObjectEntity.js to pass as a socketObject
  • Host / Slave networking model, hosted from clientid == 0 machine (You'll need to open a port on your router)
  • Uses position interpolation to compensate for latency

TUTORIAL

We'll need to walk through a few steps to set up your game for use with socketObject

  • socketObjectEntity.js

    1. Extend the generic game object class that all in-game objects inherit from. By default, when using MelonJS, this is:

      me.socketObjectEntity = me.ObjectEntity.extend({
    2. Any game objects that inherited from the generic game object class (such as Enemy, Item, Player classes) should extend 'me.socketObjectEntity'. Example:

      var AllEnemyEntity = me.socketObjectEntity.extend({
    3. Replace remove : function() with engine-specific code. Example:

      remove : function() {
       	// Replace with engine-specific code for removing game objects
       	me.game.remove(this);
       },
       
    4. Define the keys and objects that you want to pass in defindSocketObjectStructure : function(). GUID is required. Example:

       defineSocketObjectStructure : function () {
        	this.socketObjectStructure =
       	{
       		GUID: 'GUID',
       		pos: true,
       		vel: true,
       		settings: true,
       		facing: true,
       		currentAnim: true
       	}
       },
       

    The GUID property should be named to the key of the game object. If the game object is "this.id", change to "GUID:'id'"

  • Game objects inheriting from socketObjectEntity.js

    1. In the constructor of the class, to initialize a socketObject:

      • Define:

        this.settings.sendSocket = true;
        this.settings.entityName = _className;

        Example:

        this.settings.entityName = 'SkullEnemyEntity';
          for a class named
        
          
        var SkullEnemyEntity = AllEnemyEntity.extend({
      • At the bottom of the constructor:

          if (clientid == host) {
          	this.socketInit();
          }
        
    2. In the update loop of these objects, wrap all position / velocity generating logic in this condition:

       if (clientid == host) {
       	// Host controls logic for behavior of object
       }
       
    - Place this function before collision is checked and update function returns:
      
    	<pre>this.updateSocketEntity();</pre>
    
    	- Example:
      	   <pre>
      	   // check & update movement
      		this.updateSocketEntity();
      		this.updateMovement();
      		this.parent();
      		return true;
      		</pre>
    
  • socketClient.js

    1. Include socketClient.js in your index.html file before the end of the body tag.

    2. Replace var addObjectToClient = function(serverObject) { , code with engine-specific code for adding game objects.

       	var addObjectToClient = function(serverObject) {
       		// Engine-specific code for adding objects passed from the server to the game
       		var hostedEntity = new window[serverObject.settings.entityName](
       				serverObject.pos.x,
       				serverObject.pos.y,
       				{
       					image: serverObject.settings.image,
       					spritewidth: serverObject.settings.spritewidth,
       					spriteheight: serverObject.settings.spriteheight,
       					GUID: serverObject.GUID
       				}
       		);
       	 	me.game.add(hostedEntity, enemyZ++);
       		me.game.sort();
       	};
       
    3. Replace var findGameObjectByGUID = function(GUID) { code , code with engine-specific code for finding game objects by Id. Example:

      var findGameObjectByGUID = function(GUID) {
       	// Engine-specific code for finding game objects based on GUID
       	return me.game.getEntityByGUID(GUID);
       }

About

Framework for developing networked action-based games using Node.js and Socket.io

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published