netPlayer.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using HQFPSWeapons;
  5. using Mirror;
  6. using System;
  7. using UnityEngine.Animations.Rigging;
  8. public class netPlayer : NetworkBehaviour
  9. {
  10. public List<GameObject> currentZombies;
  11. public Player player;
  12. public GameObject playerModelParent;
  13. public Animator anim;
  14. [SyncVar]
  15. public string pname;
  16. [SyncVar]
  17. public string latency;
  18. [Header("Health")]
  19. // public GameObject bloodPrefab;
  20. public GameObject BloodAttach;
  21. public GameObject[] BloodFX;
  22. public GameObject deadRagdoll;
  23. [HideInInspector]
  24. public int effectIdx;
  25. [Header("Weapons")]
  26. public tpsGunData[] guns;
  27. public meleeWeaponData[] melees;
  28. [SyncVar(hook = nameof(OnEquipmentChanged))]
  29. public int curGunIndex = -1;
  30. [SyncVar(hook = nameof(OnMeleeChanged))]
  31. public int meleeIndex = -1;
  32. public bool holdingRifle;
  33. public Rig leftArm;
  34. public Rig rightArm;
  35. public Transform weaponAim;
  36. public Transform leftHandTarget;
  37. public Transform headTarget;
  38. public Transform weaponHoldPose;
  39. public Transform weaponAimPose;
  40. [Header("WeaponAnimations")]
  41. public Animator weaponAnim;
  42. [Header("Aiming")]
  43. public Transform aimHeightTarget;
  44. public Transform aimHeightTDC;
  45. public Transform aimHeightMid;
  46. public Transform aimHeightBDC;
  47. [SyncVar(hook = nameof(OnAimChanged))]
  48. public bool aiming;
  49. public Rig headRig;
  50. public Rig chestRig;
  51. public bool showLocalChar;
  52. void Start()
  53. {
  54. if (!isLocalPlayer) { return; } //stuff only for local player
  55. netPlayerStats.localPlayer = this;
  56. Collider[] colliders = gameObject.GetComponentsInChildren<Collider>();
  57. Debug.Log($"Found ${colliders.Length} colliders and disabling them");
  58. foreach (Collider collider in colliders)
  59. {
  60. collider.gameObject.layer = LayerMask.NameToLayer("netPlayer");
  61. }
  62. player = FindObjectOfType<Player>();
  63. pname = PlayerPrefs.GetString("username");
  64. if (!isServer)
  65. {
  66. CmdUpdateName(pname);
  67. }
  68. else
  69. {
  70. RpcUpdateName(pname);
  71. }
  72. if (!showLocalChar) hideTpsCharacter();
  73. // transform.GetChild(0).gameObject.SetActive(false);
  74. //prepare listeners
  75. player.EquippedItem.AddChangeListener((SaveableItem item) => { callChangeEquipment((item == null) ? "" : item.Name); });
  76. player.UseContinuously.AddListener(() => { onUsedItem(); });
  77. player.UseOnce.AddListener(() => { onUsedItem(); });
  78. player.Aim.AddStartListener(() => { callToggleAim(true); });
  79. player.Aim.AddStopListener(() => { callToggleAim(false); });
  80. player.Run.AddStartListener(() => { callToggleAim(false); });
  81. player.Death.AddListener(() => { callDeath(); });
  82. player.Respawn.AddListener(() => { callRespawn(); });
  83. callToggleAim(false);
  84. }
  85. #region initilaztionNetworkStuff
  86. void hideTpsCharacter()
  87. {
  88. foreach (SkinnedMeshRenderer renderer in playerModelParent.GetComponentsInChildren<SkinnedMeshRenderer>())
  89. {
  90. renderer.enabled = false;
  91. }
  92. }
  93. [Command]
  94. void CmdUpdateName(string e)
  95. {
  96. pname = e;
  97. RpcUpdateName(e);
  98. }
  99. [ClientRpc]
  100. void RpcUpdateName(string e)
  101. {
  102. pname = e;
  103. }
  104. #endregion
  105. // Update is called once per frame
  106. bool jumping = false;
  107. float armWeights = 0;
  108. float bodyWeights = 0;
  109. //Update latency to server
  110. [Command]
  111. void CmdUpdateLatency(string value)
  112. {
  113. latency = value;
  114. }
  115. float t1 = 0;
  116. public float yAxis;
  117. void FixedUpdate()
  118. {
  119. //updatePlayerDirection (Regardless of isLocalPlayer)
  120. // headRig.weight = (anim.GetBool("running")) ? 0: 1;
  121. if (isLocalPlayer)
  122. {//Stuff only for local player
  123. if (t1 < 2)
  124. {
  125. t1 += Time.deltaTime;
  126. }
  127. else
  128. {
  129. Debug.Log(NetworkTime.rtt);
  130. CmdUpdateLatency((NetworkTime.rtt * 1000f).ToString("n0") + " ms");
  131. t1 = 0;
  132. }
  133. yAxis = player.LookDirection.Val.y;
  134. float lookSmoothness = 0.1f;
  135. if (yAxis == 0)
  136. {
  137. aimHeightTarget.position = Vector3.Lerp(aimHeightTarget.position, aimHeightMid.position, lookSmoothness);
  138. }
  139. else if (yAxis > 0)
  140. {
  141. aimHeightTarget.position = Vector3.Lerp(aimHeightTarget.position, aimHeightMid.position + ((aimHeightTDC.position - aimHeightMid.position) * yAxis), lookSmoothness);
  142. }
  143. else if (yAxis < 0)
  144. {
  145. aimHeightTarget.position = Vector3.Lerp(aimHeightTarget.position, aimHeightMid.position + ((aimHeightBDC.position - aimHeightMid.position) * -yAxis), lookSmoothness);
  146. }
  147. anim.SetFloat("vX", Input.GetAxis("Horizontal"));
  148. anim.SetFloat("vY", Input.GetAxis("Vertical"));
  149. anim.SetBool("crouch", player.Crouch.Active);
  150. anim.SetBool("aiming", player.Aim.Active);
  151. // player.Aim.lis
  152. transform.position = player.transform.position;
  153. transform.rotation = player.transform.rotation;
  154. // Debug.Log($"walk status : {player.Walk.Active} , running status: {player.Run.Active}");
  155. int animCode = 0;
  156. if (player.Run.Active)
  157. {
  158. animCode = 2;
  159. }
  160. else if (player.Walk.Active)
  161. {
  162. animCode = 1;
  163. }
  164. if (player.Jump.Active)
  165. {
  166. animCode = 3;
  167. }
  168. CallChangeAnim(animCode);
  169. if (curGunIndex >= 0)
  170. {
  171. // leftHandTarget.position = weaponAim.TransformPoint(-player.GetComponent<ArmsPosition>().leftHandOffset) + (0.35f * leftHandTarget.right);
  172. }
  173. }
  174. else
  175. {//update for other peoples
  176. }
  177. if (curGunIndex >= 0)
  178. {
  179. // leftHandTarget.position = guns[curGunIndex].leftHandPosition.position;
  180. // leftHandTarget.rotation = guns[curGunIndex].leftHandPosition.rotation;
  181. headTarget.position = guns[curGunIndex].headTarget.position;
  182. if (aiming)
  183. {
  184. if (bodyWeights <= 1)
  185. {
  186. bodyWeights += 0.5f;
  187. headRig.weight = bodyWeights;
  188. chestRig.weight = bodyWeights;
  189. }
  190. weaponAim.position = Vector3.Lerp(weaponAim.position, guns[curGunIndex].aiming_rightHandPosition.position, 0.1f);
  191. weaponAim.rotation = Quaternion.Lerp(weaponAim.rotation, guns[curGunIndex].aiming_rightHandPosition.rotation, 0.1f);
  192. }
  193. else
  194. {
  195. if (bodyWeights >= 0)
  196. {
  197. bodyWeights -= 0.05f;
  198. //if (anim.GetBool("running")) { headRig.weight = bodyWeights; }
  199. headRig.weight = bodyWeights;
  200. chestRig.weight = bodyWeights;
  201. }
  202. weaponAim.position = Vector3.Lerp(weaponAim.position, guns[curGunIndex].holding_rightHandPosition.position, 0.1f);
  203. weaponAim.rotation = Quaternion.Lerp(weaponAim.rotation, guns[curGunIndex].holding_rightHandPosition.rotation, 0.1f);
  204. }
  205. }
  206. else
  207. {
  208. }
  209. }
  210. public void callToggleWeaponEquipment(bool value)
  211. {
  212. if (isServer)
  213. {
  214. toggleWeaponEquipment(value);
  215. RpcToggleWeaponEquipment(value);
  216. }
  217. else
  218. {
  219. CmdToggleWeaponEquipment(value);
  220. }
  221. }
  222. [Command]
  223. void CmdToggleWeaponEquipment(bool value)
  224. {
  225. toggleWeaponEquipment(value);
  226. RpcToggleWeaponEquipment(value);
  227. }
  228. [ClientRpc]
  229. void RpcToggleWeaponEquipment(bool value)
  230. {
  231. if (!isLocalPlayer) { return; }
  232. toggleWeaponEquipment(value);
  233. }
  234. public void toggleWeaponEquipment(bool value)
  235. {
  236. if (value)
  237. {
  238. leftArm.weight = 1;
  239. rightArm.weight = 1;
  240. callToggleAim(false);
  241. weaponAim.gameObject.SetActive(true);
  242. }
  243. else
  244. {
  245. //headRig.weight = 0;
  246. chestRig.weight = 0;
  247. leftArm.weight = 0;
  248. rightArm.weight = 0;
  249. weaponAim.gameObject.SetActive(false);
  250. }
  251. }
  252. public void callToggleAim(bool value)
  253. {
  254. if (isServer)
  255. {
  256. aiming = value;
  257. }
  258. else
  259. {
  260. CmdToggleAim(value);
  261. }
  262. }
  263. void OnAimChanged(bool oldAim, bool newAim)
  264. {
  265. if (newAim)
  266. {
  267. // headRig.weight = 1;
  268. chestRig.weight = 1;
  269. weaponAim.position = guns[curGunIndex].aiming_rightHandPosition.position;
  270. weaponAim.rotation = guns[curGunIndex].aiming_rightHandPosition.rotation;
  271. }
  272. else
  273. {
  274. Debug.Log(anim.GetBool("running"));
  275. // headRig.weight =(anim.GetBool("running")) ?0 : 1;
  276. chestRig.weight = (anim.GetBool("running")) ? 0 : 1; ;
  277. weaponAim.position = guns[curGunIndex].holding_rightHandPosition.position;
  278. weaponAim.rotation = guns[curGunIndex].holding_rightHandPosition.rotation;
  279. }
  280. }
  281. [Command]
  282. void CmdToggleAim(bool value)
  283. {
  284. aiming = value;
  285. }
  286. /*
  287. Animation Codes
  288. 0 Idle Breathing
  289. 1 Walking
  290. 2 Running
  291. 3 Jumping
  292. */
  293. void CallChangeAnim(int code)
  294. {
  295. switch (code)
  296. {
  297. case 0:
  298. anim.SetBool("moving", false);
  299. anim.SetBool("running", false);
  300. anim.SetBool("jumping", false);
  301. break;
  302. case 1:
  303. anim.SetBool("moving", true);
  304. anim.SetBool("running", false);
  305. anim.SetBool("jumping", false);
  306. break;
  307. case 2:
  308. anim.SetBool("moving", true);
  309. anim.SetBool("running", true);
  310. anim.SetBool("jumping", false);
  311. break;
  312. case 3:
  313. anim.SetBool("jumping", true);
  314. break;
  315. }
  316. }
  317. ///Equipment change
  318. ///
  319. public void callChangeEquipment(string name)
  320. {
  321. //Get the id of weapon using name
  322. int gunId = -1;
  323. int _meleeIndex = -1;
  324. if (name == "")
  325. {
  326. callToggleWeaponEquipment(false);
  327. }
  328. else
  329. {
  330. ItemData itemData = null;
  331. ItemDatabase.Default.TryGetItem(name, out itemData);
  332. if (itemData != null)
  333. {
  334. gunId = getWeaponWithName(itemData.Name);
  335. }
  336. }
  337. if (gunId < 0)
  338. {
  339. //This means no weapon is there for that name, lets see on melees
  340. ItemData itemData = null;
  341. ItemDatabase.Default.TryGetItem(name, out itemData);
  342. if (itemData != null)
  343. {
  344. _meleeIndex = getMeleeWithName(itemData.Name);
  345. }
  346. }
  347. //UPDATE IT USING SYNCVAR
  348. if (isServer)
  349. {
  350. curGunIndex = gunId;
  351. meleeIndex = _meleeIndex;
  352. }
  353. else
  354. {
  355. SetCurGunId(gunId, _meleeIndex);
  356. }
  357. }
  358. [Command]
  359. void SetCurGunId(int gunId, int _meleeIndex)
  360. {
  361. curGunIndex = gunId;
  362. meleeIndex = _meleeIndex;
  363. }
  364. void OnEquipmentChanged(int oldIndex, int newIndex)
  365. {
  366. //Debug.Log("THE HOOK WORKS : " + newIndex);
  367. if (newIndex >= 0)
  368. {
  369. //not a melee so disable all melees
  370. foreach (tpsGunData gun in guns)
  371. {
  372. gun.gameObject.SetActive(false);
  373. }
  374. if (!isLocalPlayer)
  375. {
  376. guns[newIndex].gameObject.SetActive(true);
  377. }
  378. guns[newIndex].gameObject.SetActive(true);
  379. toggleWeaponEquipment(true);
  380. }
  381. else
  382. {
  383. toggleWeaponEquipment(false);
  384. }
  385. }
  386. void OnMeleeChanged(int oldIndex, int newIndex)
  387. {
  388. Debug.Log("Melee Hook Works");
  389. foreach (meleeWeaponData melee in melees)
  390. {
  391. melee.gameObject.SetActive(false);
  392. }
  393. if (newIndex >= 0)
  394. {
  395. //some melee weeapon
  396. melees[newIndex].gameObject.SetActive(true);
  397. }
  398. else
  399. {
  400. //bare hands
  401. }
  402. }
  403. ///Shoot
  404. ///
  405. public void callShoot(Vector3 hitPoint, Vector3 hitNormals)
  406. {
  407. callToggleAim(true);
  408. aimHeightTarget.position = aimHeightTarget.position + new Vector3(0, 0.2f, 0);
  409. if (isServer)
  410. {
  411. broadcastShot(hitPoint, hitNormals);
  412. RpcBroadcastShot(hitPoint, hitNormals);
  413. }
  414. else
  415. {
  416. CmdBroadcastShot(hitPoint, hitNormals);
  417. }
  418. }
  419. [Command]
  420. void CmdBroadcastShot(Vector3 hitPoint, Vector3 hitNormals)
  421. {
  422. broadcastShot(hitPoint, hitNormals);
  423. RpcBroadcastShot(hitPoint, hitNormals);
  424. }
  425. [ClientRpc]
  426. void RpcBroadcastShot(Vector3 hitPoint, Vector3 hitNormals)
  427. {
  428. if (isServer) { return; }
  429. broadcastShot(hitPoint, hitNormals);
  430. }
  431. void broadcastShot(Vector3 hitPoint, Vector3 hitNormals)
  432. {
  433. // SurfaceEffects effect = (SurfaceEffects)Enum.Parse(typeof(SurfaceEffects), effectType);
  434. if (isLocalPlayer && !showLocalChar) { return; }
  435. SurfaceManager.SpawnEffect(0, SurfaceEffects.BulletHit, 1f, hitPoint, Quaternion.LookRotation(hitNormals));
  436. //shootingEffects
  437. if (curGunIndex >= 0)
  438. {
  439. guns[curGunIndex].triggerMuzzleFlash();
  440. }
  441. Debug.Log("Broadcasting shot");
  442. // weaponAnim.CrossFade("shot",0.1f);
  443. }
  444. void onUsedItem()
  445. {
  446. if (!isLocalPlayer) { return; }
  447. if (curGunIndex < 0)
  448. {
  449. if (meleeIndex >= 0)
  450. {
  451. anim.CrossFadeInFixedTime(melees[meleeIndex].animName, 0.01f);
  452. }
  453. else
  454. {
  455. anim.CrossFadeInFixedTime("punch", 0.01f);
  456. }
  457. }
  458. }
  459. int getWeaponWithName(string name)
  460. {
  461. for (int i = 0; i < guns.Length; i++)
  462. {
  463. tpsGunData gunData = guns[i];
  464. if (gunData.weaponName == name)
  465. {
  466. return i;
  467. }
  468. }
  469. return -1;
  470. }
  471. int getMeleeWithName(string name)
  472. {
  473. for (int i = 0; i < melees.Length; i++)
  474. {
  475. meleeWeaponData _melees = melees[i];
  476. if (_melees.weaponName == name)
  477. {
  478. return i;
  479. }
  480. }
  481. return -1;
  482. }
  483. public void callDamage(float damage)
  484. {
  485. RpcDamage(damage);
  486. }
  487. [ClientRpc]
  488. public void RpcDamage(float damage)
  489. {
  490. if (isLocalPlayer)
  491. {
  492. player.GetComponentInChildren<PlayerVitals>().Entity.ChangeHealth.Try(new HealthEventData(-damage));
  493. Debug.Log("You are local player, damaging");
  494. }
  495. else
  496. {
  497. Debug.Log("Nah you aint the local player, no damage for you sweetie");
  498. }
  499. }
  500. [Command(requiresAuthority = false)]
  501. public void CmdDmg(float damage)
  502. {
  503. RpcDamage(damage);
  504. Debug.Log("RPC damage");
  505. }
  506. public void hitboxDamage(HealthEventData damageData, shotType hitboxType)
  507. {
  508. float dmg = 0;
  509. switch (hitboxType)
  510. {
  511. case shotType.headshot:
  512. dmg = damageData.Delta * 5;
  513. break;
  514. case shotType.chestshot:
  515. dmg = damageData.Delta;
  516. break;
  517. case shotType.legshot:
  518. dmg = damageData.Delta / 2f;
  519. break;
  520. }
  521. dmg = -dmg;
  522. Debug.Log("Received damage from hitbox");
  523. if (!isServer)
  524. {
  525. CmdDmg(dmg); Debug.Log("Command damage " + dmg.ToString());
  526. }
  527. else
  528. {
  529. RpcDamage(dmg);
  530. }
  531. }
  532. public void callDeath()
  533. {
  534. Debug.Log("Calling Death");
  535. if (isServer)
  536. {
  537. death();
  538. RpcDeath();
  539. }
  540. else
  541. {
  542. CmdDeath();
  543. }
  544. }
  545. [Command]
  546. void CmdDeath()
  547. {
  548. death();
  549. RpcDeath();
  550. }
  551. [ClientRpc]
  552. void RpcDeath()
  553. {
  554. death();
  555. }
  556. void death()
  557. {
  558. // if (isLocalPlayer) { return; }
  559. playerModelParent.gameObject.SetActive(false);
  560. Instantiate(deadRagdoll, transform.position, transform.rotation);
  561. }
  562. public void callRespawn()
  563. {
  564. if (isServer)
  565. {
  566. Respawn();
  567. RpcRespawn();
  568. }
  569. else
  570. {
  571. CmdRespawn();
  572. }
  573. }
  574. [Command]
  575. void CmdRespawn()
  576. {
  577. Respawn();
  578. RpcRespawn();
  579. }
  580. [ClientRpc]
  581. void RpcRespawn()
  582. {
  583. Respawn();
  584. }
  585. void Respawn()
  586. {
  587. if (isLocalPlayer) { return; }
  588. playerModelParent.gameObject.SetActive(true);
  589. }
  590. }
  591. public static class netPlayerStats
  592. {
  593. public static netPlayer localPlayer;
  594. }