phaser/Phaser/system/input/Input.ts

146 lines
3.2 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;
/**
*
* @type {Mouse}
*/
2013-04-18 13:16:18 +00:00
public mouse: Mouse;
/**
*
* @type {Keyboard}
*/
2013-04-18 13:16:18 +00:00
public keyboard: Keyboard;
/**
*
* @type {Touch}
*/
2013-04-18 13:16:18 +00:00
public touch: Touch;
/**
*
* @type {Number}
*/
2013-04-18 13:16:18 +00:00
public x: number = 0;
/**
*
* @type {Number}
*/
2013-04-18 13:16:18 +00:00
public y: number = 0;
2013-04-12 16:19:56 +00:00
/**
*
* @type {Number}
*/
2013-04-18 13:16:18 +00:00
public scaleX: number = 1;
/**
*
* @type {Number}
*/
2013-04-18 13:16:18 +00:00
public scaleY: number = 1;
2013-04-12 16:19:56 +00:00
/**
*
* @type {Number}
*/
2013-04-18 13:16:18 +00:00
public worldX: number = 0;
/**
*
* @type {Number}
*/
2013-04-18 13:16:18 +00:00
public worldY: number = 0;
/**
*
* @type {Phaser.Signal}
*/
public onDown: Phaser.Signal;
/**
*
* @type {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
}
/**
* @param {Camera} [camera]
*/
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
/**
* @param {Camera} [camera]
*/
2013-04-18 13:16:18 +00:00
public getWorldY(camera?: Camera = this._game.camera) {
return camera.worldView.y + this.y;
}
/**
* @param {Number} x
* @param {Number} y
* @param {String} [color]
*/
2013-04-18 13:16:18 +00:00
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
}