Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using Random = UnityEngine.Random;
- public enum BuildingType {
- House,
- Road,
- Saloon,
- Logging,
- CornFarm,
- Sawmill,
- Mine,
- SheriffsOffice,
- TradingPost,
- Blacksmith,
- Church,
- Store
- }
- public class Building : MonoBehaviour, IBuilding {
- // Size of the building
- [SerializeField] private int length = 1, width = 1;
- private ResourceManagmentScript resourceManagment;
- private List<CellPoint> relatedCells;
- [SerializeField] private BuildingType buildingType;
- [SerializeField] private CellType cellType = CellType.Structure;
- [SerializeField] private BuildingRequirements[] buildingRequirements;
- [SerializeField] private Color ghostColor;
- public bool isGhost;
- private BuiltProximity _proximityScript;
- public bool tierUpgrade;
- [SerializeField]
- public AudioClip buildSound;
- private AudioSource audio;
- private IAIManager aiManager;
- void Start() {
- if(buildingType == BuildingType.House) {
- transform.GetChild(0).gameObject.SetActive(true);
- }
- _proximityScript = GetComponent<BuiltProximity>();
- if(isGhost) {
- if(buildingType != BuildingType.Road) return;
- transform.GetChild(0).GetComponent<MeshRenderer>().material.color = ghostColor;
- return;
- }
- // destroy nature
- RaycastHit[] hits = Physics.SphereCastAll(transform.position,
- length * (buildingType == BuildingType.Logging ? 2.5f : 5),
- transform.forward, length * 5, LayerMask.GetMask("Tree"));
- foreach(RaycastHit hit in hits) {
- Destroy(hit.transform.gameObject);
- }
- // ------ AI START ------------
- aiManager = FindObjectOfType<AIManager>();
- aiManager.UpdateNavMesh(transform.position);
- if(buildingType != BuildingType.Road) {
- if(buildingType != BuildingType.Mine) {
- aiManager.AddSpawnPoint(transform.Find("Destination").position);
- }
- }
- // ------ AI END -------------
- // use resources
- resourceManagment = FindObjectOfType<ResourceManagmentScript>();
- foreach(BuildingRequirements resource in buildingRequirements) {
- resourceManagment.SubstractResource(resource.resource, resource.amount);
- }
- audio = GetComponent<AudioSource>();
- switch(buildingType)
- {
- case BuildingType.House:
- resourceManagment.houseAmount += 1;
- resourceManagment.maxPopulation += 3;
- if(CheckForSheriffs()) {
- RandomizeHousePrefab();
- break;
- audio.PlayOneShot(buildSound);
- }
- private bool CheckForSheriffs() {
- RaycastHit[] hits = Physics.SphereCastAll(transform.position, 30, transform.forward);
- return hits.Any(hit => hit.transform.CompareTag("Sheriff"));
- }
- public void Remove() {
- if(buildingType == BuildingType.Road) return;
- // remove if population allows
- if(buildingType == BuildingType.House) {
- if(resourceManagment.GetPopulationSize() > resourceManagment.maxPopulation - 3) {
- return;
- }
- }
- // clear cells
- if(relatedCells != null && relatedCells.Count > 0) {
- foreach(CellPoint cell in relatedCells) {
- cell.SetCellType(CellType.Empty);
- }
- }
- if(buildingType != BuildingType.Road) {
- // remove destination point from AI manager
- aiManager.RemoveDestinationPoint(transform.Find("Destination").position);
- // remove building from resource manager
- resourceManagment.RemoveBuilding(buildingType);
- }
- // refund resources
- foreach(BuildingRequirements requirement in buildingRequirements) {
- resourceManagment.AddResource(requirement.resource, requirement.amount / 2);
- }
- // update navmesh
- foreach(Collider coll in transform.GetComponents(typeof(Collider))) {
- coll.enabled = false;
- }
- aiManager.UpdateNavMesh(transform.position);
- Destroy(gameObject);
- }
- public void RandomizeHousePrefab() {
- transform.GetChild(0).gameObject.SetActive(false);
- Transform building = transform.GetChild(Random.Range(1,4));
- building.gameObject.SetActive(true);
- // small random rotation
- building.Rotate(Vector3.up, Random.Range(-3, 3));
- }
- public int GetWidth() { return width; }
- public int GetLength() { return length; }
- public void SetIsGhost(bool newValue) { isGhost = newValue; }
- public BuildingType GetBuildingType() { return buildingType; }
- public BuildingRequirements[] GetBuildingRequirements() { return buildingRequirements; }
- public void SetRelatedCells(List<CellPoint> newCells) { relatedCells = new List<CellPoint>(newCells); }
- public CellType GetCellType() { return cellType; }
- public void HouseTierUpgrade() {
- tier1.SetActive(false);
- tier2.SetActive(true);
- }
- public void Production(bool work) => resourceManagment.Anount += work ? 1 : 0;
- }
- [Serializable]
- public struct BuildingRequirements {
- public Resource resource;
- public int amount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement