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/DoorInteractable.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

98 lines
3.2 KiB
C#

using UnityEngine;
namespace Item
{
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 = Player.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;
}
}
}