Files
linuxdistro-roguelite/Assets/Scripts/UI/FirstBattleGuide.cs
T

176 lines
7.8 KiB
C#

using System;
using KernelPanic.Core;
using KernelPanic.Run;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// <summary>Observes a real battle; never changes cards, targeting or turn timing.</summary>
public sealed class FirstBattleGuide
{
private const string PreferenceKey = "KernelPanic.FirstBattleGuide.v1";
private readonly Action _remember;
private readonly Label _title = new();
private readonly Label _instruction = new();
private readonly Label _receipt = new();
private readonly VisualElement _resources = new();
private readonly Foldout _tracks = new() { text = "Resources & turn order", value = false };
private readonly Button _action;
private readonly Button _skip;
private RunConfig _config;
private bool _played;
private bool _endedTurn;
private bool _completed;
private bool _started;
public VisualElement Element { get; } = new() { name = "FirstBattleGuide" };
public bool Visible => Element.style.display.value != DisplayStyle.None;
public string Instruction => _instruction.text;
public FirstBattleGuide(Action remember = null)
{
_remember = remember ?? Remember;
Element.AddToClassList("first-battle-guide");
_title.AddToClassList("guide-title");
_instruction.AddToClassList("guide-copy");
_receipt.AddToClassList("guide-copy");
var header = new VisualElement();
header.AddToClassList("guide-header");
header.Add(_title);
Element.Add(header);
_resources.AddToClassList("guide-resources");
foreach (string text in new[] { "Uptime\nHealth", "Shield\nProtection", "Cycles\nCard budget", "RAM\nHand space" })
{
var chip = new Label(text); chip.AddToClassList("guide-resource"); _resources.Add(chip);
}
Element.Add(_instruction);
Element.Add(_receipt);
Element.Add(_tracks);
var actions = new VisualElement();
actions.AddToClassList("guide-actions");
_action = new Button(Advance) { text = "Try a card" };
_skip = new Button(Dismiss) { text = "Close guide" };
actions.Add(_action);
actions.Add(_skip);
header.Add(actions);
Element.style.display = DisplayStyle.None;
}
public void Begin(RunConfig config, bool replay = false)
{
_config = config;
if (!replay && PlayerPrefs.GetInt(PreferenceKey, 0) != 0) return;
_played = _endedTurn = _completed = _started = false;
_receipt.text = "";
Element.style.display = DisplayStyle.Flex;
_action.style.display = DisplayStyle.Flex;
_skip.style.display = DisplayStyle.Flex;
_action.text = "Try a card";
_resources.style.display = DisplayStyle.Flex;
_tracks.Clear(); _tracks.value = false;
_tracks.Add(_resources);
AddTrack("NOW / Native", "Executes when played.");
if (config?.PrimaryLanguage == Language.Python || config?.SecondaryLanguage == Language.Python)
AddTrack("1 / Python queue", "End turn: oldest first.");
if (config?.PrimaryLanguage == Language.Haskell || config?.SecondaryLanguage == Language.Haskell)
AddTrack("2 / Haskell stack", "After the queue: newest first. Entry multiplier scales damage and Shield.");
AddTrack("LAST / Enemies", "Surviving enemies carry out their intents.");
_title.text = "1 / Read the battlefield";
_instruction.text = "Check enemy intents before you spend your Cycles.";
}
public void Advance()
{
if (!Visible) return;
if (_completed) { Dismiss(); return; }
_started = true;
_action.style.display = DisplayStyle.None;
_title.text = "2 / Play a card";
_instruction.text = "Select an affordable card, then its enemy target if needed. Check the preview before dispatching.";
}
public void CardPlayed(ResolutionTrack track, string name)
{
if (!Visible || _completed) return;
_played = _started = true;
_action.style.display = DisplayStyle.None;
_title.text = "3 / End turn";
_receipt.text = track switch
{
ResolutionTrack.InterpreterQueue => name + " queued / oldest first.",
ResolutionTrack.LazyStack => name + " stacked / newest first.",
_ => name + " / executes now."
};
_instruction.text = "Spend any remaining Cycles, then press [E] End turn. Shield absorbs incoming damage.";
}
public void CardResolved(string name, ResolutionTrack track)
{
if (!Visible || _completed) return;
_receipt.text = name + " resolved" + (track == ResolutionTrack.Native ? " immediately." : track == ResolutionTrack.InterpreterQueue ? " from the front of the queue." : " from the top of the stack.");
}
public void PhaseChanged(TurnPhase phase)
{
if (!Visible || _completed) return;
if (phase == TurnPhase.Interpret)
{
_endedTurn = true;
_instruction.text = "Queue: oldest first → Stack: newest first → Enemies.";
}
if (phase == TurnPhase.Execute && _endedTurn) Complete(false);
}
public void Refresh(bool waveCleared, bool runLost)
{
if (!Visible || _completed) return;
if (waveCleared || runLost) Complete(waveCleared);
}
private void Complete(bool waveCleared)
{
_completed = true;
_title.text = waveCleared ? "4 / Wave cleared" : "4 / Reassess";
_instruction.text = waveCleared
? "Spend Bits in the repository on cards, upgrades or healing. Then continue your run."
: "Check your Uptime, Shield and next enemy intents. Your turn resources have refreshed.";
_receipt.text = _config?.Distro?.Id == "gentoo" && !waveCleared
? "Compilation grew this round; it resets each wave."
: !_played && _started ? "Ending a turn without playing a card is allowed." : "Reopen anytime with ? guide.";
_action.text = "Got it";
_action.style.display = DisplayStyle.Flex;
_skip.style.display = DisplayStyle.None;
}
public void Dismiss()
{
Element.style.display = DisplayStyle.None;
_remember();
}
private void AddTrack(string title, string copy)
{
var row = new VisualElement(); row.AddToClassList("guide-track");
var heading = new Label(title); heading.AddToClassList("guide-title"); row.Add(heading);
var detail = new Label(copy); detail.AddToClassList("guide-copy"); row.Add(detail);
_tracks.Add(row);
}
public static string TrackLesson(RunConfig config)
{
string lesson = "Native cards execute now.";
if (config?.PrimaryLanguage == Language.Python || config?.SecondaryLanguage == Language.Python)
lesson += " Your Python cards queue until end turn (first in, first out).";
if (config?.PrimaryLanguage == Language.Haskell || config?.SecondaryLanguage == Language.Haskell)
lesson += " Your Haskell cards stack until end turn (last in, first out); the multiplier shown on entry scales damage and explicit Shield.";
return lesson;
}
private static void Remember()
{
PlayerPrefs.SetInt(PreferenceKey, 1);
PlayerPrefs.Save();
}
}
}