playerNetwork.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
  4. using Assets.HeroEditor4D.Common.Scripts.Enums;
  5. using Mirror;
  6. using TMPro;
  7. using Firebase.Firestore;
  8. using Firebase.Extensions;
  9. using Unity.VisualScripting;
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. using GooglePlayGames;
  13. using UnityEngine.SocialPlatforms;
  14. using Firebase.Auth;
  15. using UnityEngine.UI;
  16. using Newtonsoft.Json;
  17. using Unity.Properties;
  18. public class playerNetwork : NetworkBehaviour
  19. {
  20. public const float ATTACK_COOLDOWN = 0.6f;
  21. [HideInInspector]
  22. public StatManager statManager;
  23. //public const int XPFORLEVEL = 10;
  24. [SyncVar(hook =nameof(OnHealthChanged))] public int health = 100;
  25. public Character4D character;
  26. public characterManager characterMan;
  27. [SyncVar(hook =nameof(OnDirectionChanged))]
  28. public Vector2 directionNetwork;
  29. [SyncVar(hook =nameof(OnAnimChanged))]
  30. public int animIntNetwork;
  31. [SyncVar(hook = nameof(onKillCountChange))]
  32. public int enemyKillCount;
  33. // public int xp { get{
  34. // int val = 0;
  35. // for(int i =5; i <= enemyKillCount; i+=5){
  36. // val = enemyKillCount;
  37. // }
  38. // return val;
  39. // }}
  40. [SyncVar(hook = nameof(OnXpChanged))]
  41. public int XP;
  42. public int lvl2 { get{
  43. return GetLevelForKills2(enemyKillCount);
  44. }}
  45. public int GetLevelForKills2(int kills){
  46. int val = 0;
  47. for(int i =10; i <= kills; i+=10){
  48. val ++;
  49. }
  50. return val;
  51. }
  52. public int lvl{
  53. get{
  54. return GetLevelByXp(XP);
  55. }
  56. }
  57. public int GetLevelByXp(int xp){
  58. // int level = Mathf.CeilToInt(Mathf.Sqrt(xp/100f));
  59. // if(level <= 0){level = 1;}
  60. // return level;
  61. int level = Mathf.CeilToInt(Mathf.Log(xp / 100f,2) + 1);
  62. if(level <=0){level = 1;}
  63. return level;
  64. // int level = 1;
  65. // int xpNeeded = 100;
  66. // while (xp >= xpNeeded)
  67. // {
  68. // level++;
  69. // xpNeeded = 100 * (int)Mathf.Pow(2, level - 1);
  70. // }
  71. // return level - 1;
  72. }
  73. public int GetXpForLevel(int level){
  74. if (level <= 0)
  75. level = 1;
  76. return Mathf.FloorToInt(100 * Mathf.Pow(2, level - 1));
  77. }
  78. public float XpSliderVal{
  79. get{
  80. int nextLevelXp = GetXpForLevel(lvl);
  81. int prevLevelXp = GetXpForLevel(lvl-1);
  82. if(nextLevelXp == prevLevelXp){
  83. prevLevelXp = 0;
  84. }
  85. int range = nextLevelXp - prevLevelXp;
  86. // Debug.Log($"{XP-prevLevelXp} / {range}");
  87. //Debug.Log($"next : {nextLevelXp}, prev: {prevLevelXp}, xp:{XP}, xpSince:{XP-prevLevelXp}");
  88. return ((float)(XP-prevLevelXp) / (float)range);
  89. // int nextLevel = lvl+1;
  90. // int curLevelXp = lvl * lvl;
  91. // int nextLevelXp = nextLevel * nextLevel;
  92. // // Debug.Log($"Xp Calc: {XP}-{curLevelXp}/{nextLevelXp} - {curLevelXp}");
  93. // // Debug.Log($"{curLevelXp-XP}/{nextLevelXp - curLevelXp}");
  94. // if(XP == 0){return 0;}
  95. // return 1f - ((float)(curLevelXp - XP) / (float)(nextLevelXp- curLevelXp));
  96. }
  97. }
  98. [SyncVar]
  99. public string playerName;
  100. [SyncVar]
  101. public int playerCoin;
  102. [SyncVar]
  103. public string myCharJson;
  104. public TMP_Text txtEnemyKillCount;
  105. public TMP_Text txtPlayerName;
  106. public TMP_Text questText;
  107. public GameObject questUI;
  108. public TMP_Text coinText;
  109. public TMP_Text xpText;
  110. public TMP_Text lvlText;
  111. public Slider xpSlider;
  112. public GameObject[] xpSliderSlots;
  113. public TMP_Text xpEnableTxt;
  114. //public TMP_Text attackDmgEnableTxt;
  115. public GameObject canvas;
  116. public Slider healthBar;
  117. public Slider armorBar;
  118. public Inventory inventory;
  119. public static Transform localPlayerTransform;
  120. public GameObject healthVfx;
  121. public GameObject damageVfx;
  122. public GameObject levelUpVfx;
  123. public QuestScriptable currentQuest;
  124. public List<QuestAction> questActions;
  125. public List<string> completedQuests;
  126. public GameObject projectile;
  127. public string selectedCharacterJson = CharacterSelection.selectedCharJson;
  128. public void QuestFunction(QuestScriptable questData){
  129. currentQuest = questData;
  130. questText.text = questData.questTitle;
  131. questUI.SetActive(true);
  132. foreach(QuestAction quest in questActions){
  133. if(quest.questData == questData){
  134. quest.activate();
  135. }
  136. }
  137. }
  138. public void CompleteQuest(QuestScriptable questData){
  139. if(questData != currentQuest){
  140. Debug.LogError("Completed a quest that wasnt even active");
  141. return;
  142. }
  143. completedQuests.Add(currentQuest.name);
  144. currentQuest = null;
  145. questText.text = "Quest Completed! Found 1000 coins from Cave-Chest";
  146. playerCoin += questData.rewardAmount;
  147. coinText.text = playerCoin.ToString();
  148. //add delay
  149. StartCoroutine(DelayUI());
  150. }
  151. IEnumerator DelayUI(){
  152. yield return new WaitForSecondsRealtime(10f);
  153. questUI.SetActive(false);
  154. }
  155. public static void registerQuestAction(QuestAction action){
  156. localPlayerTransform.GetComponent<playerNetwork>().questActions.Add(action);
  157. }
  158. void Start(){
  159. // for(int i =0; i < 2000; i+=10){
  160. // int level = GetLevelByXp(i);
  161. // int xp = GetXpForLevel(level);
  162. // Debug.Log($"{i} : {level} : {xp}");
  163. // }
  164. if(!isLocalPlayer){
  165. canvas.SetActive(false);
  166. if(!isServer){
  167. CmdRequestCharJson();
  168. }
  169. }else{
  170. localPlayerTransform = transform;
  171. cameraRPG.instance.SetTarget(transform);
  172. #if UNITY_EDITOR
  173. ResetHealthAndArmor();
  174. #else
  175. LoadPlayerData();
  176. #endif
  177. statManager.OnStatsChanged += ConfigArmorHealthSliders;
  178. if(isServer){
  179. playerName = gplayAuth.userNameCloud;
  180. myCharJson = CharacterSelection.selectedCharJson;
  181. RpcBroadcastCharJson(CharacterSelection.selectedCharJson);
  182. }
  183. else{
  184. CmdSetName(gplayAuth.userNameCloud);
  185. CmdSetCharJson(CharacterSelection.selectedCharJson);
  186. }
  187. }
  188. }
  189. void LoadCharFromJson(string json){
  190. if(json.Length <=0){return;}
  191. character.FromJson(json,true);
  192. }
  193. public void SavePlayerData(){
  194. #if UNITY_EDITOR
  195. return;
  196. #endif
  197. Debug.Log("*** Save Method Got Called ! ***");
  198. if(!isLoaded){
  199. Debug.Log("*** Save Method Return ***");
  200. return;
  201. }
  202. FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
  203. //int playerCoin = int.Parse(coins.text);
  204. Dictionary<string, object> saveValues = new Dictionary<string, object>{
  205. {"playerInventory" , JsonConvert.SerializeObject(inventory.inventoryManager.GetEntries())},
  206. {"playerHealth" , health},
  207. {"playerCoin", playerCoin},
  208. {"killCount", enemyKillCount},
  209. {"xp", XP},
  210. {"completedQuest", completedQuests},
  211. {"playerStats", statManager.PlayerStats},
  212. {"characterJson", selectedCharacterJson},
  213. };
  214. DocumentReference docRef = db.Collection("PlayerData").Document(gplayAuth.userID);
  215. docRef.SetAsync(saveValues).ContinueWithOnMainThread(task => {
  216. if(task.IsCompleted){
  217. Debug.Log("**** Save Completed Firestore ****");
  218. }
  219. else{
  220. Debug.Log("**** Failed to save data to firestore ****");
  221. }
  222. });
  223. }
  224. public bool isLoaded = false;
  225. public void LoadPlayerData(){
  226. #if UNITY_EDITOR
  227. return;
  228. #endif
  229. Debug.Log("**** Data Load method got called ****");
  230. FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
  231. DocumentReference docRef = db.Collection("PlayerData").Document(gplayAuth.userID);
  232. docRef.GetSnapshotAsync().ContinueWithOnMainThread(task => {
  233. DocumentSnapshot snapshot = task.Result;
  234. if(snapshot.Exists){
  235. Debug.Log("**** Found previous Data to load ****");
  236. //load data
  237. // Dictionary<string,object> dic = snapshot.ToDictionary(ServerTimestampBehavior.Estimate);
  238. // Debug.Log("Reading data");
  239. // foreach(KeyValuePair<string,object> item in dic){
  240. // Debug.Log(item.Key + " : " +item.Value.ToString());
  241. // }
  242. //saveNameTxt.text = snapshot.GetValue<string>("playerName");
  243. //load kills
  244. int _enemyKillCount = snapshot.GetValue<int>("killCount");
  245. SetEnemyKillCount(_enemyKillCount);
  246. //load XP
  247. XP = snapshot.GetValue<int>("xp");
  248. //Load coin
  249. int _playerCoin = snapshot.GetValue<int>("playerCoin");
  250. SetPlayerCoins(_playerCoin);
  251. // coinText.text = snapshot.GetValue<int>("playerCoin").ToString();
  252. //Load Health
  253. int savedHealth = snapshot.GetValue<int>("playerHealth");
  254. SetHealth(savedHealth);
  255. ResetHealthAndArmor();
  256. healthBar.value =(savedHealth);
  257. armorBar.value = savedHealth;
  258. //load Inventory
  259. Dictionary<int, string> inventoryGetData = JsonConvert.DeserializeObject<Dictionary<int, string>>(snapshot.GetValue<string>("playerInventory"));
  260. inventory.inventoryManager.SetInventory(inventoryGetData);
  261. completedQuests = snapshot.GetValue<List<string>>("completedQuest");
  262. //stats
  263. statManager.loadFromCloudSave(snapshot.GetValue<Dictionary<string, int>>("playerStats"));
  264. isLoaded = true;
  265. }else{
  266. //show error previous data doesnt exists to load
  267. Debug.Log("**** No previous data to load ****");
  268. isLoaded = true;
  269. }
  270. });
  271. }
  272. [Command]
  273. void CmdSetName(string nameValue){
  274. playerName = nameValue;
  275. }
  276. [Command(requiresAuthority =false)]
  277. void CmdRequestCharJson(){
  278. RpcBroadcastCharJson(myCharJson);
  279. }
  280. [Command]
  281. void CmdSetCharJson(string newValue){
  282. myCharJson = newValue;
  283. RpcBroadcastCharJson(newValue);
  284. LoadCharFromJson(newValue);
  285. }
  286. [ClientRpc]
  287. void RpcBroadcastCharJson(string newValue){
  288. LoadCharFromJson(newValue);
  289. }
  290. void OnDirectionChanged(Vector2 oldVal, Vector2 newVal){
  291. character.SetDirection(newVal);
  292. }
  293. void OnAnimChanged(int oldVal, int newVal){
  294. if(isLocalPlayer){return;}
  295. character.AnimationManager.SetState((CharacterState)newVal);
  296. }
  297. float attackTimer = 0;
  298. [HideInInspector]
  299. public PlayerAttack playerAttack;
  300. void Update(){
  301. if(isLocalPlayer){
  302. if(attackTimer >0){attackTimer-=Time.deltaTime;}
  303. if(isServer){
  304. SetAnimationData(character.Direction, character.Animator.GetInteger("State"));
  305. }else{
  306. CmdUpdateAnim(character.Direction, character.Animator.GetInteger("State"));
  307. }
  308. healthBar.value =(health);
  309. armorBar.value = health;
  310. txtEnemyKillCount.text = enemyKillCount.ToString();
  311. coinText.text = playerCoin.ToString();
  312. txtPlayerName.text = gplayAuth.userNameCloud;
  313. }
  314. ShowXP();
  315. ShowLevel();
  316. }
  317. [Command]
  318. void CmdUpdateAnim(Vector2 direction, int animState){
  319. SetAnimationData(direction, animState);
  320. }
  321. void SetAnimationData(Vector2 direction, int animState){
  322. directionNetwork = direction;
  323. animIntNetwork = animState;
  324. if(!isLocalPlayer){
  325. character.AnimationManager.SetState((CharacterState)animState);
  326. character.SetDirection(direction);
  327. }
  328. }
  329. void OnHealthChanged(int oldVal, int newVal){
  330. if(!isLocalPlayer){return;}
  331. //
  332. if(oldVal < newVal){
  333. GameObject newObject = Instantiate(healthVfx , character.characterTransform());
  334. newObject.transform.localPosition = Vector3.zero;
  335. newObject.transform.parent = transform;
  336. //StartCoroutine (Couroutine_autoDisableVFX(newObject));
  337. vfxScript vfxSc = newObject.AddComponent<vfxScript>();
  338. }else if (oldVal > newVal ){
  339. //damage VFX
  340. GameObject newObject = Instantiate(damageVfx , character.characterTransform());
  341. newObject.transform.localPosition = new Vector3(0, 5f, 0);
  342. newObject.transform.parent = transform;
  343. //StartCoroutine (Couroutine_autoDisableVFX(newObject));
  344. vfxScript vfxSc = newObject.AddComponent<vfxScript>();
  345. // vfxSc.offsetVFX = new Vector3(0, 5f, 0);
  346. // vfxSc.target = character.characterTransform();
  347. }
  348. SavePlayerData();
  349. healthBar.value =(newVal);
  350. armorBar.value = newVal;
  351. }
  352. // IEnumerator Couroutine_autoDisableVFX(GameObject go){
  353. // yield return new WaitForSecondsRealtime(5f);
  354. // Destroy(go);
  355. // }
  356. void onKillCountChange(int oldval, int newval){
  357. if(!isLocalPlayer){return;}
  358. // int prevLevel = GetLevelForKills(oldval);
  359. // int newLevel = GetLevelForKills(newval);
  360. SavePlayerData();
  361. // if(newLevel > prevLevel){
  362. // StartCoroutine(uiTxtDelay(25f));
  363. // }
  364. // if(enemyKillCount == 5 ){
  365. // //SavePlayerData();
  366. // //QuestComplete();
  367. // AddCoin();
  368. // }
  369. }
  370. void OnXpChanged(int oldVal, int newVal){
  371. if(!isLocalPlayer){return;}
  372. int prevLevel = GetLevelByXp(oldVal);
  373. int newLevle = GetLevelByXp(newVal);
  374. if(newLevle > prevLevel){
  375. int levelChange = newLevle - prevLevel;
  376. GameObject newObject = Instantiate(levelUpVfx , character.characterTransform());
  377. newObject.transform.localPosition = Vector3.zero;
  378. newObject.transform.parent = transform;
  379. StartCoroutine(uiTxtDelay(25f, levelChange));
  380. }
  381. }
  382. public void SetHealth(int newvalue){
  383. if(isServer){
  384. health = newvalue;
  385. healthBar.value =(newvalue);
  386. armorBar.value = newvalue;
  387. }else{
  388. CmdSetHealth(newvalue);
  389. }
  390. }
  391. public void SetEnemyKillCount(int newValue){
  392. if(isServer){
  393. enemyKillCount = newValue;
  394. }else{
  395. CmdSetEnemyKillCount(newValue);
  396. }
  397. }
  398. public void SetPlayerCoins(int newVal){
  399. if(isServer){
  400. playerCoin = newVal;
  401. }else{
  402. CmdSetPlayerCoin(newVal);
  403. }
  404. }
  405. [Command]
  406. void CmdSetPlayerCoin(int newVal){
  407. playerCoin = newVal;
  408. }
  409. [Command]
  410. void CmdSetEnemyKillCount(int newValue){
  411. enemyKillCount = newValue;
  412. }
  413. [Command]
  414. void CmdSetHealth(int newValue){
  415. health = newValue;
  416. }
  417. public void TakeDamage(int attackDamage){
  418. serverTakeDmg(attackDamage);
  419. // if(isLocalPlayer){
  420. // takedmg(attackDamage);
  421. // }else if(isServer){
  422. // RpcTakeDamage(attackDamage);
  423. // }
  424. }
  425. void serverTakeDmg(int damage){
  426. health -= damage;
  427. if(health <=0){
  428. RpcDeath();
  429. death();
  430. }
  431. }
  432. float xpTimer = 0;
  433. public void ShowXP(){
  434. if(xpTimer >0){xpTimer -= Time.deltaTime;return;}
  435. xpTimer = 1;
  436. xpText.text = (Mathf.RoundToInt(XP/100f) * 100f).ToString();
  437. xpSlider.value = XpSliderVal;
  438. for(int i=0; i < 10; i++){
  439. float val = (float)i /10f;
  440. xpSliderSlots[i].SetActive(xpSlider.value > val);
  441. }
  442. }
  443. public void ShowLevel(){
  444. lvlText.text = lvl.ToString();
  445. }
  446. public void OnEnemyKilled(int enemyLevel){
  447. //disable take damage and disable healthbar going backwards
  448. int prevValue = lvl;
  449. // SavePlayerData();
  450. enemyKillCount++;
  451. //XP += (int)(enemyScript.XP_GAIN * (enemyLevel/2f));
  452. XP += enemyScript.XP_GAIN_Base + Mathf.FloorToInt(enemyScript.XP_GAIN * (enemyLevel-1));
  453. }
  454. IEnumerator uiTxtDelay (float delayTime, int levelChange){
  455. //enable
  456. xpEnableTxt.gameObject.SetActive(true);
  457. //int attackDamageChange= 5 * levelChange;
  458. //attackDmgEnableTxt.gameObject.SetActive(true);
  459. //attackDmgEnableTxt.text = "Attack Damage + " + attackDamageChange;
  460. yield return new WaitForSecondsRealtime(delayTime);
  461. //disable
  462. xpEnableTxt.gameObject.SetActive(false);
  463. //attackDmgEnableTxt.gameObject.SetActive(false);
  464. }
  465. // public void QuestComplete(){
  466. // //task completion logic
  467. // // Strikethrough the text
  468. // //questText.text = "<s>Kill 5 Enemies to claim 100Golds</s> Completed";
  469. // Debug.Log("First quest completed");
  470. // }
  471. public void AddCoin(){
  472. playerCoin += 100;
  473. coinText.text = playerCoin.ToString();
  474. }
  475. [ClientRpc]
  476. public void RpcDeath(){
  477. death();
  478. }
  479. public bool isDead = false;
  480. public void death(){
  481. Debug.Log("Death called");
  482. character.AnimationManager.Die();
  483. isDead = true;
  484. StartCoroutine(CouroutineWaitDeath());
  485. // throw new System.Exception();
  486. }
  487. IEnumerator CouroutineWaitDeath (){
  488. yield return new WaitForSecondsRealtime(3);
  489. isDead = false;
  490. playerRespawn();
  491. }
  492. public void OnAttack(){
  493. if(attackTimer > 0){
  494. return;
  495. }
  496. characterMan.SetActiveWeapon(78);
  497. attackTimer = ATTACK_COOLDOWN;
  498. if(isLocalPlayer){
  499. PlayAttackAnim();
  500. playerAttack.Attack(false);
  501. if(isServer){
  502. RpcPlayAttackAnim();
  503. }else{
  504. CmdPlayAttackAnim();
  505. }
  506. }
  507. }
  508. public int MagicAttackWeaponIndex = 52;
  509. public void OnMagicAttack(){
  510. if(attackTimer > 0){
  511. return;
  512. }
  513. attackTimer = ATTACK_COOLDOWN;
  514. characterMan.SetActiveWeapon(MagicAttackWeaponIndex);
  515. if(isLocalPlayer){
  516. PlayAttackAnim();
  517. //?
  518. playerAttack.MagicalAttack();
  519. playerAttack.Attack(true);
  520. if(isServer){
  521. RpcPlayAttackAnim();
  522. }else{
  523. CmdPlayAttackAnim();
  524. }
  525. }
  526. }
  527. [Command]
  528. void CmdPlayAttackAnim(){
  529. PlayAttackAnim();
  530. RpcPlayAttackAnim();
  531. }
  532. [ClientRpc]
  533. void RpcPlayAttackAnim(){
  534. if(isLocalPlayer){return;}
  535. PlayAttackAnim();
  536. }
  537. void PlayAttackAnim(){
  538. switch (character.WeaponType)
  539. {
  540. case WeaponType.Melee1H:
  541. case WeaponType.Paired:
  542. character.AnimationManager.Slash(twoHanded: false);
  543. break;
  544. case WeaponType.Melee2H:
  545. character.AnimationManager.Slash(twoHanded: true);
  546. break;
  547. case WeaponType.Bow:
  548. character.AnimationManager.ShotBow();
  549. break;
  550. }
  551. }
  552. public void playerRespawn(){
  553. Debug.Log("Respawning");
  554. // healthBar.SetMaxHealth(statManager.GetEffectiveValue("health"));
  555. ResetHealthAndArmor();
  556. healthBar.value = health;
  557. character.AnimationManager.SetState(CharacterState.Idle);
  558. Transform newSpawnLocationPlayer = GameManager.instance.spawnPointsPlayer.GetChild(Random.Range(0,GameManager.instance.spawnPointsPlayer.childCount));
  559. transform.position = newSpawnLocationPlayer.position;
  560. }
  561. void ResetHealthAndArmor(){
  562. healthBar.maxValue = statManager.GetEffectiveValue("health");
  563. health = statManager.GetEffectiveValue("health") + statManager.GetEffectiveValue("defence");
  564. // Debug.Log($"Setting armor bar, maxVal:{health}, minVal:{healthBar.maxValue}, val:{health}");
  565. armorBar.maxValue = health;
  566. armorBar.minValue = healthBar.maxValue;
  567. armorBar.value = health;
  568. }
  569. void ConfigArmorHealthSliders(){
  570. healthBar.maxValue = statManager.GetEffectiveValue("health");
  571. float maxHealth = statManager.GetEffectiveValue("health") + statManager.GetEffectiveValue("defence");
  572. // Debug.Log($"Setting armor bar, maxVal:{health}, minVal:{healthBar.maxValue}, val:{health}");
  573. armorBar.maxValue = maxHealth;
  574. armorBar.minValue = healthBar.maxValue;
  575. armorBar.value = health;
  576. healthBar.value = health;
  577. }
  578. //Pickup
  579. public void PickupObject(pickup item){
  580. if(!isServer){ Debug.LogError("Cant call command on client, 403"); return; }
  581. if(isLocalPlayer){
  582. pickupObject(item.lootData.type);
  583. }else{
  584. RpcPickupObject(item.lootData.type);
  585. }
  586. }
  587. [ClientRpc]
  588. void RpcPickupObject(string type){
  589. if(isLocalPlayer){
  590. pickupObject(type);
  591. }
  592. }
  593. void pickupObject(string type){
  594. inventory.AddItem(type);
  595. }
  596. public void DropPickup(string type){
  597. if(isServer){
  598. GameManager.instance.SpawnPickup(type,transform.position + new Vector3(0.85f,0.6f));
  599. }else{
  600. CmdDropPickup(type);
  601. }
  602. }
  603. [Command]
  604. void CmdDropPickup(string type){
  605. GameManager.instance.SpawnPickup(type,transform.position + new Vector3(4,0));
  606. }
  607. int magicalDmg = 0;
  608. public void MagicalAttack(Vector3 direction, float magicalProjectileSpawnOffset, int dmg)
  609. {
  610. if (isServer)
  611. {
  612. magicalAttack(direction, magicalProjectileSpawnOffset, dmg);
  613. }
  614. else
  615. {
  616. CmdMagicalAttack(direction, magicalProjectileSpawnOffset, dmg);
  617. }
  618. }
  619. [Command]
  620. void CmdMagicalAttack(Vector3 direction, float magicalProjectileSpawnOffset, int dmg)
  621. {
  622. magicalAttack(direction, magicalProjectileSpawnOffset, dmg);
  623. }
  624. void magicalAttack(Vector3 direction, float magicalProjectileSpawnOffset, int dmg)
  625. {
  626. magicalDmg = dmg;
  627. GameObject projectileSpawned = Instantiate(projectile, transform.position + (direction * magicalProjectileSpawnOffset), Quaternion.identity);
  628. RangeProjectile _projectile = projectileSpawned.GetComponent<RangeProjectile>();
  629. _projectile.direction = direction;
  630. _projectile.OnHit.AddListener(OnMagicalHit);
  631. _projectile.shooterId = netId;
  632. NetworkServer.Spawn(projectileSpawned);
  633. }
  634. void OnMagicalHit(enemyScript victim)
  635. {
  636. int damageamount = magicalDmg + (lvl * 5);
  637. victim.TakeMagicalDamage(damageamount, netId);
  638. }
  639. public void GoBackMenu(){
  640. startClient.instance.networkManager.StopClient();
  641. SceneManager.LoadScene("GameLogin");
  642. #if UNITY_EDITOR || UNITY_SERVER || UNITY_STANDALONE_WIN
  643. #else
  644. PlayGamesPlatform.Instance.SignOut();
  645. Firebase.Auth.FirebaseAuth.DefaultInstance.SignOut();
  646. #endif
  647. }
  648. }