playerNetwork.cs 23 KB

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