diff --git a/example/game.js b/example/game.js index 37b47f9..acd27db 100644 --- a/example/game.js +++ b/example/game.js @@ -7,14 +7,12 @@ function Game (sockets) { this.physicsInterval = 15; this.physicsDelta = this.physicsInterval / 1000; this.physicsIntervalId = 0; - this.worldState = { playerSize: 40, entitySize: 15 }; this.server = garageServer.createGarageServer(sockets, { logging: true, interpolation: true, clientSidePrediction: true, - worldState: this.worldState, smoothingFactor: 0.2 }); } @@ -39,7 +37,7 @@ Game.prototype.update = function () { var entity = entities[i], newState = gamePhysics.getNewEntityState(entity.state, self.physicsDelta); - if (newState.x < 0 - self.worldState.playerSize || newState.y < 0 - self.worldState.playerSize || newState.x > self.worldState.width || newState.y > self.worldState.height) { + if (newState.x < -200 || newState.y < -200 || newState.x > 2000 || newState.y > 2000) { self.server.removeEntity(entity.id); } else { self.server.updateEntityState(entity.id, newState); diff --git a/example/public/images/entity.png b/example/public/images/entity.png new file mode 100644 index 0000000..85370bd Binary files /dev/null and b/example/public/images/entity.png differ diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index bc37728..d5c03c6 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -2,7 +2,8 @@ $(function () { "use strict"; - var canvas = document.getElementById('gameCanvas'), ctxCanvas = canvas.getContext('2d'), keyboard = new THREEx.KeyboardState(), ships = preloadShips(); + var canvas = document.getElementById('gameCanvas'), ctxCanvas = canvas.getContext('2d'), keyboard = new THREEx.KeyboardState(), ships = preloadShips(), entityImage = new Image(); + entityImage.src = '../images/entity.png'; window.addEventListener('resize', resizeCanvas, false); @@ -30,12 +31,12 @@ $(function () { ctxCanvas.fillStyle = 'white'; var playerStates = GarageServerIO.getPlayerStates(), entityStates = GarageServerIO.getEntityStates(); + entityStates.forEach(function (entity) { + drawRotatedImage(entity.state.ang, entity.state.x, entity.state.y, entityImage); + }); playerStates.forEach(function (player) { drawRotatedImage(player.state.ang, player.state.x, player.state.y, ships[player.state.ship]); }); - entityStates.forEach(function (entity) { - ctxCanvas.fillRect(entity.state.x, entity.state.y, 5, 5); - }); }, //Update Loop function () { diff --git a/example/shared/core.js b/example/shared/core.js index aa4f592..3b45395 100644 --- a/example/shared/core.js +++ b/example/shared/core.js @@ -1,13 +1,14 @@ (function(exports){ exports.getNewPlayerState = function (state, inputs, deltaTime, garageServer) { - var i = 0, distance = 0, notLaunched = false; + var i = 0, distance = 0; if (!state.ang && state.ang !== 0) { state.ang = 0; state.x = 0; state.y = 0; state.ship = Math.floor(Math.random() * 9) + 1; + state.lastFire = new Date().getTime(); } for (i = 0; i < inputs.length; i ++) { @@ -18,11 +19,11 @@ } else if (inputs[i].input === 'up') { distance += (125 * deltaTime); } else if (inputs[i].input === 'space') { - if (garageServer && !notLaunched) { + if (garageServer && (new Date().getTime() - state.lastFire) > 1000) { var newId = guid(); garageServer.addEntity(newId); garageServer.updateEntityState(newId, { x: state.x, y: state.y, ang: state.ang } ); - notLaunched = true; + state.lastFire = new Date().getTime(); } } }