i3e-asg2/Assets/Scripts/Petrol.cs
2024-07-03 20:03:50 +08:00

75 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Petrol : Interactable
{
public float collectionTime;
public bool collecting;
public bool looking;
public int damagePerSecond;
float currentTime;
GameObject player;
bool damaged;
float time;
public override void Interact()
{
collecting = true;
}
void Update()
{ if(player==null)
{
player=PlayerDontDie.instance.transform.GetChild(2).gameObject;
}
if (collecting == true && looking == true)
{
Debug.Log("collecting");
currentTime += Time.deltaTime;
if (currentTime >= collectionTime)
{
GameManager.instance.petrolCollected = true;
Debug.Log("CollectedPetrol");
collecting=false;
}
}
if (
player.GetComponent<CharacterControl>().hitInfo.transform.name != null
&& player.GetComponent<CharacterControl>().hitInfo.transform.name == "petrol"
)
{
looking = true;
}
else
{
looking = 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;
}
}
}
}
}