147 lines
4.5 KiB
C#
147 lines
4.5 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
public class Building : MonoBehaviour
|
||
|
|
{
|
||
|
|
public enum BuildingType
|
||
|
|
{
|
||
|
|
House,
|
||
|
|
Farm,
|
||
|
|
Barracks
|
||
|
|
}
|
||
|
|
public enum BuildingState
|
||
|
|
{
|
||
|
|
Unbuilt,
|
||
|
|
Constructing,
|
||
|
|
Active,
|
||
|
|
Destroyed
|
||
|
|
}
|
||
|
|
[SerializeField] float constructionTime;
|
||
|
|
[SerializeField] private Resource.ResourceType[] resourceTypes;
|
||
|
|
[SerializeField] private int[] constructionCost;
|
||
|
|
private BuildingState buildingState;
|
||
|
|
[SerializeField] private BuildingType buildingType;
|
||
|
|
[SerializeField] private GameObject unbuiltModel, constructingModel, activeModel, destroyedModel;
|
||
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
buildingState = BuildingState.Unbuilt;
|
||
|
|
SetBuildingModel(buildingState);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
public BuildingType GetBuildingType()
|
||
|
|
{
|
||
|
|
return buildingType;
|
||
|
|
}
|
||
|
|
public BuildingState GetBuildingState()
|
||
|
|
{
|
||
|
|
return buildingState;
|
||
|
|
}
|
||
|
|
private void SetBuildingModel(BuildingState state)
|
||
|
|
{
|
||
|
|
switch (state)
|
||
|
|
{
|
||
|
|
case BuildingState.Unbuilt:
|
||
|
|
unbuiltModel.SetActive(true);
|
||
|
|
constructingModel.SetActive(false);
|
||
|
|
activeModel.SetActive(false);
|
||
|
|
destroyedModel.SetActive(false);
|
||
|
|
break;
|
||
|
|
case BuildingState.Constructing:
|
||
|
|
unbuiltModel.SetActive(false);
|
||
|
|
constructingModel.SetActive(true);
|
||
|
|
activeModel.SetActive(false);
|
||
|
|
destroyedModel.SetActive(false);
|
||
|
|
StartCoroutine(ConstructionTimer(constructionTime));
|
||
|
|
break;
|
||
|
|
case BuildingState.Active:
|
||
|
|
unbuiltModel.SetActive(false);
|
||
|
|
constructingModel.SetActive(false);
|
||
|
|
activeModel.SetActive(true);
|
||
|
|
destroyedModel.SetActive(false);
|
||
|
|
break;
|
||
|
|
case BuildingState.Destroyed:
|
||
|
|
unbuiltModel.SetActive(false);
|
||
|
|
constructingModel.SetActive(false);
|
||
|
|
activeModel.SetActive(false);
|
||
|
|
destroyedModel.SetActive(true);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public void StartBuilding()
|
||
|
|
{
|
||
|
|
if (buildingState != BuildingState.Unbuilt)
|
||
|
|
return;
|
||
|
|
|
||
|
|
for (int i = 0; i < resourceTypes.Length && i < constructionCost.Length; i++)
|
||
|
|
{
|
||
|
|
if (GameManager.Instance.GetResource(resourceTypes[i]) < constructionCost[i])
|
||
|
|
{
|
||
|
|
Debug.Log("Not enough resources");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
GameManager.Instance.AddResource(resourceTypes[i], -constructionCost[i]);
|
||
|
|
}
|
||
|
|
Debug.Log("Building Started");
|
||
|
|
buildingState = BuildingState.Constructing;
|
||
|
|
SetBuildingModel(buildingState);
|
||
|
|
}
|
||
|
|
public void FinishBuilding()
|
||
|
|
{
|
||
|
|
if (buildingState != BuildingState.Constructing)
|
||
|
|
return;
|
||
|
|
buildingState = BuildingState.Active;
|
||
|
|
SetBuildingModel(buildingState);
|
||
|
|
}
|
||
|
|
public void DestroyBuilding()
|
||
|
|
{
|
||
|
|
if (buildingState != BuildingState.Active)
|
||
|
|
return;
|
||
|
|
buildingState = BuildingState.Destroyed;
|
||
|
|
SetBuildingModel(buildingState);
|
||
|
|
}
|
||
|
|
public void CancelBuilding()
|
||
|
|
{
|
||
|
|
if (buildingState != BuildingState.Constructing)
|
||
|
|
return;
|
||
|
|
buildingState = BuildingState.Unbuilt;
|
||
|
|
SetBuildingModel(buildingState);
|
||
|
|
}
|
||
|
|
private IEnumerator ConstructionTimer(float constructionTime)
|
||
|
|
{
|
||
|
|
while (constructionTime > 0)
|
||
|
|
{
|
||
|
|
yield return Time.deltaTime;
|
||
|
|
constructionTime -= Time.deltaTime;
|
||
|
|
}
|
||
|
|
FinishBuilding();
|
||
|
|
}
|
||
|
|
public void Interact(NPC npcInteracting)
|
||
|
|
{
|
||
|
|
switch (buildingState)
|
||
|
|
{
|
||
|
|
case BuildingState.Unbuilt:
|
||
|
|
StartBuilding();
|
||
|
|
break;
|
||
|
|
case BuildingState.Constructing:
|
||
|
|
break;
|
||
|
|
case BuildingState.Active:
|
||
|
|
//DestroyBuilding();
|
||
|
|
var npcInventory = npcInteracting.GetInventory();
|
||
|
|
foreach (KeyValuePair<Resource.ResourceType, int> entry in npcInventory)
|
||
|
|
GameManager.Instance.AddResource(entry.Key, entry.Value);
|
||
|
|
break;
|
||
|
|
case BuildingState.Destroyed:
|
||
|
|
CancelBuilding();
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|