2023-03-14 00:59:59 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
namespace Player
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
[RequireComponent(typeof(Collider))]
|
|
|
|
public class PlayerInteractionHandler : MonoBehaviour
|
|
|
|
{
|
2023-03-21 19:49:47 +01:00
|
|
|
public static PlayerInteractionHandler instance;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
[SerializeField] private Light flashlight;
|
|
|
|
|
|
|
|
[SerializeField] private GameObject flashlight3D;
|
|
|
|
|
|
|
|
[SerializeField] private int materialIndex = 1;
|
|
|
|
|
|
|
|
[SerializeField] private Transform carryingPos;
|
|
|
|
|
|
|
|
[SerializeField] private bool useItemSelector = true;
|
|
|
|
|
|
|
|
[SerializeField] private PistolComponent pistol;
|
|
|
|
|
|
|
|
[SerializeField] private NoiseVisibilitySettingsManager noiseManager;
|
|
|
|
|
|
|
|
[SerializeField] private CameraShift shift;
|
|
|
|
|
|
|
|
public bool isDead;
|
|
|
|
|
|
|
|
//Check if button down
|
|
|
|
private readonly AxisIsDown fireDown = new("Fire1");
|
2023-04-03 17:02:11 +02:00
|
|
|
private bool flashlightEnabled = true;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
|
|
|
private HeavyInteractableItem heavyInvent;
|
2023-06-01 17:03:48 +02:00
|
|
|
private readonly List<HeavyInteractableItem> heavyItemsInRange = new();
|
|
|
|
private Color initColor;
|
|
|
|
private Inventory invent;
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
private ItemSelector itemSelector;
|
2023-06-01 17:03:48 +02:00
|
|
|
private readonly List<InteractableItem> itemsInRange = new();
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
private Game.InGameManager manager;
|
2023-06-01 17:03:48 +02:00
|
|
|
private Material selMaterial;
|
|
|
|
private TempInventory tempInvent;
|
|
|
|
public Inventory Inventory => invent;
|
|
|
|
public bool IsCarrying => heavyInvent != null;
|
|
|
|
public bool GunEnabled { get; private set; }
|
|
|
|
|
|
|
|
public Transform CarryingPos => carryingPos;
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
// Start is called before the first frame update
|
2023-06-01 17:03:48 +02:00
|
|
|
private void Start()
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-03-21 19:49:47 +01:00
|
|
|
instance = this;
|
2023-06-01 17:03:48 +02:00
|
|
|
invent = transform.parent.GetComponent<Inventory>();
|
2023-04-18 04:29:21 +02:00
|
|
|
//TEMP FIELD
|
2023-06-01 17:03:48 +02:00
|
|
|
tempInvent = transform.parent.GetComponent<TempInventory>();
|
2023-04-18 04:29:21 +02:00
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
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;
|
2023-06-01 17:03:48 +02:00
|
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
|
|
flashlightEnabled = flashlight.gameObject.activeSelf;
|
2023-06-01 20:25:46 +02:00
|
|
|
manager = FindObjectOfType<Game.InGameManager>();
|
2023-05-11 05:59:58 +02:00
|
|
|
}
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
// Update is called once per frame
|
2023-06-01 17:03:48 +02:00
|
|
|
private void Update()
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-05-11 05:59:58 +02:00
|
|
|
fireDown.Check();
|
2023-06-01 17:03:48 +02:00
|
|
|
if (isDead)
|
2023-05-11 05:59:58 +02:00
|
|
|
{
|
|
|
|
DropHeavy();
|
|
|
|
return;
|
|
|
|
}
|
2023-04-03 17:02:11 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
if (manager.IsPaused || isDead) return;
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("Fire1") || fireDown.IsDown())
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
if (GunEnabled)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
if (tempInvent.GetQuantityOf(pistol.projectileName) > 0)
|
2023-04-18 04:29:21 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
pistol.Fire();
|
|
|
|
noiseManager.ShotFired();
|
|
|
|
tempInvent.Remove(pistol.projectileName);
|
2023-04-18 04:29:21 +02:00
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!IsCarrying)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var t_index = 0;
|
|
|
|
var pickupFound = false;
|
2023-04-04 01:23:20 +02:00
|
|
|
if (itemsInRange.Count > 0)
|
2023-03-21 19:49:47 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
while (t_index < itemsInRange.Count && !itemsInRange[t_index].CanPickup) t_index++;
|
2023-04-04 01:23:20 +02:00
|
|
|
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-06-01 17:03:48 +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-06-01 17:03:48 +02:00
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
if (!pickupFound)
|
2023-06-01 17:03:48 +02:00
|
|
|
foreach (var item in itemsInRange)
|
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-06-01 17:03:48 +02:00
|
|
|
else if (item is HeavyItemReceiver)
|
|
|
|
if (heavyInvent != null)
|
2023-04-04 05:00:18 +02:00
|
|
|
{
|
|
|
|
heavyInvent.transform.parent = carryingPos;
|
|
|
|
heavyInvent.transform.localPosition = Vector3.zero;
|
|
|
|
heavyInvent.transform.localEulerAngles = Vector3.zero;
|
|
|
|
heavyItemsInRange.Remove(heavyInvent);
|
2023-06-01 17:03:48 +02:00
|
|
|
heavyInvent.GetComponent<Rigidbody>().constraints =
|
|
|
|
RigidbodyConstraints.FreezeRotation;
|
2023-04-04 05:00:18 +02:00
|
|
|
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
|
|
|
else
|
|
|
|
{
|
2023-04-04 05:00:18 +02:00
|
|
|
int refIndex;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (itemsInRange.Count > 0 && receiverInRange(out refIndex))
|
|
|
|
{
|
|
|
|
((HeavyItemReceiver)itemsInRange[refIndex]).Interact(ref invent, ref heavyInvent);
|
2023-04-04 05:00:18 +02:00
|
|
|
heavyInvent = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
DropHeavy();
|
2023-04-04 05:00:18 +02:00
|
|
|
}
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +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-06-01 17:03:48 +02:00
|
|
|
if (!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-06-01 17:03:48 +02:00
|
|
|
pistol.LightToggle();
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
if (Input.GetButtonDown("Fire3"))
|
2023-06-01 17:03:48 +02:00
|
|
|
if (!IsCarrying)
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
GunEnabled = !GunEnabled;
|
|
|
|
if (!GunEnabled)
|
2023-04-03 17:02:11 +02:00
|
|
|
pistol.Disable();
|
2023-06-01 17:03:48 +02:00
|
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
|
|
if (GunEnabled)
|
2023-04-03 17:02:11 +02:00
|
|
|
pistol.Enable();
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
if (GunEnabled)
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
DisableFlashlight();
|
|
|
|
|
|
|
|
var aimAxis = Input.GetAxis("Aim");
|
2023-04-04 06:04:34 +02:00
|
|
|
|
2023-05-29 06:16:05 +02:00
|
|
|
if (aimAxis > 0.5f)
|
|
|
|
pistol.aimMode = PistolComponent.AimMode.CAMERA;
|
|
|
|
else
|
|
|
|
pistol.aimMode = PistolComponent.AimMode.MODIFIED;
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
shift.SetCenter(!GunEnabled);
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
if (other.gameObject.GetComponent<InteractableItem>() != null)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var item = other.gameObject.GetComponent<InteractableItem>();
|
|
|
|
if (item is InteractableItem && item is not HeavyInteractableItem)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
item.Enable();
|
2023-03-14 00:59:59 +01:00
|
|
|
itemsInRange.Add(item);
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|
|
|
|
else if (item is HeavyInteractableItem)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
|
|
|
((HeavyInteractableItem)item).Enable();
|
|
|
|
heavyItemsInRange.Add((HeavyInteractableItem)item);
|
|
|
|
}
|
2023-04-04 02:02:28 +02:00
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
2023-06-01 17:03:48 +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
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var item = other.gameObject.GetComponent<InteractableItem>();
|
2023-03-14 00:59:59 +01:00
|
|
|
if (item is InteractableItem && item is not HeavyInteractableItem)
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
item.Disable();
|
2023-03-14 00:59:59 +01:00
|
|
|
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
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
private bool receiverInRange(out int index)
|
|
|
|
{
|
|
|
|
var i = 0;
|
|
|
|
foreach (var item in itemsInRange)
|
|
|
|
{
|
|
|
|
if (item is HeavyItemReceiver)
|
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DropHeavy()
|
|
|
|
{
|
|
|
|
if (heavyInvent != null)
|
|
|
|
{
|
|
|
|
heavyInvent.transform.parent = null;
|
|
|
|
heavyInvent.GetComponent<Rigidbody>().isKinematic = false;
|
|
|
|
heavyInvent.EnableAll();
|
|
|
|
heavyInvent = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EnableFlashlight()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
flashlightEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DisableFlashlight()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
flashlightEnabled = false;
|
|
|
|
}
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
2023-05-11 05:59:58 +02:00
|
|
|
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
internal class AxisIsDown
|
2023-05-11 05:59:58 +02:00
|
|
|
{
|
|
|
|
private float axis;
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
private readonly string axisName;
|
|
|
|
|
|
|
|
private bool down;
|
|
|
|
private bool isDown;
|
|
|
|
private bool pIsDown;
|
|
|
|
|
|
|
|
public AxisIsDown(string axisName)
|
|
|
|
{
|
|
|
|
this.axisName = axisName;
|
|
|
|
}
|
2023-05-11 05:59:58 +02:00
|
|
|
|
|
|
|
public void Check()
|
|
|
|
{
|
|
|
|
axis = Input.GetAxis(axisName);
|
|
|
|
isDown = axis > 0.5f;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (isDown != pIsDown && isDown)
|
2023-05-11 05:59:58 +02:00
|
|
|
down = true;
|
|
|
|
else
|
|
|
|
down = false;
|
|
|
|
pIsDown = isDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsDown()
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
return down;
|
2023-05-11 05:59:58 +02:00
|
|
|
}
|
2023-06-01 20:25:46 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 05:59:58 +02:00
|
|
|
}
|