phaser/Phaser/system/input/Input.ts

91 lines
2.3 KiB
TypeScript
Raw Normal View History

2013-04-12 16:19:56 +00:00
/// <reference path="../../Game.ts" />
/// <reference path="../../Signal.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - Input
*
* A game specific Input manager that looks after the mouse, keyboard and touch objects. This is updated by the core game loop.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
export class Input {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
constructor(game: Game) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.mouse = new Mouse(this._game);
this.keyboard = new Keyboard(this._game);
this.touch = new Touch(this._game);
2013-04-12 16:19:56 +00:00
this.onDown = new Phaser.Signal();
this.onUp = new Phaser.Signal();
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
private _game: Game;
2013-04-18 13:16:18 +00:00
public mouse: Mouse;
public keyboard: Keyboard;
public touch: Touch;
2013-04-18 13:16:18 +00:00
public x: number = 0;
public y: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public scaleX: number = 1;
public scaleY: number = 1;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public worldX: number = 0;
public worldY: number = 0;
public onDown: Phaser.Signal;
public onUp: Phaser.Signal;
2013-04-18 13:16:18 +00:00
public update() {
2013-04-18 13:16:18 +00:00
this.x = Math.round(this.x);
this.y = Math.round(this.y);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.worldX = this._game.camera.worldView.x + this.x;
this.worldY = this._game.camera.worldView.y + this.y;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.mouse.update();
this.touch.update();
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public reset() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.mouse.reset();
this.keyboard.reset();
this.touch.reset();
2013-04-18 13:16:18 +00:00
}
2013-04-18 13:16:18 +00:00
public getWorldX(camera?: Camera = this._game.camera) {
2013-04-18 13:16:18 +00:00
return camera.worldView.x + this.x;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public getWorldY(camera?: Camera = this._game.camera) {
return camera.worldView.y + this.y;
}
public renderDebugInfo(x: number, y: number, color?: string = 'rgb(255,255,255)') {
2013-04-12 16:19:56 +00:00
this._game.stage.context.font = '14px Courier';
2013-04-18 13:16:18 +00:00
this._game.stage.context.fillStyle = color;
this._game.stage.context.fillText('Input', x, y);
this._game.stage.context.fillText('Screen X: ' + this.x + ' Screen Y: ' + this.y, x, y + 14);
this._game.stage.context.fillText('World X: ' + this.worldX + ' World Y: ' + this.worldY, x, y + 28);
this._game.stage.context.fillText('Scale X: ' + this.scaleX.toFixed(1) + ' Scale Y: ' + this.scaleY.toFixed(1), x, y + 42);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}