Example #1
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);
        }
Example #2
0
        public BuildInfo _CheckBuildPoint(Vector3 pointer, int towerID)
        {
            BuildInfo buildInfo = new BuildInfo();

            if (disableBuildWhenInPlay && SpawnManager.GetActiveUnitCount() > 0)
            {
                buildInfo.status = _TileStatus.NotInBuildPhase;
                return(buildInfo);
            }

            Camera mainCam = Camera.main;

            if (mainCam != null)
            {
                Ray        ray = mainCam.ScreenPointToRay(pointer);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, maskPlatform))
                {
                    for (int i = 0; i < buildPlatforms.Count; i++)
                    {
                        if (hit.transform == buildPlatforms[i].thisT)
                        {
                            PlatformTD platform = buildPlatforms[i];

                            //calculating the build center point base on the input position
                            Vector3 pos = GetTilePos(platform, hit.point);

                            buildInfo.position = pos;
                            buildInfo.platform = platform;

                            //checking if tower can be built on the platform, for dragNdrop mode
                            if (towerID >= 0 && !platform.availableTowerIDList.Contains(towerID))
                            {
                                buildInfo.status = _TileStatus.Unavailable;
                            }

                            if (buildInfo.status == _TileStatus.Available)
                            {
                                //check if the position is blocked, by any other obstabcle other than the baseplane itself
                                Collider[] cols = Physics.OverlapSphere(pos, gridSize / 2 * 0.9f, ~maskAll);
                                if (cols.Length > 0)
                                {
                                    buildInfo.status = _TileStatus.NoPlatform;
                                }
                                else
                                {
                                    buildInfo.status = _TileStatus.Available;
                                }

                                if (buildInfo.status == _TileStatus.Available)
                                {
                                    //check if the platform is walkable, if so, check if building on the point wont block all possible path
                                    if (platform.IsWalkable())
                                    {
                                        if (platform.CheckForBlock(pos))
                                        {
                                            buildInfo.status = _TileStatus.Blocked;
                                        }
                                    }

                                    //map platform availableTowerIDList (which is the towers' prefabID) to the list elements' ID in towerList
                                    buildInfo.availableTowerIDList = new List <int>();
                                    for (int m = 0; m < platform.availableTowerIDList.Count; m++)
                                    {
                                        for (int n = 0; n < towerList.Count; n++)
                                        {
                                            if (platform.availableTowerIDList[m] == towerList[n].prefabID)
                                            {
                                                buildInfo.availableTowerIDList.Add(n);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            break;
                        }
                    }
                }
                else
                {
                    buildInfo.status = _TileStatus.NoPlatform;
                }
            }
            else
            {
                buildInfo.status = _TileStatus.NoPlatform;
            }

            //reverse block status for mine
            if (buildInfo.status == _TileStatus.Blocked)
            {
                //for drag n drop mode
                if (towerID >= 0 && GetTowerPrefab(towerID).type == _TowerType.Mine)
                {
                    buildInfo.status = _TileStatus.Available;
                }
                if (towerID < 0)
                {
                    bool gotMineInList = false;
                    for (int i = 0; i < buildInfo.availableTowerIDList.Count; i++)
                    {
                        if (towerList[buildInfo.availableTowerIDList[i]].type == _TowerType.Mine)
                        {
                            gotMineInList = true;
                        }
                        else
                        {
                            buildInfo.availableTowerIDList.RemoveAt(i);
                            i -= 1;
                        }
                    }
                    if (gotMineInList)
                    {
                        buildInfo.status = _TileStatus.Available;
                    }
                }
            }


            if (!UseDragNDrop())             //for PointNClick
            {
                if (buildInfo.status != _TileStatus.Available)
                {
                    IndicatorControl.ClearBuildTileIndicator();
                }
                else
                {
                    IndicatorControl.SetBuildTileIndicator(buildInfo);
                }
            }

            return(buildInfo);
        }