private static TerrainSet LoadTerrain(Ruleset ruleset, int index) { using var tileData = Common.LoadBitmapFrom($"Terrain{((index * 2) + 1)}", ruleset.Paths); using var overlayData = Common.LoadBitmapFrom($"Terrain{((index * 2) + 2)}", ruleset.Paths); // Initialize objects var terrain = new TerrainSet(); // Define transparent colors var borderColour = tileData.GetPixel(0, 0); var transparentGray = Color.FromArgb(135, 135, 135); // Define transparent back color (gray) var transparentPink = Color.FromArgb(255, 0, 255); // Define transparent back color (pink) var transparentCyan = Color.FromArgb(0, 255, 255); // Define transparent back color (cyan) tileData.SetTransparent(new Color[] { transparentGray, transparentPink }); overlayData.SetTransparent(new Color[] { transparentGray, transparentPink }); terrain.BaseTiles = Enumerable.Range(0, 11) .Select(i => tileData.Clone(new Rectangle(1, 1 + (33 * i), 64, 32))).ToArray(); var secondSpecial = tileData.GetPixel(262, 0) == borderColour ? 261 : 196; var firstSpecial = secondSpecial - 65; terrain.Specials = new[] { Enumerable.Range(0, 11) .Select(i => tileData.Clone(new Rectangle(firstSpecial, 1 + (33 * i), 64, 32))).ToArray(), Enumerable.Range(0, 11) .Select(i => tileData.Clone(new Rectangle(secondSpecial, 1 + (33 * i), 64, 32))).ToArray() }; // Blank tile terrain.Blank = tileData.Clone(new Rectangle(131, 447, 64, 32)); // 4 small dither tiles var ditherMask = new[] { tileData.Clone(new Rectangle(1, 447, 32, 16)), tileData.Clone(new Rectangle(33, 447, 32, 16)), tileData.Clone(new Rectangle(1, 463, 32, 16)), tileData.Clone(new Rectangle(33, 463, 32, 16)) }; terrain.DitherMask = ditherMask; terrain.DitherMaps = new[] { BuildDitherMaps(ditherMask[0], terrain.BaseTiles, terrain.Blank, 0, 0), BuildDitherMaps(ditherMask[1], terrain.BaseTiles, terrain.Blank, 32, 0), BuildDitherMaps(ditherMask[2], terrain.BaseTiles, terrain.Blank, 0, 16), BuildDitherMaps(ditherMask[3], terrain.BaseTiles, terrain.Blank, 32, 16) }; // Rivers, Forest, Mountains, Hills terrain.River = new Bitmap[16]; terrain.Forest = new Bitmap[16]; terrain.Mountains = new Bitmap[16]; terrain.Hills = new Bitmap[16]; for (var i = 0; i < 16; i++) { terrain.River[i] = overlayData.Clone(new Rectangle((i % 8) + 1 + ((i % 8) * 64), 3 + (i / 8) + ((2 + (i / 8)) * 32), 64, 32)); terrain.Forest[i] = overlayData.Clone(new Rectangle((i % 8) + 1 + ((i % 8) * 64), 5 + (i / 8) + ((4 + (i / 8)) * 32), 64, 32)); terrain.Mountains[i] = overlayData.Clone(new Rectangle((i % 8) + 1 + ((i % 8) * 64), 7 + (i / 8) + ((6 + (i / 8)) * 32), 64, 32)); terrain.Hills[i] = overlayData.Clone(new Rectangle((i % 8) + 1 + ((i % 8) * 64), 9 + (i / 8) + ((8 + (i / 8)) * 32), 64, 32)); } // River mouths terrain.RiverMouth = new Bitmap[4]; for (var i = 0; i < 4; i++) { terrain.RiverMouth[i] = overlayData.Clone(new Rectangle(i + 1 + (i * 64), (11 * 1) + (10 * 32), 64, 32)); } // Coast terrain.Coast = new Bitmap[8, 4]; for (var i = 0; i < 8; i++) { terrain.Coast[i, 0] = overlayData.Clone(new Rectangle((2 * i) + 1 + (2 * i * 32), 429, 32, 16)); // N terrain.Coast[i, 1] = overlayData.Clone(new Rectangle((2 * i) + 1 + (2 * i * 32), 429 + (1 * 1) + (1 * 16), 32, 16)); // S terrain.Coast[i, 2] = overlayData.Clone(new Rectangle((2 * i) + 1 + (2 * i * 32), 429 + (2 * 1) + (2 * 16), 32, 16)); // W terrain.Coast[i, 3] = overlayData.Clone(new Rectangle((2 * (i + 1)) + (((2 * i) + 1) * 32), 429 + (2 * 1) + (2 * 16), 32, 16)); // E } // Road & railroad terrain.ImprovementsMap = new Dictionary <int, ImprovementGraphic>(); var roadGraphics = new ImprovementGraphic { Levels = new Bitmap[2, 9] }; terrain.ImprovementsMap.Add(ImprovementTypes.Road, roadGraphics); for (var i = 0; i < 9; i++) { roadGraphics.Levels[0, i] = tileData.Clone(new Rectangle(i + 1 + (i * 64), 364, 64, 32)); roadGraphics.Levels[1, i] = tileData.Clone(new Rectangle(i + 1 + (i * 64), 397, 64, 32)); } terrain.ImprovementsMap.Add(ImprovementTypes.Irrigation, new ImprovementGraphic { Levels = new[, ] { { tileData.Clone(new Rectangle(456, 100, 64, 32)) }, { tileData.Clone(new Rectangle(456, 133, 64, 32)) } } }); terrain.ImprovementsMap[ImprovementTypes.Mining] = new ImprovementGraphic { Levels = new[, ] { { tileData.Clone(new Rectangle(456, 166, 64, 32)) } } }; terrain.ImprovementsMap[ImprovementTypes.Pollution] = new ImprovementGraphic { Levels = new[, ] { { tileData.Clone(new Rectangle(456, 199, 64, 32)) } } }; terrain.ImprovementsMap[ImprovementTypes.Fortress] = new ImprovementGraphic { Levels = new[, ] { { MapImages.Specials[1] } } }; // Airbase terrain.ImprovementsMap[ImprovementTypes.Airbase] = new ImprovementGraphic { Levels = new[, ] { { MapImages.Specials[2] } }, UnitLevels = new[, ] { { MapImages.Specials[3] } } }; terrain.GrasslandShield = tileData.Clone(new Rectangle(456, 232, 64, 32)); return(terrain); }
private void Awake() { terrainChunks = new List<TerrainChunk>(); connectors = new List<TerrainChunkConnector>(); currentSet = firstSet; ChooseNextSet(); }
private void IncrementSet() { currentSet = nextSet; ChooseNextSet(); }
private void ChooseNextSet() { TerrainSet newSet; do { newSet = GetRandomSet(); } while (newSet == currentSet); nextSet = newSet; }
public Chunk(FogOfWar2DTest test, Vector3Int chunkpos, TerrainSet[] terrainsets) { _test = test; chunkPosition = chunkpos; Random.InitState(chunkPosition.y * 10000 + chunkPosition.x); // find where walls need to be _walls = new bool[_chunkSize * _chunkSize]; for (int i = 0; i < _walls.Length; ++i) { _walls[i] = true; } _zones = new int[_zoneSize * _zoneSize]; int availablezones = _zones.Length; for (int i = 0; i < _zones.Length; ++i) { _zones[i] = i; int x = i % _zoneSize * 2; int y = i / _zoneSize * 2; _walls[y * _chunkSize + x] = false; } int attempts = 0; while (availablezones > 1 && attempts < 100) { ++attempts; int zoneindex = Random.Range(0, _zones.Length - 1); int zone = _zones[zoneindex]; Vector2Int zonepos = new Vector2Int(zoneindex % _zoneSize, zoneindex / _zoneSize); if (TryMerge(zonepos, new Vector2Int(-1, 0)) || TryMerge(zonepos, new Vector2Int(0, -1)) || TryMerge(zonepos, new Vector2Int(1, 0)) || TryMerge(zonepos, new Vector2Int(0, 1))) { --availablezones; } } // remove walls on chunk edges _walls[Random.Range(0, _zoneSize - 1) * 2 * _chunkSize + _chunkSize - 1] = false; _walls[_chunkSize * (_chunkSize - 1) + Random.Range(0, _zoneSize - 2) * 2] = false; // generate game objects TerrainSet terraintset = terrainsets[Mathf.Abs(chunkPosition.x + chunkPosition.y + chunkPosition.z) % terrainsets.Length]; parent = new GameObject(string.Format("Chunk_{0}_{1}_{2}", chunkPosition.x, chunkPosition.y, chunkPosition.z)).transform; parent.transform.parent = test.transform; float chunkoffset = _chunkSize * 0.5f + 1; parent.position = _test._chunkManager.ChunkToWorldPosition(chunkPosition); for (int y = 0; y < _chunkSize; ++y) { for (int x = 0; x < _chunkSize; ++x) { bool iswall = _walls[y * _chunkSize + x]; GameObject obj = new GameObject(iswall ? "Wall" : "Floor"); obj.transform.SetParent(parent.transform); obj.transform.localPosition = new Vector3(x - _chunkSize / 2, y - _chunkSize / 2, 0); obj.AddComponent <SpriteRenderer>().sprite = iswall ? terraintset.wall : terraintset.ground; if (iswall) { obj.AddComponent <BoxCollider2D>().size = Vector2.one; } } } }