///<summary> /// Getting ready to throw dice ///</summary> void PrepareThrow() { gameState = DiceGameState.PREPARING_THROW; goalSlotTween.enabled = true; goalSlotTween.Play(true); goalSlotTween.eventReceiver = this.gameObject; goalSlotTween.callWhenFinished = "GoalTweeningFinish"; DiceShowPanel.SetActive(false); }
///<summary> /// Callback after the tween of reset poistion finished ///</summary> void GoalResultShown() { if (bMakingUpThrow) { gameState = DiceGameState.MAKEUP_THROW; } else { gameState = DiceGameState.AWAITING_ACTION; } }
///<summary> /// Handle the end of the game. ///</summary> void ShowEnding(bool winning) { gameState = DiceGameState.GAME_ENDING; NGUITools.SetActive(UserPanel, false); NGUITools.SetActive(EndPanel, true); GrandScoreLabel.text = "Total Score: " + MyScore.ToString(); if (winning) { EndLabel.text = "Congratulations!!\n You just clear the game!! \nWant to Play again?"; } else { EndLabel.text = "Hmmm...\n You did great but did not beat the game.\nWant to Play again?"; } }
//---------------------------------------------------------------------- // Functions //---------------------------------------------------------------------- ///<summary> /// Use this for initialization ///</summary> void Start() { print("Hello, this is game controller Start"); dice4Roll = new List <Die> (); ResetDice(); gameState = DiceGameState.AWAITING_ACTION; HelpInfoObject = HelpInfoLabel.transform.parent.gameObject; MyRolls = 0; // make sure updated with MAX goalSlotTween = GoalSlotPanel.GetComponent <UITweener> (); goalController = GoalSlotPanel.GetComponent <GoalSlotController> (); goalController.RedemptActionCallback += RedemptNotify; goalController.ZeroPointNotification += ZeroPointNotify; NGUITools.SetActive(UserPanel, true); NGUITools.SetActive(EndPanel, false); }
///<summary> /// throw the dice out ///</summary> void ThrowDice() { if (gameState == DiceGameState.PREPARING_THROW && !bMakingUpThrow) { MyRolls++; } gameState = DiceGameState.ROLLING_DICE; ControlButton.SetActive(false); //Vector3 vForce = new Vector3( -0.5f, 0.5f, 0.1f); foreach (Die die in dice4Roll) { Vector3 vForce = myForce; //Force ( die.gameObject ); float fMag = -50 * die.transform.localScale.magnitude; die.gameObject.rigidbody.AddForce(vForce); die.gameObject.rigidbody.AddTorque(new Vector3(fMag * Random.value, fMag * Random.value, fMag * Random.value), ForceMode.Impulse); die.gameObject.rigidbody.useGravity = true; die.gameObject.rigidbody.isKinematic = false; } }
//---------------------------------------------------------------------- /// <summary> /// After dice stop rolling, transition to new state /// Show the Results. /// </summary> void ShowRollResult() { gameState = DiceGameState.SHOWING_RESULT; ResetGoalPosition(); DiceShowPanel.SetActive(true); ControlButton.SetActive(true); // construct a list of dice that lay flat, if value = 0, then it is undetermined List <Die> diceList = new List <Die> (); dice4Roll.Clear(); foreach (Die die in myDice) { if (die.value > 0) { diceList.Add(die); } else { dice4Roll.Add(die); } } SortDiceList(diceList); PrintDiceList(diceList); // debug // Move the dice to the holder spots ClearHolders(); int index = 0; foreach (Die die in diceList) { if (DiceSpots [index]) { die.transform.parent = DiceSpots [index].transform; die.transform.localPosition = new Vector3(0, 0, die.transform.localPosition.z); // if previously locked if (die.tag == "Finish") { Debug.Log(" Relock die "); LockDie(die, true); } } index++; } // dice4Roll now holds the undetermined dice if (dice4Roll.Count > 0) { gameState = DiceGameState.MAKEUP_THROW; bMakingUpThrow = true; // need this since state change after tweening } else { // all dice has value gameState = DiceGameState.AWAITING_ACTION; bMakingUpThrow = false; int [] diceVal = new int[5]; int cnt = 0; foreach (Die die in diceList) { diceVal [cnt] = die.value; // construct the array for validation cnt++; // since dice4Roll has 0 element, put the dice back to this list if not locked if (die.tag == "Untagged") { dice4Roll.Add(die); } } // validate the result, a callback maybe called from goalController goalController.ValidateGoals(diceVal); } } // end of ShowRollResult
///<summary> /// The dice stop rolling, let them sit on the table until user tap ///</summary> void SettleDice() { gameState = DiceGameState.DICE_SETTLE; HelpInfoObject.SetActive(true); HelpInfoLabel.text = "Tap table collect and sort dice."; }