Placido_GDD

GameOfLife(UI)

Jan 4th, 2022 (edited)
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class UserInput : MonoBehaviour
  6. {
  7.     //public variables
  8.  
  9.     //private variables
  10.     private Game gameManage;
  11.     private GameObject gameManager;
  12.     public GameObject UIHandler;
  13.     public bool UIActive = true;
  14.     // Start is called before the first frame update
  15.     void Start()
  16.     {
  17.        
  18.        
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         if (gameManager == null)
  25.         {
  26.             gameManager = GameObject.FindGameObjectWithTag("Manager");
  27.         }
  28.         if (gameManager != null)
  29.         {
  30.             gameManage = gameManager.GetComponent<Game>();
  31.             User_Input();
  32.             UIHandler.SetActive(UIActive);
  33.         }
  34.        
  35.     }
  36.  
  37.     void User_Input()
  38.     {
  39.         if (Input.GetMouseButtonDown(0)) //toggle if a cell is alive or dead
  40.         {
  41.             Vector2 mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  42.             int x = Mathf.RoundToInt(mousePoint.x);
  43.             int y = Mathf.RoundToInt(mousePoint.y);
  44.  
  45.             if (x >= 0 && y >= 0 && x < gameManage.screenWidth && y < gameManage.screenHeight)
  46.             {
  47.                 //we are in bounds.
  48.                 gameManage.grid[x, y].SetAlive(!gameManage.grid[x, y].isAlive);
  49.             }
  50.         }
  51.  
  52.         if (Input.GetKeyUp(KeyCode.P))
  53.         {
  54.             //toggle simulation
  55.             gameManage.simEnabled = !gameManage.simEnabled;
  56.         }
  57.  
  58.         if (Input.GetKeyUp(KeyCode.O))
  59.         {
  60.             UIActive = !UIActive;
  61.         }
  62.  
  63.     }
  64. }
  65.  
Add Comment
Please, Sign In to add comment