Last active
April 14, 2016 16:51
-
-
Save syamn/fb34097434ddbc3ffd0d86b28a54c019 to your computer and use it in GitHub Desktop.
TypeScript gaming
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
import * as EventEmitter from 'eventemitter3'; | |
import {GameEvent} from "./game_event"; | |
import {PlayerJoinedEvent} from "./player_joined_event"; | |
export class Manager { | |
private emitter: EventEmitter3.EventEmitter; | |
constructor() { | |
this.emitter = new EventEmitter(); | |
} | |
public example(): void { | |
// Register Listener | |
let func = function(event: PlayerJoinedEvent ){ | |
console.info('Called PlayerJoinedEvent !'); | |
console.info('Player ' + event.getName() + ' has joined the game!'); | |
}; | |
this.addEventListener(SceneChangeEvent, func); | |
// Emit the Event | |
this.emit(new PlayerJoinedEvent('Sakura')); | |
} | |
/** | |
* Register the GameEvent | |
* @param event Class GameEvent what will listen to. | |
* @param listener Callback function it receives GameEvent body as parameter. | |
*/ | |
public addEventListener(event: any, listener: (event: GameEvent) => void ): void { | |
if (!event || !event.EVENT_NAME) { | |
throw new Error('EVENT_NAME not defined in the specified event class'); | |
} | |
this.emitter.addListener(event.EVENT_NAME, listener); | |
console.log('Event registered: ' + event.EVENT_NAME); | |
} | |
/** | |
* Emit the GameEvent | |
* @param event GameEvent Body. | |
*/ | |
public emit(event: GameEvent ): void { | |
this.emitter.emit(event.getEventName(), event); | |
console.log('Event emitted: ' + event.getEventName()); | |
} | |
} |
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
export abstract class GameEvent { | |
public static get EVENT_NAME(): string { return null; } | |
constructor() { } | |
public getEventName(): string { | |
let event: any = <typeof GameEvent>this.constructor; | |
if (!event.EVENT_NAME) { | |
throw new Error('EVENT_NAME not defined in the specified event class'); | |
} | |
return event.EVENT_NAME; | |
} | |
} |
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
import {GameEvent} from './game_event'; | |
export class PlayerJoinedEvent extends GameEvent { | |
public static get EVENT_NAME(): string { return 'player_joined'; } | |
private name: string; | |
public getName(): string { | |
return this.name; | |
} | |
constructor(name: string ) { | |
super(); | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment