Files
bioDome/Assets/DomeShift/DomeStory.cs

238 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
namespace DomeShift
{
internal enum StoryAction
{
Blocked,
Completed,
ChapterFinished
}
internal sealed class DomeStory
{
internal const float InteractionRange = 2.35f;
internal static readonly Color Accent = new Color32(255, 194, 110, 255);
private static readonly Chapter[] Chapters =
{
new("Locked In", "Day 1",
"Bud and Doyle mistook the dome for a mall. The airlock has sealed, and their detour left smoke drifting through the rainforest.",
"Check the airlock and clear the rainforest alarm.", 0.89f, 0.99f,
new("Main airlock", "Check the airlock", "The experiment has started. The main door is staying shut.", 0f, -26f, StoryPropKind.Hatch),
new("Rainforest litter", "Pick up the smoking litter", "The source is gone. The intake is still reading smoke.", -21.5f, 10.9f, StoryPropKind.Litter, requires: 0b001),
new("Rainforest intake", "Clear the rainforest alarm", "Rainforest intake is clear. Olivia is taking inventory in the habitat.", -28.7f, 12.3f, StoryPropKind.Intake, requires: 0b010)),
new("The Secret Stash", "Early in the experiment",
"A raid on the hidden snacks left a trail of wrappers. Then Bud and Doyle fooled around in the lab, leaving Olivia with a ventilation alarm.",
"Clean the habitat and reset the lab vent.", 0.84f, 0.98f,
new("Snack wrappers", "Bag the snack wrappers", "Wrappers bagged. There is another pile farther up the habitat hall.", 0f, -22.1f, StoryPropKind.Litter),
new("Empty snack boxes", "Clear the snack boxes", "The hall is clean. Check the lab vent at the Technosphere entrance.", 0f, -15.1f, StoryPropKind.Litter),
new("Lab ventilation", "Reset the lab vent", "Lab ventilation is back. Faulkner has sent both of you to the desert.", 20f, -11.25f, StoryPropKind.Intake, requires: 0b011)),
new("The Desert Door", "After three days in the desert",
"The desert’s service door turned out to have a key. Bud and Doyle slipped out, and the open hatch broke the seal.",
"Recover the key, reseal the hatch, and check the Lung.", 0.76f, 0.97f,
new("Service key", "Recover the service key", "You have the key. Take it to the hatch at the far edge of the Great Desert.", 22f, -1.5f, StoryPropKind.Supply),
new("Desert service hatch", "Reseal the service hatch", "The hatch is sealed. The Lung needs to bring pressure back into balance.", 40f, -4f, StoryPropKind.Hatch, requires: 0b001),
new("Lung monitor", "Balance the Lung", "Pressure is steady again. Outside, a pizza delivery gives Bud a spectacularly bad idea.", 32f, -18.6f, StoryPropKind.Console, requires: 0b010)),
new("After the Party", "A month into the experiment",
"The boys threw a party to prove they cared about the environment. The guests are gone, the dome is trashed, and the scientists are ready to walk.",
"Clean up the party and bring air, water, and crops back.", 0.01f, 0.99f,
new("Lagoon trash", "Clear the lagoon shore", "The lagoon shore is clear. The water monitor is still offline.", 6f, 13.8f, StoryPropKind.Litter),
new("Orchard trash", "Clear the orchard litter", "Orchard litter is bagged. The crew can start clearing the air.", -6f, -9.5f, StoryPropKind.Litter),
new("Farm seed tray", "Pick up the seed tray", "Take the seed tray to the damaged Farm beds.", -16f, -11f, StoryPropKind.Supply, part: 2),
new("Crop beds", "Replant the Farm beds", "New seedlings are in. The Farm is back on the recovery chart.", -25f, -13.1f, StoryPropKind.Planter, requires: 0b00000100, part: 2),
new("Air filter", "Pick up the clean air filter", "Take the clean filter to the rainforest intake.", 17.5f, 11.4f, StoryPropKind.Supply, part: 1),
new("Rainforest filtration", "Install the air filter", "The intake is running clean. The fogger is cycling again.", -18f, 11.1f, StoryPropKind.Intake, requires: 0b00010010, part: 1),
new("Tide monitor", "Restart the lagoon monitor", "The lagoon monitor is back online and the water is clearing.", -3.2f, 11.8f, StoryPropKind.Console, requires: 0b00000001),
new("Homeostasis desk", "Confirm the recovery", "The dome is healthy again. Earth Day is tomorrow, and Faulkner has come back for the ceremony.", 1.6f, -16.9f, StoryPropKind.Console, requires: 0b01111111)),
new("Opening Day", "Earth Day",
"Faulkner’s surprise for the reopening has turned dangerous. Bud and Doyle know what they helped set in motion and have one last mess to put right.",
"Cancel the ceremony from the safety relays, then reopen the airlock.", 0.99f, 1f,
new("Lagoon safety relay", "Cancel the lagoon cue", "The lagoon relay is safe. Two other areas still need clearance.", 3.7f, 18.5f, StoryPropKind.Signal),
new("Farm safety relay", "Cancel the Farm cue", "The Farm relay is safe. Keep going until the airlock has clearance.", -24f, -11.8f, StoryPropKind.Signal),
new("Ceremony shutdown", "Cancel the main show", "The ceremony controls are safe. Meet everyone at the front airlock.", 21f, -14.5f, StoryPropKind.Signal, requires: 0b011),
new("Main airlock", "Reopen the dome", "The dome is safe and the experiment made it to Earth Day.", 0f, -27.2f, StoryPropKind.Hatch, requires: 0b111))
};
private int chapterIndex;
private int completed;
public int HeldPart { get; private set; } = -1;
public int ChapterNumber => chapterIndex + 1;
public int ChapterCount => Chapters.Length;
public bool IsLastChapter => chapterIndex == Chapters.Length - 1;
public string Title => Current.Title;
public string Status => Current.Status;
public string Brief => Current.Brief;
public string Goal => Current.Goal;
public int Total => Current.Tasks.Length;
public int Done
{
get
{
int count = 0;
for (int i = 0; i < Total; i++)
if (IsDone(i))
count++;
return count;
}
}
public float Homeostasis => Mathf.Lerp(Current.StartingHealth, Current.RestoredHealth, Done / (float)Total);
private Chapter Current => Chapters[chapterIndex];
public void Reset()
{
chapterIndex = 0;
completed = 0;
HeldPart = -1;
}
public void Advance()
{
chapterIndex++;
completed = 0;
HeldPart = -1;
}
public Task Target(int index) => Current.Tasks[index];
public bool IsDone(int index) => (completed & (1 << index)) != 0;
public bool IsOpen(int index)
{
if (IsDone(index))
return false;
Task task = Target(index);
if ((completed & task.Requires) != task.Requires)
return false;
if (task.Part == -1)
return true;
return task.Kind == StoryPropKind.Supply ? HeldPart == -1 : HeldPart == task.Part;
}
public int Closest(Vector2 position)
{
int closest = -1;
float nearest = InteractionRange * InteractionRange;
for (int i = 0; i < Total; i++)
{
if (IsDone(i))
continue;
float squaredDistance = (Target(i).Point - position).sqrMagnitude;
if (squaredDistance >= nearest)
continue;
nearest = squaredDistance;
closest = i;
}
return closest;
}
public string Prompt(int index)
{
if (index == -1)
return "";
Task task = Target(index);
return IsOpen(index) ? "E " + task.Verb : BlockedMessage(task);
}
public StoryAction Interact(int index, out string dispatch)
{
Task task = Target(index);
if (!IsOpen(index))
{
dispatch = BlockedMessage(task);
return StoryAction.Blocked;
}
completed |= 1 << index;
if (task.Part >= 0)
HeldPart = task.Kind == StoryPropKind.Supply ? task.Part : -1;
dispatch = task.Completed;
return completed == (1 << Total) - 1 ? StoryAction.ChapterFinished : StoryAction.Completed;
}
private string BlockedMessage(Task task)
{
if (task.Part >= 0 && task.Kind == StoryPropKind.Supply && HeldPart != -1)
return "Install the part you are carrying first";
if (task.Part >= 0 && HeldPart != task.Part)
return "Pick up the matching supply first";
return "Complete the earlier work first";
}
public int PinCount
{
get
{
int count = 0;
for (int i = 0; i < Total; i++)
if (IsOpen(i))
count++;
return count;
}
}
public Task Pin(int index)
{
for (int i = 0; i < Total; i++)
{
if (!IsOpen(i))
continue;
if (index-- == 0)
return Target(i);
}
return default;
}
internal readonly struct Task
{
public readonly string Name;
public readonly string Verb;
public readonly string Completed;
public readonly Vector2 Point;
public readonly StoryPropKind Kind;
public readonly int Requires;
public readonly int Part;
public Task(string name, string verb, string completed, float x, float z, StoryPropKind kind,
int requires = 0, int part = -1)
{
Name = name;
Verb = verb;
Completed = completed;
Point = new Vector2(x, z);
Kind = kind;
Requires = requires;
Part = part;
}
}
private readonly struct Chapter
{
public readonly string Title;
public readonly string Status;
public readonly string Brief;
public readonly string Goal;
public readonly float StartingHealth;
public readonly float RestoredHealth;
public readonly Task[] Tasks;
public Chapter(string title, string status, string brief, string goal, float startingHealth,
float restoredHealth, Task first, params Task[] remaining)
{
Title = title;
Status = status;
Brief = brief;
Goal = goal;
StartingHealth = startingHealth;
RestoredHealth = restoredHealth;
Tasks = new Task[remaining.Length + 1];
Tasks[0] = first;
remaining.CopyTo(Tasks, 1);
}
}
}
}