コード例 #1
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            UIManager.Instance.QuitGame();
        }

        if (State == GameState.Playing)
        {
            if (Dragging)
            {
                Ray        r = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit dragHit;
                bool       onSlot   = false;
                bool       canPlace = false;

                if (Physics.Raycast(r, out dragHit, 20))
                {
                    if (dragHit.collider != null)
                    {
                        if (dragHit.collider.tag == "Slot")
                        {
                            GridSlot slot = dragHit.collider.GetComponent <GridSlot>();
                            if (slot != null && slot.ParentRow.Side == Turn)
                            {
                                if (slot.IsEmpty)
                                {
                                    canPlace    = true;
                                    onSlot      = true;
                                    currentSlot = slot;
                                    currentSlot.Highlight(true);
                                    highlightedSlot           = currentSlot;
                                    Target.transform.position = new Vector3(slot.transform.position.x, 0.25f, slot.transform.position.z);
                                }
                            }
                        }
                        else
                        {
                            Vector3 pos = dragHit.point;
                            Target.transform.position = new Vector3(pos.x, 0.25f, pos.z);
                        }
                    }
                }

                Target.transform.localScale = canPlace ? Vector3.one * 0.5f : Vector3.one * 0.3f;

                if (canPlace)
                {
                    if (highlightedSlot != null)
                    {
                        highlightedSlot.Highlight(true);
                    }
                }
                else
                {
                    if (highlightedSlot != null)
                    {
                        highlightedSlot.Highlight(false);
                    }
                    highlightedSlot = null;
                }

                if (!onSlot)
                {
                    currentSlot = null;
                }

                if (Input.GetMouseButtonUp(0))
                {
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hit;
                    bool       placed = false;

                    if (currentSlot != null)
                    {
                        if ((Turn == Player.Blue && Data.BlueMana >= currentChampion.Cost) || (Turn == Player.Red && Data.RedMana >= currentChampion.Cost))
                        {
                            Target.transform.parent        = currentSlot.transform;
                            Target.transform.localPosition = Vector3.up * 0.25f;
                            Target.Slot          = currentSlot;
                            currentSlot.Champion = Target;
                            currentSlot.Highlight(false);
                            HealthBar bar = Instantiate(HealthBarPrefab, WorldSpace.transform);
                            Target.Bar = bar;
                            Target.Spawn();
                            currentDeckSlot.LoadNewCard(Turn);
                            placed = true;

                            if (Turn == Player.Blue)
                            {
                                Data = Data.Add(Player.Blue, StatType.Mana, -currentChampion.Cost);
                            }
                            else
                            {
                                Data = Data.Add(Player.Red, StatType.Mana, -currentChampion.Cost);
                            }
                        }
                    }

                    //if (Physics.Raycast(ray, out hit, 100))
                    //{
                    //    if (hit.collider != null)
                    //    {
                    //        GridSlot slot = hit.collider.GetComponent<GridSlot>();
                    //        if (slot != null)
                    //        {
                    //            if (slot.IsEmpty)
                    //            {
                    //                Debug.Log("Clicked on slot: Side: " + slot.Position.Side + " | X: " + slot.Position.X + " | Y: " + slot.Position.Y);
                    //                Target.transform.parent = slot.transform;
                    //                Target.transform.localPosition = Vector3.up * 0.25f;
                    //                slot.Champion = Target;
                    //                placed = true;
                    //            }
                    //        }
                    //    }
                    //}
                    if (!placed)
                    {
                        Destroy(Target.gameObject);
                    }
                    Dragging        = false;
                    currentChampion = null;
                    currentDeckSlot = null;
                }
            }
        }
    }