Ejemplo n.º 1
0
    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);
            }
        }
    }