forked from CommandoCorey/RiseFromTheAshes
/
ResourceBuilding.cs
113 lines (92 loc) · 3.33 KB
/
ResourceBuilding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ResourceBuilding : MonoBehaviour
{
[SerializeField] ResourceType resourceToAdd;
[SerializeField] int maxQuantityIncrease = 0;
[SerializeField] public int quantityToAdd = 10;
[SerializeField] public float timePerIncerement = 1;
[SerializeField] public bool giveToAIPlayer = false;
[Header("Floating Text Label")]
public GameObject floatingLabel;
private ResourceManager resources;
private GameManager gameManager;
private Building building;
private bool wasPaused = false;
private bool generating = false;
private int currentAmount, maxAmount;
// Start is called before the first frame update
void Start()
{
resources = ResourceManager.Instance;
gameManager = GameManager.Instance;
building = GetComponent<Building>();
//if (gameObject.CompareTag("Headquarters"))
//building.IsBuilding = false;
if (gameManager.State == GameState.Running && !building.IsBuilding)
{
Invoke("IncrementResource", timePerIncerement);
generating = true;
}
resources.IncreaseResourceMax(resourceToAdd, maxQuantityIncrease, giveToAIPlayer);
}
// Update is called once per frame
void Update()
{
currentAmount = resources.GetResource(resourceToAdd, giveToAIPlayer);
maxAmount = resources.GetResourceMax(resourceToAdd, giveToAIPlayer);
if (currentAmount >= maxAmount)
return;
if(!wasPaused && gameManager.State == GameState.Paused)
wasPaused = true;
if(gameManager.State == GameState.Running && !generating)
{
if (building.IsBuilt || gameObject.CompareTag("Headquarters"))
{
generating = true;
Invoke("IncrementResource", timePerIncerement);
}
}
if(wasPaused && gameManager.State == GameState.Running)
{
if (building.IsBuilt || gameObject.CompareTag("Headquarters"))
{
Invoke("IncrementResource", timePerIncerement);
wasPaused = false;
}
}
}
private void IncrementResource()
{
if (giveToAIPlayer)
resources.AddResourceToAI(resourceToAdd, quantityToAdd);
else
resources.AddResource(resourceToAdd, quantityToAdd);
if (floatingLabel && gameObject.layer == 8) // player building layer
{
floatingLabel.SetActive(true);
floatingLabel.GetComponent<FloatingResourceLabel>().Begin(quantityToAdd, timePerIncerement);
}
currentAmount = resources.GetResource(resourceToAdd, giveToAIPlayer);
maxAmount = resources.GetResourceMax(resourceToAdd, giveToAIPlayer);
// if reosources is not at maximum generate again
if (gameManager.State == GameState.Running && currentAmount < maxAmount)
{
Invoke("IncrementResource", timePerIncerement);
}
else
{
generating = false;
}
}
private void HideLabel()
{
floatingLabel.SetActive(false);
}
public void OnDie()
{
ResourceManager.Instance.DecreaseResourceMaximum(resourceToAdd, maxQuantityIncrease, giveToAIPlayer);
}
}