Beispiel #1
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);
    }
Beispiel #2
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));
                }
            }
        }
    }
    void DebugHexMath()
    {
        Vector3Int origin     = new Vector3Int(0, 0, 0);
        Vector3Int target     = new Vector3Int(2, 2, 0);
        Vector3Int targetCube = new Vector3Int(1, -3, 2);


        // Distance testing: Oddr then Cube
        Assert.AreEqual(HexMath.OddrDistance(origin, origin), 0);
        Assert.AreEqual(HexMath.OddrDistance(origin, origin + HexMath.CubeDirection(0)), 1);
        Assert.AreEqual(HexMath.OddrDistance(origin, origin + HexMath.CubeDirection(0) * 2), 2);
        Assert.AreEqual(HexMath.OddrDistance(origin, target), 3);
        Assert.AreEqual(HexMath.OddrDistance(origin, target + HexMath.CubeDirection(0)), 2);
        Assert.AreEqual(HexMath.OddrDistance(origin, new Vector3Int(-6, -6, 0)), 9);

        Assert.AreEqual(HexMath.CubeDistance(origin, origin), 0);
        Assert.AreEqual(HexMath.CubeDistance(origin, origin + HexMath.CubeDirection(0) * 2), 2);
        Assert.AreEqual(HexMath.CubeDistance(origin, new Vector3Int(0, 0, 0)), 0);
        Assert.AreEqual(HexMath.CubeDistance(origin, targetCube), 3);
        Assert.AreEqual(HexMath.CubeDistance(origin, target + HexMath.CubeDirection(0)), 2);
        Assert.AreEqual(HexMath.CubeDistance(origin, new Vector3Int(-1, 4, -3)), 4);

        // Cube - Hex conversion testing.
        Vector3Int Hex  = new Vector3Int(1, 1, 0);
        Vector3Int Cube = new Vector3Int(1, -2, 1);

        Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube));
        Assert.AreEqual(Cube, HexMath.OddrToCube(Hex));

        Hex  = new Vector3Int(-2, -2, 0);
        Cube = new Vector3Int(-1, 3, -2);
        Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube));
        Assert.AreEqual(Cube, HexMath.OddrToCube(Hex));

        Hex  = new Vector3Int(1, -2, 0);
        Cube = new Vector3Int(2, 0, -2);
        Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube));
        Assert.AreEqual(Cube, HexMath.OddrToCube(Hex));

        Hex  = new Vector3Int(-2, 2, 0);
        Cube = new Vector3Int(-3, 1, 2);
        Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube));
        Assert.AreEqual(Cube, HexMath.OddrToCube(Hex));

        /* Neighbor Testing: Testing around Origin: (0, 0, 0).
         *  - Radius 0. It's a marsh, so a neighbor test of radius 0 should simply return one marsh.
         *  - Radius 1. Should add 6 soil tiles and count equal 7
         *  - Radius 2. Should add 12 barren tiles and count equal 19.
         *  No tiles should be water for this setup.
         */
        NeighborTestHelper(0);
        NeighborTestHelper(1);
        NeighborTestHelper(2);
        NeighborTestHelper(3);
    }
Beispiel #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);
    }