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

96 lines
2.9 KiB
C#
Raw Permalink Normal View History

2023-03-21 19:49:47 +01:00
using UnityEngine;
2023-04-04 05:00:18 +02:00
public class DoorInteractable : HeavyItemReceiver
2023-03-21 19:49:47 +01:00
{
2023-06-01 17:03:48 +02:00
[SerializeField] private Transform powerCoreCenter;
[SerializeField] private float minAttractDist = 5;
[SerializeField] private string nameSearched = "Power Core";
[SerializeField] private Animator[] anims;
2023-03-21 19:49:47 +01:00
private HeavyInteractableItem insertedCore;
private Vector3 priorLocalPos;
private Vector3 priorLocalRot;
private Vector3 priorScale;
2023-06-01 17:03:48 +02:00
public bool Powered => insertedCore != null;
2023-03-21 19:49:47 +01:00
// Start is called before the first frame update
2023-06-01 17:03:48 +02:00
private void Start()
2023-03-21 19:49:47 +01:00
{
}
// Update is called once per frame
2023-06-01 17:03:48 +02:00
private void Update()
2023-03-21 19:49:47 +01:00
{
2023-06-01 17:03:48 +02:00
foreach (var anim in anims) anim.SetBool("IsPowered", Powered);
2023-03-21 19:49:47 +01:00
}
2023-06-01 17:03:48 +02:00
2023-03-21 19:49:47 +01:00
public override bool Interact()
{
//print("INTERACTED!");
2023-06-01 17:03:48 +02:00
if (insertedCore == null)
2023-03-21 19:49:47 +01:00
{
2023-06-01 17:03:48 +02:00
var worldHeavyItems = FindObjectsOfType<HeavyInteractableItem>();
2023-03-21 19:49:47 +01:00
//print("Found:" + worldHeavyItems.Length);
2023-06-01 17:03:48 +02:00
for (var i = 0; i < worldHeavyItems.Length; i++)
2023-03-21 19:49:47 +01:00
{
2023-06-01 17:03:48 +02:00
var item = worldHeavyItems[i];
if (!item.ItemName.Contains(nameSearched)) continue;
var dist = Vector3.Distance(item.transform.position, powerCoreCenter.transform.position);
2023-03-21 19:49:47 +01:00
//print("DIST:" + dist);
if (dist <= minAttractDist)
{
2023-06-01 17:03:48 +02:00
var _i = PlayerInteractionHandler.instance.Inventory;
Interact(ref _i, ref item);
2023-03-21 19:49:47 +01:00
return true;
}
}
}
2023-06-01 17:03:48 +02:00
2023-03-21 19:49:47 +01:00
return false;
}
2023-06-01 17:03:48 +02:00
public override bool Interact(ref Inventory inventory, ref HeavyInteractableItem heavyInvent)
2023-03-21 19:49:47 +01:00
{
//print("INTERACTED 2");
2023-06-01 17:03:48 +02:00
// print(heavyInvent);
if (heavyInvent != null && heavyInvent.ItemName.Contains(nameSearched))
2023-03-21 19:49:47 +01:00
{
//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;
2023-06-01 17:03:48 +02:00
2023-03-21 19:49:47 +01:00
heavyInvent.gameObject.transform.localPosition = Vector3.zero;
heavyInvent.gameObject.transform.localEulerAngles = Vector3.zero;
heavyInvent.transform.parent = null;
heavyInvent.gameObject.transform.localScale = priorScale;
2023-06-01 17:03:48 +02:00
2023-03-21 19:49:47 +01:00
insertedCore = heavyInvent;
2023-06-01 17:03:48 +02:00
2023-03-21 19:49:47 +01:00
return true;
}
2023-06-01 17:03:48 +02:00
if (insertedCore != null && heavyInvent == null)
2023-03-21 19:49:47 +01:00
{
heavyInvent = insertedCore;
2023-06-01 17:03:48 +02:00
2023-04-04 05:00:18 +02:00
insertedCore = null;
2023-03-21 19:49:47 +01:00
//get ref of player perhaps
return true;
}
2023-06-01 17:03:48 +02:00
return false;
2023-03-21 19:49:47 +01:00
}
2023-06-01 17:03:48 +02:00
}