summaryrefslogtreecommitdiff
path: root/GameCode/ManaSiphon.cs
blob: 89e578ea83457746dfe4e2f1cea41279e321ff43 (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
using UnityEngine;

public class ManaSiphon : MonoBehaviour, IBuildable
{
	[SerializeField]
	private int gatherRatePerSec;

	[SerializeField]
	private LayerMask layermask;

	[SerializeField]
	private GameObject UIObject;

	[SerializeField]
	private int goldBackOnDemolish;

	private bool gathering;

	private float timer = 1f;

	private void Start()
	{
		DetectMana();
		if (gathering)
		{
			ResourceManager.instance.UpdateManaGatherRate(gatherRatePerSec);
		}
	}

	private void Update()
	{
		if (timer <= 0f)
		{
			Gather();
			timer = 1f;
		}
		else if (gathering && SpawnManager.instance.combat)
		{
			timer -= Time.deltaTime;
		}
	}

	public void SetStats()
	{
	}

	public void SpawnUI()
	{
		SimpleUI component = Object.Instantiate(UIObject, base.transform.position, Quaternion.identity).GetComponent<SimpleUI>();
		component.SetDemolishable(base.gameObject, goldBackOnDemolish);
		if (gathering)
		{
			component.SetDiscriptionText("Currently gathering 1 mana/sec.");
		}
	}

	public void Demolish()
	{
		if (gathering)
		{
			ResourceManager.instance.UpdateManaGatherRate(-gatherRatePerSec);
		}
	}

	private void Gather()
	{
		ResourceManager.instance.AddMana(gatherRatePerSec);
	}

	private void DetectMana()
	{
		if ((!Physics.Raycast(base.transform.position + new Vector3(1f, 1f, 0f), -base.transform.up, out var hitInfo, 1f, layermask, QueryTriggerInteraction.Ignore) || !Rotate(hitInfo)) && (!Physics.Raycast(base.transform.position + new Vector3(-1f, 1f, 0f), -base.transform.up, out hitInfo, 1f, layermask, QueryTriggerInteraction.Ignore) || !Rotate(hitInfo)) && (!Physics.Raycast(base.transform.position + new Vector3(0f, 1f, 1f), -base.transform.up, out hitInfo, 1f, layermask, QueryTriggerInteraction.Ignore) || !Rotate(hitInfo)) && Physics.Raycast(base.transform.position + new Vector3(0f, 1f, -1f), -base.transform.up, out hitInfo, 1f, layermask, QueryTriggerInteraction.Ignore))
		{
			Rotate(hitInfo);
		}
	}

	private bool Rotate(RaycastHit hit)
	{
		if (hit.collider.GetComponent<ManaCrystal>() == null)
		{
			return false;
		}
		if ((double)Mathf.Abs(hit.collider.transform.position.y - base.transform.position.y) > 0.001)
		{
			return false;
		}
		base.transform.LookAt(hit.collider.transform.position, Vector3.up);
		gathering = true;
		return true;
	}
}