Commits

Greg Taylor  committed fae305f

Correct some region rendering issues.

  • Participants
  • Parent commits 7ebb374

Comments (0)

Files changed (1)

File lib/src/map_widget/terrain_layer.dart

     _textureAtlas = mapContainer.resourceManager.getTextureAtlas('hexTextures');
 
     // ~/ is truncating division.
-    regionsWide = mapContainer.hexesWide ~/ regionSize;
-    regionsTall = mapContainer.hexesTall ~/ regionSize;
-    print("hexes tall ${mapContainer.hexesTall}");
-    print("regions tall ${regionsTall}");
+    regionsWide = (mapContainer.hexesWide / regionSize).ceil();
+    regionsTall = (mapContainer.hexesTall / regionSize).ceil();
 
     for(var y = 0; y < regionsTall; y++) {
       mapRegions.add([]);
   int regionX;
   int regionY;
   int numHexesWide;
-  int numHexesHigh;
+  int numHexesTall;
 
   double regionStartXPixel;
   double regionStartYPixel;
       TerrainLayerSprite this.terrainLayer, int this.regionX, int this.regionY) {
     mapContainer = terrainLayer.mapContainer;
     numHexesWide = TerrainLayerSprite.regionSize;
-    numHexesHigh = TerrainLayerSprite.regionSize;
+    numHexesTall = TerrainLayerSprite.regionSize;
     regionStartXPixel = regionX * numHexesWide * terrainLayer.hexHorizOffset;
-    regionStartYPixel = regionY * numHexesHigh * terrainLayer.hexPixelHeight;
+    regionStartYPixel = regionY * numHexesTall * terrainLayer.hexPixelHeight;
+
+    // Now clamp the width/height to prevent list overruns.
+    num upperLeftHexX = regionUpperLeftHexX();
+    num upperRightHexX = upperLeftHexX + numHexesWide;
+    if(upperRightHexX > mapContainer.hexesWide) {
+      num diff = upperRightHexX - mapContainer.hexesWide;
+      numHexesWide -= diff;
+    }
+
+    num upperLeftHexY = regionUpperLeftHexY();
+    num lowerRightHexY = upperLeftHexY + numHexesTall;
+    if(lowerRightHexY > mapContainer.hexesTall) {
+      num diff = lowerRightHexY - mapContainer.hexesTall;
+      numHexesTall -= diff;
+    }
   }
 
   int regionUpperLeftHexX() {
-    return regionX * numHexesWide;
+    return regionX * TerrainLayerSprite.regionSize;
   }
 
   int regionUpperLeftHexY() {
-    return regionY * numHexesHigh;
+    return regionY * TerrainLayerSprite.regionSize;
   }
 
   double distanceToRegion(otherRegion) {
 
   void showIfNearRegion(TerrainLayerRegionSprite otherRegion) {
     double dist = distanceToRegion(otherRegion);
-    if(dist < 2.0) {
+    if(dist < 3.0) {
       showRegion();
     } else {
       hideRegion();
   }
 
   void drawAllHexes() {
-    for(var y = 0; y < numHexesHigh; y++) {
+    for(var y = 0; y < numHexesTall; y++) {
       for(var x = 0; x < numHexesWide; x++) {
         int hexX = regionUpperLeftHexX() + x;
         int hexY = regionUpperLeftHexY() + y;