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/Player/PlayerInteractionHandler.cs

159 lines
5.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class PlayerInteractionHandler : MonoBehaviour
{
private List<InteractableItem> itemsInRange= new List<InteractableItem>();
private List<HeavyInteractableItem> heavyItemsInRange = new List<HeavyInteractableItem>();
private Inventory invent;
public Inventory Inventory { get { return invent; } }
public static PlayerInteractionHandler instance;
[SerializeField]
private Light flashlight;
[SerializeField]
private GameObject flashlight3D;
[SerializeField]
private int materialIndex = 1;
private Material selMaterial;
private Color initColor;
private HeavyInteractableItem heavyInvent;
public bool IsCarrying { get { return heavyInvent !=null; } }
[SerializeField]
private Transform carryingPos;
// Start is called before the first frame update
void Start()
{
instance = this;
invent = this.transform.parent.GetComponent<Inventory>();
initColor = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].GetColor("_BaseColor");
selMaterial = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex];
}
// Update is called once per frame
void Update()
{
//
if(Input.GetButtonDown("Fire1"))
{
if (!IsCarrying)
{
int t_index = 0;
bool pickupFound = false;
if (itemsInRange.Count > 0)
{
while (t_index<itemsInRange.Count&&!itemsInRange[t_index].CanPickup)
{
t_index++;
}
if (t_index != itemsInRange.Count)
{
pickupFound = true;
invent.AddItem(itemsInRange[t_index]);
itemsInRange[0].transform.gameObject.SetActive(false);
itemsInRange.Remove(itemsInRange[t_index]);
}
}
else if (heavyItemsInRange.Count > 0)
{
pickupFound = true;
heavyInvent = heavyItemsInRange[0];
heavyInvent.transform.parent = carryingPos;
heavyInvent.transform.localPosition = Vector3.zero;
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
heavyItemsInRange.Remove(heavyItemsInRange[0]);
heavyInvent.Disable();
heavyInvent.DisableAll();
}
if (!pickupFound)
{
foreach(InteractableItem item in itemsInRange)
{
if (!item.CanPickup)
{
if(!item.Interact(ref invent, ref heavyInvent))
{
item.Interact();
}
}
}
}
}
else
{
heavyInvent.transform.parent = null;
heavyInvent.GetComponent<Rigidbody>().isKinematic = false;
heavyInvent.EnableAll();
heavyInvent = null;
}
}
if (Input.GetButtonDown("Fire2"))
{
flashlight.gameObject.SetActive(!flashlight.gameObject.activeSelf);
if (flashlight.gameObject.activeSelf)
{
flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].SetColor("_BaseColor", initColor);
selMaterial.SetColor("_EmissionColor", new Color(255,255, 255, 255));
flashlight3D.gameObject.SetActive(true);
}
else
{
flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].SetColor("_BaseColor", new Color(0, 0, 0));
selMaterial.SetColor("_EmissionColor", new Color(0, 0, 0, 0));
flashlight3D.gameObject.SetActive(false);
}
}
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.GetComponent<InteractableItem>() != null)
{
InteractableItem item = other.gameObject.GetComponent<InteractableItem>();
if(item is InteractableItem && item is not HeavyInteractableItem)
{
((InteractableItem)item).Enable();
itemsInRange.Add(item);
}else if(item is HeavyInteractableItem)
{
((HeavyInteractableItem)item).Enable();
heavyItemsInRange.Add((HeavyInteractableItem)item);
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.GetComponent<InteractableItem>() != null)
{
InteractableItem item = other.gameObject.GetComponent<InteractableItem>();
if (item is InteractableItem && item is not HeavyInteractableItem)
{
((InteractableItem)item).Disable();
itemsInRange.Remove(item);
}
else if (item is HeavyInteractableItem)
{
((HeavyInteractableItem)item).Disable();
//itemsInRange.Remove((HeavyInteractableItem)(item));
heavyItemsInRange.Remove((HeavyInteractableItem)item);
}
}
}
}