Skip to content

Commit

Permalink
Implemented Cursor object that tracks mouse movement
Browse files Browse the repository at this point in the history
  • Loading branch information
profexorgeek committed Aug 15, 2018
1 parent 8eddfae commit a83733b
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 3 deletions.
26 changes: 25 additions & 1 deletion Engine/dist/frixl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,30 @@ declare namespace Frixl.Entities {
update(delta: number): void;
}
}
declare namespace Frixl.Input {
class Cursor {
private _lastPosition;
private _position;
readonly x: number;
readonly y: number;
readonly changeX: number;
readonly changeY: number;
readonly worldX: number;
readonly worldY: number;
updateLocation(x: number, y: number): void;
}
}
declare namespace Frixl.Input {
class InputHandler {
private _cursor;
private _keysDown;
private _keysPushed;
private _buttonsDown;
private _cursor;
readonly cursor: Cursor;
constructor();
update(delta: number): void;
keyDown(charCode: number): boolean;
private onMouseMove;
private onKeyDown;
private onKeyUp;
}
Expand Down Expand Up @@ -197,6 +212,13 @@ declare namespace Frixl.Input {
SingleQuote = 222
}
}
declare namespace Frixl.Input {
enum MouseButtons {
Left = 1,
Middle = 2,
Right = 3
}
}
declare namespace Frixl.Rendering {
class Camera extends Entities.Positionable {
private _size;
Expand All @@ -206,6 +228,8 @@ declare namespace Frixl.Rendering {
readonly right: number;
readonly top: number;
readonly bottom: number;
readonly width: number;
readonly height: number;
readonly randomVectorInView: Util.Vector;
constructor(width: number, height: number);
}
Expand Down
100 changes: 100 additions & 0 deletions Engine/dist/frixl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Engine/dist/frixl.js.map

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions Engine/src/Input/Cursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Frixl.Input {

export class Cursor {
private _lastPosition: Util.Vector = new Util.Vector();
private _position: Util.Vector = new Util.Vector();

get x(): number {
return this._position.x;
}

get y(): number {
return this._position.y;
}

get changeX(): number {
return this._lastPosition.x - this._position.x;
}

get changeY(): number {
return this._lastPosition.y - this._position.y;
}

get worldX(): number {
return Game.instance.camera.x + this.x;
}

get worldY(): number {
return Game.instance.camera.y + this.y;
}

updateLocation(x: number, y: number) {
this._lastPosition.x = this._position.x;
this._lastPosition.y = this._position.y;
this._position.x = x - (Game.instance.camera.width / 2);
this._position.y = Util.GameUtil.invert(y) + (Game.instance.camera.height / 2);

}
}
}
10 changes: 9 additions & 1 deletion Engine/src/Input/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ namespace Frixl.Input {

export class InputHandler {

private _cursor: Util.Vector;
private _keysDown: any = {};
private _keysPushed: any = {};
private _buttonsDown: any = {};
private _cursor: Cursor = new Cursor();

get cursor(): Cursor {
return this._cursor;
}

constructor() {
window.addEventListener("keydown", this.onKeyDown);
window.addEventListener("keyup", this.onKeyUp);
window.addEventListener("mousemove", this.onMouseMove);
}

update(delta: number) {
Expand All @@ -24,6 +29,9 @@ namespace Frixl.Input {
return this._keysDown[keyName] === true;
}

private onMouseMove = (e: MouseEvent) => {
this._cursor.updateLocation(e.offsetX, e.offsetY);
}

private onKeyDown = (e: KeyboardEvent) => {
let keyName = Input.Keys[e.keyCode];
Expand Down
8 changes: 8 additions & 0 deletions Engine/src/Input/Mouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Frixl.Input {

export enum MouseButtons {
Left = 1,
Middle = 2,
Right = 3
}
}
8 changes: 8 additions & 0 deletions Engine/src/Rendering/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ namespace Frixl.Rendering {
return this._position.y - (this._size.y / 2);
}

get width(): number {
return this._size.x;
}

get height(): number {
return this._size.y;
}

get randomVectorInView(): Util.Vector {
return new Util.Vector(
Util.GameUtil.randomInRange(this.left, this.right),
Expand Down

0 comments on commit a83733b

Please sign in to comment.