Profile.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TowerDevil
  5. {
  6. [Serializable]
  7. public sealed class GameCenterStanding
  8. {
  9. public string playerID;
  10. public int bestNight, totalScore;
  11. }
  12. [Serializable]
  13. public sealed class Profile
  14. {
  15. public string name = "Warden", auth = "";
  16. public int avatar = 3, coins = 320, shards, bestNight, score, kills, forged, gateLevel = 1;
  17. public int[] workshop = new int[4], stock = new int[4];
  18. public bool played, tutorialDone, sound = true, music = true, haptics = true;
  19. public long lastCoinAd, lastShardAd;
  20. public List<string> ledger = new List<string>();
  21. public List<GameCenterStanding> gameCenterStandings = new List<GameCenterStanding>();
  22. public string gameCenterPlayer;
  23. internal GameCenterStanding CurrentStanding => gameCenterStandings.Find(s => s?.playerID == gameCenterPlayer);
  24. internal void SelectGameCenterPlayer(string playerID)
  25. {
  26. gameCenterPlayer = playerID;
  27. if (CurrentStanding != null) return;
  28. bool first = gameCenterStandings.Count == 0;
  29. gameCenterStandings.Add(new GameCenterStanding { playerID = playerID,
  30. bestNight = first ? bestNight : 0, totalScore = first ? score : 0 });
  31. }
  32. [NonSerialized]
  33. public Action OnSave;
  34. public void Save() => OnSave?.Invoke();
  35. public void Record(int delta, string reason)
  36. {
  37. ledger.Insert(0, (delta >= 0 ? "+" : "") + delta + " " + reason);
  38. if (ledger.Count > 30)
  39. ledger.RemoveRange(30, ledger.Count - 30);
  40. }
  41. public bool BuyUpgrade(int index)
  42. {
  43. int level = workshop[index];
  44. var prices = Rules.UpgradeCosts[index];
  45. if (level >= prices.Length || shards < prices[level])
  46. return false;
  47. shards -= prices[level];
  48. workshop[index]++;
  49. Save();
  50. return true;
  51. }
  52. public bool BuyStoredChest(int tier)
  53. {
  54. int price = Rules.ChestPrice[tier - 1];
  55. if (coins < price)
  56. return false;
  57. coins -= price;
  58. Record(-price, Rules.ChestNames[tier - 1]);
  59. stock[tier - 1]++;
  60. Save();
  61. return true;
  62. }
  63. public static Profile Decode(string json)
  64. {
  65. var p = new Profile();
  66. try
  67. {
  68. if (!string.IsNullOrEmpty(json))
  69. JsonUtility.FromJsonOverwrite(json, p);
  70. }
  71. catch (ArgumentException)
  72. {
  73. return new Profile();
  74. }
  75. Array.Resize(ref p.workshop, 4);
  76. Array.Resize(ref p.stock, 4);
  77. for (int i = 0; i < 4; i++)
  78. {
  79. p.workshop[i] = Mathf.Clamp(p.workshop[i], 0, Rules.UpgradeCosts[i].Length);
  80. p.stock[i] = Math.Max(0, p.stock[i]);
  81. }
  82. p.gateLevel = Mathf.Clamp(p.gateLevel, 1, 8);
  83. p.avatar = Mathf.Clamp(p.avatar, 0, 7);
  84. if (string.IsNullOrWhiteSpace(p.name))
  85. p.name = "Warden";
  86. p.ledger ??= new List<string>();
  87. p.gameCenterStandings ??= new List<GameCenterStanding>();
  88. return p;
  89. }
  90. }
  91. public enum Kind
  92. {
  93. Stone,
  94. Timber,
  95. Crystal,
  96. Ice,
  97. Gold,
  98. Cannon,
  99. Ballista,
  100. Arrow,
  101. Frost,
  102. Chest
  103. }
  104. public enum Phase
  105. {
  106. Plan,
  107. Assault,
  108. RestChoice,
  109. Rest,
  110. Over,
  111. Closed
  112. }
  113. public sealed class Piece
  114. {
  115. public readonly Kind Kind;
  116. public readonly int Tier;
  117. public float Cooldown;
  118. public bool Reversed;
  119. public Piece(Kind kind, int tier = 1)
  120. {
  121. Kind = kind;
  122. Tier = tier;
  123. }
  124. public bool Tower => Kind >= Kind.Cannon && Kind <= Kind.Frost;
  125. public bool Matchable => Tower ? Tier < 3 : Kind != Kind.Chest || Tier < 4;
  126. public string Art
  127. {
  128. get {
  129. if (Kind == Kind.Chest)
  130. return "chest" + Tier;
  131. string art = Kind.ToString().ToLowerInvariant() + (Tier > 1 ? Tier.ToString() : "");
  132. if (Kind == Kind.Ballista && Reversed)
  133. art += "_d";
  134. return art;
  135. }
  136. }
  137. }
  138. public static class Rules
  139. {
  140. public static readonly int[] ChestPrice = { 120, 360, 880, 2200 }, ChestMoves = { 2, 8, 26, 80 };
  141. public static readonly string[] ChestNames = { "Wooden Chest", "Silver Chest", "Golden Chest",
  142. "Legendary Chest" };
  143. public static readonly string[] WorkshopNames = { "MARCH", "VANGUARD", "TEMPER", "SCOUT" };
  144. public static readonly int[][] UpgradeCosts = { new[] { 80, 240, 576 }, new[] { 40, 120, 288 },
  145. new[] { 30, 84, 156, 252, 384 }, new[] { 30, 216, 504 } };
  146. public static readonly float[] Cooldown = { 1.05f, 1f, .55f, .5f };
  147. public static readonly int[,] Damage = { { 24, 36, 52 }, { 29, 45, 66 }, { 14, 22, 34 }, { 0, 0, 0 } };
  148. public static float HealthScale(int d) => d <= 40 ? (1 + (d - 1) * .13f) * (d <= 9 ? 1.4f : 1)
  149. : 6.07f + 2.2f * (1 - Mathf.Exp(-(d - 40) / 45f));
  150. public static int WaveSize(int d) => d % 10 == 0 ? 1
  151. : d <= 40 ? 2 + Mathf.FloorToInt(d * .9f)
  152. : 38 + Mathf.RoundToInt(16 * (1 - Mathf.Exp(-(d - 40) / 40f)));
  153. static readonly int[] Lanes = { 2, 5, 0, 4, 1, 3 };
  154. public static int BossLane(int d) => Lanes[(d / 10 - 1) % 6];
  155. }
  156. }