Heroes_of_Hiis/Assets/Project Files/Scripts/JoonasP/EssenceNodeController.cs

92 lines
2.1 KiB
C#
Raw Normal View History

2022-03-17 15:24:46 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class EssenceNodeController : MonoBehaviour
{
2022-03-21 15:52:47 +00:00
[SerializeField]
private AudioSource chime;
2022-04-04 15:57:40 +00:00
private Inventory playerInventory;
2022-03-25 16:03:38 +00:00
public bool isTouched;
private bool followPlayer = false;
2022-04-11 15:27:18 +00:00
private Transform playerCamera;
[SerializeField]
private PlayerInfo playerInfo;
2022-03-21 15:52:47 +00:00
private float timer;
private Material nodeMaterial;
private Coroutine decayCo;
private void Awake()
{
nodeMaterial = GetComponent<Renderer>().material;
2022-04-11 15:27:18 +00:00
playerInfo = PlayerInfo.Instance;
2022-04-04 15:57:40 +00:00
playerInventory = GameObject.Find("Inventory").GetComponent<Inventory>();
}
2022-03-17 15:24:46 +00:00
public void Touched()
{
2022-03-25 16:03:38 +00:00
if (!isTouched)
{
GetComponent<Renderer>().material.color = Color.cyan;
chime.Play();
isTouched = true;
StopCoroutine(decayCo);
2022-03-25 16:03:38 +00:00
}
}
public void FollowPlayer()
{
followPlayer = true;
2022-04-11 15:27:18 +00:00
playerCamera = GameObject.FindGameObjectWithTag("MainCamera").transform;
StartCoroutine(Collect());
2022-03-25 16:03:38 +00:00
2022-03-21 15:52:47 +00:00
}
public void SetPitch(float value)
{
chime.pitch = value;
2022-03-17 15:24:46 +00:00
}
2022-03-25 16:03:38 +00:00
public void SetTimer(float seconds)
{
timer = seconds;
decayCo = StartCoroutine(Decay());
}
2022-03-25 16:03:38 +00:00
IEnumerator Collect()
2022-03-25 16:03:38 +00:00
{
2022-04-11 15:27:18 +00:00
playerInfo.AddEssenceBasic(1);
2022-03-25 16:03:38 +00:00
yield return new WaitForSeconds(2f);
//TODO: Update value in player inventory
playerInventory.AddItem(GetComponent<ItemData>());
2022-03-25 16:03:38 +00:00
Destroy(gameObject);
}
IEnumerator Decay()
{
yield return new WaitForSeconds(timer);
Destroy(gameObject);
}
2022-03-25 16:03:38 +00:00
private void Update()
{
if (!isTouched)
{
//A way to either linearly reduce the alpha value of color or fade the color to gray (must react to changing timer values)
}
2022-03-25 16:03:38 +00:00
if (followPlayer)
{
2022-04-11 15:27:18 +00:00
transform.position = Vector3.Lerp(transform.position, new Vector3(playerCamera.position.x, playerCamera.position.y - 0.5f, playerCamera.position.z), Time.deltaTime);
2022-03-25 16:03:38 +00:00
}
}
2022-03-17 15:24:46 +00:00
}