321 lines
10 KiB
C#
321 lines
10 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Player
|
|
{
|
|
|
|
[RequireComponent(typeof(Collider))]
|
|
public class PlayerInteractionHandler : MonoBehaviour
|
|
{
|
|
public static PlayerInteractionHandler instance;
|
|
|
|
[SerializeField] private Light flashlight;
|
|
|
|
[SerializeField] private GameObject flashlight3D;
|
|
|
|
[SerializeField] private int materialIndex = 1;
|
|
|
|
[SerializeField] private Transform carryingPos;
|
|
|
|
[SerializeField] private bool useItemSelector = true;
|
|
|
|
[SerializeField] private Item.PistolComponent pistol;
|
|
|
|
[SerializeField] private NoiseVisibilitySettingsManager noiseManager;
|
|
|
|
[SerializeField] private CameraShift shift;
|
|
|
|
public bool isDead;
|
|
|
|
//Check if button down
|
|
private readonly AxisIsDown fireDown = new("Fire1");
|
|
private bool flashlightEnabled = true;
|
|
|
|
private Item.HeavyInteractableItem heavyInvent;
|
|
private readonly List<Item.HeavyInteractableItem> heavyItemsInRange = new();
|
|
private Color initColor;
|
|
private Item.Inventory invent;
|
|
|
|
private Item.ItemSelector itemSelector;
|
|
private readonly List<Item.InteractableItem> itemsInRange = new();
|
|
[SerializeField]
|
|
private Scriptable.GameState manager;
|
|
private Material selMaterial;
|
|
private Item.TempInventory tempInvent;
|
|
public Item.Inventory Inventory => invent;
|
|
public bool IsCarrying => heavyInvent != null;
|
|
public bool GunEnabled { get; private set; }
|
|
|
|
public Transform CarryingPos => carryingPos;
|
|
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
instance = this;
|
|
invent = transform.parent.GetComponent<Item.Inventory>();
|
|
//TEMP FIELD
|
|
tempInvent = transform.parent.GetComponent<Item.TempInventory>();
|
|
|
|
initColor = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].GetColor("_BaseColor");
|
|
selMaterial = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex];
|
|
itemSelector = Item.ItemSelector.instance;
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
flashlightEnabled = flashlight.gameObject.activeSelf;
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void Update()
|
|
{
|
|
fireDown.Check();
|
|
if (isDead)
|
|
{
|
|
DropHeavy();
|
|
return;
|
|
}
|
|
|
|
if (manager.IsPaused || isDead) return;
|
|
|
|
if (Input.GetButtonDown("Fire1") || fireDown.IsDown())
|
|
{
|
|
if (GunEnabled)
|
|
{
|
|
if (tempInvent.GetQuantityOf(pistol.projectileName) > 0)
|
|
{
|
|
pistol.Fire();
|
|
noiseManager.ShotFired();
|
|
tempInvent.Remove(pistol.projectileName);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!IsCarrying)
|
|
{
|
|
var t_index = 0;
|
|
var 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 (var item in itemsInRange)
|
|
if (!item.CanPickup)
|
|
{
|
|
if (!item.Interact(ref invent, ref heavyInvent))
|
|
item.Interact();
|
|
else if (item is Item.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();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int refIndex;
|
|
if (itemsInRange.Count > 0 && receiverInRange(out refIndex))
|
|
{
|
|
((Item.HeavyItemReceiver)itemsInRange[refIndex]).Interact(ref invent, ref heavyInvent);
|
|
heavyInvent = null;
|
|
}
|
|
else
|
|
{
|
|
DropHeavy();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Input.GetButtonDown("Fire2"))
|
|
{
|
|
//print(this.GunEnabled);
|
|
if (!GunEnabled)
|
|
{
|
|
if (!flashlight.gameObject.activeSelf)
|
|
EnableFlashlight();
|
|
else
|
|
DisableFlashlight();
|
|
}
|
|
else
|
|
{
|
|
pistol.LightToggle();
|
|
}
|
|
}
|
|
|
|
if (Input.GetButtonDown("Fire3"))
|
|
if (!IsCarrying)
|
|
{
|
|
GunEnabled = !GunEnabled;
|
|
if (!GunEnabled)
|
|
pistol.Disable();
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
if (GunEnabled)
|
|
pistol.Enable();
|
|
}
|
|
|
|
if (GunEnabled)
|
|
{
|
|
DisableFlashlight();
|
|
|
|
var aimAxis = Input.GetAxis("Aim");
|
|
|
|
if (aimAxis > 0.5f)
|
|
pistol.aimMode = Item.PistolComponent.AimMode.CAMERA;
|
|
else
|
|
pistol.aimMode = Item.PistolComponent.AimMode.MODIFIED;
|
|
}
|
|
|
|
shift.SetCenter(!GunEnabled);
|
|
}
|
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.gameObject.GetComponent<Item.InteractableItem>() != null)
|
|
{
|
|
var item = other.gameObject.GetComponent<Item.InteractableItem>();
|
|
if (item is Item.InteractableItem && item is not Item.HeavyInteractableItem)
|
|
{
|
|
item.Enable();
|
|
itemsInRange.Add(item);
|
|
}
|
|
else if (item is Item.HeavyInteractableItem)
|
|
{
|
|
((Item.HeavyInteractableItem)item).Enable();
|
|
heavyItemsInRange.Add((Item.HeavyInteractableItem)item);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.gameObject.GetComponent<Item.InteractableItem>() != null)
|
|
{
|
|
var item = other.gameObject.GetComponent<Item.InteractableItem>();
|
|
if (item is Item.InteractableItem && item is not Item.HeavyInteractableItem)
|
|
{
|
|
item.Disable();
|
|
itemsInRange.Remove(item);
|
|
}
|
|
else if (item is Item.HeavyInteractableItem)
|
|
{
|
|
((Item.HeavyInteractableItem)item).Disable();
|
|
//itemsInRange.Remove((HeavyInteractableItem)(item));
|
|
heavyItemsInRange.Remove((Item.HeavyInteractableItem)item);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool receiverInRange(out int index)
|
|
{
|
|
var i = 0;
|
|
foreach (var item in itemsInRange)
|
|
{
|
|
if (item is Item.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;
|
|
}
|
|
}
|
|
|
|
|
|
internal class AxisIsDown
|
|
{
|
|
private float axis;
|
|
|
|
|
|
private readonly string axisName;
|
|
|
|
private bool down;
|
|
private bool isDown;
|
|
private bool pIsDown;
|
|
|
|
public AxisIsDown(string axisName)
|
|
{
|
|
this.axisName = axisName;
|
|
}
|
|
|
|
public void Check()
|
|
{
|
|
axis = Input.GetAxis(axisName);
|
|
isDown = axis > 0.5f;
|
|
if (isDown != pIsDown && isDown)
|
|
down = true;
|
|
else
|
|
down = false;
|
|
pIsDown = isDown;
|
|
}
|
|
|
|
public bool IsDown()
|
|
{
|
|
return down;
|
|
}
|
|
}
|
|
|
|
} |