commit:dad934f94cba76d349e3f71f4be45d7cf13666a4
author:Chip Black
committer:Chip Black
date:Mon Oct 22 00:48:17 2018 -0500
parents:e2812563e23bbe26d95e8eb4d4bec1b9dc95965e
Remove player object from map
diff --git a/src/map.ts b/src/map.ts
line changes: +9/-18
index e35b4ec..5d5412c
--- a/src/map.ts
+++ b/src/map.ts
@@ -23,7 +23,7 @@ export default class GameMap extends Storable {
     width: number
     height: number
     cells: Cell[][]
-    player: GameObject
+    playerLocation: [number, number]
     saveProperties = ['id', 'width', 'height']
 
     constructor() {
@@ -46,13 +46,12 @@ export default class GameMap extends Storable {
         }
 
         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) {
@@ -75,31 +74,29 @@ export default class GameMap extends Storable {
         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);
     }
@@ -130,7 +127,6 @@ export default class GameMap extends Storable {
 
     load(obj: GameMapProperties) {
         super.load(obj);
-        this.player = new GameObject(GameObjectType.Player);
         this.build();
 
         for (let c of obj.cells) {
@@ -139,11 +135,6 @@ export default class GameMap extends Storable {
             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();

diff --git a/src/mapview.ts b/src/mapview.ts
line changes: +2/-2
index c21ca43..a1d418a
--- a/src/mapview.ts
+++ b/src/mapview.ts
@@ -42,7 +42,7 @@ export default class MapView {
 */
 
     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++) {
@@ -53,7 +53,7 @@ export default class MapView {
     }
 
     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)