netPlayer.cs 16 KB

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