// Use this for start override public void StartGame() { type = ManagerType.MAP; _root = new GameObject("Map"); _maps = MapParser.Parse(); _map = _maps[Game.instance.mapIndex]; #if DEBUG Debug.Log("Create a map with " + _map.tiles.Count + " tiles on " + _map.lines + " lines and " + _map.columns + " columns"); #endif for (int i = 0; i < _map.tiles.Count; i++) { Tile tile = _map.tiles[i]; GameObject go = tile.type < _gameObjects.Length ? _gameObjects[tile.type] : _gameObjects[0]; GameObject tileGO = GameObject.Instantiate(go); tile.gameObject = tileGO; tileGO.name = "Tile " + i; tileGO.transform.position = _map.GetPositionFromIndex(i); tileGO.transform.SetParent(_root.transform); PopulateTile(tile); } }
public static List<Map> Parse() { TextAsset configuration = Resources.Load(MapConfiguration.FILENAME) as TextAsset; List<Map> maps = new List<Map>(); if (configuration != null) { JSONNode json = JSON.Parse(configuration.text); if (json != null) { JSONArray nodes = json["maps"].AsArray; for (int i = 0; i < nodes.Count; i++) { JSONNode node = json["maps"][i]; Map map = new Map(node["lines"].AsInt, node["columns"].AsInt); for (int y = 0; y < map.lines; y++) { for (int x = 0; x < map.columns; x++) { int index = map.GetIndexFromPosition(x, y); int type = node["tiles"][index].AsInt; int quantity = node["quantities"][index].AsInt; map.tiles.Add(new Tile(type, index, quantity)); } } maps.Add(map); } } } if(maps.Count == 0) { Debug.LogError("Can't read map configuration"); } return maps; }