Exemplo n.º 1
0
    public void LinkElement(LinkElementPlaceholder GOToLink)
    {
        // Unlink
        if (GOToLink.elementLinked)
        {
            LinkElementPlaceholder anotherLep = GOToLink.elementLinked.GetComponent <LinkElementPlaceholder>();

            currentActivatorShowed = null;
            GameObject activator = GOToLink.type == LinkElementPlaceholder.LinkElementType.Activator ? GOToLink.gameObject : anotherLep.gameObject;
            ClearPath(TileMapPathfinding.FindPathCoordinates(V3Helper.V3ToV3Int(activator.transform.position), V3Helper.V3ToV3Int(activator.GetComponent <LinkElementPlaceholder>().elementLinked.transform.position)));

            anotherLep.elementLinked   = null;
            anotherLep.buttonText.text = "Link";

            GOToLink.elementLinked   = null;
            GOToLink.buttonText.text = "Link";
            return;
        }

        // Element pending to be linked
        if (currentLinkingObject)
        {
            // Different Link types, link
            if (currentLinkingObject.type != GOToLink.type)
            {
                if (!GOToLink.elementLinked)
                {
                    GOToLink.elementLinked = currentLinkingObject.gameObject;
                    currentLinkingObject.buttonText.text = "Unlink";
                    currentLinkingObject.elementLinked   = GOToLink.gameObject;
                    GOToLink.buttonText.text             = "Unlink";
                    currentLinkingObject.ShowCanvas(true);
                    currentLinkingObject = null;

                    foreach (KeyValuePair <GameObject, LinkElementPlaceholder.LinkElementType> linkingGO in linkingObjects)
                    {
                        LinkElementPlaceholder LEP = linkingGO.Key.GetComponent <LinkElementPlaceholder>();
                        if (LEP != GOToLink && LEP != GOToLink.elementLinked.GetComponent <LinkElementPlaceholder>())
                        {
                            LEP.ShowCanvas(false);
                        }
                    }

                    GameObject activator = GOToLink.type == LinkElementPlaceholder.LinkElementType.Activator ? GOToLink.gameObject : GOToLink.elementLinked.gameObject;
                    currentActivatorShowed = activator;
                    DrawLine(TileMapPathfinding.FindPathCoordinates(V3Helper.V3ToV3Int(activator.transform.position), V3Helper.V3ToV3Int(activator.GetComponent <LinkElementPlaceholder>().elementLinked.transform.position)));
                    return;
                }
            }
            else
            {
                // Same Link types different objects, substitute
                if (currentLinkingObject != GOToLink)
                {
                    currentLinkingObject.buttonText.text = "Link";
                    GOToLink.buttonText.text             = "Cancel";
                    currentLinkingObject = GOToLink;
                }
                // Same object, cancel linking process
                else
                {
                    currentLinkingObject.buttonText.text = "Link";
                    currentLinkingObject = null;

                    HideAllInspectors();
                }
            }
        }

        // Start Linking
        currentLinkingObject = GOToLink;
        currentLinkingObject.buttonText.text = "Cancel";

        foreach (KeyValuePair <GameObject, LinkElementPlaceholder.LinkElementType> linkingGO in linkingObjects)
        {
            LinkElementPlaceholder LEP = linkingGO.Key.GetComponent <LinkElementPlaceholder>();
            if (LEP != GOToLink && !LEP.elementLinked && LEP.type != GOToLink.type)
            {
                LEP.ShowCanvas(true);
            }
        }
    }