Advertisement
Unkn0wn4all

what is life anymore

Aug 10th, 2022
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Linq;
  6.  
  7. public class GameManager : MonoBehaviour
  8. {
  9.     public bool alwaysShowGrid = false;
  10.     public int alwaysShowGridPressed = 0;
  11.  
  12.     public Text goldDisplay;
  13.     public Text buildingCostDisplay;
  14.     public Text reactorCostDisplay;
  15.  
  16.     public CustomCursor customCursor;
  17.  
  18.     public StatManager statManager;
  19.  
  20.     private GameObject grid;
  21.  
  22.     private GameObject[] buildings;
  23.  
  24.     private GameObject[] reactors;
  25.  
  26.     private Building buildingToPlace;
  27.  
  28.     private Reactor reactorToPlace;
  29.  
  30.     public Tile[] tileGrid;
  31.     public Tile[,] tiles;
  32.  
  33.  
  34.     public void Start()
  35.     {
  36.         grid = GameObject.FindGameObjectWithTag("Grid");
  37.         grid.SetActive(false);
  38.     }
  39.     public void AlwaysShowGrid()
  40.     {
  41.         alwaysShowGridPressed++;
  42.         if (alwaysShowGridPressed == 1)
  43.         {
  44.             alwaysShowGrid = true;
  45.             grid.SetActive(true);
  46.         }
  47.         if (alwaysShowGridPressed > 1)
  48.         {
  49.             alwaysShowGridPressed = 0;
  50.             alwaysShowGrid = false;
  51.             grid.SetActive(false);
  52.         }
  53.     }
  54.     public void Update()
  55.     {
  56.         goldDisplay.text = statManager.Gold.ToString();
  57.         buildingCostDisplay.text = statManager.buildingCost.ToString();
  58.         reactorCostDisplay.text = statManager.reactorCost.ToString();
  59.  
  60.         buildings = GameObject.FindGameObjectsWithTag("BuildingPrefab");
  61.         statManager.energyBeingUsed = buildings.Length;
  62.  
  63.         reactors = GameObject.FindGameObjectsWithTag("ReactorPrefab");
  64.         statManager.energyBeingProvided = reactors.Length * 10;
  65.  
  66.         /*if (Input.GetMouseButtonDown(0) && buildingToPlace != null)
  67.         {
  68.             Tile nearestTile = null;
  69.             float shortestDistance = float.MaxValue;
  70.             foreach(Tile tile in tiles)
  71.             {
  72.                 float distance = Vector2.Distance(tile.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
  73.                 if (distance < shortestDistance)
  74.                 {
  75.                     shortestDistance = distance;
  76.                     nearestTile = tile;
  77.                 }
  78.             }
  79.             if (nearestTile.isOccupied == false)
  80.             {
  81.                 statManager.Gold -= statManager.buildingCost;
  82.                 Vector3 positionToPlace = nearestTile.transform.position;
  83.                 positionToPlace.y += 0.3f;
  84.                 Instantiate(buildingToPlace, positionToPlace, Quaternion.identity);
  85.                 buildingToPlace = null;
  86.                 nearestTile.isOccupied = true;
  87.                 customCursor.gameObject.SetActive(false);
  88.                 Cursor.visible = true;
  89.                 if (alwaysShowGrid == false)
  90.                 {
  91.                     grid.SetActive(false);
  92.                 }
  93.             }
  94.         }*/
  95.         if (Input.GetMouseButtonDown(0) && reactorToPlace != null)
  96.         {
  97.             Vector2 reactorToPlaceDimensions = reactorToPlace.GetComponent<SpriteRenderer>().bounds.size;
  98.             Tile originTile = null;
  99.             float shortestDistance = float.MaxValue;
  100.             foreach (Tile tileIs in tileGrid)
  101.             {
  102.                 float distance = Vector2.Distance(tileIs.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
  103.                 if (distance < shortestDistance)
  104.                 {
  105.                     shortestDistance = distance;
  106.                     originTile = tileIs;
  107.                 }
  108.             }
  109.             Dictionary<Tile, bool> tileOccupancyStatus = new();
  110.  
  111.             Vector2 origintranspos = originTile.transform.position;
  112.             for (int x = (int)origintranspos.x; x < reactorToPlaceDimensions.x + origintranspos.x && x < tiles.Length; x++)
  113.             {
  114.                 for (int y = (int)origintranspos.y; y < reactorToPlaceDimensions.y + origintranspos.y && y < tiles.Length; y++)
  115.                 {
  116.                     Tile tile = tiles[(int)(origintranspos.x + x), (int)(origintranspos.y + y)];
  117.                     tileOccupancyStatus.Add(tile, tile.isOccupied);
  118.                 }
  119.             }
  120.             bool canPlace = tileOccupancyStatus.All(tile => tile.Key.isOccupied == false);
  121.  
  122.             if (canPlace)
  123.             {
  124.                 statManager.Gold -= statManager.reactorCost;
  125.                 Vector3 positionToPlace = originTile.transform.position;
  126.                 Instantiate(reactorToPlace, positionToPlace, Quaternion.identity);
  127.                 reactorToPlace = null;
  128.                 originTile.isOccupied = true;
  129.                 customCursor.gameObject.SetActive(false);
  130.                 Cursor.visible = true;
  131.                 if (alwaysShowGrid == false)
  132.                 {
  133.                     grid.SetActive(false);
  134.                 }
  135.             }
  136.  
  137.         }
  138.         if (Input.GetKeyDown(KeyCode.Escape) && buildingToPlace != null)
  139.         {
  140.             buildingToPlace = null;
  141.             customCursor.gameObject.SetActive(false);
  142.             Cursor.visible = true;
  143.             if (alwaysShowGrid == false)
  144.             {
  145.                 grid.SetActive(false);
  146.             }
  147.         }
  148.         if (Input.GetKeyDown(KeyCode.Escape) && reactorToPlace != null)
  149.         {
  150.             reactorToPlace = null;
  151.             customCursor.gameObject.SetActive(false);
  152.             Cursor.visible = true;
  153.             if (alwaysShowGrid == false)
  154.             {
  155.                 grid.SetActive(false);
  156.             }
  157.         }
  158.     }
  159.     public void BuyBuilding(Building building)
  160.     {
  161.         if (statManager.Gold >= statManager.buildingCost)
  162.         {
  163.  
  164.             buildingToPlace = building;
  165.             customCursor.gameObject.SetActive(true);
  166.             customCursor.transform.localScale = new Vector3(0.4551f, 0.4551f);
  167.             customCursor.GetComponent<SpriteRenderer>().sprite = building.GetComponent<SpriteRenderer>().sprite;
  168.             Cursor.visible = false;
  169.             if (alwaysShowGrid == false)
  170.             {
  171.                 grid.SetActive(true);
  172.             }
  173.         }
  174.     }
  175.     public void BuyReactor(Reactor reactor)
  176.     {
  177.         if (statManager.Gold >= statManager.reactorCost)
  178.         {
  179.             reactorToPlace = reactor;
  180.             customCursor.gameObject.SetActive(true);
  181.             customCursor.transform.localScale = new Vector3(1f, 1.12f);
  182.             customCursor.GetComponent<SpriteRenderer>().sprite = reactor.GetComponent<SpriteRenderer>().sprite;
  183.  
  184.             Cursor.visible = false;
  185.             if (alwaysShowGrid == false)
  186.             {
  187.                 grid.SetActive(true);
  188.             }
  189.         }
  190.     }
  191. }
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement