2023-03-14 00:59:59 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-03-21 19:49:47 +01:00
|
|
|
using System.Linq;
|
2023-03-14 00:59:59 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2023-04-03 17:02:11 +02:00
|
|
|
using System.Xml;
|
2023-03-14 00:59:59 +01:00
|
|
|
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;
|
2023-03-21 19:49:47 +01:00
|
|
|
public Inventory Inventory { get { return invent; } }
|
|
|
|
public static PlayerInteractionHandler instance;
|
2023-03-14 00:59:59 +01:00
|
|
|
[SerializeField]
|
|
|
|
private Light flashlight;
|
|
|
|
[SerializeField]
|
|
|
|
private GameObject flashlight3D;
|
2023-04-03 17:02:11 +02:00
|
|
|
private bool flashlightEnabled = true;
|
2023-03-14 00:59:59 +01:00
|
|
|
[SerializeField]
|
|
|
|
private int materialIndex = 1;
|
|
|
|
private Material selMaterial;
|
|
|
|
private Color initColor;
|
|
|
|
|
|
|
|
private HeavyInteractableItem heavyInvent;
|
|
|
|
public bool IsCarrying { get { return heavyInvent !=null; } }
|
2023-04-03 17:02:11 +02:00
|
|
|
public bool GunEnabled { get { return this.gunEnabled; } }
|
|
|
|
private bool gunEnabled = false;
|
2023-03-14 00:59:59 +01:00
|
|
|
[SerializeField]
|
|
|
|
private Transform carryingPos;
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
private ItemSelector itemSelector;
|
|
|
|
[SerializeField]
|
|
|
|
private bool useItemSelector = true;
|
|
|
|
[SerializeField]
|
|
|
|
private PistolComponent pistol;
|
2023-03-21 19:49:47 +01:00
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2023-03-21 19:49:47 +01:00
|
|
|
instance = this;
|
2023-03-14 00:59:59 +01:00
|
|
|
invent = this.transform.parent.GetComponent<Inventory>();
|
|
|
|
initColor = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].GetColor("_BaseColor");
|
|
|
|
selMaterial = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex];
|
2023-04-03 17:02:11 +02:00
|
|
|
itemSelector = ItemSelector.instance;
|
|
|
|
pistol.gameObject.SetActive(this.gunEnabled);
|
|
|
|
flashlightEnabled = this.flashlight.gameObject.activeSelf;
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
|
2023-04-04 05:00:18 +02:00
|
|
|
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;
|
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2023-04-03 17:02:11 +02:00
|
|
|
|
2023-04-04 02:02:28 +02:00
|
|
|
|
2023-04-04 05:22:37 +02:00
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
if(Input.GetButtonDown("Fire1"))
|
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
if (this.GunEnabled)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
this.pistol.Fire();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!IsCarrying)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
int t_index = 0;
|
|
|
|
bool pickupFound = false;
|
|
|
|
if (itemsInRange.Count > 0)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-21 19:49:47 +01:00
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
else if (heavyItemsInRange.Count > 0)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
|
|
|
pickupFound = true;
|
2023-04-04 05:00:18 +02:00
|
|
|
|
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
heavyInvent = heavyItemsInRange[0];
|
2023-03-21 19:49:47 +01:00
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
heavyInvent.transform.parent = carryingPos;
|
|
|
|
heavyInvent.transform.localPosition = Vector3.zero;
|
|
|
|
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
|
|
|
|
heavyItemsInRange.Remove(heavyItemsInRange[0]);
|
|
|
|
heavyInvent.Disable();
|
|
|
|
heavyInvent.DisableAll();
|
2023-04-04 05:00:18 +02:00
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
|
|
|
if (!pickupFound)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
foreach (InteractableItem item in itemsInRange)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
if (!item.CanPickup)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-04-04 01:23:20 +02:00
|
|
|
if (!item.Interact(ref invent, ref heavyInvent))
|
|
|
|
{
|
|
|
|
item.Interact();
|
2023-04-04 05:00:18 +02:00
|
|
|
}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<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
|
|
|
|
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
|
|
|
|
heavyInvent.Disable();
|
|
|
|
heavyInvent.DisableAll();
|
|
|
|
|
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
2023-03-21 19:49:47 +01:00
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
2023-03-21 19:49:47 +01:00
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
2023-03-21 19:49:47 +01:00
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
else
|
|
|
|
{
|
2023-04-04 05:00:18 +02:00
|
|
|
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<Rigidbody>().isKinematic = false;
|
|
|
|
heavyInvent.EnableAll();
|
|
|
|
heavyInvent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
if (Input.GetButtonDown("Fire2"))
|
|
|
|
{
|
2023-04-04 05:00:18 +02:00
|
|
|
//print(this.GunEnabled);
|
2023-04-03 17:02:11 +02:00
|
|
|
if (!this.GunEnabled)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
if (!flashlight.gameObject.activeSelf)
|
|
|
|
{
|
|
|
|
EnableFlashlight();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DisableFlashlight();
|
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-03 17:02:11 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-03 17:02:11 +02:00
|
|
|
if (this.GunEnabled)
|
|
|
|
{
|
|
|
|
this.DisableFlashlight();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void EnableFlashlight()
|
|
|
|
{
|
2023-04-04 05:00:18 +02:00
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
flashlight.gameObject.SetActive(true);
|
|
|
|
flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].SetColor("_BaseColor", initColor);
|
|
|
|
selMaterial.SetColor("_EmissionColor", new Color(255, 255, 255, 255));
|
|
|
|
flashlight3D.gameObject.SetActive(true);
|
|
|
|
this.flashlightEnabled = true;
|
|
|
|
}
|
|
|
|
public void DisableFlashlight()
|
|
|
|
{
|
2023-04-04 05:00:18 +02:00
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
flashlight.gameObject.SetActive(false);
|
|
|
|
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);
|
|
|
|
this.flashlightEnabled = false;
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
|
2023-04-04 01:39:16 +02:00
|
|
|
|
2023-04-04 05:00:18 +02:00
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
{
|
2023-04-04 02:02:28 +02:00
|
|
|
if(other.gameObject.GetComponent<InteractableItem>() != null)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-04 02:02:28 +02:00
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
{
|
2023-04-04 02:02:28 +02:00
|
|
|
if (other.gameObject.GetComponent<InteractableItem>() != null)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2023-04-04 02:02:28 +02:00
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
}
|