コード例 #1
0
        public static TargetingInfo OnCursorDown(Vector3 pointer, int idx = -1)
        {
            if (idx < 0)
            {
                if (pendingTgtAbilityIdx >= 0)
                {
                    idx = pendingTgtAbilityIdx;
                }
                else
                {
                    return(new TargetingInfo());
                }
            }

            Ray        ray = CameraControl.GetMainCam().ScreenPointToRay(pointer);
            RaycastHit hit; LayerMask mask = 1 << TDTK.GetLayerTerrain();

            if (GetAbility(idx).requireUnitAsTarget)
            {
                if (GetAbility(idx).MustTargetHostile())
                {
                    mask |= 1 << TDTK.GetLayerCreep();
                }
                else if (GetAbility(idx).MustTargetFriendly())
                {
                    mask |= 1 << TDTK.GetLayerTower();
                }
                else
                {
                    mask |= 1 << TDTK.GetLayerTower() | 1 << TDTK.GetLayerCreep();
                }
            }

            if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
            {
                if (hit.collider.gameObject.layer == TDTK.GetLayerTerrain())
                {
                    return(new TargetingInfo(hit.point, !GetAbility(idx).requireUnitAsTarget));
                }
                else
                {
                    return(new TargetingInfo(hit.collider.gameObject.GetComponent <Unit>()));
                }
            }

            return(new TargetingInfo());
        }
コード例 #2
0
ファイル: TowerManager.cs プロジェクト: asd540578/TD_Project
        public static SelectInfo GetSelectInfo(Vector3 pointer, int ID = -1, float towerSize = 1)
        {
            InitMask();

            Ray        ray = CameraControl.GetMainCam().ScreenPointToRay(pointer);
            RaycastHit hit;

            //for free from drag and drop mode
            if (UseFreeFormMode() && instance.dndTower != null)
            {
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, maskTerrain))
                {
                    Collider[] obstacles = Physics.OverlapSphere(hit.point, towerSize, ~maskTerrain);
                    if (obstacles.Length == 1 && obstacles[0].gameObject == instance.dndTower.gameObject)
                    {
                        obstacles = new Collider[0];
                    }

                    if (obstacles.Length == 0)
                    {
                        return(new SelectInfo(hit.point));
                    }
                    else
                    {
                        return(new SelectInfo("Invalid build-point!", hit.point));
                    }
                }
                return(new SelectInfo("No valid build-point has been found"));
            }

            //try to detect a platform and determine the node on it
            bool flag = Physics.Raycast(ray, out hit, Mathf.Infinity, RaycastForTerrain() ? mask : maskPlatform);

            if (flag)
            {
                if (hit.collider.gameObject.layer == TDTK.GetLayerPlatform())
                {
                    BuildPlatform platform = hit.collider.gameObject.GetComponent <BuildPlatform>();
                    if (platform != null)
                    {
                        Vector3 pos  = platform.GetTilePos(hit.point, GetGridSize());
                        NodeTD  node = platform.GetNearestNode(pos);

                        if (ID > 0)
                        {
                            if (lastSelectID == ID && sInfo.nodeID == node.ID)
                            {
                                return(sInfo);
                            }
                            else
                            {
                                lastSelectID = ID;
                            }
                        }

                        return(new SelectInfo(platform, node.ID));
                    }
                }
                else if (RaycastForTerrain())
                {
                    return(new SelectInfo("No platform has been found", hit.point));
                }
            }

            return(new SelectInfo("No platform has been found"));
        }