Created
August 3, 2016 00:11
-
-
Save timetocode/90c548d9aad4cfd2df1f6c3b8ebd9b94 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var nengi = require('../../../nengi/nengi') | |
var common = require('../../common') | |
var Renderer = require('../renderer/Renderer') | |
var InputSystem = require('../input/InputSystem') | |
function GameScene(ip, port) { | |
this.ip = ip | |
this.port = port | |
this.renderer = new Renderer() | |
this.input = new InputSystem() | |
// this.inputPrediction = new ClientsidePredictor() | |
} | |
// called by SceneManager | |
GameScene.prototype.enterScene = function() { | |
var self = this | |
this.network = new nengi.Application(common) | |
this.network.shouldEmitGameState = true | |
this.connectAndBeginListeningToNetworkEvents(this.ip, this.port, function() { | |
console.log('connected to server') | |
self.beginListeningToUserInput() | |
}) | |
} | |
GameScene.prototype.leaveScene = function() { | |
// TODO: disconnect from websocket, stop listening to inputs | |
} | |
GameScene.prototype.update = function(delta, tick, now) { | |
this.renderer.update(delta, tick, now) | |
this.network.update() | |
this.input.update() | |
} | |
GameScene.prototype.connectAndBeginListeningToNetworkEvents = function(ip, port, callback) { | |
var self = this | |
this.network.on('connect', callback) | |
this.network.on('message', function(message) { | |
self.handleMessage(message) | |
}) | |
this.network.on('event', function(event) { | |
console.log('event', event) | |
}) | |
this.network.on('createEntity', function(entity) { | |
console.log('createEntity', entity) | |
}) | |
this.network.on('deleteEntity', function(id) { | |
console.log('deleteEntity', id) | |
}) | |
this.network.on('updateEntity', function(id, prop, value) { | |
console.log('updateEntity', id, prop, value) | |
}) | |
// set to 127.0.0.1 for dev, should use real ip in production | |
this.network.connect('127.0.0.1', port) | |
} | |
GameScene.prototype.handleMessage = function(message) { | |
console.log('message', message) | |
switch (message.netSchema.nTypeName) { | |
case 'MapDataMessage': | |
this.renderer.setupMap(message) // real code woo! | |
break | |
case 'IdentityMessage': | |
// note the entity we now control | |
break | |
default: | |
throw 'no handler for ' + message.netSchema.nTypeName | |
} | |
} | |
GameScene.prototype.handleEvent = function(event) {} | |
GameScene.prototype.handleCreateEntity = function(entity) {} | |
GameScene.prototype.handleDeleteEntity = function(id) {} | |
GameScene.prototype.handleUpdateEntity = function(id, prop, value) {} | |
GameScene.prototype.beginListeningToUserInput = function() { | |
this.input.on('mouseDown', function(x ,y) { | |
console.log('mouseDown', x, y) | |
// if (weaponCooldownReady && clickingOnWalls) { | |
// self.network.send(new MineCommand(x,y)) | |
// } else if (weaponCooldownReady) { | |
// self.network.send(new AttackCommand(x,y)) | |
//} | |
// self.inputPrediction.add(whateverCommand) | |
}) | |
this.input.on('mouseMove', function(x ,y) { | |
console.log('mouseMove', x, y) | |
// do cursor stuff for mouse over | |
}) | |
this.input.on('dropItem', function(slotNumber) { | |
console.log('drop item command', slotNumber) | |
// var dropCommand = new DropItem(num) | |
// self.network.send(dropCommand) | |
}) | |
this.input.on('keyState', function(keyState) { | |
console.log('movement keyState', keyState) | |
// var moveCommand = new Move(keyState) | |
// self.network.send(moveCommand) | |
// self.inputPrediction.add(moveCommand) | |
}) | |
} | |
module.exports = GameScene |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment