123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using HQFPSWeapons;
- using Mirror;
- using System;
- using UnityEngine.Animations.Rigging;
- public class netPlayer : NetworkBehaviour
- {
- public List<GameObject> currentZombies;
- public Player player;
- public GameObject playerModelParent;
- public Animator anim;
- [SyncVar]
- public string pname;
- [SyncVar]
- public string latency;
- [Header("Health")]
- // public GameObject bloodPrefab;
- public GameObject BloodAttach;
- public GameObject[] BloodFX;
- public GameObject deadRagdoll;
- [HideInInspector]
- public int effectIdx;
- [Header("Weapons")]
- public tpsGunData[] guns;
- public meleeWeaponData[] melees;
- [SyncVar(hook = nameof(OnEquipmentChanged))]
- public int curGunIndex = -1;
- [SyncVar(hook = nameof(OnMeleeChanged))]
- public int meleeIndex = -1;
- public bool holdingRifle;
- public Rig leftArm;
- public Rig rightArm;
- public Transform weaponAim;
- public Transform leftHandTarget;
- public Transform headTarget;
- public Transform weaponHoldPose;
- public Transform weaponAimPose;
- [Header("WeaponAnimations")]
- public Animator weaponAnim;
- [Header("Aiming")]
- public Transform aimHeightTarget;
- public Transform aimHeightTDC;
- public Transform aimHeightMid;
- public Transform aimHeightBDC;
- [SyncVar(hook = nameof(OnAimChanged))]
- public bool aiming;
- public Rig headRig;
- public Rig chestRig;
- public bool showLocalChar;
- void Start()
- {
- if (!isLocalPlayer) { return; } //stuff only for local player
- netPlayerStats.localPlayer = this;
- Collider[] colliders = gameObject.GetComponentsInChildren<Collider>();
- Debug.Log($"Found ${colliders.Length} colliders and disabling them");
- foreach (Collider collider in colliders)
- {
- collider.gameObject.layer = LayerMask.NameToLayer("netPlayer");
- }
- player = FindObjectOfType<Player>();
- pname = PlayerPrefs.GetString("username");
- if (!isServer)
- {
- CmdUpdateName(pname);
- }
- else
- {
- RpcUpdateName(pname);
- }
- if (!showLocalChar) hideTpsCharacter();
- // transform.GetChild(0).gameObject.SetActive(false);
- //prepare listeners
- player.EquippedItem.AddChangeListener((SaveableItem item) => { callChangeEquipment((item == null) ? "" : item.Name); });
- player.UseContinuously.AddListener(() => { onUsedItem(); });
- player.UseOnce.AddListener(() => { onUsedItem(); });
- player.Aim.AddStartListener(() => { callToggleAim(true); });
- player.Aim.AddStopListener(() => { callToggleAim(false); });
- player.Run.AddStartListener(() => { callToggleAim(false); });
- player.Death.AddListener(() => { callDeath(); });
- player.Respawn.AddListener(() => { callRespawn(); });
- callToggleAim(false);
- }
- #region initilaztionNetworkStuff
- void hideTpsCharacter()
- {
- foreach (SkinnedMeshRenderer renderer in playerModelParent.GetComponentsInChildren<SkinnedMeshRenderer>())
- {
- renderer.enabled = false;
- }
- }
- [Command]
- void CmdUpdateName(string e)
- {
- pname = e;
- RpcUpdateName(e);
- }
- [ClientRpc]
- void RpcUpdateName(string e)
- {
- pname = e;
- }
- #endregion
- // Update is called once per frame
- bool jumping = false;
- float armWeights = 0;
- float bodyWeights = 0;
- //Update latency to server
- [Command]
- void CmdUpdateLatency(string value)
- {
- latency = value;
- }
- float t1 = 0;
- public float yAxis;
- void FixedUpdate()
- {
- //updatePlayerDirection (Regardless of isLocalPlayer)
- // headRig.weight = (anim.GetBool("running")) ? 0: 1;
- if (isLocalPlayer)
- {//Stuff only for local player
- if (t1 < 2)
- {
- t1 += Time.deltaTime;
- }
- else
- {
- Debug.Log(NetworkTime.rtt);
- CmdUpdateLatency((NetworkTime.rtt * 1000f).ToString("n0") + " ms");
- t1 = 0;
- }
- yAxis = player.LookDirection.Val.y;
- float lookSmoothness = 0.1f;
- if (yAxis == 0)
- {
- aimHeightTarget.position = Vector3.Lerp(aimHeightTarget.position, aimHeightMid.position, lookSmoothness);
- }
- else if (yAxis > 0)
- {
- aimHeightTarget.position = Vector3.Lerp(aimHeightTarget.position, aimHeightMid.position + ((aimHeightTDC.position - aimHeightMid.position) * yAxis), lookSmoothness);
- }
- else if (yAxis < 0)
- {
- aimHeightTarget.position = Vector3.Lerp(aimHeightTarget.position, aimHeightMid.position + ((aimHeightBDC.position - aimHeightMid.position) * -yAxis), lookSmoothness);
- }
- anim.SetFloat("vX", Input.GetAxis("Horizontal"));
- anim.SetFloat("vY", Input.GetAxis("Vertical"));
- anim.SetBool("crouch", player.Crouch.Active);
- anim.SetBool("aiming", player.Aim.Active);
- // player.Aim.lis
- transform.position = player.transform.position;
- transform.rotation = player.transform.rotation;
- // Debug.Log($"walk status : {player.Walk.Active} , running status: {player.Run.Active}");
- int animCode = 0;
- if (player.Run.Active)
- {
- animCode = 2;
- }
- else if (player.Walk.Active)
- {
- animCode = 1;
- }
- if (player.Jump.Active)
- {
- animCode = 3;
- }
- CallChangeAnim(animCode);
- if (curGunIndex >= 0)
- {
- // leftHandTarget.position = weaponAim.TransformPoint(-player.GetComponent<ArmsPosition>().leftHandOffset) + (0.35f * leftHandTarget.right);
- }
- }
- else
- {//update for other peoples
- }
- if (curGunIndex >= 0)
- {
- // leftHandTarget.position = guns[curGunIndex].leftHandPosition.position;
- // leftHandTarget.rotation = guns[curGunIndex].leftHandPosition.rotation;
- headTarget.position = guns[curGunIndex].headTarget.position;
- if (aiming)
- {
- if (bodyWeights <= 1)
- {
- bodyWeights += 0.5f;
- headRig.weight = bodyWeights;
- chestRig.weight = bodyWeights;
- }
- weaponAim.position = Vector3.Lerp(weaponAim.position, guns[curGunIndex].aiming_rightHandPosition.position, 0.1f);
- weaponAim.rotation = Quaternion.Lerp(weaponAim.rotation, guns[curGunIndex].aiming_rightHandPosition.rotation, 0.1f);
- }
- else
- {
- if (bodyWeights >= 0)
- {
- bodyWeights -= 0.05f;
- //if (anim.GetBool("running")) { headRig.weight = bodyWeights; }
- headRig.weight = bodyWeights;
- chestRig.weight = bodyWeights;
- }
- weaponAim.position = Vector3.Lerp(weaponAim.position, guns[curGunIndex].holding_rightHandPosition.position, 0.1f);
- weaponAim.rotation = Quaternion.Lerp(weaponAim.rotation, guns[curGunIndex].holding_rightHandPosition.rotation, 0.1f);
- }
- }
- else
- {
- }
- }
- public void callToggleWeaponEquipment(bool value)
- {
- if (isServer)
- {
- toggleWeaponEquipment(value);
- RpcToggleWeaponEquipment(value);
- }
- else
- {
- CmdToggleWeaponEquipment(value);
- }
- }
- [Command]
- void CmdToggleWeaponEquipment(bool value)
- {
- toggleWeaponEquipment(value);
- RpcToggleWeaponEquipment(value);
- }
- [ClientRpc]
- void RpcToggleWeaponEquipment(bool value)
- {
- if (!isLocalPlayer) { return; }
- toggleWeaponEquipment(value);
- }
- public void toggleWeaponEquipment(bool value)
- {
- if (value)
- {
- leftArm.weight = 1;
- rightArm.weight = 1;
- callToggleAim(false);
- weaponAim.gameObject.SetActive(true);
- }
- else
- {
- //headRig.weight = 0;
- chestRig.weight = 0;
- leftArm.weight = 0;
- rightArm.weight = 0;
- weaponAim.gameObject.SetActive(false);
- }
- }
- public void callToggleAim(bool value)
- {
- if (isServer)
- {
- aiming = value;
- }
- else
- {
- CmdToggleAim(value);
- }
- }
- void OnAimChanged(bool oldAim, bool newAim)
- {
- if (newAim)
- {
- // headRig.weight = 1;
- chestRig.weight = 1;
- weaponAim.position = guns[curGunIndex].aiming_rightHandPosition.position;
- weaponAim.rotation = guns[curGunIndex].aiming_rightHandPosition.rotation;
- }
- else
- {
- Debug.Log(anim.GetBool("running"));
- // headRig.weight =(anim.GetBool("running")) ?0 : 1;
- chestRig.weight = (anim.GetBool("running")) ? 0 : 1; ;
- weaponAim.position = guns[curGunIndex].holding_rightHandPosition.position;
- weaponAim.rotation = guns[curGunIndex].holding_rightHandPosition.rotation;
- }
- }
- [Command]
- void CmdToggleAim(bool value)
- {
- aiming = value;
- }
- /*
- Animation Codes
- 0 Idle Breathing
- 1 Walking
- 2 Running
- 3 Jumping
- */
- void CallChangeAnim(int code)
- {
- switch (code)
- {
- case 0:
- anim.SetBool("moving", false);
- anim.SetBool("running", false);
- anim.SetBool("jumping", false);
- break;
- case 1:
- anim.SetBool("moving", true);
- anim.SetBool("running", false);
- anim.SetBool("jumping", false);
- break;
- case 2:
- anim.SetBool("moving", true);
- anim.SetBool("running", true);
- anim.SetBool("jumping", false);
- break;
- case 3:
- anim.SetBool("jumping", true);
- break;
- }
- }
- ///Equipment change
- ///
- public void callChangeEquipment(string name)
- {
- //Get the id of weapon using name
- int gunId = -1;
- int _meleeIndex = -1;
- if (name == "")
- {
- callToggleWeaponEquipment(false);
- }
- else
- {
- ItemData itemData = null;
- ItemDatabase.Default.TryGetItem(name, out itemData);
- if (itemData != null)
- {
- gunId = getWeaponWithName(itemData.Name);
- }
- }
- if (gunId < 0)
- {
- //This means no weapon is there for that name, lets see on melees
- ItemData itemData = null;
- ItemDatabase.Default.TryGetItem(name, out itemData);
- if (itemData != null)
- {
- _meleeIndex = getMeleeWithName(itemData.Name);
- }
- }
- //UPDATE IT USING SYNCVAR
- if (isServer)
- {
- curGunIndex = gunId;
- meleeIndex = _meleeIndex;
- }
- else
- {
- SetCurGunId(gunId, _meleeIndex);
- }
- }
- [Command]
- void SetCurGunId(int gunId, int _meleeIndex)
- {
- curGunIndex = gunId;
- meleeIndex = _meleeIndex;
- }
- void OnEquipmentChanged(int oldIndex, int newIndex)
- {
- //Debug.Log("THE HOOK WORKS : " + newIndex);
- if (newIndex >= 0)
- {
- //not a melee so disable all melees
- foreach (tpsGunData gun in guns)
- {
- gun.gameObject.SetActive(false);
- }
- if (!isLocalPlayer)
- {
- guns[newIndex].gameObject.SetActive(true);
- }
- guns[newIndex].gameObject.SetActive(true);
- toggleWeaponEquipment(true);
- }
- else
- {
- toggleWeaponEquipment(false);
- }
- }
- void OnMeleeChanged(int oldIndex, int newIndex)
- {
- Debug.Log("Melee Hook Works");
- foreach (meleeWeaponData melee in melees)
- {
- melee.gameObject.SetActive(false);
- }
- if (newIndex >= 0)
- {
- //some melee weeapon
- melees[newIndex].gameObject.SetActive(true);
- }
- else
- {
- //bare hands
- }
- }
- ///Shoot
- ///
- public void callShoot(Vector3 hitPoint, Vector3 hitNormals)
- {
- callToggleAim(true);
- aimHeightTarget.position = aimHeightTarget.position + new Vector3(0, 0.2f, 0);
- if (isServer)
- {
- broadcastShot(hitPoint, hitNormals);
- RpcBroadcastShot(hitPoint, hitNormals);
- }
- else
- {
- CmdBroadcastShot(hitPoint, hitNormals);
- }
- }
- [Command]
- void CmdBroadcastShot(Vector3 hitPoint, Vector3 hitNormals)
- {
- broadcastShot(hitPoint, hitNormals);
- RpcBroadcastShot(hitPoint, hitNormals);
- }
- [ClientRpc]
- void RpcBroadcastShot(Vector3 hitPoint, Vector3 hitNormals)
- {
- if (isServer) { return; }
- broadcastShot(hitPoint, hitNormals);
- }
- void broadcastShot(Vector3 hitPoint, Vector3 hitNormals)
- {
- // SurfaceEffects effect = (SurfaceEffects)Enum.Parse(typeof(SurfaceEffects), effectType);
- if (isLocalPlayer && !showLocalChar) { return; }
- SurfaceManager.SpawnEffect(0, SurfaceEffects.BulletHit, 1f, hitPoint, Quaternion.LookRotation(hitNormals));
- //shootingEffects
- if (curGunIndex >= 0)
- {
- guns[curGunIndex].triggerMuzzleFlash();
- }
- Debug.Log("Broadcasting shot");
- // weaponAnim.CrossFade("shot",0.1f);
- }
- void onUsedItem()
- {
- if (!isLocalPlayer) { return; }
- if (curGunIndex < 0)
- {
- if (meleeIndex >= 0)
- {
- anim.CrossFadeInFixedTime(melees[meleeIndex].animName, 0.01f);
- }
- else
- {
- anim.CrossFadeInFixedTime("punch", 0.01f);
- }
- }
- }
- int getWeaponWithName(string name)
- {
- for (int i = 0; i < guns.Length; i++)
- {
- tpsGunData gunData = guns[i];
- if (gunData.weaponName == name)
- {
- return i;
- }
- }
- return -1;
- }
- int getMeleeWithName(string name)
- {
- for (int i = 0; i < melees.Length; i++)
- {
- meleeWeaponData _melees = melees[i];
- if (_melees.weaponName == name)
- {
- return i;
- }
- }
- return -1;
- }
- public void callDamage(float damage)
- {
- RpcDamage(damage);
- }
- [ClientRpc]
- public void RpcDamage(float damage)
- {
- if (isLocalPlayer)
- {
- player.GetComponentInChildren<PlayerVitals>().Entity.ChangeHealth.Try(new HealthEventData(-damage));
- Debug.Log("You are local player, damaging");
- }
- else
- {
- Debug.Log("Nah you aint the local player, no damage for you sweetie");
- }
- }
- [Command(requiresAuthority = false)]
- public void CmdDmg(float damage)
- {
- RpcDamage(damage);
- Debug.Log("RPC damage");
- }
- public void hitboxDamage(HealthEventData damageData, shotType hitboxType)
- {
- float dmg = 0;
- switch (hitboxType)
- {
- case shotType.headshot:
- dmg = damageData.Delta * 5;
- break;
- case shotType.chestshot:
- dmg = damageData.Delta;
- break;
- case shotType.legshot:
- dmg = damageData.Delta / 2f;
- break;
- }
- dmg = -dmg;
- Debug.Log("Received damage from hitbox");
- if (!isServer)
- {
- CmdDmg(dmg); Debug.Log("Command damage " + dmg.ToString());
- }
- else
- {
- RpcDamage(dmg);
- }
- }
- public void callDeath()
- {
- Debug.Log("Calling Death");
- if (isServer)
- {
- death();
- RpcDeath();
- }
- else
- {
- CmdDeath();
- }
- }
- [Command]
- void CmdDeath()
- {
- death();
- RpcDeath();
- }
- [ClientRpc]
- void RpcDeath()
- {
- death();
- }
- void death()
- {
- // if (isLocalPlayer) { return; }
- playerModelParent.gameObject.SetActive(false);
- Instantiate(deadRagdoll, transform.position, transform.rotation);
- }
- public void callRespawn()
- {
- if (isServer)
- {
- Respawn();
- RpcRespawn();
- }
- else
- {
- CmdRespawn();
- }
- }
- [Command]
- void CmdRespawn()
- {
- Respawn();
- RpcRespawn();
- }
- [ClientRpc]
- void RpcRespawn()
- {
- Respawn();
- }
- void Respawn()
- {
- if (isLocalPlayer) { return; }
- playerModelParent.gameObject.SetActive(true);
- }
- }
- public static class netPlayerStats
- {
- public static netPlayer localPlayer;
- }
|