Combat.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TowerDevil
  5. {
  6. public enum Breed
  7. {
  8. Imp,
  9. Brute,
  10. Succubus,
  11. Armor,
  12. FireLord,
  13. Broodmother
  14. }
  15. public sealed class Foe
  16. {
  17. public static readonly string[] Art = { "foe_imp", "foe_brute", "foe_charmsucc",
  18. "foe_armor", "boss_fire", "boss_dragon" };
  19. static readonly int[] Health = { 26, 60, 42, 96, 520, 760 };
  20. static readonly float[] Speeds = { 1.05f, .78f, 1f, .60f, .42f, .36f };
  21. public readonly Breed Breed;
  22. public readonly int MaxHealth;
  23. public int Hp;
  24. public float Col, Row, Speed, Slow;
  25. public bool Boss => Breed >= Breed.FireLord;
  26. public int Lane => Mathf.Clamp(Mathf.FloorToInt(Col + .5f), 0, 5);
  27. public int CellRow => Row >= -.5f && Row < 5.5f ? Mathf.FloorToInt(Row + .5f) : -1;
  28. internal const float RunwayStart = 5.5f, RunwayEnd = 6.31f;
  29. public bool OnRunway => Row >= RunwayStart && Row <= RunwayEnd;
  30. public Foe(Breed breed, float col, float row, float multiplier)
  31. {
  32. Breed = breed;
  33. Col = col;
  34. Row = row;
  35. Hp = MaxHealth = Mathf.RoundToInt(Health[(int)breed] * multiplier);
  36. Speed = Speeds[(int)breed];
  37. }
  38. }
  39. public sealed partial class Run
  40. {
  41. readonly Queue<Foe> arrivals = new Queue<Foe>();
  42. readonly List<Foe> targets = new List<Foe>();
  43. public readonly List<Foe> Enemies = new List<Foe>();
  44. float elapsed, nextSpawn, emptyTime;
  45. public int WaveTotal { get; private set; }
  46. public int Remaining => arrivals.Count + Enemies.Count;
  47. public event Action<int, int, Foe, Piece> Fired;
  48. public event Action<int, int, Foe, Kind, int> Hit;
  49. public event Action GateHit;
  50. public event Action WaveCleared;
  51. public event Action<Foe> StruckGate;
  52. internal Foe ShotEnd { get; private set; }
  53. public string Forecast
  54. {
  55. get {
  56. int mod = Day % 10, scout = Profile.workshop[3];
  57. if (scout == 0 || (mod != 8 && mod != 9))
  58. return "";
  59. int d = Day + 10 - mod, lane = Rules.BossLane(d);
  60. string hint = scout == 3 ? "LANE " + (lane + 1)
  61. : scout == 2 ? (lane < 2 ? "LEFT"
  62. : lane < 4 ? "MID"
  63. : "RIGHT")
  64. : (lane < 3 ? "LEFT" : "RIGHT");
  65. return (d % 20 == 0 ? "BROODMOTHER" : "FIRE LORD") + " / " + hint + " / " +
  66. (mod == 9 ? "TOMORROW" : "IN 2 NIGHTS");
  67. }
  68. }
  69. internal void StartNight(int day, bool rest)
  70. {
  71. Day = day;
  72. Phase = rest ? Phase.Rest : Phase.Plan;
  73. Moves = 5 + Profile.workshop[0];
  74. Enemies.Clear();
  75. arrivals.Clear();
  76. elapsed = emptyTime = 0;
  77. if (day > lastPaidDay)
  78. {
  79. shardHalves += day - lastPaidDay;
  80. lastPaidDay = day;
  81. int paid = shardHalves / 2;
  82. Profile.shards += paid;
  83. EarnedShards += paid;
  84. shardHalves %= 2;
  85. }
  86. WaveTotal = Rules.WaveSize(day);
  87. nextSpawn = day % 10 == 0 ? .5f : 0f;
  88. for (int i = 0; i < WaveTotal; i++)
  89. {
  90. Breed breed = Breed.Imp;
  91. int lane;
  92. if (day % 10 == 0)
  93. {
  94. breed = day % 20 == 0 ? Breed.Broodmother : Breed.FireLord;
  95. lane = Rules.BossLane(day);
  96. }
  97. else
  98. {
  99. double roll = random.NextDouble();
  100. if (day >= 5 && roll > .93)
  101. breed = Breed.Armor;
  102. else if (day >= 2 && roll > .78)
  103. breed = Breed.Brute;
  104. else if (day >= 3 && roll > .60)
  105. breed = Breed.Succubus;
  106. lane = random.Next(6);
  107. }
  108. arrivals.Enqueue(new Foe(breed, lane, 9.6f, Rules.HealthScale(day)));
  109. }
  110. if (!rest)
  111. {
  112. Gravity(true);
  113. DeliverStock();
  114. }
  115. Profile.Save();
  116. }
  117. public void ChooseRest(bool rest)
  118. {
  119. if (Phase == Phase.RestChoice)
  120. StartNight(Day + 1, rest);
  121. }
  122. public bool BeginAttack()
  123. {
  124. if (!CanArrange)
  125. return false;
  126. Phase = Phase.Assault;
  127. return true;
  128. }
  129. public void Tick(float dt)
  130. {
  131. if (Phase != Phase.Assault || dt <= 0)
  132. return;
  133. elapsed += dt;
  134. while (arrivals.Count > 0 && nextSpawn <= elapsed)
  135. {
  136. Enemies.Add(arrivals.Dequeue());
  137. nextSpawn += .5f;
  138. }
  139. foreach (var foe in Enemies)
  140. {
  141. if (!foe.Boss)
  142. Drift(foe, dt);
  143. foe.Row -= foe.Speed * (foe.Slow > 0 ? .5f : 1f) * dt * 1.15f;
  144. foe.Slow = Mathf.Max(0, foe.Slow - dt);
  145. if (foe.Row > -.55f)
  146. continue;
  147. foe.Hp = 0;
  148. Plates = foe.Boss ? 0 : Math.Max(0, Plates - 1);
  149. StruckGate?.Invoke(foe);
  150. GateHit?.Invoke();
  151. if (Plates == 0)
  152. {
  153. Defeat();
  154. return;
  155. }
  156. }
  157. for (int r = 0; r < Size; r++)
  158. for (int c = 0; c < Size; c++)
  159. {
  160. Piece tower = Board[r, c];
  161. if (tower == null || !tower.Tower)
  162. continue;
  163. tower.Cooldown -= dt;
  164. if (tower.Cooldown <= 0)
  165. Fire(r, c, tower);
  166. }
  167. Enemies.RemoveAll(Dead);
  168. if (Remaining > 0)
  169. {
  170. emptyTime = 0;
  171. return;
  172. }
  173. emptyTime += dt;
  174. if (emptyTime < .55f)
  175. return;
  176. Score += 40 + Day * 15;
  177. if (Day % 10 == 0)
  178. Phase = Phase.RestChoice;
  179. else
  180. StartNight(Day + 1, false);
  181. WaveCleared?.Invoke();
  182. }
  183. static bool Dead(Foe f) => f.Hp <= 0;
  184. void Drift(Foe f, float dt)
  185. {
  186. float best = float.MaxValue, target = f.Col;
  187. for (int r = Mathf.Max(0, Mathf.CeilToInt(f.Row - 2.2f)); r <= Mathf.Min(5, Mathf.FloorToInt(f.Row + .35f));
  188. r++)
  189. for (int c = 0; c < Size; c++)
  190. {
  191. if (Board[r, c]?.Tower != true || Mathf.Abs(c - f.Col) > 2)
  192. continue;
  193. float distance = Mathf.Abs(c - f.Col) + (r - f.Row) * .55f;
  194. if (distance < best)
  195. {
  196. best = distance;
  197. target = c;
  198. }
  199. }
  200. f.Col = Mathf.MoveTowards(f.Col, target, .9f * dt);
  201. }
  202. internal void Fire(int r, int c, Piece tower)
  203. {
  204. targets.Clear();
  205. Foe nearest = null;
  206. float best = float.MaxValue;
  207. foreach (var f in Enemies)
  208. {
  209. if (f.Hp <= 0)
  210. continue;
  211. bool inRange = tower.Kind switch { Kind.Cannon => f.CellRow == r,
  212. Kind.Ballista => f.Lane == c && (f.CellRow >= 0 || f.OnRunway),
  213. Kind.Arrow => f.CellRow >= 0 && Mathf.Abs(f.CellRow - r) <= 1 &&
  214. Mathf.Abs(f.Lane - c) <= 1,
  215. _ => f.CellRow == r && f.Lane == c };
  216. if (!inRange)
  217. continue;
  218. targets.Add(f);
  219. float dx = f.Col - c, dy = f.Row - r;
  220. float d = tower.Kind == Kind.Cannon ? Mathf.Abs(dx)
  221. : tower.Kind == Kind.Ballista ? Mathf.Abs(dy)
  222. : dx * dx + dy * dy;
  223. bool tie = d == best && (tower.Kind == Kind.Cannon && dx > 0 || tower.Kind == Kind.Ballista && dy < 0);
  224. if (d < best || tie)
  225. {
  226. best = d;
  227. nearest = f;
  228. }
  229. }
  230. if (nearest == null)
  231. return;
  232. int index = (int)tower.Kind - (int)Kind.Cannon;
  233. tower.Cooldown = Rules.Cooldown[index];
  234. ShotEnd = nearest;
  235. if (tower.Kind == Kind.Cannon || tower.Kind == Kind.Ballista)
  236. {
  237. bool cannon = tower.Kind == Kind.Cannon;
  238. float side = 0, dist = float.MaxValue;
  239. foreach (var f in targets)
  240. {
  241. float delta = cannon ? f.Col - c : f.Row - r;
  242. if (delta == 0)
  243. continue;
  244. if (Mathf.Abs(delta) < dist || Mathf.Abs(delta) == dist && (cannon ? delta > 0 : delta < 0))
  245. {
  246. side = Mathf.Sign(delta);
  247. dist = Mathf.Abs(delta);
  248. }
  249. }
  250. if (side == 0)
  251. side = cannon ? (tower.Reversed ? 1 : -1) : 1;
  252. tower.Reversed = side > 0;
  253. float reach = 0;
  254. foreach (var f in targets)
  255. {
  256. float forward = (cannon ? f.Col - c : f.Row - r) * side;
  257. if (forward >= reach)
  258. {
  259. reach = forward;
  260. ShotEnd = f;
  261. }
  262. }
  263. Fired?.Invoke(r, c, nearest, tower);
  264. foreach (var f in targets)
  265. if ((cannon ? f.Col - c : f.Row - r) * side >= 0)
  266. Damage(r, c, tower, index, f);
  267. }
  268. else
  269. {
  270. Fired?.Invoke(r, c, nearest, tower);
  271. if (tower.Kind == Kind.Arrow)
  272. Damage(r, c, tower, index, nearest);
  273. else
  274. foreach (var f in targets)
  275. {
  276. f.Slow = 1;
  277. Hit?.Invoke(r, c, f, tower.Kind, 0);
  278. }
  279. }
  280. }
  281. void Damage(int r, int c, Piece tower, int index, Foe f)
  282. {
  283. int damage =
  284. Math.Max(1, Mathf.RoundToInt(Rules.Damage[index, tower.Tier - 1] * (1 + .08f * Profile.workshop[2]) *
  285. (f.Breed == Breed.Armor ? .7f : 1)));
  286. f.Hp -= damage;
  287. Hit?.Invoke(r, c, f, tower.Kind, damage);
  288. if (f.Hp > 0)
  289. return;
  290. Kills++;
  291. Score += f.Boss ? 500 : 18;
  292. Credit((f.Boss ? 120 : 4) * (Day > 10 ? 6 : 10));
  293. if (!f.Boss)
  294. return;
  295. Bosses++;
  296. Profile.shards += 5;
  297. EarnedShards += 5;
  298. Profile.Save();
  299. for (int i = 0; i < 8; i++)
  300. {
  301. int col = Mathf.Clamp(f.Lane + i % 2 - (i > 3 ? 1 : 0), 0, 5);
  302. var child = new Foe(i % 3 == 0 ? Breed.Succubus : Breed.Imp, col,
  303. Mathf.Clamp(f.Row + (i - 3.5f) * .14f, -.05f, 8.4f), Rules.HealthScale(Day) * .85f);
  304. child.Speed *= 1.18f;
  305. Enemies.Add(child);
  306. }
  307. WaveTotal += 8;
  308. }
  309. }
  310. }