//called when a tower building is initated in DragNDrop, use the sample tower as the model and set it in DragNDrop mode
        public string StartDragNDrop(int ID, int pointerID = -1)
        {
            UnitTower sampleTower = GetSampleTower(ID);

            if (sampleTower.type == _TowerType.Resource && !GameControl.IsGameStarted())
            {
                return("Cant Build Tower before spawn start");
            }

            IndicatorControl.SetDragNDropPhase(true);

            List <int> cost     = sampleTower.GetCost();
            int        suffCost = ResourceManager.HasSufficientResource(cost);

            if (suffCost == -1)
            {
                sampleTower.thisT.position = new Vector3(9999, 9999, 9999);
                sampleTower.thisObj.SetActive(true);

                UnitTower towerInstance = sampleTower;

                towerInstance.StartCoroutine(towerInstance.DragNDropRoutine(pointerID));

                return("");
            }

            return("Insufficient Resource   " + suffCost);
        }
Exemple #2
0
        public IEnumerator DragNDropRoutine(int pointerID = -1)
        {
            GameControl.SelectTower(this);
            yield return(null);

            Vector3 cursorPos = Vector3.zero;

            TDTK.OnDragNDrop(true);
            inDragNDropRoutine = true;

            while (inDragNDropRoutine)
            {
                if (Input.GetKeyDown(KeyCode.Escape))
                {
                    break;
                }

                bool invalidCursor = false;

                if (pointerID < 0)
                {
                    cursorPos = Input.mousePosition;
                }
                else
                {
                    cursorPos = TDTK.GetTouchPosition(pointerID);
                }

                if (cursorPos.magnitude < 0)
                {
                    invalidCursor = true;
                }

                BuildInfo buildInfo = null;

                if (!invalidCursor)
                {
                    buildInfo = BuildManager.CheckBuildPoint(cursorPos, prefabID);

                    if (buildInfo.status == _TileStatus.NoPlatform)
                    {
                        Ray        ray = Camera.main.ScreenPointToRay(cursorPos);
                        RaycastHit hit;
                        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                        {
                            thisT.position = hit.point;
                        }
                        else
                        {
                            thisT.position = ray.GetPoint(30);                          //this there is no collier, randomly place it 30unit from camera
                        }
                    }
                    else
                    {
                        thisT.position = buildInfo.position;
                        thisT.rotation = buildInfo.platform.thisT.rotation;
                    }

                    IndicatorControl.SetBuildTileIndicator(buildInfo);
                }

                bool cursorOnUI = UI.IsCursorOnUI(pointerID);

                if (pointerID < 0)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        if (cursorOnUI)
                        {
                            break;
                        }
                        string exception = BuildManager._BuildTower(srcTower, buildInfo);
                        if (exception != "")
                        {
                            TDTK.OnGameMessage(exception);
                        }
                        break;
                    }
                    if (Input.GetMouseButtonDown(1))
                    {
                        break;
                    }
                }
                else
                {
                    if (TDTK.IsTouchEnding(pointerID))
                    {
                        if (cursorOnUI)
                        {
                            break;
                        }
                        string exception = BuildManager._BuildTower(srcTower, buildInfo);
                        if (exception != "")
                        {
                            TDTK.OnGameMessage(exception);
                        }
                        break;
                    }
                }

                yield return(null);
            }

            inDragNDropRoutine = false;

            TDTK.OnDragNDrop(false);
            IndicatorControl.SetDragNDropPhase(false);
            thisObj.SetActive(false);
        }