瀏覽代碼

added mountains to texture atlas, setable map seed, forest generation

Kajetan Johannes Hammerle 5 年之前
父節點
當前提交
4349899f54

二進制
resources/tiles.png


二進制
resources/tiles.xcf


+ 1 - 1
src/pathgame/PathGame.java

@@ -14,7 +14,7 @@ import pathgame.rendering.TileRenderer;
 public class PathGame implements IGame
 {
     private final TileMapRenderer mapRenderer = new TileMapRenderer();
-    private final TileMap map = TileMapGenerator.getMap(50, 50);
+    private final TileMap map = TileMapGenerator.getMap(50, 50, 2);
 
     private final PlayerRenderer playerRenderer = new PlayerRenderer();
     private final Player player = new Player(100, 10);

+ 5 - 4
src/pathgame/tilemap/HighMap.java

@@ -4,9 +4,9 @@ import java.util.Random;
 
 public class HighMap
 {
-    public static HighMap generate(int width, int height, float zoom)
+    public static HighMap generate(long seed, int width, int height, float zoom)
     {
-        HighMap map = new HighMap(width, height);
+        HighMap map = new HighMap(seed, width, height);
         map.zoom(zoom);
         return map;
     }
@@ -14,10 +14,11 @@ public class HighMap
     private final int width;
     private final int height;
     private final float data[][];
-    private final Random r = new Random(0);
+    private final Random r;
     
-    private HighMap(int width, int height)
+    private HighMap(long seed, int width, int height)
     {
+        r = new Random(seed);
         this.width = width;
         this.height = height;
         this.data = new float[width][height];

+ 20 - 1
src/pathgame/tilemap/Tile.java

@@ -37,14 +37,24 @@ public class Tile
     
     private final int id;
     private final int energyCost;
+    private final float forestReplaceChance;
     
     /** Creates a new tile, which is automatically registered.
      *
      */
-    public Tile(int energyCost)
+    public Tile(int energyCost, float forestReplaceChance)
     {
         id = addTile(this);
         this.energyCost = energyCost;
+        this.forestReplaceChance = forestReplaceChance;
+    }
+    
+    /** Creates a new tile, which is automatically registered.
+     *
+     */
+    public Tile(int energyCost)
+    {
+        this(energyCost, 1.0f);
     }
 
     /** Returns the id of the tile.
@@ -60,4 +70,13 @@ public class Tile
     {
         return energyCost;
     }
+
+    /** Returns the chance that this tile is replaced by forest.
+     *
+     * @return the chance that this tile is replaced by forest
+     */
+    public float getForestReplaceChance()
+    {
+        return forestReplaceChance;
+    } 
 }

+ 47 - 6
src/pathgame/tilemap/TileMapGenerator.java

@@ -24,11 +24,11 @@ public class TileMapGenerator
      * @param height the height of the map
      * @return a random test map
      */
-    public static TileMap getMap(int width, int height)
+    public static TileMap getMap(int width, int height, long seed)
     {
-        Random r = new Random();
+        Random r = new Random(seed);
         
-        HighMap highMap = HighMap.generate(width, height, Math.min(width, height) * 0.125f);
+        HighMap highMap = HighMap.generate(seed, width, height, Math.min(width, height) * 0.125f);
         
         TileMap map = new TileMap(width, height);
         for(int x = 0; x < width; x++)
@@ -38,13 +38,19 @@ public class TileMapGenerator
                 switch((int) (highMap.get(x, y) * 5))
                 {
                     case 0: map.setTile(x, y, Tiles.DEEP_WATER); break;
-                    case 1: map.setTile(x, y, Tiles.SHALLOW_WATER); break;
+                    case 1: map.setTile(x, y, Tiles.SHALLOW_WATER); break; 
                     case 2: map.setTile(x, y, randomGrass(r)); break;
-                    case 3: map.setTile(x, y, Tiles.FOREST); break;
-                    case 4: map.setTile(x, y, Tiles.FOREST); break;
+                    case 3: map.setTile(x, y, Tiles.HILL); break;
+                    case 4: map.setTile(x, y, Tiles.MOUNTAIN); break;
                 }
             }
         }
+        
+        // max forest size: squared 20% of map size
+        int forestSize = ((width + height) / 2) / 5;
+        forestSize *= forestSize;
+        
+        generateForest(map, r, forestSize, 10, 1);
         return map;
     }
     
@@ -52,4 +58,39 @@ public class TileMapGenerator
     {
         return GRASS_MAP.floorEntry(r.nextInt(100)).getValue();
     }
+    
+    private static void generateForest(TileMap map, Random r, int depth, int placements, int jumpRadius)
+    {
+        for(int i = 0; i < placements; i++)
+        {
+            int x = r.nextInt(map.getWidth());
+            int y = r.nextInt(map.getHeight());
+            while(map.getTile(x, y).getForestReplaceChance() < 1.0f)
+            {
+                x = r.nextInt(map.getWidth());
+                y = r.nextInt(map.getHeight());
+            }
+            
+            for(int j = 0; j < depth; j++)
+            {
+                int oldX = x;
+                int oldY = y;
+
+                x += r.nextInt(jumpRadius * 2 + 1) - jumpRadius;
+                y += r.nextInt(jumpRadius * 2 + 1) - jumpRadius;
+                x = Math.min(Math.max(x, 0), map.getWidth() - 1);
+                y = Math.min(Math.max(y, 0), map.getHeight() - 1);
+                
+                if(r.nextFloat() < map.getTile(x, y).getForestReplaceChance())
+                {
+                    map.setTile(x, y, Tiles.FOREST);
+                }
+                else
+                {
+                    x = oldX;
+                    y = oldY;
+                }
+            }
+        }
+    }
 }

+ 4 - 4
src/pathgame/tilemap/Tiles.java

@@ -17,8 +17,8 @@ public class Tiles
     };
     
     public final static Tile FOREST = new Tile(2);
-    public final static Tile SHALLOW_WATER = new Tile(3);
-    public final static Tile DEEP_WATER = new Tile(5);
-    public final static Tile HILL = new Tile(3);
-    public final static Tile MOUNTAIN = new Tile(5);
+    public final static Tile SHALLOW_WATER = new Tile(3, 0.0f);
+    public final static Tile DEEP_WATER = new Tile(5, 0.0f);
+    public final static Tile HILL = new Tile(3, 0.5f);
+    public final static Tile MOUNTAIN = new Tile(5, 0.0f);
 }