public FacilitiesTileTypeRoot GetFacilitiesTypes() { // Get tile type definitions from JSON file string facilitiesJSONContents = facilitiesTileTypesJSON.text; // Get types root FacilitiesTileTypeRoot facilitiesRoot = JsonUtility.FromJson <FacilitiesTileTypeRoot>(facilitiesJSONContents); return(facilitiesRoot); }
public void Initialize(EnvironmentTile firstPosition) { facilitiesTypes = GameTiles.instance.GetFacilitiesTypes(); foreach (FacilitiesTileType type in facilitiesTypes.tileTypes) { // can't build cities if (type.Name == "City") { continue; } // create a button for this facility and add it to the parent object // instantiate new button GameObject button = Instantiate(buttonTemplate) as GameObject; button.SetActive(true); BuildButton btnScript = button.GetComponent <BuildButton>(); // change the icon of the button // get all sliced tiles from our tileset Sprite[] tileSprites = Resources.LoadAll <Sprite>(facilitiesTileset.name); Sprite matchingSprite = tileSprites[0]; // find the sprite foreach (Sprite sprite in tileSprites) { if (sprite.name == type.SpriteName) { matchingSprite = sprite; break; } } btnScript.SetImage(matchingSprite); btnScript.SetType(type); btnScript.Enable(type.IsBuildable(firstPosition)); // set parent of the button with the parent of the button template's parent // (our content object) button.transform.SetParent(buttonTemplate.transform.parent, false); buttons.Add(button); } }
public void GetWorldTiles() { /* initialize tiles dictionaries */ environmentTiles = new Dictionary <Vector3Int, EnvironmentTile>(); facilitiesTiles = new Dictionary <Vector3Int, FacilityTile>(); // Get types EnvironmentTileTypeRoot environmentRoot = GetEnvironmentTypes(); FacilitiesTileTypeRoot facilitiesRoot = GetFacilitiesTypes(); // Generate resource names lists from the first type that is in our types lists foreach (KeyValuePair <string, int> type in environmentRoot.tileTypes[0].GenerateResourcesDictionary()) { EnvironmentResourceNames.Add(type.Key); } foreach (KeyValuePair <string, int> type in facilitiesRoot.tileTypes[0].GenerateResourcesDictionary()) { FacilitiesResourceNames.Add(type.Key); } // Add game tiles to our tile dictionary for further referencing, should it be the case (spoiler alert: it will!) // iterate through all tiles in the tilemap foreach (Vector3Int pos in environmentTilemap.cellBounds.allPositionsWithin) { var localPlace = new Vector3Int(pos.x, pos.y, pos.z); // ENVIRONMENT TILES // if there is a tile here... if (environmentTilemap.HasTile(localPlace)) { // Find its type according to our parsed JSON data var TileBase = environmentTilemap.GetTile(localPlace); EnvironmentTileType tileType = environmentRoot.FindType(environmentTilemap.GetSprite(localPlace).name); // find the type of this tile by the name of its sprite // Assign type defined variables to actual tile representation that is to be stored in our dictionary of tiles var environmentTile = new EnvironmentTile { LocalPlace = localPlace, TileBase = environmentTilemap.GetTile(localPlace), TilemapMember = environmentTilemap, // Amount of resources available in this tile Name = tileType.Name, Resources = tileType.GenerateResourcesDictionary() }; // add the tile representation to our dictionnary of tiles environmentTiles.Add(environmentTile.LocalPlace, environmentTile); } // FACILITY TILES if (facilitiesTilemap.HasTile(localPlace)) { var TileBase = facilitiesTilemap.GetTile(localPlace); FacilitiesTileType tileType = facilitiesRoot.FindType(TileBase.name); // find the type of this tile by the name of its sprite HealthBar healthBar = null; Cross cross = null; if (tileType.Extractor) { // Attach healthbar if facility is an extractor var bar = Instantiate(healthBarTemplate); bar.SetActive(true); HealthBar script = bar.GetComponent <HealthBar>(); script.MoveTo(localPlace); healthBar = script; } // Attach cross var crossInstance = Instantiate(crossTemplate); cross = crossInstance.GetComponent <Cross>(); cross.MoveTo(localPlace); crossInstance.SetActive(false); var facilityTile = new FacilityTile { LocalPlace = localPlace, TileBase = facilitiesTilemap.GetTile(localPlace), TilemapMember = facilitiesTilemap, // Here, we represent consumption by negative values for its resource // and we represent production by positive values Name = tileType.Name, Resources = tileType.GenerateResourcesDictionary(), Extractor = tileType.Extractor, HealthBar = healthBar, PollutionRadius = tileType.PollutionRadius }; if (facilityTile.PollutionRadius > 0) { ApplyPollution(facilityTile); } facilitiesTiles.Add(facilityTile.LocalPlace, facilityTile); } } }