Rigidbody CreateBall(int team_, Vector3 position_) { Rigidbody rigidBody = (Rigidbody)Instantiate(BocceBallPrefab, position_, new Quaternion()); BocceBall component = rigidBody.GetComponent <BocceBall>(); if (component) { component.Team = team_; } rigidBody.GetComponent <Renderer>().material.color = GetTeamColor(team_); return(rigidBody); }
bool AreBallsMoving() { IEnumerable <GameObject> gameObjects = GameObject.FindGameObjectsWithTag("BocceBall"); bool inMotion = gameObjects.Count(ball => { BocceBall component = ball.GetComponent <BocceBall>(); if (component && component.InBounds && component.IsMoving) { return(true); } return(false); }) > 0; return(inMotion); }
List <float> GetBallDistances(int team) { List <float> distances = new List <float>(); IEnumerable <GameObject> gameObjects = GameObject.FindGameObjectsWithTag("BocceBall").Where(gameObject => { BocceBall component = gameObject.GetComponent <BocceBall>(); return(component && component.Team == team && component.InBounds); }); foreach (GameObject gameObject in gameObjects) { BocceBall component = gameObject.GetComponent <BocceBall>(); if (component) { float distanceSq = component.GetDistanceSqToPoint(jack.transform.position); distances.Add(distanceSq); } } return(distances); }
void Update() { // pausing if (Input.GetKeyDown(KeyCode.Escape)) { TogglePause(); } if (!Paused) { switch (gameMode) { case GameMode.Setup: { currentBall = jack = CreateBall(); currentBall.transform.localScale *= 0.5f; // jack is smaller // random direction and force for the JACK Quaternion xQuaternion = Quaternion.AngleAxis(Random.Range(-30, 30), Vector3.up); Quaternion yQuaternion = Quaternion.AngleAxis(Random.Range(0, 15), -Vector3.right); Quaternion originalRotation = transform.localRotation; PlayerCamera.gameObject.transform.localRotation = originalRotation * xQuaternion * yQuaternion; BallForceBar.BarProgress = Random.Range(0.5f, 1.0f); ThrowBall(PlayerCamera.gameObject.transform.forward * (MaxBallForce * BallForceBar.BarProgress)); SetGameMode(GameMode.AimingFinish); break; } case GameMode.Aiming: { if (Input.GetKeyDown(KeyCode.Space)) { currentBall = CreateBall(currentTeam); teamBalls [currentTeam] -= 1; ThrowBall(PlayerCamera.gameObject.transform.forward * (MaxBallForce * BallForceBar.BarProgress)); SetGameMode(GameMode.AimingFinish); } else { if (forceIncreasing) { BallForceBar.BarProgress += ForceIncrement; if (BallForceBar.BarProgress >= 1.0f) { BallForceBar.BarProgress = 1.0f; forceIncreasing = false; } } else { BallForceBar.BarProgress -= ForceIncrement; if (BallForceBar.BarProgress <= 0.0f) { BallForceBar.BarProgress = 0.0f; forceIncreasing = true; } } } break; } case GameMode.AimingFinish: { if (currentWaitTime <= 0.0f) { if (!AreBallsMoving()) { if (prevGameMode != GameMode.Setup) { int closestTeam = GetClosestTeam(); currentTeam = (closestTeam == 0 ? 1 : 0); // if the team is out of balls let the other finish if (teamBalls [currentTeam] == 0) { currentTeam = (currentTeam == 0 ? 1 : 0); } } BocceBall jackComponent = jack.GetComponent <BocceBall>(); if (jackComponent && !jackComponent.InBounds) { // jack is out of bounds SetGameMode(GameMode.Setup); } else { SetGameMode(teamBalls.Sum() > 0 ? GameMode.Aiming : GameMode.RoundResult); } } } else { currentWaitTime -= Time.deltaTime; } break; } case GameMode.RoundResult: { if (Input.GetKeyDown(KeyCode.Space)) { if (teamScore.Max() >= WinningScore) { SetGameMode(GameMode.GameResult); } else { SetGameMode(GameMode.Setup); } } break; } case GameMode.GameResult: { if (Input.GetKeyDown(KeyCode.Space)) { SceneManager.LoadScene(0); } break; } } } }