Пример #1
0
    public static void CheckIfRunnerOut(Runner runner)
    {
        Fielder fielder = GetClosestFielderToTransform(runner.targetBase[0].transform, false);

        if (fielder.ballInHands && Utility.CheckEqual(fielder.transform.position, runner.targetBase[0].transform.position, 0.1f))
        {
            if (GetFurthestRunner() == null)
            {
                Debug.LogWarning("This should never show up");
                GameControl.ballInPlay = false;
            }
            string aud;
            if (GameControl.outsThisPlay == 1)
            {
                aud = "doubleplay";
            }
            else if (GameControl.outsThisPlay == 2)
            {
                aud = "tripleplay";
            }
            else
            {
                TextPopUps.instance.ShowPopUp("out");
                aud = "out" + Random.Range(1, 5);
            }
            AudioControl.instance.PlayAudio(aud);
            runner.SetOut();
            GameControl.instance.HandleOut();
        }
    }
Пример #2
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        string tag = collision.transform.tag;

        if (tag == "Runner")
        {
            if (targetFielder != null && targetFielder.position == Fielder.Position.catcher)
            {
                collision.GetComponent <Runner>().SwingBat(curPitch.type == Pitcher.Pitches.strike);
            }
        }
        else if (tag == "Fielder")
        {
            if (curPitch != null && curPitch.type != Pitcher.Pitches.homerun)
            {
                if (GameControl.curInning.pitchesThrownThisInning > 0)
                {
                    if (curHeight < 4.0f)
                    {
                        if (hasHitGround == false && wasHit)
                        {
                            Field.mostRecentBatter.SetOut();
                            GameControl.instance.HandleOut();
                            string aud = "out" + Random.Range(1, 5);
                            AudioControl.instance.PlayAudio(aud);
                            TextPopUps.instance.ShowPopUp("out");
                        }
                        hasHitGround = true;
                        if (collision.GetComponentInParent <Fielder>().ballInHands == false)
                        {
                            string aud = "catch";
                            AudioControl.instance.PlayAudio(aud);
                        }
                        collision.GetComponentInParent <Fielder>().ballInHands = true;
                        targetFielder = null;
                        endPoint      = Vector3.zero;
                        startPoint    = Vector3.zero;
                        anim.SetBool("Moving", false);
                    }
                }
                else
                {
                    Field.fielders.Find(x => x.position == Fielder.Position.pitcher && x.inningOver == false).ballInHands = true;
                }
            }
        }
        else if (tag == "Wall" && curPitch.type != Pitcher.Pitches.homerun)
        {
            startPoint = transform.position;
            endPoint   = transform.position + new Vector3(Random.Range(-0.25f, 0.25f), Random.Range(-0.5f, -0.05f), 0);
        }
        else if (tag == "Wall" && curPitch.type == Pitcher.Pitches.homerun && shownHomeRunPopup == false)
        {
            shownHomeRunPopup = true;
            AudioControl.instance.PlayAudio("homerun");
            TextPopUps.instance.ShowPopUp("homerun");
            Invoke("HideBallSprite", Random.Range(0.2f, 0.6f));
            Invoke("SetCameraParent", 2f);
        }
    }
Пример #3
0
 private void Start()
 {
     ball    = Field.ball.GetComponent <Ball>();
     fielder = GetComponent <Fielder>();
     ball.TemporarilyDisableCollision();
     fielder.ballInHands = true;
     ball.anim.SetBool("Moving", false);
 }
Пример #4
0
    private void ReturnToPitcher()
    {
        GameControl.ballInPlay = false;
        anim.SetBool("Moving", false);
        Fielder pitcher = Field.fielders.Find(x => x.position == Fielder.Position.pitcher && x.inningOver == false);

        endPoint            = pitcher.glove.position;
        pitcher.ballInHands = true;
    }
Пример #5
0
    private void ReturnToPitcher()
    {
        GetComponent <SpriteRenderer>().enabled = true;
        GameControl.ballInPlay = false;
        anim.SetBool("Moving", false);
        Fielder pitcher = Field.fielders.Find(x => x.position == Fielder.Position.pitcher && x.inningOver == false);

        endPoint            = pitcher.glove.position;
        pitcher.ballInHands = true;
        wasHit = false;
    }
Пример #6
0
 public void ThrowBall(Fielder target)
 {
     Field.ballHasBeenThrown = true;
     Field.ball.TemporarilyDisableCollision(0.3f);
     Field.ball.curSpeed      = throwSpeed;
     Field.ball.maxHeight     = 3.0f;
     Field.ball.endPoint      = target.glove.transform.position;
     Field.ball.startPoint    = Field.ball.transform.position;
     Field.ball.targetFielder = target;
     Field.ball.curPitch.type = Pitcher.Pitches.hit;
     Debug.Log(transform.parent.name + " throwing to " + target.transform.parent.name);
     ballInHands = false;
 }
Пример #7
0
 public void ThrowBall(Fielder target)
 {
     if (target == this)
     {
         return;
     }
     Field.ballHasBeenThrown = true;
     Field.ball.TemporarilyDisableCollision(0.3f);
     Field.ball.curSpeed      = throwSpeed;
     Field.ball.maxHeight     = 3.0f;
     Field.ball.endPoint      = target.glove.transform.position;
     Field.ball.startPoint    = Field.ball.transform.position;
     Field.ball.targetFielder = target;
     Field.ball.curPitch.type = Pitcher.Pitches.hit;
     ballInHands = false;
 }
Пример #8
0
    public static Fielder GetClosestFielderToLocation(Vector2 loc, bool accountForDistFromStartPos = true)
    {
        Fielder closestPlayer = fielders[0];
        float   lowestDist    = float.MaxValue;

        foreach (var player in fielders)
        {
            float distance = Vector2.Distance(player.transform.position, loc) + Vector2.Distance(player.transform.position, player.startPosition.position);
            if (distance < lowestDist)
            {
                closestPlayer = player;
                lowestDist    = distance;
            }
        }

        return(closestPlayer);
    }
Пример #9
0
    public void AddFielderToField(Fielder.Position pos, GameObject obj)
    {
        if (fieldParent == null)
        {
            fieldParent = GameObject.Find("Players").transform;
        }
        Vector3    loc = GetTeamInField().dugout.transform.position;
        GameObject go  = Instantiate(fielderPrefab, loc, Quaternion.identity, fieldParent);

        go.name = pos.ToString();
        if (pos == Fielder.Position.pitcher)
        {
            go.transform.GetChild(0).gameObject.AddComponent <Pitcher>();
        }
        Fielder fielder = go.GetComponentInChildren <Fielder>();

        fielder.SetPosition(pos);
        Field.fielders.Add(fielder);
    }
Пример #10
0
    public static Fielder GetClosestFielderToTransform(Transform loc, bool accountForDistFromStartPos = true)
    {
        if (loc == null)
        {
            return(null);
        }
        Fielder closestPlayer = null;
        float   lowestDist    = float.MaxValue;

        foreach (Fielder player in fielders)
        {
            float distance = Vector2.Distance(player.transform.position, loc.position);
            distance += accountForDistFromStartPos ? Vector2.Distance(player.transform.position, player.startPosition.position) : 0f;
            if (distance < lowestDist)
            {
                closestPlayer = player;
                lowestDist    = distance;
            }
        }

        return(closestPlayer);
    }
Пример #11
0
    public static void WhatDoIDoWithTheBall(Fielder player)
    {
        if (GetFurthestRunner() == null)
        {
            player.ThrowBall(GetPlayerToThrowBallTo());
            return;
        }
        Transform baseLocation = GetFurthestRunner().targetBase[0].transform;

        GetClosestFielderToTransform(baseLocation, false).movementTarget = baseLocation.position;
        if (player == GetClosestFielderToTransform(baseLocation))
        {
            if (Utility.CheckEqual(player.transform.position, baseLocation.position, 0.1f) && !GameControl.ballInPlay)
            {
                player.ThrowBall(GetPlayerToThrowBallTo());
            }
            return;
        }
        else
        {
            player.ThrowBall(GetPlayerToThrowBallTo());
        }
    }
Пример #12
0
 public void ThrowPitch()
 {
     if (fielder.ballInHands == true)
     {
         GameControl.curInning.pitchesThrownThisInning += 1;
         Field.ballHasBeenThrown = false;
         fielder.anim.SetBool("isThrowing", true);
         fielder.ballInHands = false;
         ball.TemporarilyDisableCollision(0.2f);
         ball.curHeight         = 1f;
         ball.curSpeed          = pitchSpeed;
         ball.maxHeight         = 3.0f;
         ball.startPoint        = ball.transform.position;
         ball.shownHomeRunPopup = false;
         fielder.team.pitches  += 1;
         Fielder catcher = Field.fielders.Find(x => x.position == Fielder.Position.catcher);
         ball.endPoint      = catcher.glove.position;
         ball.targetFielder = catcher;
     }
     else
     {
         fielder.ThrowBall(fielder);
     }
 }
Пример #13
0
    public static void FielderAI()
    {
        if (runners.Find(x => x.isAdvancing) == null && ballHasBeenThrown)
        {
            GameControl.ballInPlay = false;
        }
        Fielder fielderWithBall = fielders.Find(x => x.ballInHands);
        bool    pitcherHasBall  = false;

        if (fielderWithBall != null)
        {
            ball.transform.parent        = fielderWithBall.glove.gameObject.transform;
            ball.transform.localPosition = Vector3.zero;
            ball.curHeight = 1f;
            pitcherHasBall = fielderWithBall.position == Fielder.Position.pitcher;
        }
        else
        {
            if (ball != null)
            {
                ball.transform.parent = fieldParent;
            }
        }

        if (fieldersCanReact)
        {
            if (GameControl.ballInPlay || pitcherHasBall == false)
            {
                MoveFieldersToPlayPosition();
                if (ball.hasHitGround && ballHasBeenThrown == false)
                {
                    GetClosestFielderToTransform(ball.transform, false).movementTarget = ball.transform.position + ballOffset;
                }
                else
                {
                    GetClosestFielderToLocation(ballLandingSpot).movementTarget = ballLandingSpot;
                }
                if (fielderWithBall != null)
                {
                    WhatDoIDoWithTheBall(fielderWithBall);
                }
            }
        }

        if (GameControl.ballInPlay == false)
        {
            if (fielderWithBall != null && fielderWithBall.position != Fielder.Position.pitcher)
            {
                fielderWithBall.ThrowBall(GetPlayerToThrowBallTo());
            }
            else if (fielderWithBall != null && fielderWithBall.position == Fielder.Position.pitcher)
            {
                GameControl.instance.SetCameraToFollowBall(false);
                GameControl.playIsActive = false;
                fieldersCanReact         = false;
                ball.wasHit = false;
            }
            MoveFieldersToStartPosition(false);
        }

        if (ball != null && ball.targetFielder != null)
        {
            ball.targetFielder.movementTarget = ball.endPoint;
        }
    }