Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created May 25, 2014 12:06
Show Gist options
  • Save rightfold/292062ccc704dc122c3b to your computer and use it in GitHub Desktop.
Save rightfold/292062ccc704dc122c3b to your computer and use it in GitHub Desktop.
export class Character {
private x: number;
private y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
private moveTo(x, y, isPositionFree: (number, number) => boolean) {
if (isPositionFree(x, y)) {
this.x = x;
this.y = y;
}
}
moveUp(dt: number, isPositionFree: (number, number) => boolean) {
this.moveTo(this.x, this.y - dt / 16, isPositionFree);
}
moveDown(dt: number, isPositionFree: (number, number) => boolean) {
this.moveTo(this.x, this.y + dt / 16, isPositionFree);
}
moveLeft(dt: number, isPositionFree: (number, number) => boolean) {
this.moveTo(this.x - dt / 16, this.y, isPositionFree);
}
moveRight(dt: number, isPositionFree: (number, number) => boolean) {
this.moveTo(this.x + dt / 16, this.y, isPositionFree);
}
step(dt: number) {
}
render(context: CanvasRenderingContext2D) {
context.fillStyle = 'red';
context.fillRect(this.x - 1, this.y - 1, 2, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment