netPlayer.cs 14 KB

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