This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Item/InteractableItem.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2023-03-14 00:59:59 +01:00
using TMPro;
2023-06-01 17:03:48 +02:00
using UnityEngine;
2023-03-14 00:59:59 +01:00
using UnityEngine.UI;
[RequireComponent(typeof(Collider))]
[RequireComponent(typeof(Rigidbody))]
2023-03-21 19:49:47 +01:00
public abstract class InteractableItem : CarryableItem
2023-03-14 00:59:59 +01:00
{
2023-06-01 17:03:48 +02:00
[SerializeField] private Canvas interactionCanvas;
[SerializeField] protected bool canPickup;
2023-03-14 00:59:59 +01:00
private Image[] interaction_images;
2023-06-01 17:03:48 +02:00
private TMP_Text[] interaction_texts;
protected bool isEnabled;
protected float target_alpha;
2023-03-14 00:59:59 +01:00
2023-06-01 17:03:48 +02:00
public bool CanPickup => canPickup;
public bool IsEnabled => isEnabled;
private void Awake()
{
BaseAwake();
}
private void FixedUpdate()
{
BaseFixedUpdate();
}
2023-03-14 00:59:59 +01:00
public void Enable()
{
//print("Enabled!");
2023-06-01 17:03:48 +02:00
interactionCanvas.transform.LookAt(GameObject.FindGameObjectWithTag("MainCamera").transform.position);
2023-03-14 00:59:59 +01:00
interactionCanvas.transform.Rotate(0, 180, 0);
target_alpha = 1;
isEnabled = true;
}
2023-06-01 17:03:48 +02:00
2023-03-14 00:59:59 +01:00
public void Disable()
{
//print("Disabled!");
2023-03-14 00:59:59 +01:00
target_alpha = 0;
isEnabled = true;
}
2023-06-01 17:03:48 +02:00
2023-03-21 19:49:47 +01:00
public abstract bool Interact();
2023-06-01 17:03:48 +02:00
public abstract bool Interact(ref Inventory inventory, ref HeavyInteractableItem heavyInvent);
2023-03-14 00:59:59 +01:00
protected void BaseAwake()
{
interaction_texts = interactionCanvas.GetComponentsInChildren<TMP_Text>();
interaction_images = interactionCanvas.GetComponentsInChildren<Image>();
2023-06-01 17:03:48 +02:00
foreach (var text in interaction_texts) text.color = new Color(text.color.r, text.color.g, text.color.b, 0);
foreach (var image in interaction_images)
2023-03-14 00:59:59 +01:00
image.color = new Color(image.color.r, image.color.g, image.color.b, 0);
}
2023-06-01 17:03:48 +02:00
protected void BaseFixedUpdate()
2023-03-14 00:59:59 +01:00
{
2023-06-01 17:03:48 +02:00
foreach (var text in interaction_texts)
text.color = Color.Lerp(new Color(text.color.r, text.color.g, text.color.b, text.color.a),
new Color(text.color.r, text.color.g, text.color.b, target_alpha), 10 * Time.deltaTime);
foreach (var image in interaction_images)
image.color = Color.Lerp(new Color(image.color.r, image.color.g, image.color.b, image.color.a),
new Color(image.color.r, image.color.g, image.color.b, target_alpha), 10 * Time.deltaTime);
2023-03-14 00:59:59 +01:00
}
2023-06-01 17:03:48 +02:00
}