Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Linq;
- public class GameManager : MonoBehaviour
- {
- public bool alwaysShowGrid = false;
- public int alwaysShowGridPressed = 0;
- public Text goldDisplay;
- public Text buildingCostDisplay;
- public Text reactorCostDisplay;
- public CustomCursor customCursor;
- public StatManager statManager;
- private GameObject grid;
- private GameObject[] buildings;
- private GameObject[] reactors;
- private Building buildingToPlace;
- private Reactor reactorToPlace;
- public Tile[] tileGrid;
- public Tile[,] tiles;
- public void Start()
- {
- grid = GameObject.FindGameObjectWithTag("Grid");
- grid.SetActive(false);
- }
- public void AlwaysShowGrid()
- {
- alwaysShowGridPressed++;
- if (alwaysShowGridPressed == 1)
- {
- alwaysShowGrid = true;
- grid.SetActive(true);
- }
- if (alwaysShowGridPressed > 1)
- {
- alwaysShowGridPressed = 0;
- alwaysShowGrid = false;
- grid.SetActive(false);
- }
- }
- public void Update()
- {
- goldDisplay.text = statManager.Gold.ToString();
- buildingCostDisplay.text = statManager.buildingCost.ToString();
- reactorCostDisplay.text = statManager.reactorCost.ToString();
- buildings = GameObject.FindGameObjectsWithTag("BuildingPrefab");
- statManager.energyBeingUsed = buildings.Length;
- reactors = GameObject.FindGameObjectsWithTag("ReactorPrefab");
- statManager.energyBeingProvided = reactors.Length * 10;
- /*if (Input.GetMouseButtonDown(0) && buildingToPlace != null)
- {
- Tile nearestTile = null;
- float shortestDistance = float.MaxValue;
- foreach(Tile tile in tiles)
- {
- float distance = Vector2.Distance(tile.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
- if (distance < shortestDistance)
- {
- shortestDistance = distance;
- nearestTile = tile;
- }
- }
- if (nearestTile.isOccupied == false)
- {
- statManager.Gold -= statManager.buildingCost;
- Vector3 positionToPlace = nearestTile.transform.position;
- positionToPlace.y += 0.3f;
- Instantiate(buildingToPlace, positionToPlace, Quaternion.identity);
- buildingToPlace = null;
- nearestTile.isOccupied = true;
- customCursor.gameObject.SetActive(false);
- Cursor.visible = true;
- if (alwaysShowGrid == false)
- {
- grid.SetActive(false);
- }
- }
- }*/
- if (Input.GetMouseButtonDown(0) && reactorToPlace != null)
- {
- Vector2 reactorToPlaceDimensions = reactorToPlace.GetComponent<SpriteRenderer>().bounds.size;
- Tile originTile = null;
- float shortestDistance = float.MaxValue;
- foreach (Tile tileIs in tileGrid)
- {
- float distance = Vector2.Distance(tileIs.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
- if (distance < shortestDistance)
- {
- shortestDistance = distance;
- originTile = tileIs;
- }
- }
- Dictionary<Tile, bool> tileOccupancyStatus = new();
- Vector2 origintranspos = originTile.transform.position;
- for (int x = (int)origintranspos.x; x < reactorToPlaceDimensions.x + origintranspos.x && x < tiles.Length; x++)
- {
- for (int y = (int)origintranspos.y; y < reactorToPlaceDimensions.y + origintranspos.y && y < tiles.Length; y++)
- {
- Tile tile = tiles[(int)(origintranspos.x + x), (int)(origintranspos.y + y)];
- tileOccupancyStatus.Add(tile, tile.isOccupied);
- }
- }
- bool canPlace = tileOccupancyStatus.All(tile => tile.Key.isOccupied == false);
- if (canPlace)
- {
- statManager.Gold -= statManager.reactorCost;
- Vector3 positionToPlace = originTile.transform.position;
- Instantiate(reactorToPlace, positionToPlace, Quaternion.identity);
- reactorToPlace = null;
- originTile.isOccupied = true;
- customCursor.gameObject.SetActive(false);
- Cursor.visible = true;
- if (alwaysShowGrid == false)
- {
- grid.SetActive(false);
- }
- }
- }
- if (Input.GetKeyDown(KeyCode.Escape) && buildingToPlace != null)
- {
- buildingToPlace = null;
- customCursor.gameObject.SetActive(false);
- Cursor.visible = true;
- if (alwaysShowGrid == false)
- {
- grid.SetActive(false);
- }
- }
- if (Input.GetKeyDown(KeyCode.Escape) && reactorToPlace != null)
- {
- reactorToPlace = null;
- customCursor.gameObject.SetActive(false);
- Cursor.visible = true;
- if (alwaysShowGrid == false)
- {
- grid.SetActive(false);
- }
- }
- }
- public void BuyBuilding(Building building)
- {
- if (statManager.Gold >= statManager.buildingCost)
- {
- buildingToPlace = building;
- customCursor.gameObject.SetActive(true);
- customCursor.transform.localScale = new Vector3(0.4551f, 0.4551f);
- customCursor.GetComponent<SpriteRenderer>().sprite = building.GetComponent<SpriteRenderer>().sprite;
- Cursor.visible = false;
- if (alwaysShowGrid == false)
- {
- grid.SetActive(true);
- }
- }
- }
- public void BuyReactor(Reactor reactor)
- {
- if (statManager.Gold >= statManager.reactorCost)
- {
- reactorToPlace = reactor;
- customCursor.gameObject.SetActive(true);
- customCursor.transform.localScale = new Vector3(1f, 1.12f);
- customCursor.GetComponent<SpriteRenderer>().sprite = reactor.GetComponent<SpriteRenderer>().sprite;
- Cursor.visible = false;
- if (alwaysShowGrid == false)
- {
- grid.SetActive(true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement