Ejemplo n.º 1
0
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (BuildManager.UseDragNDrop() && BuildManager.InDragNDrop())
                {
                    return;
                }
                if (!UITowerView.IsOn() && !AbilityManager.IsSelectingTarget() && !UIPerkMenu.IsOn() && !UIFPS.IsOn())
                {
                    TogglePause();
                }
            }

            //keyboard for cursor based input start here
            if (!enableInput)
            {
                return;
            }

            if (Input.touchCount > 1)
            {
                return;
            }
            if (UI.IsCursorOnUI(0) || UI.IsCursorOnUI())
            {
                return;
            }

            if (Input.GetMouseButtonDown(0))
            {
                OnCursorDown(Input.mousePosition);
            }
        }
Ejemplo n.º 2
0
        //~ void OnGUI(){
        //~ GUI.Label(new Rect(10, Screen.height/2, 130, 35), "ability:");
        //~ if(GUI.Button(new Rect(80, Screen.height/2, 130, 35), ""+targetMode)){
        //~ if(targetMode==_TargetMode.DragNDrop) targetMode=_TargetMode.SelectNDeploy;
        //~ else if(targetMode==_TargetMode.SelectNDeploy) targetMode=_TargetMode.DragNDrop;
        //~ }

        //~ GUI.Label(new Rect(10, Screen.height/2+40, 130, 35), "build:");
        //~ if(GUI.Button(new Rect(80, Screen.height/2+40, 130, 35), ""+buildMode)){
        //~ if(buildMode==_BuildMode.DragNDrop) buildMode=_BuildMode.PointNBuild;
        //~ else if(buildMode==_BuildMode.PointNBuild) buildMode=_BuildMode.PointNBuild;
        //~ }
        //~ }



        void Update()
        {
            if (!isGameScene)
            {
                return;
            }

            if (TowerManager.InDragNDropPhase())
            {
                //for mobile, to prevent getting stuck in DnD phase if user click but not drag the button
                //if(Input.touchCount==0) TowerManager.ExitDragNDropPhase();
                return;
            }

            //for mobile, to prevent getting stuck in target select phase if user click but not drag the button
            //if(AbilityManager.InTargetSelectionMode()){
            //	if(Input.touchCount==0) AbilityManager.ClearSelect();
            //}

            int pointerID = Input.touchCount == 0 ? -1 : 0;

            if (Input.GetMouseButtonDown(0) && !UI.IsCursorOnUI(pointerID))
            {
                if (!InTargetSelectionMode())
                {
                    OnCursorDown();
                }
            }

            if (Input.GetMouseButtonUp(0) && !wasCursorOnUI)
            {
                if (AbilityManager.InTargetSelectionMode())
                {
                    OnCursorDownAbilityTargetMode();
                }
                else if (pendingAbilityIdx >= 0)
                {
                    OnCursorDownAbilityTargetMode(pendingAbilityIdx);
                    pendingAbilityIdx = -1;
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                UIAbilityButton.ClearSelect();                          //ClearSelectedAbility();
            }
            wasCursorOnUI = UI.IsCursorOnUI(pointerID);                 //to be used for next frame
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        // Update is called once per frame
        void Update()
        {
            if (fpsOn)
            {
                return;
            }

            if (Time.timeScale == 1)
            {
                deltaT = Time.deltaTime;
            }
            else if (Time.timeScale > 1)
            {
                deltaT = Time.deltaTime / Time.timeScale;
            }
            else
            {
                deltaT = 0.015f;
            }


                        #if UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_BLACKBERRY
            if (!UI.IsCursorOnUI(0) && !BuildManager.InDragNDrop())
            {
                if (enableTouchPan)
                {
                    Quaternion camDir = Quaternion.Euler(0, transform.eulerAngles.y, 0);
                    if (Input.touchCount == 1)
                    {
                        Touch touch = Input.touches[0];
                        if (touch.phase == TouchPhase.Moved)
                        {
                            Vector3 deltaPos = touch.position;

                            if (lastTouchPos != new Vector3(9999, 9999, 9999))
                            {
                                deltaPos      = deltaPos - lastTouchPos;
                                moveMagnitude = new Vector3(deltaPos.x, 0, deltaPos.y).magnitude *0.1f;
                                moveDir       = new Vector3(deltaPos.x, 0, deltaPos.y).normalized *-1;
                            }

                            lastTouchPos = touch.position;

                            if (moveMagnitude > 10)
                            {
                                UIMainControl.ClearSelectedTower();
                            }
                        }
                    }
                    else
                    {
                        lastTouchPos = new Vector3(9999, 9999, 9999);
                    }

                    Vector3 dir = thisT.InverseTransformDirection(camDir * moveDir) * moveMagnitude;
                    thisT.Translate(dir * panSpeed * deltaT);

                    moveMagnitude = moveMagnitude * (1 - deltaT * 10);
                }

                if (enableTouchZoom)
                {
                    if (Input.touchCount == 2)
                    {
                        Touch touch1 = Input.touches[0];
                        Touch touch2 = Input.touches[1];

                        //~ Vector3 zoomScreenPos=(touch1.position+touch2.position)/2;

                        if (touch1.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved)
                        {
                            Vector3 dirDelta = (touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition);
                            Vector3 dir      = touch1.position - touch2.position;
                            float   dot      = Vector3.Dot(dirDelta.normalized, dir.normalized);

                            if (Mathf.Abs(dot) > 0.7f)
                            {
                                touchZoomSpeed = dir.magnitude - dirDelta.magnitude;
                            }
                        }
                    }

                    currentZoom += Time.deltaTime * zoomSpeed * touchZoomSpeed;

                    touchZoomSpeed = touchZoomSpeed * (1 - Time.deltaTime * 15);
                }

                if (enableTouchRotate)
                {
                    if (Input.touchCount == 2)
                    {
                        Touch touch1 = Input.touches[0];
                        Touch touch2 = Input.touches[1];

                        Vector2 delta1 = touch1.deltaPosition.normalized;
                        Vector2 delta2 = touch2.deltaPosition.normalized;
                        Vector2 delta  = (delta1 + delta2) / 2;

                        float rotX = thisT.rotation.eulerAngles.x - delta.y * rotationSpeed;
                        float rotY = thisT.rotation.eulerAngles.y + delta.x * rotationSpeed;
                        rotX = Mathf.Clamp(rotX, minRotateAngle, maxRotateAngle);

                        thisT.rotation = Quaternion.Euler(rotX, rotY, 0);
                    }
                }
            }
                        #endif



                        #if UNITY_EDITOR || !(UNITY_IPHONE && UNITY_ANDROID && UNITY_WP8 && UNITY_BLACKBERRY)
            //mouse and keyboard
            if (enableMouseRotate)
            {
                if (Input.GetMouseButtonDown(1))
                {
                    initialMousePosX = Input.mousePosition.x;
                    initialMousePosY = Input.mousePosition.y;
                    initialRotX      = thisT.eulerAngles.y;
                    initialRotY      = thisT.eulerAngles.x;
                }

                if (Input.GetMouseButton(1))
                {
                    float deltaX    = Input.mousePosition.x - initialMousePosX;
                    float deltaRotX = (.1f * (initialRotX / Screen.width));
                    float rotX      = deltaX * rotationSpeed + deltaRotX;

                    float deltaY    = initialMousePosY - Input.mousePosition.y;
                    float deltaRotY = -(.1f * (initialRotY / Screen.height));
                    float rotY      = deltaY * rotationSpeed + deltaRotY;
                    float y         = rotY + initialRotY;

                    //limit the rotation
                    if (y > maxRotateAngle)
                    {
                        initialRotY -= (rotY + initialRotY) - maxRotateAngle;
                        y            = maxRotateAngle;
                    }
                    else if (y < minRotateAngle)
                    {
                        initialRotY += minRotateAngle - (rotY + initialRotY);
                        y            = minRotateAngle;
                    }

                    thisT.rotation = Quaternion.Euler(y, rotX + initialRotX, 0);
                }
            }

            Quaternion direction = Quaternion.Euler(0, thisT.eulerAngles.y, 0);


            if (enableKeyPanning)
            {
                if (Input.GetButton("Horizontal"))
                {
                    Vector3 dir = transform.InverseTransformDirection(direction * Vector3.right);
                    thisT.Translate(dir * panSpeed * deltaT * Input.GetAxisRaw("Horizontal"));
                }

                if (Input.GetButton("Vertical"))
                {
                    Vector3 dir = transform.InverseTransformDirection(direction * Vector3.forward);
                    thisT.Translate(dir * panSpeed * deltaT * Input.GetAxisRaw("Vertical"));
                }
            }
            if (enableMousePanning)
            {
                Vector3 mousePos = Input.mousePosition;
                Vector3 dirHor   = transform.InverseTransformDirection(direction * Vector3.right);
                if (mousePos.x <= 0)
                {
                    thisT.Translate(dirHor * panSpeed * deltaT * -3);
                }
                else if (mousePos.x <= mousePanningZoneWidth)
                {
                    thisT.Translate(dirHor * panSpeed * deltaT * -1);
                }
                else if (mousePos.x >= Screen.width)
                {
                    thisT.Translate(dirHor * panSpeed * deltaT * 3);
                }
                else if (mousePos.x > Screen.width - mousePanningZoneWidth)
                {
                    thisT.Translate(dirHor * panSpeed * deltaT * 1);
                }

                Vector3 dirVer = transform.InverseTransformDirection(direction * Vector3.forward);
                if (mousePos.y <= 0)
                {
                    thisT.Translate(dirVer * panSpeed * deltaT * -3);
                }
                else if (mousePos.y <= mousePanningZoneWidth)
                {
                    thisT.Translate(dirVer * panSpeed * deltaT * -1);
                }
                else if (mousePos.y >= Screen.height)
                {
                    thisT.Translate(dirVer * panSpeed * deltaT * 3);
                }
                else if (mousePos.y > Screen.height - mousePanningZoneWidth)
                {
                    thisT.Translate(dirVer * panSpeed * deltaT * 1);
                }
            }


            if (enableMouseZoom)
            {
                float zoomInput = Input.GetAxis("Mouse ScrollWheel");
                if (zoomInput != 0)
                {
                    currentZoom += zoomSpeed * zoomInput;
                    currentZoom  = Mathf.Clamp(currentZoom, -maxZoomDistance, -minZoomDistance);
                }
            }
                        #endif


            if (avoidClipping)
            {
                Vector3    aPos = thisT.TransformPoint(new Vector3(0, 0, currentZoom));
                Vector3    dirC = aPos - thisT.position;
                float      dist = Vector3.Distance(aPos, thisT.position);
                RaycastHit hit;
                obstacle = Physics.Raycast(thisT.position, dirC, out hit, dist);

                if (!obstacle)
                {
                    currentZoom = Mathf.Clamp(currentZoom, -maxZoomDistance, -minZoomDistance);
                    float camZ = Mathf.Lerp(camT.localPosition.z, currentZoom, Time.deltaTime * 4);
                    camT.localPosition = new Vector3(camT.localPosition.x, camT.localPosition.y, camZ);
                }
                else
                {
                    dist = Vector3.Distance(hit.point, thisT.position) * 0.85f;
                    float camZ = Mathf.Lerp(camT.localPosition.z, -dist, Time.deltaTime * 50);
                    camT.localPosition = new Vector3(camT.localPosition.x, camT.localPosition.y, camZ);
                }
            }
            else
            {
                currentZoom = Mathf.Clamp(currentZoom, -maxZoomDistance, -minZoomDistance);
                float camZ = Mathf.Lerp(camT.localPosition.z, currentZoom, Time.deltaTime * 4);
                camT.localPosition = new Vector3(camT.localPosition.x, camT.localPosition.y, camZ);
            }


            float x = Mathf.Clamp(thisT.position.x, minPosX, maxPosX);
            float z = Mathf.Clamp(thisT.position.z, minPosZ, maxPosZ);

            thisT.position = new Vector3(x, thisT.position.y, z);
        }
Ejemplo n.º 5
0
        IEnumerator SelectAbilityTargetRoutine(Ability ability, int pointerID = -1)
        {
            yield return(null);

            Vector3 cursorPos  = Vector3.zero;
            Unit    targetUnit = null;

            LayerMask mask = maskAOE;

            if (ability.singleUnitTargeting)
            {
                if (ability.targetType == Ability._TargetType.Hybrid)
                {
                    mask |= 1 << TDTK.GetLayerTower() | 1 << TDTK.GetLayerCreep();
                }
                else if (ability.targetType == Ability._TargetType.Friendly)
                {
                    mask |= 1 << TDTK.GetLayerTower();
                }
                else if (ability.targetType == Ability._TargetType.Hostile)
                {
                    mask |= 1 << TDTK.GetLayerCreep();
                }
            }

            Transform indicator = ability.indicator;

            if (indicator == null)
            {
                indicator = defaultIndicator;
                float scale = ability.singleUnitTargeting ? BuildManager.GetGridSize() : ability.GetAOERadius() * 2;
                indicator.localScale = new Vector3(scale, scale, scale);
            }

            //TDTK.OnGameMessage("SelectAbilityTargetRoutine   "+pointerID);

            isSelectingTarget = true;
            TDTK.OnAbilityTargetSelectModeE(true);

            if (pointerID >= 0)
            {
                while (true)
                {
                    if (TDTK.IsTouchStarting(pointerID))
                    {
                        break;
                    }
                    yield return(null);
                }
            }

            bool cursorOnUI = true;

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

                bool invalidCursor = false;
                bool invalidTarget = false;

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

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


                if (!invalidCursor && !cursorOnUI)
                {
                    Ray        ray = Camera.main.ScreenPointToRay(cursorPos);
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
                    {
                        indicator.position = hit.point;

                        targetUnit = null;

                        if (ability.singleUnitTargeting)
                        {
                            targetUnit = hit.transform.GetComponent <Unit>();
                            if (targetUnit != null)
                            {
                                indicator.position = targetUnit.thisT.position;
                            }
                            else
                            {
                                invalidTarget = true;
                            }
                        }
                    }
                }

                indicator.gameObject.SetActive(!invalidCursor);

                if (pointerID == -1)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        if (cursorOnUI)
                        {
                            break;
                        }
                        if (!invalidTarget)
                        {
                            ActivateAbility(ability, indicator.position, targetUnit);
                        }
                        else
                        {
                            TDTK.OnGameMessage("Invalid target for ability");
                        }
                        break;
                    }
                    if (Input.GetMouseButtonDown(1))
                    {
                        break;
                    }
                }
                else
                {
                    if (TDTK.IsTouchEnding(pointerID))
                    {
                        //TDTK.OnGameMessage("SelectAbilityTargetRoutine   "+pointerID+"   "+UI.IsCursorOnUI(pointerID));
                        if (cursorOnUI)
                        {
                            break;
                        }
                        if (!invalidTarget)
                        {
                            ActivateAbility(ability, indicator.position, targetUnit);
                        }
                        else
                        {
                            TDTK.OnGameMessage("Invalid target for ability");
                        }
                        break;
                    }
                }

                //check in previous frame cause IsCursorOnUI wont return true if the touch is ending
                cursorOnUI = UI.IsCursorOnUI(pointerID);

                yield return(null);
            }

            yield return(null);

            indicator.gameObject.SetActive(false);

            isSelectingTarget = false;
            TDTK.OnAbilityTargetSelectModeE(false);
        }