440 lines
11 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using UnityEngine.Animations.Rigging;
using Debuffs;
using Characters;
using StarterAssets;
public class Interactions : NetworkBehaviour
{
[Header("Animations")]
public Rig takePropRig;
public Rig gunRig;
public AudioSource audioSource;
public Camera cam;
public GameObject gun;
public List<Renderer> gunRenderers;
public Renderer gunTube;
public Light gunTubeLight;
public ParticleSystem gunVFX;
public ParticleSystem vfx;
public Transform shootPoint;
public Light flashLight;
public GameObject hitVFX;
[SyncVar]
private float gunLayerWeight, _gunSpread;
private bool _lightState = false, _gunReady = true;
[HideInInspector] public int _fakeCubeCount = 1;
private AudioSource _gunAudioSource;
private AudioClip _shootClip, _reloadClip, _flashLightClip;
public Transform leftHandPos, rightHandPos;
public GameObject _eyesObject;
[Header("Prop Prefs")]
public GameObject propPrefab;
private RaycastHit _hit;
[Header("Settings")]
public float maxHitDistance = 6f;
[Header("GUI Staff")]
public float hitDistance;
public bool canInterract;
[Header("Links")]
public StarterAssets.PersonController controller;
private PropGrab _grabbedProp;
private PropBehaviour _grabbedPropBehaviour;
private bool _canAttack = true;
void Start()
{
_gunAudioSource = gun.GetComponent<AudioSource>();
_shootClip = Resources.Load<AudioClip>("Audio/CharacterSounds/Gun/Shoot2");
_reloadClip = Resources.Load<AudioClip>("Audio/CharacterSounds/Gun/Reload2");
_flashLightClip = Resources.Load<AudioClip>("Audio/CharacterSounds/FlashLight");
}
void Update()
{
SetGun();
if (!isLocalPlayer) return;
if (_grabbedProp != null)
{
UpdateHandsTransform();
}
Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
PropGrab(ray);
GunInteraction(ray);
FakeCubeSpawn();
FlashLight();
}
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Respawn")
{
_canAttack = false;
controller.floatingInfo.SetActive(true);
}
}
void OnTriggerExit(Collider collider)
{
if (collider.tag == "Respawn")
{
_canAttack = true;
controller.floatingInfo.SetActive(false);
}
}
void PropGrab(Ray ray)
{
// _eyesObject.transform.position, _eyesObject.transform.TransformDirection(Vector3.forward)
if (Physics.Raycast(ray, out _hit, maxHitDistance, LayerMask.NameToLayer("Player")))
{
IInteractable interactable = _hit.transform.gameObject.GetComponent<IInteractable>();
canInterract = interactable != null;
if (Input.GetKeyDown(KeyCode.E))
{
if (interactable != null)
{
interactable.Interact();
}
}
}
hitDistance = _hit.distance > 0 ? _hit.distance : maxHitDistance;
if (Input.GetKeyUp(KeyCode.E))
{
DropProp();
}
}
public void SetGrabbedProp(PropGrab prop)
{
_grabbedProp = prop;
// CmdSetGrabbedProp(prop);
_grabbedPropBehaviour = prop != null ? prop.GetComponent<PropBehaviour>() : null;
takePropRig.weight = prop != null ? 1f : 0f;
}
void FakeCubeSpawn()
{
if (Input.GetKeyDown(KeyCode.Q) && _fakeCubeCount > 0)
{
CmdSpawnObject(_eyesObject.transform.position + _eyesObject.transform.forward, _eyesObject.transform.rotation);
_fakeCubeCount--;
}
}
void GunInteraction(Ray ray)
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
CmdSetGunRigWeight(1f);
}
if (Input.GetKeyUp(KeyCode.Mouse1))
{
CmdSetGunRigWeight(0f);
}
if (gunLayerWeight == 1 && _gunSpread < Time.time)
{
if (_gunAudioSource.isPlaying && _gunReady)
{
_gunAudioSource.Stop();
}
if (Input.GetKeyDown(KeyCode.Mouse0) && _canAttack && controller.currentEnergy > controller.shootDepletion)
{
_gunSpread = Time.time + .5f;
controller.InstantStaminaReduction(controller.shootDepletion);
Physics.Raycast(ray, out RaycastHit hitInfo); //shootPoint.position, new Vector3(Screen.width / 2, Screen.height / 2, 0) GameObject instance = Instantiate(hitVFX, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
if (hitInfo.transform != null)
{
var pawn = hitInfo.transform.gameObject.GetComponent<Pawn>();
if (pawn != null)
{
CmdHitPawn(pawn);
}
//
// var player = hitInfo.transform.gameObject.GetComponent<PersonController>();
// if (player != null)
// {
// CmdReductStamina(player);
// CmdAddDebuff(player);
// }
// var alien = hitInfo.transform.gameObject.GetComponent<EnemyController>();
// if (alien != null)
// {
// CmdHitAlien(alien);
// }
}
CmdShoot(hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
}
}
else
{//if (_gunReady && controller.currentEnergy < controller.shootDepletion)
_gunReady = false;
}
if (_gunSpread > Time.time && !_gunReady)
{
_gunReady = true;
CmdReload();
}
}
[Command(requiresAuthority = false)]
void CmdHitPawn(Pawn pawn)
{
pawn.TakeDamage();
RpcHitPawn(pawn);
}
[ClientRpc]
void RpcHitPawn(Pawn pawn)
{
print("HIT " + pawn.name);
pawn.TakeDamage();
}
[Command]
void CmdShoot(Vector3 pos, Quaternion rot)
{
RpcShoot();
GameObject instance = Instantiate(hitVFX, pos, rot);
NetworkServer.Spawn(instance);
}
[ClientRpc]
void RpcShoot()
{
vfx.Stop();
if (gun.activeInHierarchy)
_gunAudioSource.PlayOneShot(_shootClip);
vfx.Play();
}
[Command]
void CmdReload()
{
RpcReload();
}
[ClientRpc]
void RpcReload()
{
gunVFX.Play();
if (gun.activeInHierarchy)
{
_gunAudioSource.PlayOneShot(_reloadClip);
}
}
private float _lightIntensity;
void SetGun()
{
gunRig.weight = Mathf.Lerp(gunRig.weight, gunLayerWeight, Time.deltaTime * 5f);
_lightIntensity = Mathf.Lerp(_lightIntensity, (_gunSpread < Time.time) && (controller.currentEnergy >= controller.shootDepletion) ? 1f : 0f, Time.deltaTime * 5f);
if (gunLayerWeight == 1) gun.SetActive(true);
if (gunRig.weight <= 0.1 || controller.currentEnergy <= 0) gun.SetActive(false);
if (gun != null)
{
foreach (var renderer in gunRenderers)
{
renderer.material.SetFloat("_Dissolve", 1 - gunRig.weight);
}
gunTube.material.SetColor("_BaseColor", controller.gunColor * _lightIntensity);
gunTubeLight.enabled = gunLayerWeight == 1;
gunTubeLight.color = controller.gunColor * _lightIntensity;
}
}
[Command]
void CmdSetGunRigWeight(float value)
{
gunLayerWeight = value;
}
void FlashLight()
{
if (Input.GetKeyDown(KeyCode.F))
{
CmdLight();
}
if (_lightState)
{
controller.currentEnergy -= controller._flashLightDepletion * Time.deltaTime;
}
}
[Command]
void CmdLight()
{
_lightState = !_lightState;
RpcLight(_lightState);
}
[ClientRpc]
void RpcLight(bool state)
{
audioSource.PlayOneShot(_flashLightClip);
flashLight.enabled = state;
}
[Command]
public void DropProp()
{
// if (_grabbedProp == null) return;
// _grabbedProp.ResetPlayer();
// _grabbedProp.Interact();
if (_grabbedProp != null)
{
_grabbedProp.ResetPlayer();
}
}
[Command]
void CmdAddDebuff(Pawn player)
{
RpcSetDebuff(player);
Interactions interactions = player.GetComponent<Interactions>();
PropGrab prop = null;
if (interactions != null)
{
prop = interactions._grabbedProp;
}
if (prop != null)
{
prop.ResetPlayer();
}
}
[Command(requiresAuthority = false)]
void CmdRemoveDebuff(DebuffBase debuff, Pawn player)
{
RpcRemoveDebuff(debuff, player);
}
[ClientRpc]
void RpcSetDebuff(Pawn player)
{
var debuff = new TaserDebuff();
player.TakeDamage();
debuff.OnDebuffEnd += CmdRemoveDebuff;
debuff.Enable(player);
StartCoroutine(debuff.Act(player));
}
[ClientRpc]
void RpcRemoveDebuff(DebuffBase debuff, Pawn player)
{
debuff.Disable(player);
debuff.OnDebuffEnd -= CmdRemoveDebuff;
}
[Command]
void CmdSpawnObject(Vector3 pos, Quaternion rot)
{
GameObject gameObject = Instantiate(propPrefab, pos, rot);
NetworkServer.Spawn(gameObject);
}
// [Command(requiresAuthority = false)]
// void CmdSetGrabbedProp(PropGrab prop)
// {
// _grabbedProp = prop;
// RpcSetGrabbedProp(prop);
// }
// [ClientRpc]
// void RpcSetGrabbedProp(PropGrab prop)
// {
// _grabbedPropBehaviour = prop != null ? prop.GetComponent<PropBehaviour>() : null;
// takePropRig.weight = prop != null ? 1f : 0f;
// }
void UpdateHandsTransform()
{
CmdUpdateHands();
// if (_grabbedPropBehaviour == null) return;
// leftHandPos.position = _grabbedPropBehaviour.leftHandPos.position;
// rightHandPos.position = _grabbedPropBehaviour.rightHandPos.position;
// leftHandPos.rotation = _grabbedPropBehaviour.leftHandPos.rotation;
// rightHandPos.rotation = _grabbedPropBehaviour.rightHandPos.rotation;
}
[Command]
void CmdUpdateHands()
{
RpcUpdateHands();
}
[ClientRpc]
void RpcUpdateHands()
{
if (_grabbedPropBehaviour == null) return;
leftHandPos.position = _grabbedPropBehaviour.leftHandPos.position;
rightHandPos.position = _grabbedPropBehaviour.rightHandPos.position;
leftHandPos.rotation = _grabbedPropBehaviour.leftHandPos.rotation;
rightHandPos.rotation = _grabbedPropBehaviour.rightHandPos.rotation;
}
}