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

74 lines
2.4 KiB
C#
Raw Normal View History

2023-06-15 19:03:27 +02:00
using FishNet.Object;
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;
2023-06-02 06:30:58 +02:00
namespace Item
2023-03-14 00:59:59 +01:00
{
2023-06-02 06:30:58 +02:00
[RequireComponent(typeof(Collider))]
[RequireComponent(typeof(Rigidbody))]
public abstract class InteractableItem : CarryableItem
{
[SerializeField] private Canvas interactionCanvas;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] protected bool canPickup;
2023-03-14 00:59:59 +01:00
2023-06-02 06:30:58 +02:00
private Image[] interaction_images;
private TMP_Text[] interaction_texts;
protected bool isEnabled;
protected float target_alpha;
2023-03-14 00:59:59 +01:00
2023-06-02 06:30:58 +02:00
public bool CanPickup => canPickup;
public bool IsEnabled => isEnabled;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private void Awake()
{
BaseAwake();
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private void FixedUpdate()
{
BaseFixedUpdate();
}
2023-03-14 00:59:59 +01:00
2023-06-02 06:30:58 +02:00
public void Enable()
{
//print("Enabled!");
interactionCanvas.transform.LookAt(GameObject.FindGameObjectWithTag("MainCamera").transform.position);
interactionCanvas.transform.Rotate(0, 180, 0);
target_alpha = 1;
isEnabled = true;
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
public void Disable()
{
//print("Disabled!");
target_alpha = 0;
isEnabled = true;
}
2023-06-01 17:03:48 +02:00
2023-06-15 19:03:27 +02:00
2023-06-02 06:30:58 +02:00
public abstract bool Interact();
2023-06-15 19:06:08 +02:00
2023-06-02 06:30:58 +02:00
public abstract bool Interact(ref Inventory inventory, ref HeavyInteractableItem heavyInvent);
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
protected void BaseAwake()
{
interaction_texts = interactionCanvas.GetComponentsInChildren<TMP_Text>();
interaction_images = interactionCanvas.GetComponentsInChildren<Image>();
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)
image.color = new Color(image.color.r, image.color.g, image.color.b, 0);
}
2023-03-14 00:59:59 +01:00
2023-06-02 06:30:58 +02:00
protected void BaseFixedUpdate()
{
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
}