Exemple #1
0
    ITile FindSpawnTile()
    {
        Vector3Int myPos = Vector3Int.FloorToInt(transform.position);

        int x = 0;
        int z = -1;

        for (; x < attachment.GetDimension().x; x++)
        {
            ITile tile = map.GetTile(myPos.x + x, myPos.z + z);
            if (tile != null && tile.GetAttachment() is Road)
            {
                return(tile);
            }
        }

        z++;

        for (; z < attachment.GetDimension().z; z++)
        {
            ITile tile = map.GetTile(myPos.x + x, myPos.z + z);
            if (tile != null && tile.GetAttachment() is Road)
            {
                return(tile);
            }
        }

        x--;

        for (; x > -1; x--)
        {
            ITile tile = map.GetTile(myPos.x + x, myPos.z + z);
            if (tile != null && tile.GetAttachment() is Road)
            {
                return(tile);
            }
        }

        z--;

        for (; z > -1; z--)
        {
            ITile tile = map.GetTile(myPos.x + x, myPos.z + z);
            if (tile != null && tile.GetAttachment() is Road)
            {
                return(tile);
            }
        }

        return(null);
    }
Exemple #2
0
    ITile GetDestinationRoadTile()
    {
        if (steps >= maxSteps && memory.Count > 0)
        {
            ITile destinationTile = memory[memory.Count - 1];
            memory.RemoveAt(memory.Count - 1);
            return(destinationTile);
        }

        Vector3Int pos = Vector3Int.FloorToInt(transform.position);

        ITile       tile       = map.GetTile(pos.x, pos.z);
        IAttachment attachment = tile.GetAttachment();

        if (attachment != null)
        {
            if (attachment is Road)
            {
                Road road = (Road)attachment;

                List <ITile> adjacentRoadTiles = new List <ITile>();
                List <ITile> adjacentTiles     = map.GetAdjacentTiles(pos.x, pos.z);
                foreach (ITile adjacentTile in adjacentTiles)
                {
                    if (adjacentTile.GetAttachment() is Road)
                    {
                        adjacentRoadTiles.Add(adjacentTile);
                    }
                }

                if (adjacentRoadTiles.Count > 1)
                {
                    adjacentRoadTiles.Remove(lastTile);
                }

                if (adjacentRoadTiles.Count > 0)
                {
                    return(adjacentRoadTiles[Random.Range(0, adjacentRoadTiles.Count)]);
                }
                else
                {
                    // NOP
                }
            }
        }

        return(null);
    }
Exemple #3
0
    public void OnNeighborChanged(ITile neighbor)
    {
        int dx = neighbor.GetCoordinate().x - GetCoordinate().x;
        int dz = neighbor.GetCoordinate().z - GetCoordinate().z;

        bool isRight    = (dx == 1 && dz == 0);
        bool isLeft     = (dx == -1 && dz == 0);
        bool isForward  = (dx == 0 && dz == 1);
        bool isBackward = (dx == 0 && dz == -1);

        IAttachment attachment         = GetAttachment();
        IAttachment neighborAttachment = neighbor.GetAttachment();

        if (attachment is Road)
        {
            Road road = (Road)attachment;

            bool isNeighborRoad = (neighborAttachment is Road);
            if (isNeighborRoad && isRight)
            {
                road.connectRight = true;
            }
            if (isNeighborRoad && isLeft)
            {
                road.connectLeft = true;
            }
            if (isNeighborRoad && isForward)
            {
                road.connectForward = true;
            }
            if (isNeighborRoad && isBackward)
            {
                road.connectBackward = true;
            }
        }
    }