Пример #1
0
        private static bool TryFindAdjacentPosition(Tile main, Tile possibleAdj, out AdjacentTile adjacent)
        {
            for (var rotation = 0; rotation < 4; rotation++)
            {
                if (CheckAdjacencyOnEachSide(main, out adjacent))
                {
                    return(true);
                }

                if (CheckAdjacencyOnEachSide(main.FlipVertically(), out adjacent))
                {
                    return(true);
                }

                if (CheckAdjacencyOnEachSide(main.FlipHorizontally(), out adjacent))
                {
                    return(true);
                }

                main = main.RotateLeft();
            }

            adjacent = default;
            return(false);

            bool CheckAdjacencyOnEachSide(Tile checkedTile, out AdjacentTile adj)
            {
                if (checkedTile.CanPlaceTop(possibleAdj))
                {
                    adj = new AdjacentTile(checkedTile, Side.Top);
                    return(true);
                }

                if (checkedTile.CanPlaceBottom(possibleAdj))
                {
                    adj = new AdjacentTile(checkedTile, Side.Bottom);
                    return(true);
                }

                if (checkedTile.CanPlaceLeft(possibleAdj))
                {
                    adj = new AdjacentTile(checkedTile, Side.Left);
                    return(true);
                }

                if (checkedTile.CanPlaceRight(possibleAdj))
                {
                    adj = new AdjacentTile(checkedTile, Side.Right);
                    return(true);
                }

                adj = default;
                return(false);
            }
        }
Пример #2
0
    public void Generate()
    {
        List <Vector3Int> positions = new List <Vector3Int>();
        List <TileBase>   tiles     = new List <TileBase>();

        WorldManager wg = FindObjectOfType <WorldManager>();

        Vector3Int currentPosition = Vector3Int.RoundToInt(transform.position);
        float      currentHeight   = wg.worldData.heightMap[currentPosition.x, currentPosition.y];

        AddTile(currentPosition);

        int currentIteration = 0;

        Vector3 currentDirection = Vector3.zero;

        while (currentHeight > oceanBiome.height - 0.05f && currentIteration <= maxIterations)
        {
            AdjacentTile[] adjacent = new AdjacentTile[4]
            {
                new AdjacentTile(currentPosition + Vector3Int.up, wg.worldData.heightMap),
                new AdjacentTile(currentPosition + Vector3Int.right, wg.worldData.heightMap),
                new AdjacentTile(currentPosition + Vector3Int.down, wg.worldData.heightMap),
                new AdjacentTile(currentPosition + Vector3Int.left, wg.worldData.heightMap)
            };

            int lowestIndex = 0;

            for (int i = 1; i < adjacent.Length; i++)
            {
                //if (currentPosition - adjacent[i].pos == currentDirection)
                //    adjacent[i].height -= directionSensitivity;

                if (currentIteration % forceChangeDirection == 0)
                {
                    if (currentPosition - adjacent[i].pos == currentDirection)
                    {
                        adjacent[i].height += directionSensitivity * 2;
                    }
                }

                if (Vector3.Distance(adjacent[i].pos, transform.position) > Vector3.Distance(currentPosition, transform.position))
                {
                    adjacent[i].height -= directionSensitivity;
                }

                if (adjacent[i].height < adjacent[lowestIndex].height && !positions.Contains(adjacent[i].pos))
                {
                    lowestIndex = i;
                }
            }

            currentDirection = currentPosition - adjacent[lowestIndex].pos;
            currentHeight    = adjacent[lowestIndex].height;
            currentPosition  = adjacent[lowestIndex].pos;

            AddTile(currentPosition);

            currentIteration++;

            if (currentIteration >= maxIterations)
            {
                return;
            }
        }

        ObjectStore.instance.mapDisplay.DrawTerrain(new TilemapData(positions.ToArray(), tiles.ToArray()));

        //FUNCTIONS
        void AddTile(Vector3Int pos)
        {
            positions.Add(pos);
            tiles.Add(waterTile);
        }
    }
Пример #3
0
    public void setNewNextEnemyLoc(int currentEnemyIndex)
    {
        float        distance = modulus(Vector3.Distance(enemyData[currentEnemyIndex].enemyLocation, enemyData[currentEnemyIndex].targetEnemyLocation));
        AdjacentTile nextTile = AdjacentTile.Current;

        if (GetTileAt((int)enemyData[currentEnemyIndex].enemyLocation.x, (int)enemyData[currentEnemyIndex].enemyLocation.y + 1) == TileType.Floor)
        {
            float newDistance = modulus(Vector3.Distance(new Vector3(enemyData[currentEnemyIndex].enemyLocation.x, enemyData[currentEnemyIndex].enemyLocation.y + 1, 0), enemyData[currentEnemyIndex].targetEnemyLocation));

            if (newDistance < distance)
            {
                distance = newDistance;
                nextTile = AdjacentTile.Up;
            }
        }

        if (GetTileAt((int)enemyData[currentEnemyIndex].enemyLocation.x + 1, (int)enemyData[currentEnemyIndex].enemyLocation.y) == TileType.Floor)
        {
            float newDistance = modulus(Vector3.Distance(new Vector3(enemyData[currentEnemyIndex].enemyLocation.x + 1, enemyData[currentEnemyIndex].enemyLocation.y, 0), enemyData[currentEnemyIndex].targetEnemyLocation));

            if (newDistance < distance)
            {
                distance = newDistance;
                nextTile = AdjacentTile.Right;
            }
        }

        if (GetTileAt((int)enemyData[currentEnemyIndex].enemyLocation.x, (int)enemyData[currentEnemyIndex].enemyLocation.y - 1) == TileType.Floor)
        {
            float newDistance = modulus(Vector3.Distance(new Vector3(enemyData[currentEnemyIndex].enemyLocation.x, enemyData[currentEnemyIndex].enemyLocation.y - 1, 0), enemyData[currentEnemyIndex].targetEnemyLocation));

            if (newDistance < distance)
            {
                distance = newDistance;
                nextTile = AdjacentTile.Down;
            }
        }

        if (GetTileAt((int)enemyData[currentEnemyIndex].enemyLocation.x - 1, (int)enemyData[currentEnemyIndex].enemyLocation.y) == TileType.Floor)
        {
            float newDistance = modulus(Vector3.Distance(new Vector3(enemyData[currentEnemyIndex].enemyLocation.x - 1, enemyData[currentEnemyIndex].enemyLocation.y, 0), enemyData[currentEnemyIndex].targetEnemyLocation));

            if (newDistance < distance)
            {
                distance = newDistance;
                nextTile = AdjacentTile.Left;
            }
        }

        switch (nextTile)
        {
        case (AdjacentTile.Current):
            enemyData[currentEnemyIndex].nextEnemyLocation = enemyData[currentEnemyIndex].enemyLocation;
            enemyData[currentEnemyIndex].enemyIsAtMaxLoc   = true;
            break;

        case (AdjacentTile.Up):
            enemyData[currentEnemyIndex].nextEnemyLocation = new Vector3(enemyData[currentEnemyIndex].enemyLocation.x, enemyData[currentEnemyIndex].enemyLocation.y + 1, 0);
            enemyData[currentEnemyIndex].enemyIsAtMaxLoc   = false;
            break;

        case (AdjacentTile.Right):
            enemyData[currentEnemyIndex].nextEnemyLocation = new Vector3(enemyData[currentEnemyIndex].enemyLocation.x + 1, enemyData[currentEnemyIndex].enemyLocation.y, 0);
            enemyData[currentEnemyIndex].enemyIsAtMaxLoc   = false;
            break;

        case (AdjacentTile.Down):
            enemyData[currentEnemyIndex].nextEnemyLocation = new Vector3(enemyData[currentEnemyIndex].enemyLocation.x, enemyData[currentEnemyIndex].enemyLocation.y - 1, 0);
            enemyData[currentEnemyIndex].enemyIsAtMaxLoc   = false;
            break;

        case (AdjacentTile.Left):
            enemyData[currentEnemyIndex].nextEnemyLocation = new Vector3(enemyData[currentEnemyIndex].enemyLocation.x - 1, enemyData[currentEnemyIndex].enemyLocation.y, 0);
            enemyData[currentEnemyIndex].enemyIsAtMaxLoc   = false;
            break;
        }
    }