Advertisement
GaelVanhalst

Linked: Shrine Of Time

Nov 11th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //The shrine of time is an object in the scene that the player can activate to start a time trial.
  5. public class ShrineOfTime : MonoBehaviour
  6. {
  7.     //Turning variable for animation
  8.     private const string Turning = "Turning";
  9.     private static int TurningId = 0;
  10.  
  11.     [SerializeField] private Animator _animator = null;
  12.     [SerializeField] private float _defaultRecordTime = 3000;
  13.  
  14.     //Player
  15.     private bool _playerIn;
  16.  
  17.     private void Awake()
  18.     {
  19.         TurningId=Animator.StringToHash(Turning);
  20.         _animator.SetBool(TurningId,false);
  21.     }
  22.  
  23.     private void Start()
  24.     {
  25.         //If level hasn't been beaten before or time trial mode is already active, then the shrine needs to be hidden
  26.         if (!GameManager.HasBeatenCurrentLevel() || GameManager.TimeTrialMode)
  27.         {
  28.             gameObject.SetActive(false);
  29.         }
  30.     }
  31.  
  32.     //Update when player can trigger the shrine
  33.     private IEnumerator PlayerInUpdate()
  34.     {
  35.         //If player is already in, then this update is running already
  36.         if (_playerIn) yield break;
  37.  
  38.         _playerIn = true;
  39.         while (_playerIn)
  40.         {
  41.             if (InputManager.InteractDown())
  42.             {
  43.                 GameManager.StartTimeTrial(_defaultRecordTime);
  44.             }
  45.             yield return null;
  46.         }
  47.     }
  48.  
  49.     public void OnTriggerEnter(Collider other)
  50.     {
  51.         var playerScript = other.GetComponent<Player>();
  52.         if (playerScript != null)
  53.         {
  54.             //Player entered. Start update and make the hourglass turn
  55.             StartCoroutine(PlayerInUpdate());
  56.             _animator.SetBool(TurningId, true);
  57.         }
  58.     }
  59.  
  60.     public void OnTriggerExit(Collider other)
  61.     {
  62.         var playerScript = other.GetComponent<Player>();
  63.         if (playerScript != null)
  64.         {
  65.             //Player left. Stop the hourglass from turning
  66.             _playerIn = false;
  67.             _animator.SetBool(TurningId, false);
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement