Last active
June 12, 2018 07:25
-
-
Save Ratstail91/dfc6636b5d7337cf5fda61f555d9dfdd to your computer and use it in GitHub Desktop.
My new chunk pager system. Still incomplete, but awesome.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Tilemaps; | |
[RequireComponent(typeof(MapEtcher))] | |
public class GameController : MonoBehaviour { | |
public Tilemap tilemap; //set by the user | |
MapGenerator mapGenerator = new MapGenerator(); | |
MapEtcher mapEtcher; | |
void Start() { | |
//components | |
mapEtcher = GetComponent<MapEtcher> (); | |
mapGenerator.randomSeed = 21795; | |
mapGenerator.Initialize (100, 100, 40, 40); | |
for (int i = -300; i <= 300; i += 100) { | |
for (int j = -300; j <= 300; j += 100) { | |
mapGenerator.Generate (i, j); | |
} | |
} | |
mapEtcher.Etch (mapGenerator, ref tilemap); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Tilemaps; | |
public class MapEtcher : MonoBehaviour { | |
//structures | |
[System.Serializable] | |
public class TileBlockInfo { | |
public MapGenerator.BlockType blockType; | |
public Tile tile; | |
} | |
//public members | |
public TileBlockInfo[] tileInfos; | |
//private members | |
Dictionary<MapGenerator.BlockType, Tile> tileLookup = new Dictionary<MapGenerator.BlockType, Tile> (); | |
void Awake(){ | |
UnityEngine.Assertions.Assert.IsTrue(tileInfos != null, "tileInfos are null"); | |
for (int i = 0; i < tileInfos.Length; i++){ | |
tileLookup[tileInfos[i].blockType] = tileInfos[i].tile; | |
} | |
} | |
public void Etch(MapGenerator mapGenerator, ref Tilemap tilemap) { | |
//TODO: could multithreading help here? | |
foreach(KeyValuePair<Vector2Int, MapGenerator.BlockType[,]> chunkPair in mapGenerator.chunkDictionary) { | |
for (int i = 0; i < mapGenerator.chunkWidth; i++) { | |
for (int j = 0; j < mapGenerator.chunkHeight; j++) { | |
tilemap.SetTile (new Vector3Int (chunkPair.Key.x + i, chunkPair.Key.y + j, 0), tileLookup[ chunkPair.Value[i, j] ]); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Tilemaps; | |
public class MapGenerator { | |
//structures | |
public enum BlockType { | |
EMPTY = 0, GROUND = 1, WATER = 2 | |
} | |
//public members | |
public int randomSeed = 0; | |
public int chunkWidth; | |
public int chunkHeight; | |
public int sampleX; | |
public int sampleY; | |
public Dictionary<Vector2Int, BlockType[,]> chunkDictionary = new Dictionary<Vector2Int, BlockType[,]> (); | |
//methods | |
public void Initialize(int _chunkWidth, int _chunkHeight, int _sampleX, int _sampleY) { | |
chunkWidth = _chunkWidth; | |
chunkHeight = _chunkHeight; | |
sampleX = _sampleX; | |
sampleY = _sampleY; | |
} | |
public void Generate(int posX, int posY) { | |
//NOTE: custom scaling is used to allow interesting effects | |
Clear(posX, posY); | |
//new chunk | |
BlockType[,] chunk = new BlockType[chunkWidth, chunkHeight]; | |
//generate ground | |
for (int i = 0; i < chunkWidth; i++) { | |
for (int j = 0; j < chunkHeight; j++) { | |
float perlin = 0; | |
for (float octaves = 1; octaves <= 4; octaves++) { | |
perlin += Mathf.PerlinNoise ( | |
(float)(posX + i) / (sampleX / octaves) + randomSeed + 99999f + octaves, | |
(float)(posY + j) / (sampleY / octaves) + randomSeed + 99999f + octaves | |
) * 1f/octaves; | |
} | |
float scaledPerlin = Scale(posX + i, posY + j, perlin/3); | |
//determine the block type (geared for our purpose) | |
if (scaledPerlin <= 0.2f) { | |
chunk [i, j] = BlockType.GROUND; | |
} else if (j + posY <= 50) { | |
chunk [i, j] = BlockType.WATER; | |
} | |
} | |
} | |
//finally | |
chunkDictionary.Add(new Vector2Int(posX, posY), chunk); | |
} | |
public void Clear(int x, int y) { | |
Vector2Int key = new Vector2Int (x, y); | |
if (chunkDictionary.ContainsKey (key)) { | |
chunkDictionary.Remove (key); | |
} | |
} | |
public void ClearAll() { | |
chunkDictionary.Clear (); | |
} | |
float Scale(int x, int y, float perlin) { | |
return perlin * y / chunkHeight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment