summaryrefslogtreecommitdiff
path: root/GameCode/BuildingManager.cs
blob: 4ace1acf9f76d20ec3336870aef15cb11fc4a1b5 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using UnityEngine;

public class BuildingManager : MonoBehaviour
{
	public static BuildingManager instance;

	[SerializeField]
	private GameObject buildingGhost;

	private GameObject currentGhost;

	[SerializeField]
	private GameObject placementFXObject;

	[SerializeField]
	public GameObject levelUpFX;

	[SerializeField]
	private LayerMask buildableMask;

	private bool buildSpotAvailable;

	private GameObject thingToBuild;

	private int buildingCost;

	private int priceIncrease;

	private TowerType tType;

	public bool buildMode { get; private set; }

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		buildMode = false;
	}

	private void FixedUpdate()
	{
		if (buildMode)
		{
			buildSpotAvailable = SamplePoint();
		}
	}

	private void Update()
	{
		if (!buildMode)
		{
			return;
		}
		if (Input.GetMouseButtonDown(0) && BuildingCheck())
		{
			ResourceManager.instance.Spend(buildingCost);
			DamageTracker.instance.AddCost(tType, buildingCost);
			Build();
			if (!Input.GetKey(KeyCode.LeftShift))
			{
				ExitBuildMode();
			}
			else
			{
				buildingCost += priceIncrease;
			}
		}
		if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
		{
			ExitBuildMode();
		}
	}

	public void EnterBuildMode(GameObject objectToBuild, int cost, int _priceIncrease, TowerType type)
	{
		buildMode = true;
		thingToBuild = objectToBuild;
		buildingCost = cost;
		priceIncrease = _priceIncrease;
		tType = type;
	}

	private void ExitBuildMode()
	{
		HideGhost();
		buildMode = false;
	}

	private void Build()
	{
		GameObject gameObject = Object.Instantiate(thingToBuild, currentGhost.transform.position, Quaternion.identity);
		gameObject.GetComponent<IBuildable>()?.SetStats();
		Object.Instantiate(placementFXObject, gameObject.transform.position + Vector3.up * 0.333f, Quaternion.identity);
		buildSpotAvailable = SamplePoint();
	}

	private bool BuildingCheck()
	{
		if (!PauseMenu.instance.paused && buildSpotAvailable && ResourceManager.instance.CheckMoney(buildingCost))
		{
			return true;
		}
		if (!ResourceManager.instance.CheckMoney(buildingCost) && !PauseMenu.instance.paused && buildSpotAvailable)
		{
			DamageNumber component = ObjectPool.instance.SpawnObject(ObjectPool.ObjectType.DamageNumber, currentGhost.transform.position + Vector3.up, Quaternion.identity).GetComponent<DamageNumber>();
			component.SetText("Not enough gold", "Grey", 1f);
			component.SetHoldTime(0.25f);
		}
		return false;
	}

	private bool SamplePoint()
	{
		if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo, 2000f, buildableMask, QueryTriggerInteraction.Ignore))
		{
			if (hitInfo.collider.gameObject.layer != LayerMask.NameToLayer("Grass"))
			{
				HideGhost();
				return false;
			}
			Vector3 vector = new Vector3(Mathf.Round(hitInfo.point.x), Mathf.Round(3f * hitInfo.point.y) / 3f, Mathf.Round(hitInfo.point.z));
			if (Vector3.SqrMagnitude(hitInfo.point - vector) < 0.25f)
			{
				string text = "";
				if (vector.y > 0.34f)
				{
					text = "+" + (Mathf.RoundToInt(vector.y * 3f) - 1);
				}
				DisplayGhost(vector, text);
				return true;
			}
			HideGhost();
			return false;
		}
		HideGhost();
		return false;
	}

	private void DisplayGhost(Vector3 pos, string text)
	{
		if (currentGhost == null)
		{
			currentGhost = Object.Instantiate(buildingGhost, pos, Quaternion.identity);
		}
		else
		{
			currentGhost.SetActive(value: true);
			currentGhost.transform.position = pos;
		}
		currentGhost.GetComponent<BuildingGhost>().SetText(text);
	}

	private void HideGhost()
	{
		if (currentGhost != null)
		{
			currentGhost.SetActive(value: false);
		}
	}
}