Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class PinSetter : MonoBehaviour {    
  7.    
  8.     public Text standingDisplay;
  9.     public bool ballLeftBox = false;    
  10.     public GameObject pinSet;
  11.  
  12.     private int lastStandingCount = -1;
  13.     private int lastSettledCount = 10;
  14.     private float lastChangeTime;
  15.  
  16.     private Ball ball;
  17.     private Animator animator;
  18.  
  19.     ActionMaster actionMaster = new ActionMaster();
  20.  
  21.     // Use this for initialization
  22.     void Start () {
  23.         ball = GameObject.FindObjectOfType<Ball>();
  24.         animator = GetComponent<Animator>();
  25.     }
  26.  
  27.     // Update is called once per frame
  28.     void Update()
  29.     {
  30.         standingDisplay.text = CountStanding().ToString();
  31.  
  32.         if (ballLeftBox) {
  33.             UpdateStandCountAndSettle();
  34.         }
  35.     }
  36.  
  37.     public void RaisePins()
  38.     {
  39.                 foreach (Pin pin in GameObject.FindObjectsOfType<Pin>())
  40.         {          
  41.                 pin.Raise();            
  42.         }
  43.     }
  44.  
  45.     public void LowerPins()
  46.     {
  47.         foreach (Pin pin in GameObject.FindObjectsOfType<Pin>())
  48.         {
  49.             pin.Lower();
  50.  
  51.         }
  52.     }
  53.  
  54.     public void RenewPins()
  55.     {
  56.                Instantiate(pinSet, new Vector3(0, 0, 1829), Quaternion.identity);
  57.     }
  58.  
  59.     void UpdateStandCountAndSettle()
  60.     {
  61.         int currentStanding = CountStanding();
  62.  
  63.         // call PinsHaveSettled () when they have
  64.         if (currentStanding != lastStandingCount)
  65.         {
  66.             lastChangeTime = Time.time;
  67.             lastStandingCount = currentStanding;
  68.             return;
  69.         }
  70.         float settleTime = 5f; //how long to wait to consider pins settled
  71.         if ((Time.time - lastChangeTime) > settleTime) // if last change > 3s* ago
  72.         {
  73.             PinsHaveSettled();
  74.         }
  75.     }
  76.  
  77.     void PinsHaveSettled() {
  78.  
  79.         int pinFall = lastSettledCount - CountStanding();
  80.         lastSettledCount = CountStanding();
  81.        
  82.         ActionMaster.Action action = actionMaster.Bowl(pinFall);
  83.         print("pinFall: " + pinFall + " " + actionMaster.Bowl(pinFall));
  84.  
  85.         if (action == ActionMaster.Action.Tidy)
  86.         {
  87.             animator.SetTrigger("tidyTrigger");
  88.         }
  89.         else if (action == ActionMaster.Action.Reset) {
  90.             animator.SetTrigger("resetTrigger");
  91.             lastSettledCount = 10;
  92.         }
  93.         else if (action == ActionMaster.Action.EndTurn)
  94.         {
  95.             animator.SetTrigger("resetTrigger");
  96.             lastSettledCount = 10;
  97.         }
  98.         else if (action == ActionMaster.Action.EndGame)
  99.         {
  100.             throw new UnityException("Can't handle end game yet");
  101.         }
  102.  
  103.         lastStandingCount = -1; //Indicates new frame
  104.         standingDisplay.color = new Color(0, 167, 0); //back to green text r 0, g 167, b 49, a 255
  105.         ballLeftBox = false;
  106.         ball.Reset();
  107.     }
  108.  
  109.     int CountStanding() {
  110.         int standing = 0;
  111.         foreach (Pin pin in GameObject.FindObjectsOfType<Pin>())
  112.         {if (pin.IsStanding())
  113.             {
  114.                 standing++;
  115.             }            
  116.            
  117.         }
  118.         return standing;
  119.     }
  120.    
  121.     void OnTriggerExit(Collider collider) {
  122.         GameObject objectExiting = collider.gameObject;
  123.        
  124.         if (objectExiting.GetComponentInParent<Pin>())
  125.         {
  126.                     Destroy(objectExiting.transform.parent.gameObject);
  127.         }
  128.         // This bit is what was removed with the OnTriggerEnter method and placed on the GutterBall script.
  129.         // I placed it back in here to test if it was the extra box that broke the game.
  130.         else if (objectExiting.GetComponent<Ball>())
  131.         {
  132.             ballLeftBox = true;
  133.             standingDisplay.color = Color.red;
  134.         }
  135.     }    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement