Advertisement
Guest User

Untitled

a guest
Mar 24th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.92 KB | None | 0 0
  1. All classes of "Pandemic JAM Simulator". Sorry for the quick and dirty code. Doubts: joseamoyano@gmail.com
  2.  
  3.  
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8.  
  9. public class AuxiliarButton : MonoBehaviour
  10. {
  11.     public void ExitApp()
  12.     {
  13.         Application.Quit();
  14.     }
  15.  
  16.     public void Restart()
  17.     {
  18.         SceneManager.LoadScene(0);
  19.     }
  20. }
  21.  
  22.  
  23.  
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using UnityEngine;
  27.  
  28. public class CheckRole : MonoBehaviour
  29. {
  30.     [SerializeField]
  31.     Sprite citizen, cured, dead, infected, police, sanitary;
  32.  
  33.     SpriteRenderer spriteRenderer;
  34.  
  35.     RegularMovement regularMovement;
  36.  
  37.     private void Awake()
  38.     {
  39.         spriteRenderer = GetComponent<SpriteRenderer>();
  40.         regularMovement = GetComponent<RegularMovement>();
  41.     }
  42.  
  43.     private void Start()
  44.     {
  45.         regularMovement.StateInString();
  46.         //ChangeState(regularMovement.stateInString);
  47.     }
  48.  
  49.     public void InitialStates(string state)
  50.     {
  51.         if (state == "police")
  52.         {
  53.             spriteRenderer.sprite = police;
  54.         }
  55.         else if (state == "sanitary")
  56.         {
  57.             spriteRenderer.sprite = sanitary;
  58.         }
  59.         else if (state == "infected")
  60.         {
  61.             spriteRenderer.sprite = infected;
  62.         }
  63.     }
  64.  
  65.     public void ChangeState(string state)
  66.     {
  67.         if(state == "cured")
  68.         {
  69.             spriteRenderer.sprite = cured;
  70.         }
  71.         else if(state == "infected")
  72.         {
  73.             spriteRenderer.sprite = infected;
  74.         }
  75.         else if(state == "dead")
  76.         {
  77.             spriteRenderer.sprite = dead;
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. using System.Collections;
  85. using System.Collections.Generic;
  86. using UnityEngine;
  87.  
  88. public class Diseable : MonoBehaviour
  89. {
  90.     public int count;
  91.     RegularMovement regularMovement;
  92.  
  93.     private void Awake()
  94.     {
  95.         regularMovement = GetComponent<RegularMovement>();
  96.     }
  97.  
  98.     // Start is called before the first frame update
  99.     void Start()
  100.     {
  101.        
  102.     }
  103.  
  104.     // Update is called once per frame
  105.     void Update()
  106.     {
  107.         if (regularMovement.isInfected)
  108.         {
  109.             count++;
  110.  
  111.             if (count >= 2000)
  112.             {
  113.                 int temp = Random.Range(0, 100);
  114.  
  115.                 if (temp >= 0 && temp <= 10)
  116.                 {
  117.                     regularMovement.Die();
  118.                 }
  119.                 else
  120.                 {
  121.                    //gameObject.GetComponent<RegularMovement>().isInfected = false;
  122.                     gameObject.GetComponent<RegularMovement>().isCured = true;
  123.                     gameObject.GetComponent<CheckRole>().ChangeState("cured");
  124.                 }
  125.                 gameObject.GetComponent<RegularMovement>().isInfected = false;
  126.             }
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132.  
  133.  
  134. using System.Collections;
  135. using System.Collections.Generic;
  136. using UnityEngine;
  137. using UnityEngine.UI;
  138.  
  139. public class GenerateCitizen : MonoBehaviour
  140. {
  141.     public int totalNumberCitizen;
  142.  
  143.     public GameObject citizen, infected, police, sanitary;
  144.  
  145.     public List<GameObject> citizenList;
  146.  
  147.     public int numberOfPolice;
  148.     public int numberOfSanitary;
  149.  
  150.     public int numberOfEducation;
  151.  
  152.     [SerializeField]
  153.     Slider slider;
  154.     [SerializeField]
  155.     Text text;
  156.  
  157.     [SerializeField]
  158.     GameObject button1, button2, button3, button4, slider1, text1,text4;
  159.  
  160.     [SerializeField]
  161.     Text textButton1, textButton2, textButton3;
  162.    
  163.  
  164.     void CreateCitizen()
  165.     {
  166.         for (int i = 0; i <= numberOfPolice; i++)
  167.             citizenList.Add(police);
  168.  
  169.         for (int i = 0; i <= numberOfSanitary; i++)
  170.             citizenList.Add(sanitary);
  171.  
  172.         for (int i = 0; i <= totalNumberCitizen; i++)
  173.             citizenList.Add(citizen);
  174.  
  175.         citizenList.Add(infected);
  176.        
  177.         for (int i = 0; i < citizenList.Count; i++)
  178.         {
  179.             GameObject temp = Instantiate(citizenList[i], new Vector2(Random.Range(-8, 8), Random.Range(5.5f, -3.5f)), Quaternion.identity);
  180.  
  181.  
  182.             if(!temp.GetComponent<RegularMovement>().isPolice && !temp.GetComponent<RegularMovement>().isSanitary && numberOfEducation>0)
  183.             {
  184.                 numberOfEducation--;
  185.                 temp.GetComponent<RegularMovement>().isResponsable = true;
  186.             }
  187.         }
  188.     }  
  189.  
  190.     public void AddPolices()
  191.     {
  192.         numberOfPolice++;
  193.         textButton1.text = numberOfPolice.ToString();
  194.     }
  195.  
  196.     public void AddSanitaries()
  197.     {
  198.         numberOfSanitary++;
  199.         textButton2.text = numberOfSanitary.ToString();
  200.     }
  201.  
  202.     public void AddEducation()
  203.     {
  204.         numberOfEducation++;
  205.         textButton3.text = numberOfEducation.ToString();
  206.     }
  207.  
  208.     public void AddCitizen()
  209.     {
  210.         totalNumberCitizen = (int)slider.value;
  211.         text.text = "Citizens = " + (int)slider.value;
  212.     }
  213.  
  214.     public void StartSimulation()
  215.     {
  216.         CreateCitizen();
  217.  
  218.         button1.SetActive(false);
  219.         button2.SetActive(false);
  220.         button3.SetActive(false);
  221.         button4.SetActive(false);
  222.         slider1.SetActive(false);
  223.         text1.SetActive(false);
  224.         text4.SetActive(false);
  225.     }
  226. }
  227.  
  228.  
  229.  
  230.  
  231. using System.Collections;
  232. using System.Collections.Generic;
  233. using UnityEngine;
  234.  
  235. public class RegularMovement : MonoBehaviour
  236. {
  237.     [SerializeField]
  238.     Rigidbody2D rb2d;
  239.  
  240.     [SerializeField]
  241.     Vector2 regularVelocity;
  242.  
  243.    
  244.     public bool isInfected, isResponsable, isDead, isPolice, isSanitary, isCured;
  245.     public string stateInString;
  246.  
  247.     [SerializeField]
  248.     Collider2D coll2D;
  249.  
  250.     [SerializeField]
  251.     float magnitudV;
  252.  
  253.     MonoBehaviour policeBehaviour, sanitarBehaviour;
  254.  
  255.     CheckRole checkRole;
  256.  
  257.  
  258.     private void Awake()
  259.     {
  260.         checkRole = GetComponent<CheckRole>();
  261.     }
  262.  
  263.     // Start is called before the first frame update
  264.     void Start()
  265.     {
  266.         BeginMovement();
  267.         StateInString();
  268.     }
  269.  
  270.     public void StateInString()
  271.     {
  272.         if (isInfected)
  273.             stateInString = "infected";
  274.         else if(isPolice)
  275.             stateInString = "police";
  276.         else if (isPolice)
  277.             stateInString = "sanitary";
  278.  
  279.         checkRole.InitialStates(stateInString);
  280.     }
  281.  
  282.     // Update is called once per frame
  283.     void Update()
  284.     {
  285.         if (!isResponsable && !isDead && rb2d.velocity.magnitude < 0.5f)
  286.             BeginMovement();
  287.  
  288.             magnitudV = rb2d.velocity.magnitude;
  289.  
  290.         if (isResponsable)
  291.             rb2d.velocity = Vector2.zero;
  292.     }
  293.  
  294.     void BeginMovement()
  295.     {
  296.         if (!isResponsable)
  297.         {
  298.             regularVelocity = new Vector2(Random.Range(-3, 3), Random.Range(-3, 3)).normalized;
  299.             rb2d.velocity = regularVelocity;
  300.         }
  301.         else
  302.         {
  303.             rb2d.velocity = Vector2.zero;
  304.         }
  305.     }
  306.  
  307.     private void OnCollisionEnter2D(Collision2D collision)
  308.     {
  309.         if (!isResponsable)
  310.         {
  311.             if (collision.gameObject.layer == 8)
  312.             {
  313.                 regularVelocity *= new Vector2(-1, 1)/*.normalized*/;
  314.                 rb2d.velocity = regularVelocity;
  315.             }
  316.             else if (collision.gameObject.layer == 9)
  317.             {
  318.                 regularVelocity *= new Vector2(1, -1)/*.normalized*/;
  319.                 rb2d.velocity = regularVelocity;
  320.             }
  321.             else if (collision.gameObject.layer == 10)
  322.             {
  323.                 rb2d.velocity *= new Vector2(-1, -1);
  324.                 CheckIfIsSpecialCitizen(collision);
  325.             }            
  326.         }
  327.         else
  328.         {
  329.             rb2d.velocity = Vector2.zero;
  330.         }
  331.     }
  332.  
  333.     void CheckIfIsSpecialCitizen(Collision2D collision)
  334.     {
  335.         GameObject tempCitizen = collision.gameObject;
  336.  
  337.         if (!tempCitizen.GetComponent<RegularMovement>().isPolice && !tempCitizen.GetComponent<RegularMovement>().isSanitary)
  338.         {
  339.             if (isPolice)
  340.             {
  341.                 tempCitizen.GetComponent<RegularMovement>().isResponsable = true;
  342.                 rb2d.velocity = Vector2.zero;              
  343.             }
  344.             else if (isSanitary && tempCitizen.GetComponent<RegularMovement>().isInfected)
  345.             {
  346.                 tempCitizen.GetComponent<RegularMovement>().isInfected = false;
  347.                 tempCitizen.GetComponent<RegularMovement>().isCured = true;
  348.                 tempCitizen.GetComponent<CheckRole>().ChangeState("cured");
  349.             }
  350.         }
  351.         if (isInfected && !tempCitizen.GetComponent<RegularMovement>().isPolice && !tempCitizen.GetComponent<RegularMovement>().isSanitary)
  352.         {
  353.             tempCitizen.GetComponent<RegularMovement>().isInfected = true;
  354.             tempCitizen.GetComponent<CheckRole>().ChangeState("infected");
  355.         }
  356.     }
  357.  
  358.     public void Die()
  359.     {
  360.         isDead = true;
  361.         gameObject.GetComponent<CheckRole>().ChangeState("dead");
  362.         rb2d.velocity = Vector2.zero;
  363.     }
  364. }
  365.  
  366.  
  367.  
  368. using System.Collections;
  369. using System.Collections.Generic;
  370. using UnityEngine;
  371.  
  372. public class Walls : MonoBehaviour
  373. {
  374.     [SerializeField]
  375.     GameObject rightWall, leftWall, upWall, downWall;
  376.    
  377.  
  378.     // Start is called before the first frame update
  379.     void Start()
  380.     {
  381.         GenerateCollidersAcrossScreen();
  382.     }        
  383.  
  384.     void GenerateCollidersAcrossScreen()
  385.     {
  386.         Vector3 ScreenSize = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
  387.         leftWall.transform.position = new Vector3(-ScreenSize.x - 0.25f, 0, leftWall.transform.position.z);
  388.         rightWall.transform.position = new Vector3(ScreenSize.x + 0.25f, 0, rightWall.transform.position.z);
  389.         upWall.transform.position = new Vector3(0, ScreenSize.y + 0.25f, upWall.transform.position.z);
  390.         downWall.transform.position = new Vector3(0, -ScreenSize.y + 1.75f, downWall.transform.position.z);
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement