The Ophelia Interface

When we create games with Ophelia, we utilize the Ophelia Interface. To access the interface, we must include the file containing the interface. This is done with Express, like this:

require('../server/ophelia');

Naturally, we will have to adapt this path to where the engine in located relative to our game project.
There are three primary functions to the interface: methods calls, trigger definitions and event callbacks. Methods calls are very straightforward. We invoke a method from the interface that will affect the game state. Trigger definitions we have looked at in the previous post. Event callbacks look like this:

Ophelia.registerCallback('onUpdate', function(data){
	// Code that is invoked with each update
}

Using the above we can put together a file such as this:

var Ophelia = require('../../../../server/ophelia');

/* Objects conrolled by players */
var playerObjects = {};

/* When the game starts */
Ophelia.registerCallback('onGameState', function(data){

	/* Get a list of the players in the game */
	var players = Ophelia.getPlayersInGame();

	/* Assign each player a player object */
	for(var i = 0; i < players.length; i++){
		playerObjects[players[i].id] = Ophelia.createObject('player');
});

/* When a player joins */
Ophelia.registerCallback('onPlayerJoin', function(data){

	var id = data.joiningPlayer.id;

	/* Give the new a player an object */
	playerObjects[id] = Ophelia.createObject('player');
});

/* When a player leaves */
Ophelia.registerCallback('onPlayerLeave', function(data){

	var id = data.leavingPlayer.id;

	/* Remove the leaving player's object */
	Ophelia.removeObject(playerObjects[id]);
});

/* Behavior for player objects */
Ophelia.defineTrigger('onPlayerUpdate', function(data){

	var player = data.updatedObject;

	/* Move the object */
	player.xPosition++;
});
Be Sociable, Share!

Published by

Albert Öhrling

Studies computer science in Skellefteå, at Lulea University of Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *