Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class BuildManager : MonoBehaviour {
- private UIController ui;
- private CursorCollider cursor;
- private ResourceManagmentScript resourceManager;
- private Vector3 placementRotation;
- private bool _destroyMode;
- [SerializeField] private List<StructurePrefabs> buildPrefabs;
- private TutorialIntro tut;
- [SerializeField] private GameObject houseGhost,
- cornGhost,
- loggingGhost,
- sheriffsGhost,
- sawmillGhost,
- tradeGhost,
- mineGhost,
- saloonGhost,
- blacksmithGhost,
- churchGhost,
- storeGhost;
- [SerializeField] private GameObject roadStraight, roadDeadEnd, roadCorner, roadThreeWay, roadFourWay;
- private GameObject currentPrefab;
- private bool canBuild;
- private CellPoint[,] grid;
- private BaseGrid baseGrid;
- public Action<bool> DestroyModeActivated;
- void Start() {
- ui = FindObjectOfType<UIController>();
- ui.OnStructurePlacement += SelectStructure;
- grid = FindObjectOfType<BaseGrid>().grid;
- cursor = FindObjectOfType<CursorCollider>();
- baseGrid = FindObjectOfType<BaseGrid>();
- resourceManager = FindObjectOfType<ResourceManagmentScript>();
- tut = FindObjectOfType<TutorialIntro>();
- }
- private void Update() {
- // rotation
- if(Input.GetKeyDown(KeyCode.E)) {
- placementRotation += Vector3.up * 90;
- cursor.RotateGhost(placementRotation);
- } else if(Input.GetKeyDown(KeyCode.Q)) {
- placementRotation += -Vector3.up * 90;
- cursor.RotateGhost(placementRotation);
- }
- if(Input.GetKeyDown(KeyCode.T)) SetDestroyMode(true);
- else if(Input.GetKeyUp(KeyCode.T)) SetDestroyMode(false);
- // place structure
- BuildingType currentBuildingType = currentPrefab.GetComponent<IBuilding>().GetBuildingType();
- if(Input.GetKeyDown(KeyCode.Mouse0) && !EventSystem.current.IsPointerOverGameObject() && !_destroyMode && currentBuildingType != BuildingType.Road && currentBuildingType != BuildingType.Mine) {
- PlaceStructure();
- } else if(Input.GetKey(KeyCode.Mouse0) && !EventSystem.current.IsPointerOverGameObject() && !_destroyMode &&
- currentPrefab.GetComponent<IBuilding>().GetBuildingType() == BuildingType.Road) {
- PlaceStructure();
- }
- }
- private void PlaceStructure() {
- if(!canBuild) return;
- IBuilding buildingInterface = currentPrefab.GetComponent<IBuilding>();
- // return if mine, can only build mine in caves
- if(buildingInterface.GetBuildingType() == BuildingType.Mine) return;
- // if pop is at max, player can only build houses and roads
- if(resourceManager.AtMaxPopulation()) {
- BuildingType building = buildingInterface.GetBuildingType();
- if(building != BuildingType.Road && building != BuildingType.House) {
- return;
- }
- }
- // check if resources are enough
- foreach(BuildingRequirements buildingRequirement in buildingInterface.GetBuildingRequirements()) {
- if(resourceManager.CheckResourceEnough(buildingRequirement.resource, buildingRequirement.amount)) continue;
- return;
- }
- CellPoint buildCellPoint = cursor.GetNearestCell();
- if(buildCellPoint == null || currentPrefab == null) return;
- int width = buildingInterface.GetWidth();
- int length = buildingInterface.GetLength();
- int xPos = buildCellPoint.GetCellX();
- int zPos = buildCellPoint.GetCellZ();
- int modifiedXPos = xPos - width / 2;
- int modifiedZPos = zPos - length / 2;
- List<CellPoint> currentCells = new List<CellPoint>();
- // check if cells are empty
- for(int i = 0; i < width; i++) {
- for(int j = 0; j < length; j++) {
- if(!grid[modifiedXPos + i, modifiedZPos + j].IsEmpty()) {
- return;
- }
- currentCells.Add(grid[modifiedXPos + i, modifiedZPos + j]);
- }
- }
- CellType newCellType = currentPrefab.GetComponent<IBuilding>().GetCellType();
- if(newCellType == CellType.Structure) {
- // check if near road
- bool isNearRoad = false;
- foreach(CellPoint cell in currentCells) {
- CellType[] neighbourCellTypes = baseGrid.GetCellNeighbourTypes(cell.GetCellX(), cell.GetCellZ());
- if(neighbourCellTypes.Any(cellType => cellType == CellType.Road)) {
- isNearRoad = true;
- }
- }
- if(!isNearRoad) {
- return;
- }
- }
- // set new cell type
- foreach(CellPoint cell in currentCells) {
- cell.SetCellType(newCellType);
- }
- Vector3 buildPos = cursor.GetGhostPosition();
- if(buildPos == Vector3.zero) return;
- GameObject newStructure = null;
- if(width > 1 || length > 1) { // Spawn big structure
- newStructure = Instantiate(currentPrefab, buildPos, Quaternion.Euler(placementRotation), transform);
- if(currentPrefab.GetComponent<IBuilding>().GetBuildingType() == BuildingType.CornFarm) {
- tut.WoodBuilt();
- }
- } else {
- switch(newCellType) {
- // spawn normal structure
- case CellType.Structure:
- newStructure = Instantiate(currentPrefab, buildCellPoint.transform.position, Quaternion.Euler(placementRotation), transform);
- BuildingType newBuilding = currentPrefab.GetComponent<IBuilding>()!.GetBuildingType();
- switch(newBuilding) {
- case BuildingType.House:
- tut.HouseBuiltUI();
- break;
- case BuildingType.Logging:
- tut.FarmBuilt();
- break;
- }
- break;
- case CellType.Road: {
- // spawn road
- CellPoint[] neighbourCellTypes = baseGrid.GetCellNeighbours(buildCellPoint.GetCellX(), buildCellPoint.GetCellZ());
- GameObject newRoad = FixRoad(ref buildCellPoint.relatedStructure, neighbourCellTypes, buildCellPoint, true);
- tut.RoadPlacedUI();
- newRoad.GetComponent<IBuilding>().SetRelatedCells(currentCells);
- return;
- }
- }
- }
- if(newStructure is null) return;
- newStructure.GetComponent<IBuilding>()?.SetRelatedCells(currentCells);
- buildCellPoint.relatedStructure = newStructure;
- }
- // 0 = down, 1 = up, 2 = right, 3 = left
- public GameObject FixRoad(ref GameObject roadPiece, CellPoint[] neighbours, CellPoint buildCell, bool fixNeighbourRoads) {
- int roadConnections = neighbours.Count(neighbour => neighbour.GetCurrentCellType() == CellType.Road);
- if(roadPiece != null) { Destroy(roadPiece); }
- CellType[] neighbourCellTypes = baseGrid.GetCellNeighbourTypes(buildCell.GetCellX(), buildCell.GetCellZ());
- switch(roadConnections) {
- case 0:
- roadPiece = Instantiate(roadStraight, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
- break;
- case 1:
- // Dead end
- if(neighbourCellTypes[0] == CellType.Road) {
- roadPiece = Instantiate(roadDeadEnd, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 90, 0)));
- } else if(neighbourCellTypes[1] == CellType.Road) {
- roadPiece = Instantiate(roadDeadEnd, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 270, 0)));
- } else if(neighbourCellTypes[2] == CellType.Road) {
- roadPiece = Instantiate(roadDeadEnd, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
- } else if(neighbourCellTypes[3] == CellType.Road) {
- roadPiece = Instantiate(roadDeadEnd, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 180, 0)));
- }
- break;
- case 2: // Straights & Corners
- // Straights
- if(neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[1] == CellType.Road && neighbourCellTypes[2] != CellType.Road && neighbourCellTypes[3] != CellType.Road) { // up & down
- roadPiece = Instantiate(roadStraight, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 90, 0)));
- } else if(neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[3] == CellType.Road && neighbourCellTypes[1] != CellType.Road && neighbourCellTypes[0] != CellType.Road) { // left & right
- roadPiece = Instantiate(roadStraight, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
- // Corners
- } else if(neighbourCellTypes[1] == CellType.Road && neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[3] != CellType.Road && neighbourCellTypes[0] != CellType.Road) {
- roadPiece = Instantiate(roadCorner, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 270, 0)));
- } else if(neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[3] != CellType.Road && neighbourCellTypes[1] != CellType.Road){
- roadPiece = Instantiate(roadCorner, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
- } else if(neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[3] == CellType.Road && neighbourCellTypes[2] != CellType.Road && neighbourCellTypes[1] != CellType.Road){
- roadPiece = Instantiate(roadCorner, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 90, 0)));
- } else if(neighbourCellTypes[3] == CellType.Road && neighbourCellTypes[1] == CellType.Road && neighbourCellTypes[0] != CellType.Road && neighbourCellTypes[2] != CellType.Road){
- roadPiece = Instantiate(roadCorner, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 180, 0)));
- }
- break;
- case 3:
- // Three way
- if(neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[1] == CellType.Road && neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[3] != CellType.Road) {
- roadPiece = Instantiate(roadThreeWay, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 180, 0)));
- } else if(neighbourCellTypes[1] == CellType.Road && neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[3] == CellType.Road && neighbourCellTypes[0] != CellType.Road) {
- roadPiece = Instantiate(roadThreeWay, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 90, 0)));
- } else if(neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[3] == CellType.Road && neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[1] != CellType.Road) {
- roadPiece = Instantiate(roadThreeWay, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 270, 0)));
- } else if(neighbourCellTypes[3] == CellType.Road && neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[1] == CellType.Road && neighbourCellTypes[2] != CellType.Road) {
- roadPiece = Instantiate(roadThreeWay, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
- }
- break;
- case 4:
- // Four way
- if(neighbourCellTypes[0] == CellType.Road && neighbourCellTypes[1] == CellType.Road &&
- neighbourCellTypes[2] == CellType.Road && neighbourCellTypes[3] == CellType.Road) {
- roadPiece = Instantiate(roadFourWay, buildCell.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
- }
- break;
- }
- if(fixNeighbourRoads) {
- // Fix the neighbouring roads
- foreach(CellPoint neighbour in neighbours) {
- if(grid[neighbour.GetCellX(), neighbour.GetCellZ()].GetCurrentCellType() == CellType.Road)
- FixRoad(ref neighbour.relatedStructure, baseGrid.GetCellNeighbours(neighbour.GetCellX(), neighbour.GetCellZ()), neighbour, false);
- }
- }
- return roadPiece;
- }
- public void SetDestroyMode(bool destroy) {
- _destroyMode = destroy;
- DestroyModeActivated?.Invoke(destroy);
- }
- public bool GetDestroyMode() {
- return _destroyMode;
- }
- }
- [Serializable]
- public struct StructurePrefabs {
- public GameObject[] prefabs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement