コード例 #1
0
    // Neighbor Test Helper
    void NeighborTestHelper(int radius)
    {
        int neighborCount = 1;

        for (int c = 0; c <= radius; c++)
        {
            neighborCount += c * 6;
        }

        Tilemap           tilemap       = GameObject.FindGameObjectWithTag("testTilemap").GetComponent <Tilemap>();
        List <Vector3Int> neighborTiles = new List <Vector3Int>();

        neighborTiles = HexMath.OddrRange(new Vector3Int(0, 0, 0), radius);
        Dictionary <string, int> neighborTestDict = new Dictionary <string, int>();

        Assert.AreEqual(neighborTiles.Count, neighborCount);

        for (int i = 0; i < neighborTiles.Count; i++)
        {
            if (neighborTestDict.ContainsKey(tilemap.GetTile(neighborTiles[i]).name))
            {
                neighborTestDict[tilemap.GetTile(neighborTiles[i]).name]++;
            }
            else
            {
                neighborTestDict.Add(tilemap.GetTile(neighborTiles[i]).name, 1);
            }
        }
        foreach (KeyValuePair <string, int> kvp in neighborTestDict)
        {
            Debug.Log(kvp.Key + ": " + kvp.Value);
        }
    }
コード例 #2
0
    // PLACE WATER
    public void PlaceWater(Vector3Int tileLocation)
    {
        // Set the target tile to be a water tile.
        tilemap.SetTile(tileLocation, groundTileFromName["Water"]);

        // Update groundTiles dictionary.
        groundTiles[tileLocation].ThisTile        = groundTileFromName["Water"];
        groundTiles[tileLocation].DistanceToWater = 0;

        // Get the neighbors.
        List <Vector3Int> neighbors = HexMath.OddrRange(tileLocation, 5);

        foreach (Vector3Int nPosition in neighbors)
        {
            // Look for the tile in the groundTiles dictionary.
            if (groundTiles.ContainsKey(nPosition))
            {
                // Take a look at the neighbor. Look at its NearbyWaterTiles dictionary and DistanceToWater.
                // We want to add this new water into the dictionary and, if DistanceToWater just shrank, update
                // the neighbor to the appropriate tile type.
                int dTW = HexMath.OddrDistance(tileLocation, nPosition);
                if (dTW == 0)
                {
                    // The tile has 0 distance to water, as in, it's looking at itself, so skip this one.
                    continue;
                }
                else if (groundTiles[nPosition].NearbyWaterTiles.ContainsKey(dTW))
                {
                    // This value for dTW exists in the dictionary. Merely add this new water tile to the list.
                    groundTiles[nPosition].NearbyWaterTiles[dTW].Add(tileLocation);
                }
                else
                {
                    // This value for dTW did not exist in the dictionary. Create a new key value pair.
                    groundTiles[nPosition].NearbyWaterTiles.Add(dTW, new List <Vector3Int> {
                        tileLocation
                    });
                }

                // Did the neighbor get closer to water?
                if (dTW < groundTiles[nPosition].DistanceToWater)
                {
                    // dTW changed, so we can update groundTiles dictionary.
                    groundTiles[nPosition].DistanceToWater = dTW;

                    // Which tile should it be, according to its distanceToWater?
                    Tile nTile = GetTileByWaterDistance(dTW, marshDistance, soilDistance);

                    // Start a cooroutine to change the tile.
                    StartCoroutine(DelayedGroundTileChange(nPosition));
                }
            }
        }

        // Last but not least, clear any plant on that tile?
        plantTileManager.ClearPlants(tileLocation);
    }
コード例 #3
0
    // REMOVE WATER
    public void RemoveWater(Vector3Int tileLocation)
    {
        // Set the target tile to be a marsh tile. (OR WHATEVER REMOVED WATER TURNS INTO)
        tilemap.SetTile(tileLocation, groundTileFromName["Marsh"]);
        // Update groundTiles dictionary.
        groundTiles[tileLocation].ThisTile = groundTileFromName["Marsh"];

        // Get the neighbors.
        List <Vector3Int> neighbors = HexMath.OddrRange(tileLocation, 5);

        foreach (Vector3Int nPosition in neighbors)
        {
            // Look for the tile in the groundTiles dictionary.
            if (groundTiles.ContainsKey(nPosition))
            {
                // Take a look at the neighbor. Look at its NearbyWaterTiles dictionary and DistanceToWater.
                // We want to remove this water from the dictionary and, if DistanceToWater just rose, update
                // the neighbor to the appropriate tile type.
                int dTW = HexMath.OddrDistance(tileLocation, nPosition);
                if (groundTiles[nPosition].NearbyWaterTiles.ContainsKey(dTW))
                {
                    // This value for dTW exists in the dictionary.
                    if (groundTiles[nPosition].NearbyWaterTiles[dTW].Count >= 2)
                    {
                        // Now if it's the not final item in for this key, just remove it from the list.
                        groundTiles[nPosition].NearbyWaterTiles[dTW].Remove(tileLocation);
                    }
                    else
                    {
                        // Otherwise, the list of water tiles at this exact dTW was only this tile.
                        // Therefore, remove the key value pair from the NearbyWaterTiles dictionary.
                        groundTiles[nPosition].NearbyWaterTiles.Remove(dTW);
                    }
                }
                else
                {
                    // SOMETHING HAS GONE TERRIBLY WRONG: This was a water tile and was within the radius of
                    // influence of this neighbor, yet doesn't show up in the neighbors NearbyWaterTiles?????
                    Debug.Log("Something has gone wrong, removed water tile, at " + tileLocation + " not found" +
                              " in neighbor's near by water tiles, at " + nPosition);
                }

                // Did the neighbor get further from water?
                dTW = GetDistanceToWater(nPosition, groundTiles[nPosition].NearbyWaterTiles);
                if (dTW != groundTiles[nPosition].DistanceToWater)
                {
                    // dTW changed, so we can update groundTiles dictionary.
                    groundTiles[nPosition].DistanceToWater = dTW;

                    // Start a cooroutine to change the tile.
                    StartCoroutine(DelayedGroundTileChange(nPosition));
                }
            }
        }
    }
コード例 #4
0
    public Dictionary <int, List <Vector3Int> > GetNearbyWaterTiles(Vector3Int tilePos, Tilemap tilemap, int soilDistance)
    {
        Dictionary <int, List <Vector3Int> > result = new Dictionary <int, List <Vector3Int> >();

        // Use HexMath to get list of nearbyTiles.
        List <Vector3Int> neighborPositions = HexMath.OddrRange(tilePos, soilDistance);

        // For each neighborPosition, check the tile there, looking out for water tiles.
        foreach (Vector3Int nPos in neighborPositions)
        {
            // Is there even a tile here?
            if (!tilemap.HasTile(nPos))
            {
                continue;
            }

            // Is this tile water?
            if (tilemap.GetTile <Tile>(nPos).name.Equals("Water", System.StringComparison.Ordinal))
            {
                // Get the distance from this tile to the water tile.
                int distanceToWater = HexMath.OddrDistance(tilePos, nPos);

                // If the result dictionary has an entry for this distance, add nPos to its value's list.
                if (result.ContainsKey(distanceToWater))
                {
                    result[distanceToWater].Add(nPos);
                }
                // Otherwise, the result dictionary has no entry for this distance yet, so make one.
                else
                {
                    result.Add(distanceToWater, new List <Vector3Int> {
                        nPos
                    });
                }
            }
        }

        return(result);
    }