//////////////////////////////////////////////////////////////////////// ON HOOP COLLISION //this is thrown by each hoop trigger when the ball goes through the right way. public void OnHoopCollision(GameObject ball, HoopScript hScript) { //if the ball is the active ball and it enters it's active hoop if (ball.GetComponent <BallScript> ().ActiveHoop == hScript) { //set the ball's new target hoop SetTargetHoop(gameRules.GetNextHoop(hScript, ball), ball.GetComponent <BallScript> ()); //mark the ball as having gone through the hoop for the gameRules gameRules.BallThroughHoop = true; } }
private GameRulesScript gameRules; // the game rules for the game // Use this for initialization void Start() { //get the sprite and rigidbody components rb = GetComponent <Rigidbody2D>(); sprite = GetComponentInChildren <SpriteRenderer> (); //get the gamerules gameRules = GetComponentInParent <GameRulesScript> (); //set various other defaults activeHoop = gameRules.GetHoop(1); isMoving = false; hasWon = false; foulBall = false; }
//This trigger only works if the hoop is a hoop, not a peg. void OnTriggerEnter2D(Collider2D other) { //if the script is attached to a hoop, not a peg if (!isPegNotHoop) { GameObject ball = other.gameObject; HoopScript hoop = this; //if going in the right direction if (RightDirectionCheck(other)) { //fire the OnHoopCollision event. ballController.OnHoopCollision(ball, hoop); } } }
////////////////////////////////////////////////////////// GET NEXT HOOP /// Get the next hoop, given the last hoop public HoopScript GetNextHoop(HoopScript h, GameObject b) { int i; //for each hoop before the last one for (i = 0; i < (hoopScriptList.Count - 1); i++) { //if that hoop is the current hoop if (hoopScriptList [i].Equals(h)) { //increment the score if (b == ballController.ballB1) { scoreBlue1.text = (i + 1).ToString(); } else if (b == ballController.ballB2) { scoreBlue2.text = (i + 1).ToString(); } else if (b == ballController.ballR1) { scoreRed1.text = (i + 1).ToString(); } else if (b == ballController.ballR2) { scoreRed2.text = (i + 1).ToString(); } else { print("broke the scores somehow?"); } //return the next hoop print("setting hoop to" + (i + 2)); return(hoopScriptList [i + 1]); } } //now we're at the last hoop //if the current hoop is the last hoop in the list if (hoopScriptList [i].Equals(h)) { if (b == ballController.ballB1) { scoreBlue1.text = (i + 1).ToString(); } else if (b == ballController.ballB2) { scoreBlue2.text = (i + 1).ToString(); } else if (b == ballController.ballR1) { scoreRed1.text = (i + 1).ToString(); } else if (b == ballController.ballR2) { scoreRed2.text = (i + 1).ToString(); } else { print("broke the scores somehow?"); } //then set that ball as having won print("one ball has won"); b.GetComponent <BallScript> ().HasWon = true; return(hoopScriptList [0]); } //if the current hoop is NOT any hoop before last OR the last hoop, something's wrong. print("you dun goofed. trying to find a nonexistant hoop"); return(hoopScriptList [0]); }
/////////////////////////////////////////////////////////////////////////////////SET TARGET HOOP /// Set the target hoop for the ball. void SetTargetHoop(HoopScript hoop, BallScript ball) { ball.ActiveHoop = hoop; }