63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { lerp } from "@common/utils";
|
|
import Item, { ItemType } from "./item";
|
|
import Tile from "./tile";
|
|
|
|
const MOVE_DURATION = .1;
|
|
|
|
export default class Character extends Item {
|
|
public tile = new Tile([0, 0], 1);
|
|
private path: Tile[] = [];
|
|
private pathProgress = 0;
|
|
|
|
get moveBonus() {
|
|
if (this.type === ItemType.CHAR_RUNNER || this.type === ItemType.ENEMY_DOG) return 1;
|
|
if (this.type === ItemType.ENEMY_ZOMBIE) return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
public get displayPosition(): [number, number] {
|
|
if (this.path.length > 1) {
|
|
const numTransitions = this.path.length - 1;
|
|
const progress = this.pathProgress * numTransitions;
|
|
const currentTileIdx = Math.floor(progress);
|
|
|
|
const currentTile = this.path[currentTileIdx];
|
|
|
|
if (!currentTile) {
|
|
return [this.tile.centerX, this.tile.centerY];
|
|
}
|
|
|
|
const nextTile = this.path[currentTileIdx + 1];
|
|
|
|
if (!nextTile) {
|
|
return [currentTile.centerX, currentTile.centerY];
|
|
}
|
|
|
|
const transitionFraction = progress - currentTileIdx;
|
|
|
|
return [
|
|
lerp(currentTile.centerX, nextTile.centerX, transitionFraction),
|
|
lerp(currentTile.centerY, nextTile.centerY, transitionFraction),
|
|
]
|
|
}
|
|
return [this.tile.centerX, this.tile.centerY];
|
|
}
|
|
|
|
public moveTo(tile: Tile, path: Tile[]) {
|
|
this.pathProgress = 0;
|
|
this.tile = tile;
|
|
this.path = path;
|
|
}
|
|
|
|
public update(dt: number) {
|
|
if (this.path.length > 1) {
|
|
if (this.pathProgress >= 1) {
|
|
this.path = [];
|
|
this.pathProgress = 0;
|
|
} else {
|
|
this.pathProgress += dt / (MOVE_DURATION * (this.path.length - 1));
|
|
}
|
|
}
|
|
}
|
|
} |