netPlayer.cs 13 KB

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