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/DoorInteractable.cs
2023-06-01 08:03:48 -07:00

96 lines
2.9 KiB
C#

using UnityEngine;
public class DoorInteractable : HeavyItemReceiver
{
[SerializeField] private Transform powerCoreCenter;
[SerializeField] private float minAttractDist = 5;
[SerializeField] private string nameSearched = "Power Core";
[SerializeField] private Animator[] anims;
private HeavyInteractableItem insertedCore;
private Vector3 priorLocalPos;
private Vector3 priorLocalRot;
private Vector3 priorScale;
public bool Powered => insertedCore != null;
// Start is called before the first frame update
private void Start()
{
}
// Update is called once per frame
private void Update()
{
foreach (var anim in anims) anim.SetBool("IsPowered", Powered);
}
public override bool Interact()
{
//print("INTERACTED!");
if (insertedCore == null)
{
var worldHeavyItems = FindObjectsOfType<HeavyInteractableItem>();
//print("Found:" + worldHeavyItems.Length);
for (var i = 0; i < worldHeavyItems.Length; i++)
{
var item = worldHeavyItems[i];
if (!item.ItemName.Contains(nameSearched)) continue;
var dist = Vector3.Distance(item.transform.position, powerCoreCenter.transform.position);
//print("DIST:" + dist);
if (dist <= minAttractDist)
{
var _i = PlayerInteractionHandler.instance.Inventory;
Interact(ref _i, ref item);
return true;
}
}
}
return false;
}
public override bool Interact(ref Inventory inventory, ref HeavyInteractableItem heavyInvent)
{
//print("INTERACTED 2");
// print(heavyInvent);
if (heavyInvent != null && heavyInvent.ItemName.Contains(nameSearched))
{
//print("DOOR OPEN!");
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
heavyInvent.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
priorLocalPos = heavyInvent.transform.localPosition;
priorLocalRot = heavyInvent.transform.localEulerAngles;
priorScale = heavyInvent.transform.localScale;
heavyInvent.transform.parent = powerCoreCenter;
heavyInvent.gameObject.transform.localPosition = Vector3.zero;
heavyInvent.gameObject.transform.localEulerAngles = Vector3.zero;
heavyInvent.transform.parent = null;
heavyInvent.gameObject.transform.localScale = priorScale;
insertedCore = heavyInvent;
return true;
}
if (insertedCore != null && heavyInvent == null)
{
heavyInvent = insertedCore;
insertedCore = null;
//get ref of player perhaps
return true;
}
return false;
}
}