107 lines
3.4 KiB
C#
107 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
[System.Serializable]
|
|
public class Technology
|
|
{
|
|
public string techName;
|
|
public enum AvailabilityState { Locked, Unlocked, Researching, Learned }
|
|
public AvailabilityState availabilityState;
|
|
public string techDescription;
|
|
public Sprite techImage;
|
|
public List<ResourceAmount> resourceCosts;
|
|
public List<RequiredTech> techRequirements;
|
|
public List<TechModifiers> techModifiers;
|
|
[SerializeField]
|
|
private int researchedTurns;
|
|
public int requiredResearchTurns;
|
|
public Color techUIColor;
|
|
public Technology()
|
|
{
|
|
if (availabilityState == AvailabilityState.Locked)
|
|
{
|
|
TechEvents.OnTechResearchCompleted += CheckRequirements;
|
|
}
|
|
}
|
|
public void Unlock()
|
|
{
|
|
availabilityState = AvailabilityState.Unlocked;
|
|
TechEvents.TechUnlocked(this);
|
|
TechEvents.OnTechResearchCompleted -= CheckRequirements;
|
|
Debug.Log("Tech was unlocked:" + this.techName);
|
|
}
|
|
public void Learn()
|
|
{
|
|
TechEvents.TechResearchCompleted(this);
|
|
TechEvents.OnTurnPassed -= NewTurn;
|
|
availabilityState = AvailabilityState.Learned;
|
|
Debug.Log("Tech was learned: " + this.techName);
|
|
}
|
|
public void NewTurn()
|
|
{
|
|
if (availabilityState == AvailabilityState.Researching)
|
|
{
|
|
researchedTurns++;
|
|
if (researchedTurns >= requiredResearchTurns)
|
|
{
|
|
Learn();
|
|
}
|
|
}
|
|
}
|
|
private void CheckRequirements(Technology tech)
|
|
{
|
|
if (availabilityState == AvailabilityState.Locked)
|
|
{
|
|
RequiredTech requireTech = techRequirements.FirstOrDefault(rt=>rt.techName == tech.techName);
|
|
if (requireTech != null && !requireTech.completed)
|
|
{
|
|
requireTech.completed = true;
|
|
if (!techRequirements.Any(t=>t.completed == false))
|
|
{
|
|
Unlock();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public bool StartResearching()
|
|
{
|
|
bool hasEnoughResources = false;
|
|
List<ResourceAmount> resourceAmounts = new List<ResourceAmount>();
|
|
for (int i = 0; i < resourceCosts.Count; i++)
|
|
{
|
|
ResourceAmount currentAmount = ResourceManager.Instance.GetResourceAmount(resourceCosts[i].resourceType);
|
|
ResourceAmount cost = resourceCosts[i];
|
|
if (currentAmount.resourceAmount >= cost.resourceAmount)
|
|
{
|
|
resourceAmounts.Add(currentAmount);
|
|
}
|
|
}
|
|
if (resourceAmounts.Count >= resourceCosts.Count)
|
|
{
|
|
hasEnoughResources = true;
|
|
for (int i = 0; i < resourceCosts.Count; i++)
|
|
{
|
|
resourceAmounts[i].resourceAmount -= resourceCosts[i].resourceAmount;
|
|
}
|
|
availabilityState = AvailabilityState.Researching;
|
|
TechEvents.TechResearchStarted(this);
|
|
TechEvents.OnTurnPassed += NewTurn;
|
|
}
|
|
return hasEnoughResources;
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public class RequiredTech
|
|
{
|
|
public string techName;
|
|
public bool completed;
|
|
}
|
|
[System.Serializable]
|
|
public class TechModifiers
|
|
{
|
|
public enum ModifierType { Gold, Steel, Wood, Water, Population, Food }
|
|
public ModifierType modifier;
|
|
public float amount;
|
|
}
|