Profile.cs 5.0 KB

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