Commits

Greg Taylor committed 7ebb374

Units are moving around and rotating on the map.

  • Participants
  • Parent commits ea0b95f

Comments (0)

Files changed (6)

lib/src/map_widget/map_container.dart

   ResourceManager resourceManager;
   Stage stage;
   UnitStore unitStore;
+  EventBus eventBus;
 
   HexMapKeyboardInputHandler keyboardInputHandler;
   HexMapMouseInputHandler mouseInputHandler;
   UnitLayerSprite unitLayer;
   HexMapDataManager dataManager;
 
-  MapContainer(Stage this.stage, UnitStore this.unitStore) {
+  MapContainer(Stage this.stage, EventBus this.eventBus, UnitStore this.unitStore) {
     // Retrieves and sets the hexElevations/hexTerrain and other instance
     // attribs that rely on remote data.
     dataManager = new HexMapDataManager(this);
     // Renders and manages the terrain layer.
     terrainLayer = new TerrainLayerSprite(this);
-    // Renders and manages units on the map.
-    unitLayer = new UnitLayerSprite(this, unitStore);
   }
 
   /**
         y = pivotY = stage.contentRectangle.height / 2.0;
         keyboardInputHandler = new HexMapKeyboardInputHandler(this);
         mouseInputHandler = new HexMapMouseInputHandler(this);
-        unitLayer.renderTestUnit();
+        // Renders and manages units on the map.
+        unitLayer = new UnitLayerSprite(this, unitStore);
+        unitLayer.addAllFromStore();
         completer.complete();
       });
 

lib/src/map_widget/map_widget.dart

 import 'dart:async' as async;
 import 'dart:convert' as convert;
 import 'package:stagexl/stagexl.dart';
+import 'package:event_bus/event_bus.dart';
 import "package:hexmap/src/javelin/javelin.dart";
 import "package:hexmap/src/unit_store/unit_store.dart";
 

lib/src/map_widget/terrain_layer.dart

     // ~/ is truncating division.
     regionsWide = mapContainer.hexesWide ~/ regionSize;
     regionsTall = mapContainer.hexesTall ~/ regionSize;
+    print("hexes tall ${mapContainer.hexesTall}");
+    print("regions tall ${regionsTall}");
 
     for(var y = 0; y < regionsTall; y++) {
       mapRegions.add([]);
    * in/out based on mouse pointer location. Is this my pivotX/pivotY?
    */
   void modScale(Point scalePoint, double scaleAmount) {
-    if(scaleAmount + mapContainer.scaleX > 1.5)
-      return;
+    //if(scaleAmount + mapContainer.scaleX > 1.5)
+    //  return;
     if(scaleAmount + mapContainer.scaleX < 0.6)
       return;
 

lib/src/map_widget/unit_layer.dart

 part of map_widget;
 
 class UnitLayerSprite extends Sprite {
-  MapContainer mapContainer;
-  TextureAtlas _textureAtlas;
   UnitStore unitStore;
+  EventBus eventBus;
+
+  MapContainer mapContainer;
+  TerrainLayerSprite terrainLayer;
 
   Map<String,UnitSprite> _unitSprites;
 
   UnitLayerSprite(MapContainer this.mapContainer, UnitStore this.unitStore) {
+    eventBus = mapContainer.eventBus;
+    terrainLayer = mapContainer.terrainLayer;
     _unitSprites = new Map<String,UnitSprite>();
     x = 0.0;
     y = 0.0;
     this.addTo(mapContainer);
+
+    eventBus.on(unitAddedEvent).listen(addUnit);
+    eventBus.on(unitUpdatedEvent).listen(updateUnit);
   }
 
-  void renderTestUnit() {
-    _textureAtlas = mapContainer.resourceManager.getTextureAtlas('unitTextures');
-    var bitmapData = _textureAtlas.getBitmapData('Anubis');
-    var coords = mapContainer.terrainLayer.getPixelsFromHexCoords(10, 1);
-    var bitmap = new Bitmap(bitmapData)
-    ..x = coords['hexUpperLeftXPixel']
-    ..y = coords['hexUpperLeftYPixel']
-    // Adds the hex to the region Sprite.
-    ..addTo(this);
+  void addAllFromStore() {
+    unitStore.getAllUnits().forEach((unit) {
+      addUnit(unit);
+    });
+  }
+
+  void addUnit(Unit unit) {
+    _unitSprites[unit.contactId] = new UnitSprite(unit, this);
+  }
+
+  void updateUnit(Unit unit) {
+    _unitSprites[unit.contactId].updateFromUnit(unit);
   }
 }
 
 
-class UnitSprite extends Sprite {
+class UnitSprite {
+  UnitLayerSprite unitLayer;
+  TextureAtlas _textureAtlas;
+  Unit unit;
+  Bitmap bitmap;
+
+  UnitSprite(Unit this.unit, UnitLayerSprite this.unitLayer) {
+    _textureAtlas = unitLayer.mapContainer.resourceManager.getTextureAtlas('unitTextures');
+    var bitmapData = _textureAtlas.getBitmapData('Anubis');
+    var coords = unitLayer.terrainLayer.getPixelsFromHexCoords(unit.x_coord, unit.y_coord);
+    bitmap = new Bitmap(bitmapData);
+    bitmap.pivotX = bitmap.width / 2.0;
+    bitmap.pivotY = bitmap.height / 2.0;
+    bitmap.x = coords['hexUpperLeftXPixel'] + bitmap.pivotX;
+    bitmap.y = coords['hexUpperLeftYPixel'] + bitmap.pivotY;
+    bitmap.rotation = unit.heading * (math.PI / 180);
+    bitmap.addTo(unitLayer);
+
+  }
 
+  void updateFromUnit(newUnit) {
+    var coords = unitLayer.terrainLayer.getPixelsFromHexCoords(
+        newUnit.x_coord, newUnit.y_coord);
+    bitmap.x = coords['hexUpperLeftXPixel'] + bitmap.pivotX;
+    bitmap.y = coords['hexUpperLeftYPixel'] + bitmap.pivotY;
+    bitmap.rotation = newUnit.heading * (math.PI / 180);
+  }
 }

lib/src/unit_store/store.dart

     //});
   }
 
+  getAllUnits() {
+    return _units.values;
+  }
+
   void addNewUnit(Unit unit) {
     print("Adding ${unit}");
     _units[unit.contactId] = unit;
   var socket = new html.WebSocket('ws://127.0.0.1:9000', ["wamp"]);
   var client = new BattleSnakeClient(socket, unitStore);
 
-  MapContainer mapContainer = new MapContainer(stage, unitStore);
+  MapContainer mapContainer = new MapContainer(stage, eventBus, unitStore);
   mapContainer.setup()
     .then((resourceManager) {
       stage.addChild(mapContainer);