| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace TowerDevil
- {
- public enum Breed
- {
- Imp,
- Brute,
- Succubus,
- Armor,
- FireLord,
- Broodmother
- }
- public sealed class Foe
- {
- public static readonly string[] Art = { "foe_imp", "foe_brute", "foe_charmsucc",
- "foe_armor", "boss_fire", "boss_dragon" };
- static readonly int[] Health = { 26, 60, 42, 96, 520, 760 };
- static readonly float[] Speeds = { 1.05f, .78f, 1f, .60f, .42f, .36f };
- public readonly Breed Breed;
- public readonly int MaxHealth;
- public int Hp;
- public float Col, Row, Speed, Slow;
- public bool Boss => Breed >= Breed.FireLord;
- public int Lane => Mathf.Clamp(Mathf.FloorToInt(Col + .5f), 0, 5);
- public int CellRow => Row >= -.5f && Row < 5.5f ? Mathf.FloorToInt(Row + .5f) : -1;
- internal const float RunwayStart = 5.5f, RunwayEnd = 6.31f;
- public bool OnRunway => Row >= RunwayStart && Row <= RunwayEnd;
- public Foe(Breed breed, float col, float row, float multiplier)
- {
- Breed = breed;
- Col = col;
- Row = row;
- Hp = MaxHealth = Mathf.RoundToInt(Health[(int)breed] * multiplier);
- Speed = Speeds[(int)breed];
- }
- }
- public sealed partial class Run
- {
- readonly Queue<Foe> arrivals = new Queue<Foe>();
- readonly List<Foe> targets = new List<Foe>();
- public readonly List<Foe> Enemies = new List<Foe>();
- float elapsed, nextSpawn, emptyTime;
- public int WaveTotal { get; private set; }
- public int Remaining => arrivals.Count + Enemies.Count;
- public event Action<int, int, Foe, Piece> Fired;
- public event Action<int, int, Foe, Kind, int> Hit;
- public event Action GateHit;
- public event Action<Foe> StruckGate;
- internal Foe ShotEnd { get; private set; }
- public string Forecast
- {
- get {
- int mod = Day % 10, scout = Profile.workshop[3];
- if (scout == 0 || (mod != 8 && mod != 9))
- return "";
- int d = Day + 10 - mod, lane = Rules.BossLane(d);
- string hint = scout == 3 ? "LANE " + (lane + 1)
- : scout == 2 ? (lane < 2 ? "LEFT"
- : lane < 4 ? "MID"
- : "RIGHT")
- : (lane < 3 ? "LEFT" : "RIGHT");
- return (d % 20 == 0 ? "BROODMOTHER" : "FIRE LORD") + " / " + hint + " / " +
- (mod == 9 ? "TOMORROW" : "IN 2 NIGHTS");
- }
- }
- internal void StartNight(int day, bool rest)
- {
- Day = day;
- Phase = rest ? Phase.Rest : Phase.Plan;
- Moves = 5 + Profile.workshop[0];
- Enemies.Clear();
- arrivals.Clear();
- elapsed = emptyTime = 0;
- if (day > lastPaidDay)
- {
- shardHalves += day - lastPaidDay;
- lastPaidDay = day;
- int paid = shardHalves / 2;
- Profile.shards += paid;
- EarnedShards += paid;
- shardHalves %= 2;
- }
- WaveTotal = Rules.WaveSize(day);
- nextSpawn = day % 10 == 0 ? .5f : 0f;
- for (int i = 0; i < WaveTotal; i++)
- {
- Breed breed = Breed.Imp;
- int lane;
- if (day % 10 == 0)
- {
- breed = day % 20 == 0 ? Breed.Broodmother : Breed.FireLord;
- lane = Rules.BossLane(day);
- }
- else
- {
- double roll = random.NextDouble();
- if (day >= 5 && roll > .93)
- breed = Breed.Armor;
- else if (day >= 2 && roll > .78)
- breed = Breed.Brute;
- else if (day >= 3 && roll > .60)
- breed = Breed.Succubus;
- lane = random.Next(6);
- }
- arrivals.Enqueue(new Foe(breed, lane, 9.6f, Rules.HealthScale(day)));
- }
- if (!rest)
- {
- Gravity(true);
- DeliverStock();
- }
- Profile.Save();
- }
- public void ChooseRest(bool rest)
- {
- if (Phase == Phase.RestChoice)
- StartNight(Day + 1, rest);
- }
- public bool BeginAttack()
- {
- if (!CanArrange)
- return false;
- Phase = Phase.Assault;
- return true;
- }
- public void Tick(float dt)
- {
- if (Phase != Phase.Assault || dt <= 0)
- return;
- elapsed += dt;
- while (arrivals.Count > 0 && nextSpawn <= elapsed)
- {
- Enemies.Add(arrivals.Dequeue());
- nextSpawn += .5f;
- }
- foreach (var foe in Enemies)
- {
- if (!foe.Boss)
- Drift(foe, dt);
- foe.Row -= foe.Speed * (foe.Slow > 0 ? .5f : 1f) * dt * 1.15f;
- foe.Slow = Mathf.Max(0, foe.Slow - dt);
- if (foe.Row > -.55f)
- continue;
- foe.Hp = 0;
- Plates = foe.Boss ? 0 : Math.Max(0, Plates - 1);
- StruckGate?.Invoke(foe);
- GateHit?.Invoke();
- if (Plates == 0)
- {
- Defeat();
- return;
- }
- }
- for (int r = 0; r < Size; r++)
- for (int c = 0; c < Size; c++)
- {
- Piece tower = Board[r, c];
- if (tower == null || !tower.Tower)
- continue;
- tower.Cooldown -= dt;
- if (tower.Cooldown <= 0)
- Fire(r, c, tower);
- }
- Enemies.RemoveAll(Dead);
- if (Remaining > 0)
- {
- emptyTime = 0;
- return;
- }
- emptyTime += dt;
- if (emptyTime < .55f)
- return;
- Score += 40 + Day * 15;
- if (Day % 10 == 0)
- Phase = Phase.RestChoice;
- else
- StartNight(Day + 1, false);
- }
- static bool Dead(Foe f) => f.Hp <= 0;
- void Drift(Foe f, float dt)
- {
- float best = float.MaxValue, target = f.Col;
- for (int r = Mathf.Max(0, Mathf.CeilToInt(f.Row - 2.2f)); r <= Mathf.Min(5, Mathf.FloorToInt(f.Row + .35f));
- r++)
- for (int c = 0; c < Size; c++)
- {
- if (Board[r, c]?.Tower != true || Mathf.Abs(c - f.Col) > 2)
- continue;
- float distance = Mathf.Abs(c - f.Col) + (r - f.Row) * .55f;
- if (distance < best)
- {
- best = distance;
- target = c;
- }
- }
- f.Col = Mathf.MoveTowards(f.Col, target, .9f * dt);
- }
- internal void Fire(int r, int c, Piece tower)
- {
- targets.Clear();
- Foe nearest = null;
- float best = float.MaxValue;
- foreach (var f in Enemies)
- {
- if (f.Hp <= 0)
- continue;
- bool inRange = tower.Kind switch { Kind.Cannon => f.CellRow == r,
- Kind.Ballista => f.Lane == c && (f.CellRow >= 0 || f.OnRunway),
- Kind.Arrow => f.CellRow >= 0 && Mathf.Abs(f.CellRow - r) <= 1 &&
- Mathf.Abs(f.Lane - c) <= 1,
- _ => f.CellRow == r && f.Lane == c };
- if (!inRange)
- continue;
- targets.Add(f);
- float dx = f.Col - c, dy = f.Row - r;
- float d = tower.Kind == Kind.Cannon ? Mathf.Abs(dx)
- : tower.Kind == Kind.Ballista ? Mathf.Abs(dy)
- : dx * dx + dy * dy;
- bool tie = d == best && (tower.Kind == Kind.Cannon && dx > 0 || tower.Kind == Kind.Ballista && dy < 0);
- if (d < best || tie)
- {
- best = d;
- nearest = f;
- }
- }
- if (nearest == null)
- return;
- int index = (int)tower.Kind - (int)Kind.Cannon;
- tower.Cooldown = Rules.Cooldown[index];
- ShotEnd = nearest;
- if (tower.Kind == Kind.Cannon || tower.Kind == Kind.Ballista)
- {
- bool cannon = tower.Kind == Kind.Cannon;
- float side = 0, dist = float.MaxValue;
- foreach (var f in targets)
- {
- float delta = cannon ? f.Col - c : f.Row - r;
- if (delta == 0)
- continue;
- if (Mathf.Abs(delta) < dist || Mathf.Abs(delta) == dist && (cannon ? delta > 0 : delta < 0))
- {
- side = Mathf.Sign(delta);
- dist = Mathf.Abs(delta);
- }
- }
- if (side == 0)
- side = cannon ? (tower.Reversed ? 1 : -1) : 1;
- tower.Reversed = side > 0;
- float reach = 0;
- foreach (var f in targets)
- {
- float forward = (cannon ? f.Col - c : f.Row - r) * side;
- if (forward >= reach)
- {
- reach = forward;
- ShotEnd = f;
- }
- }
- Fired?.Invoke(r, c, nearest, tower);
- foreach (var f in targets)
- if ((cannon ? f.Col - c : f.Row - r) * side >= 0)
- Damage(r, c, tower, index, f);
- }
- else
- {
- Fired?.Invoke(r, c, nearest, tower);
- if (tower.Kind == Kind.Arrow)
- Damage(r, c, tower, index, nearest);
- else
- foreach (var f in targets)
- {
- f.Slow = 1;
- Hit?.Invoke(r, c, f, tower.Kind, 0);
- }
- }
- }
- void Damage(int r, int c, Piece tower, int index, Foe f)
- {
- int damage =
- Math.Max(1, Mathf.RoundToInt(Rules.Damage[index, tower.Tier - 1] * (1 + .08f * Profile.workshop[2]) *
- (f.Breed == Breed.Armor ? .7f : 1)));
- f.Hp -= damage;
- Hit?.Invoke(r, c, f, tower.Kind, damage);
- if (f.Hp > 0)
- return;
- Kills++;
- Score += f.Boss ? 500 : 18;
- Credit((f.Boss ? 120 : 4) * (Day > 10 ? 6 : 10));
- if (!f.Boss)
- return;
- Bosses++;
- Profile.shards += 5;
- EarnedShards += 5;
- Profile.Save();
- for (int i = 0; i < 8; i++)
- {
- int col = Mathf.Clamp(f.Lane + i % 2 - (i > 3 ? 1 : 0), 0, 5);
- var child = new Foe(i % 3 == 0 ? Breed.Succubus : Breed.Imp, col,
- Mathf.Clamp(f.Row + (i - 3.5f) * .14f, -.05f, 8.4f), Rules.HealthScale(Day) * .85f);
- child.Speed *= 1.18f;
- Enemies.Add(child);
- }
- WaveTotal += 8;
- }
- }
- }
|