Advertisement
Unkn0wn4all

Untitled

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