コード例 #1
0
        //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);
        }
コード例 #2
0
        public void _SetTileIndicator(Vector3 cursor)
        {
            Ray        ray = Camera.main.ScreenPointToRay(cursor);
            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)
                    {
                        continue;
                    }
                    Vector3 tilePos = GetTilePos(buildPlatforms[i], hit.point);

                    Collider[] cols = Physics.OverlapSphere(tilePos, gridSize / 2 * 0.9f, ~maskIndicator);
                    if (cols.Length == 0)
                    {
                        IndicatorControl.SetIndicatorCursor(tilePos, buildPlatforms[i].thisT.rotation);
                    }
                    else
                    {
                        break;
                    }

                    return;
                }
            }
            IndicatorControl.ClearIndicatorCursor();
        }
コード例 #3
0
        public static string _BuildTower(UnitTower tower, BuildInfo bInfo)              //called from UnitTower.DragNDropRoutine
        {
            if (bInfo == null)
            {
                if (!UseDragNDrop())
                {
                    return("Select a Build Point First");
                }
                else
                {
                    return("Invalid build position");
                }
            }

            if (bInfo.status != _TileStatus.Available)
            {
                return("Invalid build position");
            }

            //dont allow building of resource tower before game started
            if (tower.type == _TowerType.Resource && !GameControl.IsGameStarted())
            {
                return("Cant Build Tower before spawn start");
            }

            UnitTower sampleTower = GetSampleTower(tower);

            //check if there are sufficient resource
            List <int> cost     = sampleTower.GetCost();
            int        suffCost = ResourceManager.HasSufficientResource(cost);

            if (suffCost == -1)
            {
                ResourceManager.SpendResource(cost);

                GameObject towerObj      = (GameObject)Instantiate(tower.gameObject, bInfo.position, bInfo.platform.thisT.rotation);
                UnitTower  towerInstance = towerObj.GetComponent <UnitTower>();
                towerInstance.InitTower(instance.towerCount += 1);
                towerInstance.Build();

                //if(bInfo.platform!=null) towerObj.transform.parent=bInfo.platform.transform;

                //register the tower to the platform
                if (bInfo.platform != null)
                {
                    bInfo.platform.BuildTower(bInfo.position, towerInstance);
                }

                //clear the build info and indicator for build manager
                //ClearBuildIndicator();
                IndicatorControl.ClearBuildTileIndicator();

                return("");
            }

            return("Insufficient Resource");
        }
コード例 #4
0
 public void _SelectTower(UnitTower tower = null)
 {
     if (tower == null)
     {
         IndicatorControl.ClearRangeIndicator();
     }
     else
     {
         IndicatorControl.ShowTowerRangeIndicator(tower);
     }
 }
コード例 #5
0
ファイル: UnitTower.cs プロジェクト: ledotrong/tower-defense
        public void RemoveFromGame()
        {
            IndicatorControl.TowerRemoved(this);

            destroyed = true;
            if (occupiedPlatform != null)
            {
                occupiedPlatform.UnbuildTower(occupiedNode);
            }

            base.Destroyed(PlayAnimDestroyed());
        }
コード例 #6
0
        public void Init()
        {
            instance = this;
            thisT    = transform;

            float gridSize = BuildManager.GetGridSize();

            if (indicatorSelected != null)
            {
                indicatorSelected            = (Transform)Instantiate(indicatorSelected);
                indicatorSelected.localScale = new Vector3(gridSize, gridSize, gridSize);
                indicatorSelected.parent     = thisT;
                indicatorSelected.name       = "TileIndicator_Selected";

                indicatorSelectedRenderer = indicatorSelected.GetChild(0).GetComponent <Renderer>();

                indicatorSelectedObj = indicatorSelected.gameObject;
                indicatorSelectedObj.SetActive(false);
            }

            if (indicatorCursor != null)
            {
                indicatorCursor            = (Transform)Instantiate(indicatorCursor);
                indicatorCursor.localScale = new Vector3(gridSize, gridSize, gridSize);
                indicatorCursor.parent     = thisT;
                indicatorCursor.name       = "TileIndicator_Cursor";

                indicatorCursorObj = indicatorCursor.gameObject;
                indicatorCursorObj.SetActive(false);
            }


            if (rangeIndicator != null)
            {
                rangeIndicator        = (Transform)Instantiate(rangeIndicator);
                rangeIndicator.parent = thisT;
                rangeIndicatorObj     = rangeIndicator.gameObject;
            }
            if (rangeIndicatorCone != null)
            {
                rangeIndicatorCone        = (Transform)Instantiate(rangeIndicatorCone);
                rangeIndicatorCone.parent = thisT;
                rangeIndicatorConeObj     = rangeIndicatorCone.gameObject;
            }

            _ClearRangeIndicator();


            //~ #if !UNITY_STANDALONE_WIN && UNITY_STANDALONE_LINUX && UNITY_STANDALONE && UNITY_WEBPLAYER
            //~ disableCursorIndicator=true;
            //~ #endif
        }
コード例 #7
0
ファイル: UnitTower.cs プロジェクト: ledotrong/tower-defense
        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);
        }
コード例 #8
0
 public static void TowerScanAngleChanged(UnitTower tower)
 {
     IndicatorControl.TowerScanAngleChanged(tower);
 }
コード例 #9
0
        void Awake()
        {
            Time.fixedDeltaTime = timeStep;

            instance = this;
            thisT    = transform;

            ObjectPoolManager.Init();

            NodeGenerator nodeGenerator = (NodeGenerator)FindObjectOfType(typeof(NodeGenerator));

            if (nodeGenerator != null)
            {
                nodeGenerator.Awake();
            }
            PathFinder pathFinder = (PathFinder)FindObjectOfType(typeof(PathFinder));

            if (pathFinder != null)
            {
                pathFinder.Awake();
            }

            PathTD[] paths = FindObjectsOfType(typeof(PathTD)) as PathTD[];
            if (paths.Length > 0)
            {
                for (int i = 0; i < paths.Length; i++)
                {
                    paths[i].Init();
                }
            }

            TDTK.InitDB();

            ResourceManager rscManager = (ResourceManager)FindObjectOfType(typeof(ResourceManager));

            if (rscManager != null)
            {
                rscManager.Init();
            }

            PerkManager perkManager = (PerkManager)FindObjectOfType(typeof(PerkManager));

            if (perkManager != null)
            {
                perkManager.Init();
            }

            BuildManager buildManager = (BuildManager)FindObjectOfType(typeof(BuildManager));

            if (buildManager != null)
            {
                buildManager.Init();
            }

            AbilityManager abilityManager = (AbilityManager)FindObjectOfType(typeof(AbilityManager));

            if (abilityManager != null)
            {
                abilityManager.Init();
            }

            FPSControl fpsControl = (FPSControl)FindObjectOfType(typeof(FPSControl));

            if (fpsControl != null)
            {
                fpsControl.Init();
            }

            IndicatorControl indicatorControl = (IndicatorControl)FindObjectOfType(typeof(IndicatorControl));

            if (indicatorControl != null)
            {
                indicatorControl.Init();
            }


            //if(loadAudioManager){
            //	Instantiate(Resources.Load("AudioManager", typeof(GameObject)));
            //}

            Time.timeScale = 1;
        }
コード例 #10
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);
        }