Advertisement
kissemisse

Building

Mar 14th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using Random = UnityEngine.Random;
  7.  
  8. public enum BuildingType {
  9.     House,
  10.     Road,
  11.     Saloon,
  12.     Logging,
  13.     CornFarm,
  14.     Sawmill,
  15.     Mine,
  16.     SheriffsOffice,
  17.     TradingPost,
  18.     Blacksmith,
  19.     Church,
  20.     Store
  21. }
  22.  
  23. public class Building : MonoBehaviour, IBuilding {
  24.     // Size of the building
  25.     [SerializeField] private int length = 1, width = 1;
  26.  
  27.     private ResourceManagmentScript resourceManagment;
  28.  
  29.     private List<CellPoint> relatedCells;
  30.  
  31.     [SerializeField] private BuildingType buildingType;
  32.     [SerializeField] private CellType cellType = CellType.Structure;
  33.  
  34.     [SerializeField] private BuildingRequirements[] buildingRequirements;
  35.  
  36.     [SerializeField] private Color ghostColor;
  37.  
  38.     public bool isGhost;
  39.  
  40.     private BuiltProximity _proximityScript;
  41.     public bool tierUpgrade;
  42.  
  43.     [SerializeField]
  44.     public AudioClip buildSound;
  45.  
  46.     private AudioSource audio;
  47.  
  48.     private IAIManager aiManager;
  49.    
  50.  
  51.     void Start() {
  52.        
  53.         if(buildingType == BuildingType.House) {
  54.             transform.GetChild(0).gameObject.SetActive(true);
  55.         }
  56.  
  57.         _proximityScript = GetComponent<BuiltProximity>();
  58.  
  59.         if(isGhost) {
  60.             if(buildingType != BuildingType.Road) return;
  61.             transform.GetChild(0).GetComponent<MeshRenderer>().material.color = ghostColor;
  62.             return;
  63.         }
  64.        
  65.         // destroy nature
  66.         RaycastHit[] hits = Physics.SphereCastAll(transform.position,
  67.             length * (buildingType == BuildingType.Logging ? 2.5f : 5),
  68.             transform.forward, length * 5, LayerMask.GetMask("Tree"));
  69.  
  70.         foreach(RaycastHit hit in hits) {
  71.             Destroy(hit.transform.gameObject);
  72.         }
  73.  
  74.         // ------ AI START ------------
  75.         aiManager = FindObjectOfType<AIManager>();
  76.         aiManager.UpdateNavMesh(transform.position);
  77.        
  78.         if(buildingType != BuildingType.Road) {
  79.             if(buildingType != BuildingType.Mine) {
  80.                 aiManager.AddSpawnPoint(transform.Find("Destination").position);
  81.             }
  82.         }
  83.         // ------ AI END -------------
  84.  
  85.         // use resources
  86.         resourceManagment = FindObjectOfType<ResourceManagmentScript>();
  87.         foreach(BuildingRequirements resource in buildingRequirements) {
  88.             resourceManagment.SubstractResource(resource.resource, resource.amount);
  89.         }
  90.        
  91.         audio = GetComponent<AudioSource>();
  92.         switch(buildingType)
  93.         {
  94.             case BuildingType.House:
  95.                 resourceManagment.houseAmount += 1;
  96.                 resourceManagment.maxPopulation += 3;
  97.                 if(CheckForSheriffs()) {
  98.                     RandomizeHousePrefab();
  99.             break;
  100.                
  101.         audio.PlayOneShot(buildSound);
  102.     }
  103.  
  104.     private bool CheckForSheriffs() {
  105.         RaycastHit[] hits = Physics.SphereCastAll(transform.position, 30, transform.forward);
  106.         return hits.Any(hit => hit.transform.CompareTag("Sheriff"));
  107.     }
  108.  
  109.     public void Remove() {
  110.        
  111.         if(buildingType == BuildingType.Road) return;
  112.  
  113.         // remove if population allows
  114.         if(buildingType == BuildingType.House) {
  115.             if(resourceManagment.GetPopulationSize() > resourceManagment.maxPopulation - 3) {
  116.                 return;
  117.             }
  118.         }
  119.  
  120.         // clear cells
  121.         if(relatedCells != null && relatedCells.Count > 0) {
  122.             foreach(CellPoint cell in relatedCells) {
  123.                 cell.SetCellType(CellType.Empty);
  124.             }
  125.         }
  126.  
  127.         if(buildingType != BuildingType.Road) {
  128.             // remove destination point from AI manager
  129.             aiManager.RemoveDestinationPoint(transform.Find("Destination").position);
  130.            
  131.             // remove building from resource manager
  132.             resourceManagment.RemoveBuilding(buildingType);
  133.         }
  134.        
  135.         // refund resources
  136.         foreach(BuildingRequirements requirement in buildingRequirements) {
  137.             resourceManagment.AddResource(requirement.resource, requirement.amount / 2);
  138.         }
  139.  
  140.         // update navmesh
  141.         foreach(Collider coll in transform.GetComponents(typeof(Collider))) {
  142.             coll.enabled = false;
  143.         }
  144.         aiManager.UpdateNavMesh(transform.position);
  145.        
  146.         Destroy(gameObject);
  147.     }
  148.  
  149.     public void RandomizeHousePrefab() {
  150.        
  151.         transform.GetChild(0).gameObject.SetActive(false);
  152.        
  153.         Transform building = transform.GetChild(Random.Range(1,4));
  154.         building.gameObject.SetActive(true);
  155.  
  156.        
  157.         // small random rotation
  158.         building.Rotate(Vector3.up, Random.Range(-3, 3));
  159.     }
  160.  
  161.     public int GetWidth() { return width; }
  162.     public int GetLength() { return length; }
  163.  
  164.     public void SetIsGhost(bool newValue) { isGhost = newValue; }
  165.  
  166.     public BuildingType GetBuildingType() { return buildingType; }
  167.  
  168.     public BuildingRequirements[] GetBuildingRequirements() { return buildingRequirements; }
  169.  
  170.     public void SetRelatedCells(List<CellPoint> newCells) { relatedCells = new List<CellPoint>(newCells); }
  171.  
  172.     public CellType GetCellType() { return cellType; }
  173.  
  174.     public void HouseTierUpgrade() {
  175.         tier1.SetActive(false);
  176.         tier2.SetActive(true);    
  177.     }
  178.    
  179.     public void Production(bool work) => resourceManagment.Anount += work ? 1 : 0;
  180. }
  181.  
  182. [Serializable]
  183. public struct BuildingRequirements {
  184.     public Resource resource;
  185.     public int amount;
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement