using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Xml; using Unity.VisualScripting; using UnityEngine; [RequireComponent(typeof(Collider))] public class PlayerInteractionHandler : MonoBehaviour { private List itemsInRange= new List(); private List heavyItemsInRange = new List(); private Inventory invent; private TempInventory tempInvent; public Inventory Inventory { get { return invent; } } public static PlayerInteractionHandler instance; [SerializeField] private Light flashlight; [SerializeField] private GameObject flashlight3D; private bool flashlightEnabled = true; [SerializeField] private int materialIndex = 1; private Material selMaterial; private Color initColor; private HeavyInteractableItem heavyInvent; public bool IsCarrying { get { return heavyInvent !=null; } } public bool GunEnabled { get { return this.gunEnabled; } } private bool gunEnabled = false; [SerializeField] private Transform carryingPos; public Transform CarryingPos { get { return this.carryingPos; } } private ItemSelector itemSelector; [SerializeField] private bool useItemSelector = true; [SerializeField] private PistolComponent pistol; [SerializeField] private NoiseVisibilitySettingsManager noiseManager; // Start is called before the first frame update void Start() { instance = this; invent = this.transform.parent.GetComponent(); //TEMP FIELD tempInvent = this.transform.parent.GetComponent(); initColor = flashlight3D.GetComponent().materials[materialIndex].GetColor("_BaseColor"); selMaterial = flashlight3D.GetComponent().materials[materialIndex]; itemSelector = ItemSelector.instance; pistol.gameObject.SetActive(this.gunEnabled); flashlightEnabled = this.flashlight.gameObject.activeSelf; } private bool receiverInRange(out int index) { int i = 0; foreach(InteractableItem item in this.itemsInRange) { if(item is HeavyItemReceiver) { index = i; return true; } i++; } index = -1; return false; } // Update is called once per frame void Update() { if(Input.GetButtonDown("Fire1")) { if (this.GunEnabled) { if (tempInvent.GetQuantityOf(this.pistol.projectileName) > 0) { this.pistol.Fire(); this.noiseManager.ShotFired(); tempInvent.Remove(this.pistol.projectileName); } } else { 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().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 if(item is HeavyItemReceiver) { if(heavyInvent != null) { heavyInvent.transform.parent = carryingPos; heavyInvent.transform.localPosition = Vector3.zero; heavyInvent.transform.localEulerAngles = Vector3.zero; heavyItemsInRange.Remove(heavyInvent); heavyInvent.GetComponent().constraints = RigidbodyConstraints.FreezeRotation; heavyInvent.GetComponent().isKinematic = true; heavyInvent.Disable(); heavyInvent.DisableAll(); } } } } } } else { int refIndex; if (itemsInRange.Count > 0&&receiverInRange(out refIndex)) { ((HeavyItemReceiver)itemsInRange[refIndex]).Interact(ref this.invent, ref heavyInvent); heavyInvent = null; } else { heavyInvent.transform.parent = null; heavyInvent.GetComponent().isKinematic = false; heavyInvent.EnableAll(); heavyInvent = null; } } } } if (Input.GetButtonDown("Fire2")) { //print(this.GunEnabled); if (!this.GunEnabled) { if (!flashlight.gameObject.activeSelf) { EnableFlashlight(); } else { DisableFlashlight(); } } else { this.pistol.LightToggle(); } } if (Input.GetButtonDown("Fire3")) { if (!this.IsCarrying) { this.gunEnabled = !this.gunEnabled; if(!this.gunEnabled) pistol.Disable(); pistol.gameObject.SetActive(this.gunEnabled); if (this.gunEnabled) pistol.Enable(); } } if (this.GunEnabled) { this.DisableFlashlight(); float aimAxis = Input.GetAxis("Aim"); } else { } } public void EnableFlashlight() { flashlight.gameObject.SetActive(true); flashlight3D.GetComponent().materials[materialIndex].SetColor("_BaseColor", initColor); selMaterial.SetColor("_EmissionColor", new Color(255, 255, 255, 255)); flashlight3D.gameObject.SetActive(true); this.flashlightEnabled = true; } public void DisableFlashlight() { flashlight.gameObject.SetActive(false); flashlight3D.GetComponent().materials[materialIndex].SetColor("_BaseColor", new Color(0, 0, 0)); selMaterial.SetColor("_EmissionColor", new Color(0, 0, 0, 0)); flashlight3D.gameObject.SetActive(false); this.flashlightEnabled = false; } private void OnTriggerEnter(Collider other) { if(other.gameObject.GetComponent() != null) { InteractableItem item = other.gameObject.GetComponent(); 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() != null) { InteractableItem item = other.gameObject.GetComponent(); 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); } } } }