Пример #1
0
    /**
     *   this is the updated fucntion for the state
     */
    public override void Excute(GameObject CallingObject)
    {
        FieldPlayer PlayerScript = CallingObject.GetComponent <FieldPlayer>();

        float dot = Vector3.Dot(CallingObject.transform.right, (PlayerScript.Ball.transform.position - CallingObject.transform.position).normalized);
        float KickPower;

        //If goal keeper has ball/already a recieving player / ball behind player the chase ball
        if (PlayerScript.GetTeam().GetPitch().GoalKeeperHasBall() || (dot < 0))
        {
            PlayerScript.ChangeState(CallingObject, ChaseBall.Instance());
        }

        //If he can shoot
        Vector2 ShootingTarget = new Vector2();

        if (PlayerScript.InShootingRange())
        {
            if (PlayerScript.GetTeam().CanShoot(CallingObject.transform.position, out ShootingTarget, PlayerScript.MaxShootingForce, PlayerScript.ShootingConfidence))
            {
                //Get the Target the player is aiming at
                Vector2 ShotTarget = PlayerScript.AddNoiseToTarget(ShootingTarget);

                //Get the direction of the shot
                Vector2 KickDir = (ShotTarget - (Vector2)PlayerScript.Ball.transform.position).normalized;

                //Set a kick power based on if the player is facing the ball
                KickPower = PlayerScript.MaxShootingForce * dot;



                //Add force to the ball
                PlayerScript.Ball.GetComponent <Football>().AddForce(KickDir * KickPower, ShotTarget, "Shooting Towards Goal");


                PlayerScript.ChangeState(CallingObject, Wait.Instance());

                PlayerScript.FindSupport();

                return;
            }
        }

        KickPower = PlayerScript.PassingForce * dot;

        Player  Reciever      = null;
        Vector3 PassingTarget = new Vector3();



        //Attempt to pass to player
        if (PlayerScript.IsThreatened() && PlayerScript.GetTeam().FindPass(PlayerScript, out Reciever, out PassingTarget, KickPower))
        {
            float FacingPassingPlayerDot = Vector3.Dot(CallingObject.transform.right, (PassingTarget - CallingObject.transform.position).normalized);

            if (dot < 0.7f) //not facing the player
            {
                PlayerScript.FindSupport();

                PlayerScript.ChangeState(CallingObject, Dribble.Instance());

                return;
            }


            Vector2 PassTarget = PlayerScript.AddNoiseToTarget(PassingTarget); //TODO **** Make sure this works! *****

            //Get the direction of the shot
            Vector2 KickDir = (PassTarget - (Vector2)PlayerScript.Ball.transform.position).normalized;

            //Add force to the ball
            PlayerScript.Ball.GetComponent <Football>().AddForce(KickDir * KickPower, PassTarget, "Passing To Player Indepentently");



            //GCHandle Handle = GCHandle.Alloc(PassTarget);
            //System.IntPtr PositionPtr = (System.IntPtr)Handle;

            Dispatcher.Instance().DispatchMessage(0, CallingObject, Reciever.gameObject, PlayerMessages.ReceiveBall, PassTarget);

            PlayerScript.ChangeState(CallingObject, Wait.Instance());

            PlayerScript.FindSupport();

            return;
        }
        else
        {
            PlayerScript.FindSupport();

            PlayerScript.ChangeState(CallingObject, Dribble.Instance());
        }
    }