i3e-asg2/Assets/Scripts/Petrol.cs
2024-07-05 01:22:55 +08:00

94 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class Petrol : Interactable
{
public float collectionTime;
public bool collecting;
public bool looking;
public int damagePerSecond;
public Slider slider;
float currentTime;
GameObject player;
bool damaged;
float time;
public AudioSource source;
public override void Interact()
{
collecting = true;
source.Play();
}
void Start()
{
slider.value = 0f;
}
void Update()
{
if (player == null)
{
player = PlayerDontDie.instance.transform.GetChild(2).gameObject;
}
if (slider == null)
{
slider = GameObject.Find("PetrolSlider").GetComponent<Slider>();
}
if (
player.GetComponent<CharacterControl>().hitInfo.transform.name != null
&& player.GetComponent<CharacterControl>().hitInfo.transform.name == "petrol"
&& collecting
)
{
looking = true;
}
else
{
looking = false;
}
if (collecting == true && looking == true)
{
Debug.Log("collecting");
currentTime += Time.deltaTime;
slider.value = currentTime;
if (currentTime >= collectionTime)
{
GameManager.instance.petrolCollected = true;
GameObject.Find("PetrolObjective").GetComponent<TextMeshProUGUI>().color =Color.green;
source.Stop();
collecting = false;
}
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (!damaged)
{
GameManager.instance.damagePlayer(damagePerSecond);
damaged = true;
}
else
{
time += Time.deltaTime;
if (time >= 1)
{
time = 0;
damaged = false;
}
}
}
}
}