using System; using System.Collections.Generic; using UnityEngine; namespace TowerDevil { [Serializable] public sealed class Profile { public string name = "Warden", auth = ""; public int avatar = 3, coins = 320, shards, bestNight, score, kills, forged, gateLevel = 1; public int[] workshop = new int[4], stock = new int[4]; public bool played, tutorialDone, sound = true, music = true, haptics = true; public long lastCoinAd, lastShardAd; public List ledger = new List(); [NonSerialized] public Action OnSave; public void Save() => OnSave?.Invoke(); public void Record(int delta, string reason) { ledger.Insert(0, (delta >= 0 ? "+" : "") + delta + " " + reason); if (ledger.Count > 30) ledger.RemoveRange(30, ledger.Count - 30); } public bool BuyUpgrade(int index) { int level = workshop[index]; var prices = Rules.UpgradeCosts[index]; if (level >= prices.Length || shards < prices[level]) return false; shards -= prices[level]; workshop[index]++; Save(); return true; } public bool BuyStoredChest(int tier) { int price = Rules.ChestPrice[tier - 1]; if (coins < price) return false; coins -= price; Record(-price, Rules.ChestNames[tier - 1]); stock[tier - 1]++; Save(); return true; } public static Profile Decode(string json) { var p = new Profile(); try { if (!string.IsNullOrEmpty(json)) JsonUtility.FromJsonOverwrite(json, p); } catch (ArgumentException) { return new Profile(); } Array.Resize(ref p.workshop, 4); Array.Resize(ref p.stock, 4); for (int i = 0; i < 4; i++) { p.workshop[i] = Mathf.Clamp(p.workshop[i], 0, Rules.UpgradeCosts[i].Length); p.stock[i] = Math.Max(0, p.stock[i]); } p.gateLevel = Mathf.Clamp(p.gateLevel, 1, 8); p.avatar = Mathf.Clamp(p.avatar, 0, 7); if (string.IsNullOrWhiteSpace(p.name)) p.name = "Warden"; p.ledger ??= new List(); return p; } } public enum Kind { Stone, Timber, Crystal, Ice, Gold, Cannon, Ballista, Arrow, Frost, Chest } public enum Phase { Plan, Assault, RestChoice, Rest, Over, Closed } public sealed class Piece { public readonly Kind Kind; public readonly int Tier; public float Cooldown; public bool Reversed; public Piece(Kind kind, int tier = 1) { Kind = kind; Tier = tier; } public bool Tower => Kind >= Kind.Cannon && Kind <= Kind.Frost; public bool Matchable => Tower ? Tier < 3 : Kind != Kind.Chest || Tier < 4; public string Art { get { if (Kind == Kind.Chest) return "chest" + Tier; string art = Kind.ToString().ToLowerInvariant() + (Tier > 1 ? Tier.ToString() : ""); if (Kind == Kind.Ballista && Reversed) art += "_d"; return art; } } } public static class Rules { public static readonly int[] ChestPrice = { 120, 360, 880, 2200 }, ChestMoves = { 2, 8, 26, 80 }; public static readonly string[] ChestNames = { "Wooden Chest", "Silver Chest", "Golden Chest", "Legendary Chest" }; public static readonly string[] WorkshopNames = { "MARCH", "VANGUARD", "TEMPER", "SCOUT" }; public static readonly int[][] UpgradeCosts = { new[] { 80, 240, 576 }, new[] { 40, 120, 288 }, new[] { 30, 84, 156, 252, 384 }, new[] { 30, 216, 504 } }; public static readonly float[] Cooldown = { 1.05f, 1f, .55f, .5f }; public static readonly int[,] Damage = { { 24, 36, 52 }, { 29, 45, 66 }, { 14, 22, 34 }, { 0, 0, 0 } }; public static float HealthScale(int d) => d <= 40 ? (1 + (d - 1) * .13f) * (d <= 9 ? 1.4f : 1) : 6.07f + 2.2f * (1 - Mathf.Exp(-(d - 40) / 45f)); public static int WaveSize(int d) => d % 10 == 0 ? 1 : d <= 40 ? 2 + Mathf.FloorToInt(d * .9f) : 38 + Mathf.RoundToInt(16 * (1 - Mathf.Exp(-(d - 40) / 40f))); static readonly int[] Lanes = { 2, 5, 0, 4, 1, 3 }; public static int BossLane(int d) => Lanes[(d / 10 - 1) % 6]; } }