//-----------------------------------------------------------------------------------------------------------// /// <summary> /// Generic class for calling an action for all the neighbours /// </summary> /// <typeparam name="T"> Generic tileGoData type </typeparam> /// <param name="_tileToCheck"> The tile we want to check the neighbours around </param> /// <param name="_action"> The action we want to call on the neighbours </param> private void TileCheckNeighbours <T>(TileGOData _tileToCheck, System.Action <T> _action) where T : TileGOData { _action(_tileToCheck.Up <T>()); _action(_tileToCheck.Right <T>()); _action(_tileToCheck.Down <T>()); _action(_tileToCheck.Left <T>()); }
public void NewMap(Vector2 mapWorldOrigin, int level, int darknessLevel = 0, int mapWidth = 9, int mapHeight = 9) { cameraShaker = CameraShaker.instance; this.darknessLevel = darknessLevel; this.mapWidth = mapWidth; this.mapHeight = mapHeight; pool = ObjectPool.instance; if (tileHolder == null) { tileHolder = new GameObject(); tileHolder.name = "_TILES_"; } tileHolder.transform.position = mapWorldOrigin; // Generate the map if (Map != null) { Map.ResetMap(); } else { Map = new GameMap(mapWidth, mapHeight, mapWorldOrigin, OnTileChange); } // Make darness map darknessMap = new Darkness[mapWidth * mapHeight]; // Spawn GObjs TileGOs = new TileGOData[Map.Tiles.Length]; Vector2Int exitTilePos = Vector2Int.zero; Vector2Int entranceTilePos = Vector2Int.zero; for (int i = 0; i < Map.Tiles.Length; i++) { if (Map.Tiles[i].tileType == TileType.Empty) { continue; } // Spawn tile GO GameObject tileGO = pool.GetObjectForType("Tile", true, Map.Tiles[i].WorldPosition); if (tileGO == null) { // Make a new one? return; } tileGO.transform.SetParent(tileHolder.transform); TileGOData tileGOData = new TileGOData(); tileGOData.mainGO = tileGO; tileGOData.renderer = tileGO.GetComponentInChildren <SpriteRenderer>(); // Set Sprite RenderSystem.instance.Render(Map.Tiles[i].tileType.ToString(), tileGOData.renderer); darknessMap[i] = new Darkness(Map.Tiles[i].GridPosition.x, Map.Tiles[i].GridPosition.y, 0); // Set exit if (exitTilePos == Vector2Int.zero) { if (UnityEngine.Random.Range(1, 50) == 1) { exitTilePos = Map.Tiles[i].GridPosition; } // Set entrance for levels after level 0 else if (level > 0 && entranceTilePos == Vector2Int.zero) { if (UnityEngine.Random.Range(1, 12) == 1) { entranceTilePos = Map.Tiles[i].GridPosition; } } } TileGOs[i] = tileGOData; } // Clean up array TileGOs = TileGOs.Where(go => go.mainGO != null).ToArray(); // if we still have no exit tile, place it in the center of the room if (exitTilePos == Vector2Int.zero) { exitTilePos = new Vector2Int(mapWidth / 2, mapHeight / 2); } Map.SetTileType(exitTilePos.x, exitTilePos.y, TileType.Exit); if (level > 0) { Map.SetTileType(entranceTilePos.x, entranceTilePos.y, TileType.Entrance); } // chance to start with darnkess if (darknessLevel > 0) { SetDarkness(); } Global.OnMapCreated onMapCreated = new OnMapCreated(); onMapCreated.entranceWorldPosition = entranceTilePos; onMapCreated.exitWorldPosition = exitTilePos; onMapCreated.FireEvent(); }