Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.UI;
- public class Bomb : MonoBehaviour
- {
- [SerializeField]
- private GameEvent detonationEvent;
- [SerializeField]
- private FloatVariable detonationCountdown;
- [SerializeField]
- private FloatReference secondsTilDetonation;
- [SerializeField]
- private Text statusText;
- private CancellationTokenSource cancelToken;
- private void Start()
- {
- cancelToken = new CancellationTokenSource();
- Task.Run(() => SetBomb(), cancelToken.Token);
- }
- private void OnDestroy()
- {
- cancelToken.Cancel(false);
- }
- async void SetBomb()
- {
- statusText.text = "Setting bomb!";
- await TimeSpan.FromSeconds(1);
- detonationCountdown.Value = secondsTilDetonation.Value;
- await Task.Delay(TimeSpan.FromSeconds((int)secondsTilDetonation.Value));
- statusText.text = ("Bomb is detonating!");
- await TimeSpan.FromSeconds(1);
- detonationEvent.Raise();
- statusText.text = ("You lost!");
- }
- private void Update()
- {
- if (detonationCountdown.Value > 0)
- {
- detonationCountdown.Value -= Time.deltaTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment