mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-06-27 16:10:34 +08:00
56 lines
2.1 KiB
JavaScript
56 lines
2.1 KiB
JavaScript
$(function () {
|
|
|
|
"use strict";
|
|
|
|
var canvas = document.getElementById('gameCanvas'), ctxCanvas = canvas.getContext('2d'), keyboard = new THREEx.KeyboardState(),
|
|
playerSize = 0, entitySize = 0;
|
|
|
|
GarageServerIO.initializeGarageServer('http://garageserver_io.jbillmann.c9.io', {
|
|
logging: true,
|
|
onReady: startGame,
|
|
onUpdatePlayerPrediction: GamePhysics.getNewPlayerState,
|
|
onInterpolation: GamePhysics.getInterpolatedState,
|
|
onWorldState: function (state) {
|
|
document.getElementById('gameCanvas').style.width = state.width + 'px';
|
|
document.getElementById('gameCanvas').style.height = state.height + 'px';
|
|
playerSize = state.playerSize;
|
|
entitySize = state.entitySize;
|
|
}
|
|
});
|
|
|
|
function startGame() {
|
|
GameLoop.start(
|
|
//Render Loop
|
|
function () {
|
|
ctxCanvas.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
var playerStates = GarageServerIO.getPlayerStates(),
|
|
entityStates = GarageServerIO.getEntityStates();
|
|
playerStates.forEach(function (player) {
|
|
ctxCanvas.fillRect(player.state.x, player.state.y, playerSize, playerSize);
|
|
});
|
|
entityStates.forEach(function (entity) {
|
|
ctxCanvas.fillRect(entity.state.x, entity.state.y, entitySize, entitySize);
|
|
});
|
|
},
|
|
//Update Loop
|
|
function () {
|
|
if (keyboard.pressed('left')) {
|
|
GarageServerIO.addInput('left');
|
|
}
|
|
if (keyboard.pressed('right')) {
|
|
GarageServerIO.addInput('right');
|
|
}
|
|
if (keyboard.pressed('down')) {
|
|
GarageServerIO.addInput('down');
|
|
}
|
|
if (keyboard.pressed('up')) {
|
|
GarageServerIO.addInput('up');
|
|
}
|
|
if (keyboard.pressed('space')) {
|
|
GarageServerIO.addInput('space');
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}); |