Ejemplo n.º 1
0
    void MoveAwayFrom(GameObject go)
    {
        if (movementFrozen)
        {
            return;
        }

        float fauxInput = 0;

        Vector3 dV = go.transform.position - this.transform.position;

        //to see if we changed directions
        bool lastMoveRight = wasMovingRight;
        bool lastMoveLeft  = wasMovingLeft;

        if (dV.x < 0)
        {
            fauxInput      = 1;
            wasMovingRight = true;
            wasMovingLeft  = false;
        }
        else if (dV.x > 0)
        {
            fauxInput      = -1;
            wasMovingRight = false;
            wasMovingLeft  = true;
        }
        else
        {
            wasMovingRight = false;
            wasMovingLeft  = false;
        }

        Teleporter[] tp1             = Teleporter.FindClosestPair(this.transform.position);
        float        minTeleDistance = 10;
        float        meAndPlayer     = Vector3.Distance(this.transform.position, go.transform.position);
        float        meAndTP1        = Vector3.Distance(this.transform.position, tp1[0].transform.position);
        float        meAndTP2        = Vector3.Distance(this.transform.position, tp1[1].transform.position);
        float        himAndTP1       = Vector3.Distance(go.transform.position, tp1[0].transform.position);
        float        himAndTP2       = Vector3.Distance(go.transform.position, tp1[1].transform.position);

        //to figure out if we should take a portal, add the distance between:
        //you and the portal closest to you + him and portal closest to him
        //IFF portals are different AND added distance is LESS than distance(you, him)
        //only then will you take the portal.

        if (meAndTP1 < meAndTP2 && himAndTP1 < himAndTP2)
        {
            //we are both closer to same portal - so dont go towards a portal. just keep running
        }
        else if (meAndTP2 < meAndTP1 && himAndTP2 < himAndTP1)
        {
            //also no good, same case but with different portals
        }
        if (meAndTP1 < meAndTP2 && himAndTP2 < himAndTP1)
        {
            //now we are getting somewhere. i am closer to TP1. He is closer to TP2.
            if (meAndTP1 + himAndTP2 < meAndPlayer)
            {
                //our combined teleporter distances are greater than our actual distance. go towards the portal
                Vector3 dP = go.transform.position - this.transform.position;
                if (dP.x > 0)
                {
                    fauxInput      = 1;
                    wasMovingRight = true;
                    wasMovingLeft  = false;
                }
                else if (dP.x < 0)
                {
                    fauxInput      = -1;
                    wasMovingRight = false;
                    wasMovingLeft  = true;
                }

                //PopText.Create("TAKE PORTAL", Color.white, 120,this.transform.position);
            }
        }
        else if (meAndTP2 < meAndTP1 && himAndTP1 < himAndTP2)
        {
            //now i am closer to TP2. He is closer to TP1.
            if (meAndTP2 + himAndTP1 < meAndPlayer)
            {
                //NICE! we should take the portal.
                Vector3 dP = go.transform.position - this.transform.position;
                if (dP.x > 0)
                {
                    fauxInput      = 1;
                    wasMovingRight = true;
                    wasMovingLeft  = false;
                }
                else if (dP.x < 0)
                {
                    fauxInput      = -1;
                    wasMovingRight = false;
                    wasMovingLeft  = true;
                }

                //PopText.Create("TAKE PORTAL", Color.white, 120,this.transform.position);
            }
        }

        //did we change directions?
        if (lastMoveRight != wasMovingRight && lastMoveLeft != wasMovingLeft)
        {
            //we changed
            directionSwitchedCount++;
        }

        bool shouldSprint = ShouldISprint();

        myPlayer.xInput  = fauxInput;
        myPlayer.xInput *= myPlayer.accelerationSpeed;

        if (shouldSprint && myPlayer.xInput != 0 && !myPlayer.exhausted)
        {
            myPlayer.COMMAND_Sprint();
        }
        else
        {
            myPlayer.COMMAND_DontSprint();
        }
    }