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/Legacy/Item/InteractableItem.cs
MarcoHampel f8590432ba Assemblies Made and Refactored Code Folders
Created assemblies for the new design code. Relocated legacy scripts into a legacy folder and made a "new_design" folder for new design.
2023-09-11 19:39:27 -04:00

74 lines
2.4 KiB
C#

using FishNet.Object;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Item
{
[RequireComponent(typeof(Collider))]
[RequireComponent(typeof(Rigidbody))]
public abstract class InteractableItem : CarryableItem
{
[SerializeField] private Canvas interactionCanvas;
[SerializeField] protected bool canPickup;
private Image[] interaction_images;
private TMP_Text[] interaction_texts;
protected bool isEnabled;
protected float target_alpha;
public bool CanPickup => canPickup;
public bool IsEnabled => isEnabled;
private void Awake()
{
BaseAwake();
}
private void FixedUpdate()
{
BaseFixedUpdate();
}
public void Enable()
{
//print("Enabled!");
interactionCanvas.transform.LookAt(GameObject.FindGameObjectWithTag("MainCamera").transform.position);
interactionCanvas.transform.Rotate(0, 180, 0);
target_alpha = 1;
isEnabled = true;
}
public void Disable()
{
//print("Disabled!");
target_alpha = 0;
isEnabled = true;
}
public abstract bool Interact();
public abstract bool Interact(ref Inventory inventory, ref HeavyInteractableItem heavyInvent);
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);
}
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);
}
}
}