Files
linuxdistro-roguelite/Assets/Scripts/Run/TutorialGuide.cs
T
2026-10-27 11:35:32 +01:00

184 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using KernelPanic.Combat;
using KernelPanic.Core;
namespace KernelPanic.Run
{
public enum TutorialAction { Read, SelectCard, TargetEnemy, EndTurn, PlayShield, Free }
public sealed class TutorialStep
{
public readonly string Id, Title, Copy, Anchor;
public readonly TutorialAction Action;
public string CardId { get; internal set; }
public int TargetSlot { get; internal set; }
public TutorialStep(string id, string title, string copy, string anchor, TutorialAction action = TutorialAction.Read)
{ Id = id; Title = title; Copy = copy; Anchor = anchor; Action = action; }
}
/// <summary>Advances from observed combat state, never from fabricated damage or a Next button on action steps.</summary>
public sealed class TutorialGuide
{
private readonly int _lesson;
private readonly Language? _language;
private readonly IReadOnlyList<TutorialStep> _steps;
private bool _played, _resolved, _ended;
public int Index { get; private set; }
public int Count => _steps.Count;
public TutorialStep Step => Active ? _steps[Index] : null;
public bool Active => Index < _steps.Count;
public bool Reading => Active && Step.Action == TutorialAction.Read;
public CardInstance Card { get; private set; }
public EnemyInstance Enemy { get; private set; }
public bool CanEndTurn => !Active || Step.Action == TutorialAction.EndTurn;
private ResolutionTrack ExpectedTrack => _language.HasValue ? Card?.Definition.ResolutionTrack ?? ResolutionTrack.Native
: _lesson != 2 ? ResolutionTrack.Native
: Step?.Id.Contains("haskell") == true ? ResolutionTrack.LazyStack : ResolutionTrack.InterpreterQueue;
public TutorialGuide(int lesson) { _lesson = lesson; _steps = Build(lesson); }
public TutorialGuide(Language language) { _language = language; _steps = LanguageTraining.BuildSteps(language); }
public bool AllowsCard(CardInstance card) => !Active || !Reading && ReferenceEquals(card, Card)
&& Step.Action is TutorialAction.SelectCard or TutorialAction.TargetEnemy or TutorialAction.PlayShield;
public bool AllowsEnemy(EnemyInstance enemy) => !Active || Step.Action == TutorialAction.TargetEnemy && ReferenceEquals(enemy, Enemy);
public void Acknowledge(CombatManager combat)
{
if (!Reading || !combat.TutorialInputReady) return;
Advance(combat);
}
public void CardPlayed(ResolutionTrack track)
{ if (Active && !Reading && track == ExpectedTrack) _played = true; }
public void CardResolved(ResolutionTrack track)
{ if (Active && !Reading && track == ExpectedTrack) _resolved = true; }
public void PhaseChanged(TurnPhase phase) { if (Active && Step.Action == TutorialAction.EndTurn && phase == TurnPhase.Interpret) _ended = true; }
public void Update(CombatManager combat)
{
if (!Active || combat.PlayerState == null) return;
PickReferences(combat);
switch (Step.Action)
{
case TutorialAction.SelectCard:
if (Card != null && combat.PendingTargetCard == Card) Advance(combat);
else if (_played && (Queued(combat) || _resolved))
{ Advance(combat); if (Step?.Action == TutorialAction.TargetEnemy) Advance(combat); }
break;
case TutorialAction.TargetEnemy:
if (_lesson == 1)
{
if (Step.Id == "target-zombie" && Enemy?.PendingRevive == true) Advance(combat);
else if (Step.Id == "finish-zombie" && Enemy?.HasRevived == true && Enemy.State.IsDefeated) Advance(combat);
}
else if (_lesson == 3)
{ if (_resolved && Enemy?.IntentRevealed == true) Advance(combat); }
else if (_played && (Queued(combat) || _resolved)) Advance(combat);
break;
case TutorialAction.PlayShield:
if (_resolved && Card != null && !combat.HandController.Cards.Contains(Card)) Advance(combat);
break;
case TutorialAction.EndTurn:
if (_ended && combat.TutorialInputReady
&& (_lesson != 1 || Enemy?.HasRevived == true)) Advance(combat);
break;
}
combat.TutorialReading = Reading;
}
private bool Queued(CombatManager combat) => Card != null && (combat.InterpreterQueue.Cards.Contains(Card)
|| combat.LazyStack.Entries.Any(entry => entry.Card == Card));
private void Advance(CombatManager combat)
{
Index++;
if (Active && Step.Action is TutorialAction.SelectCard or TutorialAction.PlayShield)
{
Card = null; _played = _resolved = false;
combat.ClearTutorialSelection();
}
if (Active && Step.Action == TutorialAction.EndTurn) _ended = false;
PickReferences(combat);
combat.TutorialReading = Reading;
}
private void PickReferences(CombatManager combat)
{
if (!Active) return;
if (_language.HasValue)
{
Enemy = combat.Enemies.ElementAtOrDefault(Step.TargetSlot);
if (Enemy == null || Enemy.State.IsDefeated)
Enemy = combat.Enemies.FirstOrDefault(enemy => !enemy.State.IsDefeated);
if (Card == null && Step.CardId != null && combat.HandController != null)
Card = combat.HandController.Cards.FirstOrDefault(card => card.Definition.Id == Step.CardId);
return;
}
if (Enemy == null)
Enemy = combat.Enemies.FirstOrDefault(enemy => _lesson == 1 ? enemy.ArchetypeId == "zombie_process"
: _lesson == 3 ? enemy.ArchetypeId == "daemon" : true);
if (Card != null || combat.HandController == null) return;
string id = _lesson == 1 ? Step.Id is "shield-card" or "shield-result" ? "lang_rs_borrow"
: Index >= 13 ? "lang_rs_fn_main" : "lang_rs_unwrap"
: _lesson == 3 ? Step.Action == TutorialAction.PlayShield ? "lang_rs_borrow" : "lang_rs_fn_main" : null;
Card = combat.HandController.Cards.FirstOrDefault(card => !card.IsBroken && !card.IsLocked
&& (id != null ? card.Definition.Id == id : card.Definition.ResolutionTrack == ExpectedTrack)
&& combat.GetDisplayedPlayCost(card) <= combat.PlayerState.Cycles);
}
public static IReadOnlyList<TutorialStep> Build(int lesson)
{
if (lesson == 3) return new[] {
new TutorialStep("cron", "Cron Job / scheduled damage", "Countdown 2 means two enemy turns before its heavy attack is ready. Use that time to prepare.", "enemy:cron_job"),
new TutorialStep("daemon", "Miniboss / Daemon", "Surge: 8-10 damage. Harden: 6-8 Shield. Bypass: 3-4 through Shield. Hit it to reveal which action is coming.", "enemy:daemon"),
new TutorialStep("select-reveal", "Expose the miniboss", "Select fn main(). This Native attack deals damage immediately.", "card", TutorialAction.SelectCard),
new TutorialStep("target-daemon", "Hit the Daemon", "Click the Daemon to reveal what it plans to do.", "enemy", TutorialAction.TargetEnemy),
new TutorialStep("revealed-daemon", "Exposed for this round", "Shield stops Surge, not Bypass. Harden deals no damage. Its next action will be hidden again; hit it each round to reveal.", "enemy:daemon"),
new TutorialStep("countdown-turn", "Watch the timer", "End turn. Daemon carries out its revealed action; Cron Job counts down.", "TutorialEndTurn", TutorialAction.EndTurn),
new TutorialStep("cron-ticking", "One turn left", "Cron Job shows countdown 1. Daemon has hidden its new action again.", "enemy:cron_job"),
new TutorialStep("countdown-ready", "Let the timer expire", "End turn once more. Watch Cron Job's countdown become an attack intent.", "TutorialEndTurn", TutorialAction.EndTurn),
new TutorialStep("cron-armed", "Heavy attack ready", "Cron Job will hit on the next End turn. Defeat it first, or prepare Shield for the displayed damage.", "enemy:cron_job"),
new TutorialStep("shield-cron", "Prepare a defense", "Play borrow() to help absorb Cron Job's hit. Remember: Daemon's Bypass ignores Shield.", "card", TutorialAction.PlayShield),
new TutorialStep("practice", "Clear the final wave", "Finish both enemies. Cron Job resets to countdown 2 after attacking. Clear this wave to claim your training rewards.", "enemies")
};
if (lesson < 1 || lesson > TutorialCampaign.LessonCount) throw new ArgumentOutOfRangeException(nameof(lesson));
if (lesson == 1) return new[] {
new TutorialStep("uptime", "This is your Uptime", "Uptime is your health. At 0, the run ends. These loan cards keep your collection safe.", "TutorialUptime"),
new TutorialStep("shield", "Shield protects Uptime", "Shield absorbs ordinary damage before Uptime. This number is your current protection.", "TutorialShield"),
new TutorialStep("cycles", "Cycles pay for cards", "Cycles pay card costs. Your budget refreshes each round.", "TutorialCycles"),
new TutorialStep("ram", "RAM is hand capacity", "Used RAM / capacity. Most cards use 1 RAM; Java uses 2. Free RAM lets you hold more cards.", "TutorialRam"),
new TutorialStep("attack-card", "Read your attack card", "unwrap() deals damage immediately: it is Native. Read its cost and effect here.", "card"),
new TutorialStep("intent", "Read the enemy's intent", "This is Zombie Process's next action. Check the attack range before ending your turn.", "zombie-intent"),
new TutorialStep("select-attack", "1 / Select the attack", "Click unwrap(). Selecting a targeted card does not spend Cycles yet.", "card", TutorialAction.SelectCard),
new TutorialStep("target-zombie", "2 / Choose the zombie", "Click Zombie Process to dispatch your selected attack.", "zombie", TutorialAction.TargetEnemy),
new TutorialStep("pending-revival", "Zero Uptime, but not gone", "RESTART PENDING: this zombie will revive once. A killing hit from 30% Uptime or less prevents revival.", "zombie"),
new TutorialStep("end-turn", "3 / End your turn", "Press End turn (or E). Watch the zombie return when the next round begins.", "TutorialEndTurn", TutorialAction.EndTurn),
new TutorialStep("revived", "The zombie has respawned", "RESTART USED: it returned at half Uptime and cannot revive again. Your Cycles have refreshed.", "zombie"),
new TutorialStep("shield-card", "4 / Add protection", "Click borrow() to gain Shield. No target needed; existing Shield is kept.", "card", TutorialAction.PlayShield),
new TutorialStep("shield-result", "Your Shield increased", "Your Shield increased. It absorbs damage, but does not heal Uptime.", "TutorialShield"),
new TutorialStep("select-finisher", "5 / Select a finisher", "Select fn main(), then choose the revived zombie.", "card", TutorialAction.SelectCard),
new TutorialStep("finish-zombie", "6 / Finish the revived zombie", "Click the zombie. Its revival is spent, so this hit can terminate it permanently.", "zombie", TutorialAction.TargetEnemy),
new TutorialStep("piles", "Track your cards", "Played cards go to discard; an empty draw pile reshuffles it. Exhausted cards stay out for this wave.", "TutorialPiles"),
new TutorialStep("practice", "Finish the encounter", "Finish the orphan processes. Use card → enemy, then End turn as needed. A defeated zombie can leave an orphan.", "enemies")
};
return new[] {
new TutorialStep("leak", "A new threat: Memory Leak", "Memory Leak grows stronger each round and applies damage over time. Your purchased patch carried over.", "enemy:memory_leak"),
new TutorialStep("firewall", "A protector: firewalld", "firewalld shields itself and its neighbors. Check its intent before choosing a target.", "enemy:firewalld"),
new TutorialStep("queue-intro", "Delayed cards / the queue", "Queued cards wait until End turn, then resolve oldest first (FIFO). Optional language fights teach combos from your collection.", "TutorialQueue"),
new TutorialStep("select-python", "Try a payload / print()", "This training hand has print(). Select it, then choose the highlighted enemy to see how a payload enters the queue.", "card", TutorialAction.SelectCard),
new TutorialStep("target-python", "Select a target", "Click the highlighted enemy to complete this play.", "enemy", TutorialAction.TargetEnemy),
new TutorialStep("queued-python", "Python is waiting here", "The card is queued, not resolved. With multiple entries, the oldest executes first.", "TutorialQueue"),
new TutorialStep("stack-intro", "Haskell uses the stack", "Haskell resolves after Python, newest first (LIFO). Its entry multiplier scales damage and Shield.", "TutorialStack"),
new TutorialStep("select-haskell", "Select the Haskell card", "Click thunk(), then choose the highlighted enemy.", "card", TutorialAction.SelectCard),
new TutorialStep("target-haskell", "Choose its target", "Click this enemy. Your attack will wait on the stack.", "enemy", TutorialAction.TargetEnemy),
new TutorialStep("queued-haskell", "Both tracks are ready", "Order: Python queue → Haskell stack → surviving enemies. Deferred attacks can retarget defeated enemies.", "TutorialStack"),
new TutorialStep("end-round", "Run the queued actions", "Press End turn (or E). Watch Python resolve before Haskell.", "TutorialEndTurn", TutorialAction.EndTurn),
new TutorialStep("resolved", "The waiting cards resolved", "The waiting cards resolved. Check enemy Uptime, Shield and intents.", "enemies"),
new TutorialStep("practice", "Clear wave two", "Finish the remaining enemies. Next: Cron Job's countdown and a Daemon miniboss.", "enemies")
};
}
}
}