Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class PinSetter : MonoBehaviour {
- public Text standingDisplay;
- public bool ballLeftBox = false;
- public GameObject pinSet;
- private int lastStandingCount = -1;
- private int lastSettledCount = 10;
- private float lastChangeTime;
- private Ball ball;
- private Animator animator;
- ActionMaster actionMaster = new ActionMaster();
- // Use this for initialization
- void Start () {
- ball = GameObject.FindObjectOfType<Ball>();
- animator = GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update()
- {
- standingDisplay.text = CountStanding().ToString();
- if (ballLeftBox) {
- UpdateStandCountAndSettle();
- }
- }
- public void RaisePins()
- {
- foreach (Pin pin in GameObject.FindObjectsOfType<Pin>())
- {
- pin.Raise();
- }
- }
- public void LowerPins()
- {
- foreach (Pin pin in GameObject.FindObjectsOfType<Pin>())
- {
- pin.Lower();
- }
- }
- public void RenewPins()
- {
- Instantiate(pinSet, new Vector3(0, 0, 1829), Quaternion.identity);
- }
- void UpdateStandCountAndSettle()
- {
- int currentStanding = CountStanding();
- // call PinsHaveSettled () when they have
- if (currentStanding != lastStandingCount)
- {
- lastChangeTime = Time.time;
- lastStandingCount = currentStanding;
- return;
- }
- float settleTime = 5f; //how long to wait to consider pins settled
- if ((Time.time - lastChangeTime) > settleTime) // if last change > 3s* ago
- {
- PinsHaveSettled();
- }
- }
- void PinsHaveSettled() {
- int pinFall = lastSettledCount - CountStanding();
- lastSettledCount = CountStanding();
- ActionMaster.Action action = actionMaster.Bowl(pinFall);
- print("pinFall: " + pinFall + " " + actionMaster.Bowl(pinFall));
- if (action == ActionMaster.Action.Tidy)
- {
- animator.SetTrigger("tidyTrigger");
- }
- else if (action == ActionMaster.Action.Reset) {
- animator.SetTrigger("resetTrigger");
- lastSettledCount = 10;
- }
- else if (action == ActionMaster.Action.EndTurn)
- {
- animator.SetTrigger("resetTrigger");
- lastSettledCount = 10;
- }
- else if (action == ActionMaster.Action.EndGame)
- {
- throw new UnityException("Can't handle end game yet");
- }
- lastStandingCount = -1; //Indicates new frame
- standingDisplay.color = new Color(0, 167, 0); //back to green text r 0, g 167, b 49, a 255
- ballLeftBox = false;
- ball.Reset();
- }
- int CountStanding() {
- int standing = 0;
- foreach (Pin pin in GameObject.FindObjectsOfType<Pin>())
- {if (pin.IsStanding())
- {
- standing++;
- }
- }
- return standing;
- }
- void OnTriggerExit(Collider collider) {
- GameObject objectExiting = collider.gameObject;
- if (objectExiting.GetComponentInParent<Pin>())
- {
- Destroy(objectExiting.transform.parent.gameObject);
- }
- // This bit is what was removed with the OnTriggerEnter method and placed on the GutterBall script.
- // I placed it back in here to test if it was the extra box that broke the game.
- else if (objectExiting.GetComponent<Ball>())
- {
- ballLeftBox = true;
- standingDisplay.color = Color.red;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement