Created
July 17, 2013 19:38
-
-
Save Buildstarted/6023752 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
| export class Client { | |
| static Events = { | |
| Initialized: 'client.events.initialized', | |
| PlayerJoined: 'client.events.playerJoined', | |
| PlayerUpdate: 'client.events.playerUpdate', | |
| PlayerLocation: 'client.events.playerLocation', | |
| PlayerMessage: 'client.events.playerMessage', | |
| PlayerLeave: 'client.events.playerLeave', | |
| ChunkLoaded: 'client.events.chunkLoaded', | |
| ChunkUpdated: 'client.events.chunkUpdated', | |
| }; | |
| private _connection: HubConnection; | |
| private _server: HubProxy; | |
| constructor() { } | |
| public Init(): JQueryDeferred { | |
| var self: Client = this; | |
| var def = $.Deferred(); | |
| this._connection = $.hubConnection(); | |
| this._server = this._connection.createHubProxy("server"); | |
| this.InitializeEvents(); | |
| this._connection.start().done(() => { | |
| //initialize | |
| self.Initialize(); | |
| def.resolve(); | |
| }); | |
| return def; | |
| } | |
| public MoveTo(vector: eg.Vector2d): void { | |
| var self: Client = this; | |
| this._server.invoke("moveTo", vector.X, vector.Y); | |
| } | |
| public SendMessage(text: string): void { | |
| this._server.invoke("sendMessage", text); | |
| } | |
| private Initialize() { | |
| var self: Client = this; | |
| this._server.invoke("join") | |
| .done(() => { | |
| var chunks = []; | |
| for (var x = -1; x < 2; x++) { | |
| for (var y = -1; y < 2; y++) { | |
| chunks.push(self.LoadChunk(x, y)); | |
| } | |
| } | |
| $.when.apply(null, chunks).done(function () { | |
| $(self).trigger(Client.Events.Initialized); | |
| }); | |
| }); | |
| } | |
| public LoadChunk(x: number, y: number) { | |
| var self = this; | |
| this._server.invoke("loadChunk", x, y); | |
| } | |
| public LeaveChunk(chunk: any) { | |
| this._server.invoke("leaveChunk", chunk.x, chunk.y); | |
| } | |
| private InitializeEvents(): void { | |
| var self: Client = this; | |
| this._server.on("join", (player) => { | |
| $(self).trigger(Client.Events.PlayerJoined, player); | |
| }); | |
| this._server.on("playerMessage", (message) => { | |
| $(self).trigger(Client.Events.PlayerMessage, message); | |
| }); | |
| this._server.on("playerUpdate", (update) => { | |
| $(self).trigger(Client.Events.PlayerUpdate, update); | |
| }); | |
| this._server.on("setLocation", (location) => { | |
| $(self).trigger(Client.Events.PlayerLocation, location); | |
| }); | |
| this._server.on("log", log => { | |
| //console.log(log); | |
| }); | |
| this._server.on("mapLoaded", chunk=> { | |
| $(self).trigger(Client.Events.ChunkLoaded, chunk); | |
| }); | |
| this._server.on("playerLeave", player => { | |
| $(self).trigger(Client.Events.PlayerLeave, player); | |
| }); | |
| this._server.on("updateChunk", chunkUpdate => { | |
| $(self).trigger(Client.Events.ChunkUpdated, chunkUpdate); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment