Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6.  
  7. public class game : MonoBehaviour
  8. {
  9.  
  10.     GameObject Danger1;
  11.     GameObject Danger2;
  12.     GameObject Danger3;
  13.     public Text incomingThreats;
  14.  
  15.     public Queue<Threat> threats = new Queue<Threat>();
  16.  
  17.     public class Threat{
  18.         public int position;
  19.         float finalTime;
  20.        
  21.         public Threat(){
  22.             position = UnityEngine.Random.Range(1,4);
  23.             finalTime = Time.time + 0.75f;
  24.         }
  25.  
  26.         public bool CountDown(){
  27.             if(Time.time >= finalTime){
  28.                 return true;
  29.             }
  30.             else{
  31.                 return false;
  32.             }
  33.         }
  34.     }
  35.     void Start()
  36.     {
  37.         Danger1=GameObject.Find("Danger1");
  38.         Danger2=GameObject.Find("Danger2");
  39.         Danger3=GameObject.Find("Danger3");
  40.         incomingThreats.text = "";
  41.         Danger1.SetActive (false);
  42.         Danger2.SetActive (false);
  43.         Danger3.SetActive (false);
  44.     }
  45.    
  46.     bool waitActive = false;
  47.     IEnumerator Wait(float seconds){
  48.         waitActive = true;
  49.         yield return new WaitForSeconds (seconds);
  50.         waitActive = false;
  51.     }
  52.    
  53.     IEnumerator ReactivateWithDelay(float seconds, GameObject obj){
  54.         obj.SetActive (true);
  55.         yield return new WaitForSeconds(seconds);
  56.         obj.SetActive (false);
  57.     }
  58.  
  59.  
  60.     void ThreatGenerator(){
  61.             if(!waitActive){
  62.                 StartCoroutine(Wait(1.0f));
  63.                 Threat incomingThreat = new Threat();
  64.                 threats.Enqueue(incomingThreat);
  65.                 DangerNotification(incomingThreat.position);
  66.             }
  67.     }
  68.  
  69.     void DangerNotification(int pos){
  70.         switch (pos)
  71.       {
  72.           case 1:
  73.             ReactivateWithDelay(1.0f, Danger1);
  74.             break;
  75.           case 2:
  76.             ReactivateWithDelay(1.0f, Danger2);
  77.             break;
  78.           case 3:
  79.             ReactivateWithDelay(1.0f, Danger3);
  80.             break;
  81.         }
  82.     }
  83.  
  84.     int Fight(){
  85.         if (Input.GetKeyDown("left")){
  86.             return 1;
  87.         }
  88.         else if (Input.GetKeyDown("up")){
  89.             return 2;
  90.         }
  91.         else if (Input.GetKeyDown("right")){
  92.             return 3;
  93.         }
  94.         else{
  95.             return 0;
  96.         }
  97.     }
  98.  
  99.     void Update()
  100.     {
  101.         ThreatGenerator();
  102.         if (threats.Count != 0){
  103.         incomingThreats.text = threats.Peek().position.ToString();
  104.         }
  105.         else{
  106.             incomingThreats.text = "";
  107.         }
  108.         if (Fight()==threats.Peek().position){
  109.             threats.Dequeue();
  110.         }
  111.         else if(Fight()!=0){
  112.             incomingThreats.text = "You loose";
  113.         }
  114.         if (threats.Count !=0){
  115.             if (threats.Peek().CountDown() == true){
  116.                 incomingThreats.text = "You loose";
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement