Remove player object from map
width: number
height: number
cells: Cell[][]
- player: GameObject
+ playerLocation: [number, number]
saveProperties = ['id', 'width', 'height']
constructor() {
}
if (!this.player) {
- this.player = new GameObject(GameObjectType.Player);
- this.cells[0][0].addObject(this.player);
+ this.playerLocation = [0, 0];
}
}
movePlayer(x: number, y: number) {
- const { x: oldX, y: oldY } = this.player;
+ const [ oldX, oldY ] = this.playerLocation;
let rebuild = false;
if (x + 1 > this.width) {
if (rebuild)
this.build();
- this.cells[oldY][oldX].removeObject(this.player);
- this.player.moveTo(x, y);
- this.cells[y][x].addObject(this.player);
+ this.playerLocation = [x, y];
gs.mapView.updateAll();
gs.mapView.updateCoords(x, y);
}
movePlayerRel(dx: number, dy: number) {
- const { x, y } = this.player;
+ const [ x, y ] = this.playerLocation;
this.movePlayer(x + dx, y + dy);
}
getPlayerPosition(): [number, number] {
- const { x, y } = this.player;
- return [x, y];
+ const [ x, y ] = this.playerLocation;
+ return [ x, y ];
}
getCurrentCell(): Cell {
- const { x, y } = this.player;
+ const [ x, y ] = this.playerLocation;
return this.cells[y][x];
}
toggleNavigable() {
- const { x, y } = this.player;
+ const [ x, y ] = this.playerLocation;
this.cells[y][x].navigable = !this.cells[y][x].navigable;
gs.mapView.update(x, y);
}
load(obj: GameMapProperties) {
super.load(obj);
- this.player = new GameObject(GameObjectType.Player);
this.build();
for (let c of obj.cells) {
const p = Array.from(this.cells[y][x].objects).find(
([id, obj]) => obj.type == GameObjectType.Player
);
- if (p) {
- // Special case hack to position the player object
- this.player = p[1];
- this.player.moveTo(x, y);
- }
}
gs.mapView.updateAll();
*/
updateAll() {
- const { x: px, y: py } = gs.map.player;
+ const [ px, py ] = gs.map.getPlayerPosition();
for (let j = py - 4; j <= py + 4; j++) {
for (let i = px - 4; i <= px + 4; i++) {
}
update(x: number, y: number) {
- const { x: px, y: py } = gs.map.player;
+ const [ px, py ] = gs.map.getPlayerPosition();
const cx = x - px + 4;
const cy = y - py + 4;
if (cx < 0 || cy < 0 || cx >= MAP_WIDTH || cx >= MAP_HEIGHT)