105 lines
3.2 KiB
C#
105 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class DoorInteractable : HeavyItemReceiver
|
|
{
|
|
[SerializeField]
|
|
private Transform powerCoreCenter;
|
|
private HeavyInteractableItem insertedCore;
|
|
private Vector3 priorLocalPos;
|
|
private Vector3 priorLocalRot;
|
|
private Vector3 priorScale;
|
|
|
|
[SerializeField]
|
|
private float minAttractDist = 5;
|
|
[SerializeField]
|
|
private string nameSearched = "Power Core";
|
|
|
|
public bool Powered { get { return this.insertedCore!= null; } }
|
|
[SerializeField]
|
|
private Animator anim;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
anim.SetBool("IsPowered", Powered);
|
|
}
|
|
public override bool Interact()
|
|
{
|
|
//print("INTERACTED!");
|
|
if(this.insertedCore== null)
|
|
{
|
|
HeavyInteractableItem[] worldHeavyItems = GameObject.FindObjectsOfType<HeavyInteractableItem>();
|
|
//print("Found:" + worldHeavyItems.Length);
|
|
for(int i = 0; i < worldHeavyItems.Length; i++)
|
|
{
|
|
HeavyInteractableItem item = worldHeavyItems[i];
|
|
|
|
if (!item.ItemName.Contains(nameSearched))
|
|
{
|
|
continue;
|
|
}
|
|
float dist = Vector3.Distance(item.transform.position, powerCoreCenter.transform.position);
|
|
//print("DIST:" + dist);
|
|
if (dist <= minAttractDist)
|
|
{
|
|
Inventory _i = PlayerInteractionHandler.instance.Inventory;
|
|
this.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;
|
|
}
|
|
else if(insertedCore!=null&&heavyInvent==null)
|
|
{
|
|
heavyInvent = insertedCore;
|
|
print("Picked up:" + heavyInvent.name);
|
|
insertedCore = null;
|
|
//get ref of player perhaps
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|