From 3fb2121cc0d00cbd42b2ca10b5dfb399a4df1a04 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 21 Mar 2024 10:28:46 +0800 Subject: *misc --- GameCode/ManaSiphon.cs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 GameCode/ManaSiphon.cs (limited to 'GameCode/ManaSiphon.cs') diff --git a/GameCode/ManaSiphon.cs b/GameCode/ManaSiphon.cs new file mode 100644 index 0000000..89e578e --- /dev/null +++ b/GameCode/ManaSiphon.cs @@ -0,0 +1,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(); + 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() == 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; + } +} -- cgit v1.1-26-g67d0