using System.Collections; using System.Collections.Generic; using UnityEngine; using Mirror; using UnityEngine.Animations.Rigging; using Debuffs; using Player; 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 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(); _shootClip = Resources.Load("Audio/CharacterSounds/Gun/Shoot2"); _reloadClip = Resources.Load("Audio/CharacterSounds/Gun/Reload2"); _flashLightClip = Resources.Load("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(); 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() : 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 player = hitInfo.transform.gameObject.GetComponent(); var player = hitInfo.transform.gameObject.GetComponent(); if (player != null) { CmdReductStamina(player); CmdAddDebuff(player); } var alien = hitInfo.transform.gameObject.GetComponent(); 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 CmdReductStamina(PersonController player) { RpcReductStamina(player); } [ClientRpc] void RpcReductStamina(PersonController player) { player.TakeDamage(); player.InstantStaminaReduction(controller.shootDepletion * 0.7f); } [Command] void CmdHitAlien(EnemyController alien) { RpcHitAlien(alien); } [ClientRpc] void RpcHitAlien(EnemyController alien) { alien.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(); 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() : 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; } }