예제 #1
0
    private void TileLinkButtonListener(Button button)
    {
        if (chosenHead == null)
        {
            chosenHead = button;
            int headIndex = GetTileIndexFromButton(chosenHead);

            Button[] buttons = GameObject.FindObjectsOfType <Button>();
            foreach (Button other in buttons)
            {
                if (other.name.StartsWith("tilelinkbutton"))
                {
                    int otherIndex = GetTileIndexFromButton(other);
                    int diff       = headIndex - otherIndex;

                    if (!((this is Snake && ((diff > 0 && diff <= _maxLength) || (headIndex + GameManager.Tiles.Length - otherIndex <= _maxLength))) ||  //checks whether tail is behind snake head within range
                          (this is Ladder && ((diff < 0 && -diff <= _maxLength) || (otherIndex + GameManager.Tiles.Length - headIndex <= _maxLength))))) //checks whether tail is infront of ladder head within range
                    {
                        other.interactable = false;
                        other.GetComponent <Image>().color = chosenHead.colors.disabledColor;
                    }
                }
            }
            return;
        }

        //set head of new tilelink
        tiles[0] = GameManager.Tiles[GetTileIndexFromButton(chosenHead)];
        //set tail of new tilelink
        tiles[1] = GameManager.Tiles[GetTileIndexFromButton(button)];

        //add tile link to both head and tail for easier referencing
        Head.AddTileLink(this);
        Tail.AddTileLink(this);

        tileLinkObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Vector3 headPos = Head.transform.position;
        Vector3 tailPos = Tail.transform.position;

        //scale tile link so that it can reach both tiles
        tileLinkObj.transform.localScale = new Vector3(1, 1, Mathf.Sqrt(Mathf.Pow(headPos.x - tailPos.x, 2) +
                                                                        Mathf.Pow(headPos.z - tailPos.z, 2)));

        //rotate so that it aligns with both tiles
        Vector3 sum = headPos + tailPos;

        tileLinkObj.transform.position = new Vector3(sum.x / 2, sum.y / 2, sum.z / 2);
        tileLinkObj.transform.rotation = Quaternion.LookRotation(headPos - tailPos, Vector3.forward);

        //fixes tile link rotation on tiles with rotation
        //tileLinkObj.transform.eulerAngles += new Vector3(0, 0, Head.transform.eulerAngles.y == 90 && Tail.transform.eulerAngles.y == 90 ? 90 : 0);

        //set material
        tileLinkObj.GetComponent <Renderer>().material = this is Snake?Resources.Load("Materials/snake") as Material : Resources.Load("Materials/ladder") as Material;

        base.Use();
        MenuManager.SwitchToMenuWithInventory(MenuManager.TurnOptions);
        MenuManager.SwitchToCamera(MenuManager.MainCamera);
    }