Exemple #1
0
    void Update()
    {
        //turning the ship
        if (leftClick == 1)
        {
            control.Turn();
            //arcArrow.Turn ();
        }
        if (leftClick == 2)
        {
            arcArrow.launchArrow.transform.position = finalCoor;
        }

        /////////////////////////////////////For rotating the ship////////////////////////////////////////

        if (Input.GetMouseButtonDown(0) && leftClick == 0)
        {
            Vector2      mousePosition = cam.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hitInfo       = Physics2D.Raycast(mousePosition, Vector2.zero);

            if (hitInfo.collider != null)
            {
                Debug.Log("hit");

                //set the gameobject to the ship if it's a hit
                string name = hitInfo.collider.gameObject.name;
                control = GameObject.Find(name).GetComponent <ShipControls>();
                control.SelectedShip(hitInfo.collider.gameObject);
                phys      = GameObject.Find(name).GetComponent <ShipPhysics> ();
                rigidbody = GameObject.Find(name).GetComponent <Rigidbody2D>();
                phys.SetRigid(rigidbody);
                phys.SetCurrent(hitInfo.collider.gameObject);

                leftClick++;
            }
            else
            {
                Debug.Log("No hit");
            }
        } //if bracket

        ///////////////////////////////////For Selecting the direction/////////////////////////////////////

        else if (Input.GetMouseButtonDown(0) && leftClick == 1)
        {
            Debug.Log("LeftClick1");
            leftClick++;

            //Gets the position of ship and mouse
            Vector3 shipPos  = control.GetShipPosition();
            Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);

            //Move launch arc to clicked ship
            //arcArrow.ChangeArcPos(shipPos);
            //CalculateOffset (shipPos, mousePos);

            //math for launch arc
            float theta     = ConeDirection(mousePos, shipPos);
            float coneStart = theta - coneWidth / 2;
            float coneEnd   = theta + coneWidth / 2;

            tempDirection = theta;
            float spacing = Mathf.PI / 180;

            //begin moving launch arrow
            dirThread = new Thread(() => startDirectionCounter(spacing, coneStart, coneEnd, shipPos));
            dirThread.Start();
        }

        ////////////////////////////////////For selection power///////////////////////////
        else if (Input.GetMouseButtonDown(0) && leftClick == 2)
        {
            dirThread.Abort();
            leftClick++;
            powerInc = true;
        }

        //On the last click, we get the final power and move the ship
        else if (Input.GetMouseButtonDown(0) && leftClick == 3)
        {
            leftClick = 0;
            Debug.Log("Power:" + finalPower.ToString());
            Debug.Log("Angle:" + tempDirection.ToString());
            powerInc = false;
            powerDec = false;
            click    = true;

            moveShip(50);


            finalPower = 0;
        }

        if (powerInc)
        {
            finalPower += Time.deltaTime * barSpeed;

            //stops the power bar from going past its max length
            finalPower = Mathf.Clamp(finalPower, 0, fullWidth);

            //start decreasing power when the power reaches the maximum
            if (finalPower == fullWidth)
            {
                powerInc = false;
                powerDec = true;
            }
            powerBar.UpdatePower(finalPower, fullWidth);

            //set the width of the GUI Texture equal to that power value
            //GUITexture bar = GameObject.Find("powerBar").GetComponent<GUITexture>();
        }

        if (powerDec)
        {
            finalPower -= Time.deltaTime * barSpeed;

            //stops the power bar from goin past 0
            finalPower = Mathf.Clamp(finalPower, 0, fullWidth);

            //starts increasing power when the power reaches 0
            if (finalPower == 0)
            {
                powerInc = true;
                powerDec = false;
            }
            powerBar.UpdatePower(finalPower, fullWidth);
        }


        ////////////////////////////////////For right clicking/////////////////////////////

        if (Input.GetMouseButtonDown(1) && leftClick != 0)
        {
            leftClick--;
            powerInc   = false;
            powerDec   = false;
            finalPower = 0;
        }

        this.stopShip();
    }//update bracket