Combat.cs 12 KB

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