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
    public void DrawPreview(Map map, Vector3Int coordinate, GameObject brushPrefab)
    {
        if (!previewObject && brushPrefab != null)
        {
            previewObject          = Instantiate(brushPrefab);
            attachment             = previewObject.GetComponentInChildren(typeof(IAttachment)) as IAttachment;
            previewObjectRenderers = previewObject.GetComponentsInChildren <Renderer>();
        }

        if (previewObject)
        {
            previewObject.transform.position = new Vector3(coordinate.x, 0, coordinate.z);

            if (map.IsWithinBounds(coordinate.x, coordinate.z) &&
                map.IsWithinBounds(coordinate.x + attachment.GetDimension().x - 1,
                                   coordinate.z + attachment.GetDimension().z - 1))
            {
                previewObject.SetActive(true);
            }
            else
            {
                previewObject.SetActive(false);
            }

            bool occupied = map.IsTileSpaceOccupied(coordinate.x,
                                                    coordinate.z,
                                                    attachment.GetDimension().x,
                                                    attachment.GetDimension().z);

            if (occupied)
            {
                foreach (Renderer rend in previewObjectRenderers)
                {
                    rend.material.color = Color.red;
                }
            }
            else
            {
                foreach (Renderer rend in previewObjectRenderers)
                {
                    rend.material.color = Color.green;
                }
            }
        }
    }
Exemple #3
0
    public bool Attach(int x, int z, GameObject attachmentPrefab)
    {
        if (x < 0 || x >= mapData.width)
        {
            return(false);
        }
        if (z < 0 || z >= mapData.length)
        {
            return(false);
        }

        Tile tile = GetTile(x, z);

        if (!tile.HasAttachment())
        {
            GameObject attachmentGO = Instantiate(attachmentPrefab);
            attachmentGO.transform.SetParent(transform);
            attachmentGO.transform.localPosition = Vector3.zero;
            attachmentGO.transform.position      = new Vector3(x, 0, z);
            IAttachment attachment = attachmentGO.GetComponent(typeof(IAttachment)) as IAttachment;

            // A large attachment occupies more than one tile, therefore we must ensure that none of the affected tiles have any attachment.
            Vector3Int dim = attachment.GetDimension();
            if (IsTileSpaceOccupied(x, z, dim.x, dim.z))
            {
                Debug.LogWarning("Can not attach object to tile. At least one tile is already occupied...");
                Destroy(attachmentGO);
                return(false);
            }

            bool attached = tile.Attach(attachment);
            if (attached)
            {
                bool claimedTiles = AssignClaimantToTiles(x, z, dim.x, dim.z);
                if (!claimedTiles)
                {
                    Debug.LogError("Failed to claim nearby tiles!");
                    return(false);
                }

                List <ITile> adjacentTiles = GetAdjacentTiles(x, z);
                foreach (ITile adjacentTile in adjacentTiles)
                {
                    adjacentTile.OnNeighborChanged(tile);
                    tile.OnNeighborChanged(adjacentTile);
                }

                return(true);
            }
            else
            {
                Debug.LogError("Failed to attach...");
                return(false);
            }
        }
        else
        {
            Debug.LogWarning("Failed to attach, because the tile already has an attachment...");
            return(false);
        }
    }