Ejemplo n.º 1
0
        public void TrySelectTarget(PointerInfo info)
        {
            if (state != State.Normal)
            {
                throw new InvalidOperationException("Trying to select towers outside of Normal state");
            }
            UIPointer uiPointer = WrapPointer(info);

            if (uiPointer.overUI)
            {
                return;
            }
            RaycastHit hit;

            if (Physics.Raycast(uiPointer.ray, out hit, float.MaxValue))
            {
                switch (hit.collider.tag)
                {
                case "Tower":
                case "Enemy":
                    XEventBus.Instance.Post(EventId.UserClick, new XEventArgs(hit.collider.gameObject));
                    TowerAgent tower = hit.collider.GetComponent <TowerAgent>();
                    if (tower != null)
                    {
                        SelectTarget(tower);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        //根据Ghost放置Target

        public void TryPlaceTarget(PointerInfo pointerInfo)
        {
            UIPointer pointer = WrapPointer(pointerInfo);

            // Do nothing if we're over UI
            if (pointer.overUI)
            {
                return;
            }
            BuyTarget(pointer);
        }
Ejemplo n.º 3
0
        //移动Ghost
        public void TryMoveGhost(PointerInfo pointerInfo, bool hideWhenInvalid = false)
        {
            if (m_CurrentGhost == null)
            {
                throw new InvalidOperationException("Trying to move the Target ghost when we don't have one");
            }
            UIPointer pointer = WrapPointer(pointerInfo);

            // Do nothing if we're over UI
            if (pointer.overUI && hideWhenInvalid)
            {
                m_CurrentGhost.Hide();
                return;
            }
            MoveGhost(pointer, hideWhenInvalid);
        }
Ejemplo n.º 4
0
        protected void MoveGhost(UIPointer pointer, bool hideWhenInvalid = false)
        {
            if (m_CurrentGhost == null || !isBuilding)
            {
                throw new InvalidOperationException(
                          "Trying to position a Target ghost while the UI is not currently in the building state.");
            }

            PlacementAreaRaycast(ref pointer);
            if (pointer.raycast != null)
            {
                MoveGhostWithRaycastHit(pointer.raycast.Value);
            }
            else
            {
                MoveGhostOntoWorld(pointer.ray, hideWhenInvalid);
            }
        }
Ejemplo n.º 5
0
        public void BuyTarget(UIPointer pointer)
        {
            if (!isBuilding)
            {
                throw new InvalidOperationException("Trying to buy towers when not in a Build Mode");
            }
            if (m_CurrentGhost == null)
            {
                return;
            }
            PlacementAreaRaycast(ref pointer);
            if (!pointer.raycast.HasValue || pointer.raycast.Value.collider == null)
            {
                CancelGhostPlacement();
                return;
            }


            PlaceTargetGhost(pointer);
        }
Ejemplo n.º 6
0
        protected void PlacementAreaRaycast(ref UIPointer pointer)
        {
            pointer.raycast = null;

            if (pointer.overUI)
            {
                return;
            }
            //RaycastHit2D hit = Physics2D.Raycast(pointer.ray.origin, Vector2.zero, float.MaxValue, placementAreaMask);
            //if (hit)
            //{
            //    pointer.raycast = hit;
            //}

            RaycastHit hit;

            if (Physics.Raycast(pointer.ray, out hit, float.MaxValue, placementAreaMask))
            {
                pointer.raycast = hit;
            }
        }
Ejemplo n.º 7
0
        protected void PlaceTargetGhost(UIPointer pointer)
        {
            if (m_CurrentGhost == null || !isBuilding)
            {
                throw new InvalidOperationException(
                          "Trying to position a Target ghost while the UI is not currently in a building state.");
            }

            MoveGhost(pointer);

            if (m_CurrentArea != null)
            {
                bool isFits = IsGhostAtValidPosition();

                if (isFits)
                {
                    if (currentSelectedTarget)
                    {
                        currentSelectedTarget.UpdateTargetPos(m_CurrentArea, m_GridPosition);
                        currentSelectedTarget.Show();
                        CancelGhostPlacement();
                    }
                    else
                    {
                        int  cost = m_CurrentGhost.defaultLevel.Cost;
                        bool successfulPurchase = BattleField.instance.currency.TryPurchase(cost);
                        if (successfulPurchase)
                        {
                            GameObject createdTarget = Instantiate(Resources.Load <GameObject>("Prefab/Game/Tower"), m_CurrentArea.transform);

                            TowerAgent tower      = createdTarget.GetComponent <TowerAgent>();
                            object[]   myObjArray = { m_CurrentArea, m_GridPosition };
                            tower.Initialize(m_CurrentGhost.towerId, myObjArray);

                            CancelGhostPlacement();
                        }
                        else
                        {
                            KindlyReminderUI.instance.ShowTip(new ShowTipParams {
                                msg = "金币不足"
                            });
                            CancelGhostPlacement();
                        }
                    }
                }
                else
                {
                    if (!currentSelectedTarget)
                    {
                        return;
                    }
                    currentSelectedTarget.UpdateTargetPos(m_CurrentArea, m_GridPosition);
                    currentSelectedTarget.Show();
                    CancelGhostPlacement();

                    Tower target = m_CurrentArea.GetController().GetComponent <Tower>();
                    if (target != currentSelectedTarget)
                    {
                        if (target.currentLevel == currentSelectedTarget.currentLevel)
                        {
                            if (!target.isAtMaxLevel)
                            {
                                int  upgradeCost       = target.GetCostForNextLevel();
                                bool successfulUpgrade = BattleField.instance.currency.TryPurchase(upgradeCost);
                                if (successfulUpgrade)
                                {
                                    target.UpgradeTarget();
                                    CancelGhostPlacement(true);
                                }
                                else
                                {
                                    KindlyReminderUI.instance.ShowTip(new ShowTipParams {
                                        msg = "没钱升什么级"
                                    });
                                    CancelGhostPlacement();
                                }
                            }
                            else
                            {
                                KindlyReminderUI.instance.ShowTip(new ShowTipParams {
                                    msg = "炮台已经是最高等级了"
                                });
                            }
                        }
                        else
                        {
                            CancelGhostPlacement();
                            KindlyReminderUI.instance.ShowTip(new ShowTipParams {
                                msg = "不是同一级别的炮台"
                            });
                        }
                    }
                    else
                    {
                        CancelGhostPlacement();
                    }
                }
            }
        }