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

98 lines
3.2 KiB
C#
Raw Permalink Normal View History

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