2023-03-14 00:59:59 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2023-06-15 18:03:19 +02:00
|
|
|
using FishNet.Connection;
|
|
|
|
using FishNet.Object;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
namespace Player
|
|
|
|
{
|
|
|
|
|
2023-03-14 00:59:59 +01:00
|
|
|
[RequireComponent(typeof(Collider))]
|
2023-06-15 18:03:19 +02:00
|
|
|
public class PlayerInteractionHandler : NetworkBehaviour
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
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;
|
|
|
|
|
2023-06-02 06:30:58 +02:00
|
|
|
[SerializeField] private Item.PistolComponent pistol;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
[SerializeField] private NoiseVisibilitySettingsManager noiseManager;
|
|
|
|
|
|
|
|
[SerializeField] private CameraShift shift;
|
|
|
|
|
|
|
|
public bool isDead;
|
|
|
|
|
2023-06-15 18:03:19 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
//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
|
|
|
|
2023-06-02 06:30:58 +02:00
|
|
|
private Item.HeavyInteractableItem heavyInvent;
|
|
|
|
private readonly List<Item.HeavyInteractableItem> heavyItemsInRange = new();
|
2023-06-01 17:03:48 +02:00
|
|
|
private Color initColor;
|
2023-06-02 06:30:58 +02:00
|
|
|
private Item.Inventory invent;
|
2023-03-14 00:59:59 +01:00
|
|
|
|
2023-06-02 06:30:58 +02:00
|
|
|
private Item.ItemSelector itemSelector;
|
|
|
|
private readonly List<Item.InteractableItem> itemsInRange = new();
|
|
|
|
[SerializeField]
|
|
|
|
private Scriptable.GameState manager;
|
2023-06-01 17:03:48 +02:00
|
|
|
private Material selMaterial;
|
2023-06-02 06:30:58 +02:00
|
|
|
private Item.TempInventory tempInvent;
|
|
|
|
public Item.Inventory Inventory => invent;
|
2023-06-01 17:03:48 +02:00
|
|
|
public bool IsCarrying => heavyInvent != null;
|
|
|
|
public bool GunEnabled { get; private set; }
|
2023-06-15 18:11:38 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
public Transform CarryingPos => carryingPos;
|
2023-06-15 18:11:38 +02:00
|
|
|
private bool inputEnbaled = true;
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-15 18:03:19 +02:00
|
|
|
public override void OnStartClient()
|
|
|
|
{
|
|
|
|
base.OnStartClient();
|
2023-06-15 18:11:38 +02:00
|
|
|
inputEnbaled = IsOwner;
|
|
|
|
|
2023-06-15 18:03:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
private void Start()
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-03-21 19:49:47 +01:00
|
|
|
instance = this;
|
2023-06-02 06:30:58 +02:00
|
|
|
invent = transform.parent.GetComponent<Item.Inventory>();
|
2023-04-18 04:29:21 +02:00
|
|
|
//TEMP FIELD
|
2023-06-02 06:30:58 +02:00
|
|
|
tempInvent = transform.parent.GetComponent<Item.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-06-02 06:30:58 +02:00
|
|
|
itemSelector = Item.ItemSelector.instance;
|
2023-06-01 17:03:48 +02:00
|
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
|
|
flashlightEnabled = flashlight.gameObject.activeSelf;
|
2023-06-15 18:03:19 +02:00
|
|
|
shift = GameObject.Find("CameraHidden").GetComponent<CameraShift>();
|
2023-06-02 06:30:58 +02:00
|
|
|
|
2023-05-11 05:59:58 +02:00
|
|
|
}
|
2023-07-15 02:07:57 +02:00
|
|
|
|
|
|
|
[ObserversRpc(ExcludeOwner = true)]
|
2023-06-15 18:19:21 +02:00
|
|
|
public void SwitchWeaponMode()
|
|
|
|
{
|
2023-07-15 02:07:57 +02:00
|
|
|
|
2023-06-15 18:19:21 +02:00
|
|
|
GunEnabled = !GunEnabled;
|
|
|
|
if (!GunEnabled)
|
|
|
|
pistol.Disable();
|
|
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
|
|
if (GunEnabled)
|
|
|
|
pistol.Enable();
|
|
|
|
}
|
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-15 18:11:38 +02:00
|
|
|
if ((manager.IsPaused || isDead)&&!inputEnbaled) return;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-06-15 18:11:38 +02:00
|
|
|
if ((Input.GetButtonDown("Fire1") || fireDown.IsDown())&&inputEnbaled)
|
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-07-15 01:39:52 +02:00
|
|
|
print("Calling RPC:Fire(). Isclient:" + IsClient);
|
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-02 06:30:58 +02:00
|
|
|
else if (item is Item.HeavyItemReceiver)
|
2023-06-01 17:03:48 +02:00
|
|
|
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))
|
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
((Item.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-06-15 18:11:38 +02:00
|
|
|
if (Input.GetButtonDown("Fire2")&&inputEnbaled)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
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-06-15 18:11:38 +02:00
|
|
|
if (Input.GetButtonDown("Fire3")&&inputEnbaled)
|
2023-06-01 17:03:48 +02:00
|
|
|
if (!IsCarrying)
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
2023-06-15 19:03:27 +02:00
|
|
|
|
|
|
|
SwitchWeaponMode();
|
2023-07-15 02:07:57 +02:00
|
|
|
if (!IsServer) {
|
2023-06-15 19:03:27 +02:00
|
|
|
GunEnabled = !GunEnabled;
|
|
|
|
if (!GunEnabled)
|
|
|
|
pistol.Disable();
|
|
|
|
pistol.gameObject.SetActive(GunEnabled);
|
|
|
|
if (GunEnabled)
|
|
|
|
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-06-15 18:11:38 +02:00
|
|
|
|
2023-05-29 06:16:05 +02:00
|
|
|
if (aimAxis > 0.5f)
|
2023-06-02 06:30:58 +02:00
|
|
|
pistol.aimMode = Item.PistolComponent.AimMode.CAMERA;
|
2023-05-29 06:16:05 +02:00
|
|
|
else
|
2023-06-02 06:30:58 +02:00
|
|
|
pistol.aimMode = Item.PistolComponent.AimMode.MODIFIED;
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
2023-06-15 18:11:38 +02:00
|
|
|
if(!inputEnbaled)
|
|
|
|
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-02 06:30:58 +02:00
|
|
|
if (other.gameObject.GetComponent<Item.InteractableItem>() != null)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
var item = other.gameObject.GetComponent<Item.InteractableItem>();
|
|
|
|
if (item is Item.InteractableItem && item is not Item.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
|
|
|
}
|
2023-06-02 06:30:58 +02:00
|
|
|
else if (item is Item.HeavyInteractableItem)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
((Item.HeavyInteractableItem)item).Enable();
|
|
|
|
heavyItemsInRange.Add((Item.HeavyInteractableItem)item);
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
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-06-02 06:30:58 +02:00
|
|
|
if (other.gameObject.GetComponent<Item.InteractableItem>() != null)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
var item = other.gameObject.GetComponent<Item.InteractableItem>();
|
|
|
|
if (item is Item.InteractableItem && item is not Item.HeavyInteractableItem)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
item.Disable();
|
2023-03-14 00:59:59 +01:00
|
|
|
itemsInRange.Remove(item);
|
|
|
|
}
|
2023-06-02 06:30:58 +02:00
|
|
|
else if (item is Item.HeavyInteractableItem)
|
2023-03-14 00:59:59 +01:00
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
((Item.HeavyInteractableItem)item).Disable();
|
2023-03-14 00:59:59 +01:00
|
|
|
//itemsInRange.Remove((HeavyInteractableItem)(item));
|
2023-06-02 06:30:58 +02:00
|
|
|
heavyItemsInRange.Remove((Item.HeavyInteractableItem)item);
|
2023-03-14 00:59:59 +01:00
|
|
|
}
|
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)
|
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
if (item is Item.HeavyItemReceiver)
|
2023-06-01 17:03:48 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|