Guest User

Untitled

a guest
Nov 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class Bomb : MonoBehaviour
  8. {
  9.     [SerializeField]
  10.     private GameEvent detonationEvent;
  11.    
  12.     [SerializeField]
  13.     private FloatVariable detonationCountdown;
  14.    
  15.     [SerializeField]
  16.     private FloatReference secondsTilDetonation;
  17.    
  18.     [SerializeField]
  19.     private Text statusText;
  20.  
  21.     private CancellationTokenSource cancelToken;
  22.    
  23.     private void Start()
  24.     {
  25.         cancelToken = new CancellationTokenSource();
  26.         Task.Run(() => SetBomb(), cancelToken.Token);
  27.     }
  28.  
  29.     private void OnDestroy()
  30.     {
  31.         cancelToken.Cancel(false);
  32.     }
  33.  
  34.     async void SetBomb()
  35.     {
  36.         statusText.text = "Setting bomb!";
  37.         await TimeSpan.FromSeconds(1);
  38.         detonationCountdown.Value = secondsTilDetonation.Value;
  39.         await Task.Delay(TimeSpan.FromSeconds((int)secondsTilDetonation.Value));
  40.         statusText.text = ("Bomb is detonating!");
  41.         await TimeSpan.FromSeconds(1);
  42.         detonationEvent.Raise();
  43.         statusText.text = ("You lost!");
  44.     }
  45.  
  46.     private void Update()
  47.     {
  48.         if (detonationCountdown.Value > 0)
  49.         {
  50.             detonationCountdown.Value -= Time.deltaTime;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment