85 lines
2.1 KiB
C#
85 lines
2.1 KiB
C#
|
using TMPro;
|
||
|
using UnityEngine;
|
||
|
using Player.Information;
|
||
|
|
||
|
public class ConstructManager : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private bool isMaker = true;
|
||
|
|
||
|
[SerializeField] private string resourceName = "";
|
||
|
|
||
|
[SerializeField] private float resourceMax = 50f;
|
||
|
|
||
|
[SerializeField] private float resourceRate = 1f;
|
||
|
|
||
|
[SerializeField] private float radiusEffect = 5f;
|
||
|
|
||
|
private GameObject player;
|
||
|
private PlayerStats playerStats;
|
||
|
private float resource;
|
||
|
|
||
|
private TMP_Text resourceIndicator;
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
private void Start()
|
||
|
{
|
||
|
resourceIndicator = transform.Find("ResourceIndicator").GetComponent<TMP_Text>();
|
||
|
|
||
|
if (isMaker)
|
||
|
{
|
||
|
resourceIndicator = transform.Find("ResourceIndicator").GetComponent<TMP_Text>();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
resourceIndicator.enabled = false;
|
||
|
player = GameObject.FindGameObjectWithTag("Player");
|
||
|
playerStats = player.GetComponent<PlayerStats>();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
private void Update()
|
||
|
{
|
||
|
if (isMaker)
|
||
|
{
|
||
|
if (resource != resourceMax)
|
||
|
{
|
||
|
resource = Mathf.Clamp(resource + resourceRate * Time.deltaTime, 0f, resourceMax);
|
||
|
resourceIndicator.SetText(resourceName + ":\n" + Mathf.FloorToInt(resource));
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (Vector3.Distance(player.transform.position, transform.position) <= radiusEffect)
|
||
|
playerStats.ChangeHealth(resourceRate * Time.deltaTime);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// called whenever the player collects the resources from this construct
|
||
|
public float CollectAll()
|
||
|
{
|
||
|
if (isMaker)
|
||
|
{
|
||
|
var amount = resource;
|
||
|
resource = 0f;
|
||
|
return amount;
|
||
|
}
|
||
|
|
||
|
return 0f;
|
||
|
}
|
||
|
|
||
|
public void Disassemble()
|
||
|
{
|
||
|
Destroy(gameObject);
|
||
|
}
|
||
|
|
||
|
public bool IsMaker()
|
||
|
{
|
||
|
return isMaker;
|
||
|
}
|
||
|
|
||
|
public string GetResource()
|
||
|
{
|
||
|
return resourceName;
|
||
|
}
|
||
|
}
|