Created
March 10, 2016 21:59
-
-
Save timetocode/00395b4f661d1373742a to your computer and use it in GitHub Desktop.
An example nengi client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// nengi client side! | |
var nengi = require('../../nengi/nengi') | |
var common = require('../common') | |
var app = new nengi.Application(common) | |
app.shouldEmitGameState = true | |
// actions | |
var Move = require('./input/Move') | |
var Attack = require('./input/Attack') | |
var InputManager = require('./InputManager') | |
var input = new InputManager() | |
var ExamplePIXIRenderer = require('./ExamplePIXIRenderer') | |
var renderer = new ExamplePIXIRenderer() | |
// connect to the server (see: config.js) | |
app.connect() | |
app.on('message', function(message) { | |
console.log('message', message) | |
}) | |
/* | |
* create the clientside representation of an entity | |
* `entity` is a copy of the serverside entity | |
*/ | |
app.on('createEntity', function(entity) { | |
console.log('createEntity', entity) | |
renderer.createEntity(entity) | |
}) | |
/* | |
* update the clientside representation entity | |
* `id`: the id of the entity that changed | |
* `prop`: the property on the entity that changed | |
* `value`: the new value of that property | |
*/ | |
app.on('updateEntity', function(id, prop, value) { | |
//console.log('updateEntity', id, prop, value) | |
renderer.updateEntity(id, prop, value) | |
}) | |
/* | |
* remove the clientside representation of an entity | |
* `id` is the id of the entity to remove | |
* entities can be removed for many reasons, but commonly | |
* are removed because they left the client's field of view | |
* or were destroyed. | |
*/ | |
app.on('deleteEntity', function(id) { | |
renderer.deleteEntity(id) | |
}) | |
/* | |
* Example of sending input to the server. | |
* mouseDown sends an attack aim at a certain x,y | |
*/ | |
input.on('mouseDown', function(x, y) { | |
//console.log('mouseDown', x, y) | |
var attack = new Attack() | |
attack.x = x | |
attack.y = y | |
app.send(attack) | |
}) | |
/* | |
* The keyState is a sample of W A S D collected every frame, | |
* and emitted by the input manager. | |
*/ | |
input.on('keyState', function(keyState) { | |
//console.log('keyState', keyState) | |
var move = new Move() | |
move.W = keyState.W | |
move.A = keyState.A | |
move.S = keyState.S | |
move.D = keyState.D | |
app.send(move) | |
}) | |
var update = function(delta) { | |
app.update(delta) | |
renderer.update(delta) | |
input.update(delta) | |
} | |
module.exports.update = update |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment