using UnityEngine; namespace DomeShift { public enum GamePhase { Title, Playing, Paused, Won, Lost, Viewer } public enum PlayableCharacter { Bud, Doyle } [DisallowMultipleComponent] [RequireComponent(typeof(DomeEnvironment))] public sealed class DomeGame : MonoBehaviour { private enum RunMode { Challenge, Explore, Story } private const int AllRepaired = 0b111; private const int Airlock = 6; private const float WalkSpeed = 8.2f; private const string MuteKey = "DomeShift.Muted"; private static readonly string[] StationNames = { "Generator", "Cooling", "Hydroponics" }; private static readonly string[] PartNames = { "Fuse", "Mesh filter", "Seed pack" }; private static readonly string[] Repairs = { "Power's back. Good news for everything that was blinking.", "Cooling's back. The tomatoes can stop sweating.", "Hydroponics is online. The basil has withdrawn its complaint." }; private static readonly Color[] PartColors = { new Color32(255, 186, 92, 255), new Color32(106, 215, 247, 255), new Color32(164, 228, 128, 255) }; private static readonly float[] PointRanges = { 2.6f, 2.6f, 2.6f, 1.9f, 1.9f, 1.9f, 2.05f }; private static readonly float[] MarkerHeights = { 2.35f, 2.75f, 2.35f, 1.65f, 1.65f, 1.65f, 2.6f }; // Footprints include clearance for the worker's shoulders. private static readonly Rect[] Obstacles = { Footprint(0, 0.48f, 1.17f, 0.65f), Footprint(1, 0.53f, 1.10f, 0.81f), Footprint(2, 0.62f, 1.35f, 0.67f), Footprint(3, 0f, 0.44f, 0.44f), Footprint(4, 0f, 0.44f, 0.44f), Footprint(5, 0f, 0.44f, 0.44f) }; private readonly DomeStory story = new(); private readonly DomeCameraRig cameraRig = new(); private DomeEnvironment environment; private Transform environmentRoot; private DomeWorld world; private DomeStoryProps storyProps; private DomeHud hud; private DomeAudio audioBus; private Camera domeCamera; private GUIStyle markerStyle; private Vector2 playerPosition; private Vector2Int windowSize = new(1920, 1080); private float worldTime; private float displayTransitionUntil = -1f; private int nearby = -1; private RunMode mode; public GamePhase Phase { get; private set; } = GamePhase.Title; public PlayableCharacter Character { get; private set; } = PlayableCharacter.Bud; public bool Exploring => mode == RunMode.Explore; public bool InStory => mode == RunMode.Story; public DomeEnvironment Profile => environment; public string StoryTitle => story.Title; public string StoryBrief => story.Brief; public string StoryGoal => story.Goal; public string StoryStatus => story.Status; public float Homeostasis => story.Homeostasis; public int StoryChapter => story.ChapterNumber; public int StoryChapterCount => story.ChapterCount; public int StoryDone => story.Done; public int StoryTotal => story.Total; public int StoryPinCount => story.PinCount; public int StoryCarried => story.HeldPart; public bool MapOpen { get; private set; } public bool Cutaway { get; private set; } public bool Overview { get; private set; } public DomeRegion CurrentRegion => DomeEnvironment.RegionAt(playerPosition); public Vector2 MapPosition => DomeEnvironment.MapCoordinates(playerPosition); public float RoundLength => 150f; public float TimeLeft { get; private set; } public int RepairedMask { get; private set; } public int Carried { get; private set; } = -1; public bool Muted => audioBus.Muted; public bool Fullscreen => Screen.fullScreen; public bool CanToggleFullscreen => !Application.isEditor; public string Dispatch { get; private set; } = ""; public Vector2 MapPoint(int index) => DomeEnvironment.MapCoordinates(DomeEnvironment.Site(index)); public Vector2 StoryPin(int index) => DomeEnvironment.MapCoordinates(story.Pin(index).Point); public string StoryPinName(int index) => story.Pin(index).Name; public string Prompt { get { if (InStory) return story.Prompt(nearby); if (nearby == -1) return RepairedMask == AllRepaired ? "Head to the airlock" : ""; if (nearby == Airlock) return RepairedMask == AllRepaired ? "E Cycle the airlock" : "Airlock sealed until repairs are complete"; if (nearby < PartNames.Length) { if (IsRepaired(nearby)) return $"{StationNames[nearby]} is online"; return Carried == nearby ? $"E Install {PartNames[nearby].ToLowerInvariant()}" : $"{StationNames[nearby]} needs a {PartNames[nearby].ToLowerInvariant()}"; } int part = nearby - PartNames.Length; return Carried == -1 ? $"E Pick up {PartNames[part].ToLowerInvariant()}" : $"E Swap for {PartNames[part].ToLowerInvariant()}"; } } private void Awake() { environment = GetComponent(); BuildWorld(); hud = new DomeHud(); audioBus = gameObject.AddComponent(); audioBus.Muted = PlayerPrefs.GetInt(MuteKey, 0) == 1; GameObject cameraObject = new("Dome Camera"); cameraObject.transform.SetParent(transform, false); cameraObject.tag = "MainCamera"; domeCamera = cameraObject.AddComponent(); cameraObject.AddComponent(); domeCamera.clearFlags = CameraClearFlags.SolidColor; domeCamera.backgroundColor = new Color32(30, 51, 58, 255); domeCamera.orthographic = true; domeCamera.nearClipPlane = 0.1f; domeCamera.farClipPlane = 400f; domeCamera.allowMSAA = true; TimeLeft = RoundLength; SetPlayerPosition(DomeEnvironment.Spawn); FrameDome(); world.Tick(Vector2.zero, Carried, RepairedMask, 0f); } private void Update() { if (Input.GetKeyDown(KeyCode.M)) ToggleMute(); if (Input.GetKeyDown(KeyCode.F11)) ToggleFullscreen(); bool enter = Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter); bool escape = Input.GetKeyDown(KeyCode.Escape); switch (Phase) { case GamePhase.Title: UpdateTitle(enter); break; case GamePhase.Playing: if (escape) Pause(); else PlayFrame(); break; case GamePhase.Paused: if (Input.GetKeyDown(KeyCode.B)) ChooseCharacter(); else if (escape || enter) Resume(); break; case GamePhase.Won: case GamePhase.Lost: if (Input.GetKeyDown(KeyCode.B)) ChooseCharacter(); else if (enter || Input.GetKeyDown(KeyCode.R)) Restart(); break; case GamePhase.Viewer: if (escape || Input.GetKeyDown(KeyCode.B)) ExitViewer(); else if (Input.GetKeyDown(KeyCode.V)) ToggleRoof(); break; } FrameDome(); } private void OnApplicationFocus(bool hasFocus) { if (!hasFocus && Phase == GamePhase.Playing && Time.unscaledTime >= displayTransitionUntil) Pause(); } private void OnGUI() { if (Phase == GamePhase.Playing && !MapOpen && !(Exploring && Overview) && Event.current.type == EventType.Repaint) DrawMarkers(); hud.Draw(this); } public void StartShift() { EnterDome(RunMode.Challenge); } public void StartExplore() { EnterDome(RunMode.Explore); } public void StartStory() { EnterDome(RunMode.Story); } public void StartViewer() { if (Phase != GamePhase.Title) return; ResetRun(); mode = RunMode.Challenge; Overview = true; SetCutaway(false); world.SetShiftEquipmentVisible(false); world.Player.gameObject.SetActive(false); Phase = GamePhase.Viewer; } public void ExitViewer() { if (Phase == GamePhase.Viewer) ReturnToTitle(); } public void ApplyViewParameters(float width, float depth, float roofHeight, int bays, float glass, float density, float relief, int seed) { if (Phase != GamePhase.Viewer || !environment.ApplyViewportSettings(width, depth, roofHeight, bays, glass, density, relief, seed)) return; BuildWorld(); SetPlayerPosition(DomeEnvironment.Spawn); world.SetShiftEquipmentVisible(false); world.Player.gameObject.SetActive(false); world.Tick(Vector2.zero, -1, 0, 0f); SetCutaway(Cutaway); } public void ResetCamera() { cameraRig.Reset(); } private void BuildWorld() { environmentRoot = environment.BuildForGame(); world = DomeWorld.Create(environmentRoot, environment); world.SetCharacter(Character); storyProps = null; } private void EnterDome(RunMode newMode) { ResetRun(); mode = newMode; Overview = false; SetCutaway(true); world.SetShiftEquipmentVisible(!InStory); if (InStory) { story.Reset(); ShowStoryChapter(); Dispatch = $"{Character}, check the main airlock. Tab opens the map."; } else { Dispatch = Exploring ? $"{Character}, wander the dome at your own pace. Press Tab for the map." : $"{Character}, match the spares on the stands to the station colors."; } Phase = GamePhase.Playing; audioBus.Play(DomeAudio.Cue.Start); } public void SelectCharacter(PlayableCharacter character) { if (Phase != GamePhase.Title || Character == character || (character != PlayableCharacter.Bud && character != PlayableCharacter.Doyle)) return; Character = character; world.SetCharacter(Character); } public void ChooseCharacter() { if (Phase != GamePhase.Won && Phase != GamePhase.Lost && Phase != GamePhase.Paused) return; ReturnToTitle(); } private void ReturnToTitle() { ResetRun(); mode = RunMode.Challenge; Overview = false; world.SetShiftEquipmentVisible(true); SetCutaway(false); Phase = GamePhase.Title; } public void Restart() { EnterDome(mode); } public void Resume() { if (Phase == GamePhase.Paused) Phase = GamePhase.Playing; } public void ToggleMute() { audioBus.Muted = !audioBus.Muted; PlayerPrefs.SetInt(MuteKey, audioBus.Muted ? 1 : 0); PlayerPrefs.Save(); } public void ToggleFullscreen() { if (!CanToggleFullscreen) return; Resolution desktop = Screen.currentResolution; displayTransitionUntil = Time.unscaledTime + 0.75f; if (Fullscreen) { Screen.SetResolution(Mathf.Min(windowSize.x, desktop.width), Mathf.Min(windowSize.y, desktop.height), FullScreenMode.Windowed); return; } windowSize = new Vector2Int(Screen.width, Screen.height); Screen.SetResolution(desktop.width, desktop.height, FullScreenMode.FullScreenWindow); } public void ToggleMap() { MapOpen = !MapOpen; } public void ToggleRoof() { SetCutaway(!Cutaway); if (!Cutaway) Overview = true; } public void ToggleOverview() { Overview = !Overview; if (!Overview) SetCutaway(true); } private void SetCutaway(bool cutaway) { Cutaway = cutaway; environment.SetCutaway(cutaway); } private void Pause() { Phase = GamePhase.Paused; UpdateCrew(Vector2.zero); } private void ResetRun() { TimeLeft = RoundLength; RepairedMask = 0; Carried = -1; nearby = -1; worldTime = 0f; MapOpen = false; Dispatch = ""; cameraRig.Reset(); storyProps?.Hide(); world.Player.gameObject.SetActive(true); SetPlayerPosition(DomeEnvironment.Spawn); world.Tick(Vector2.zero, Carried, RepairedMask, worldTime); } private void UpdateTitle(bool enter) { if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1)) SelectCharacter(PlayableCharacter.Bud); else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2)) SelectCharacter(PlayableCharacter.Doyle); if (Input.GetKeyDown(KeyCode.S)) StartStory(); else if (Input.GetKeyDown(KeyCode.P)) StartViewer(); else if (Input.GetKeyDown(KeyCode.X)) StartExplore(); else if (enter) StartShift(); else AnimateWorld(Vector2.zero); } private void PlayFrame() { if (Input.GetKeyDown(KeyCode.Tab)) ToggleMap(); if (Input.GetKeyDown(KeyCode.V)) ToggleRoof(); if (Input.GetKeyDown(KeyCode.C)) ToggleOverview(); if (mode == RunMode.Challenge) TimeLeft = Mathf.Max(0f, TimeLeft - Time.deltaTime); if (mode == RunMode.Challenge && TimeLeft <= 0f) { Phase = GamePhase.Lost; Dispatch = "The safety timer locked out the dome."; world.Tick(Vector2.zero, Carried, RepairedMask, worldTime); audioBus.Play(DomeAudio.Cue.Lost); return; } Vector2 input = new( Axis(KeyCode.D, KeyCode.RightArrow, KeyCode.A, KeyCode.LeftArrow), Axis(KeyCode.W, KeyCode.UpArrow, KeyCode.S, KeyCode.DownArrow)); Vector2 positionBefore = playerPosition; Move(CameraRelative(input) * (WalkSpeed * Mathf.Min(Time.deltaTime, 0.08f))); nearby = InStory ? story.Closest(playerPosition) : ClosestPoint(); if (nearby >= 0 && Input.GetKeyDown(KeyCode.E)) { if (InStory) InteractWithStory(); else Interact(); nearby = InStory ? story.Closest(playerPosition) : ClosestPoint(); } Vector2 displacement = playerPosition - positionBefore; AnimateWorld(Phase == GamePhase.Playing ? displacement.normalized : Vector2.zero); } private static int Axis(KeyCode positive, KeyCode positiveArrow, KeyCode negative, KeyCode negativeArrow) { int forward = Input.GetKey(positive) || Input.GetKey(positiveArrow) ? 1 : 0; int backward = Input.GetKey(negative) || Input.GetKey(negativeArrow) ? 1 : 0; return forward - backward; } private Vector2 CameraRelative(Vector2 input) { Transform view = domeCamera.transform; Vector3 forward = Vector3.ProjectOnPlane(view.forward, Vector3.up).normalized; Vector3 right = Vector3.ProjectOnPlane(view.right, Vector3.up).normalized; Vector3 local = environmentRoot.InverseTransformVector(forward * input.y + right * input.x); return new Vector2(local.x, local.z).normalized; } private void AnimateWorld(Vector2 motion) { worldTime += Time.deltaTime; UpdateCrew(motion); if (InStory) storyProps.Animate(worldTime); } private void UpdateCrew(Vector2 motion) { int carry = InStory ? story.HeldPart : Carried; int repairs = InStory && Phase == GamePhase.Won ? AllRepaired : RepairedMask; world.Tick(motion, carry, repairs, worldTime); } private void Move(Vector2 movement) { Vector2 horizontal = new(playerPosition.x + movement.x, playerPosition.y); if (CanWalk(horizontal)) SetPlayerPosition(horizontal); Vector2 vertical = new(playerPosition.x, playerPosition.y + movement.y); if (CanWalk(vertical)) SetPlayerPosition(vertical); } private bool CanWalk(Vector2 position) { if (!InStory) { foreach (Rect obstacle in Obstacles) { if (obstacle.Contains(position)) return false; } } return environment.CanWalk(position); } private static Rect Footprint(int index, float zOffset, float halfWidth, float halfDepth) { const float clearance = 0.27f; Vector2 point = DomeEnvironment.Site(index); return new Rect(point.x - halfWidth - clearance, point.y + zOffset - halfDepth - clearance, (halfWidth + clearance) * 2f, (halfDepth + clearance) * 2f); } private void SetPlayerPosition(Vector2 position) { playerPosition = position; world.Player.localPosition = new Vector3(position.x, environment.SurfaceY(position), position.y); } private bool IsRepaired(int station) { return (RepairedMask & (1 << station)) != 0; } private int ClosestPoint() { int closest = -1; float nearestDistance = float.PositiveInfinity; for (int i = 0; i < PointRanges.Length; i++) { if (i >= PartNames.Length && i < Airlock) { int part = i - PartNames.Length; if (Carried == part || IsRepaired(part)) continue; } float distance = (playerPosition - DomeEnvironment.Site(i)).sqrMagnitude; if (distance <= PointRanges[i] * PointRanges[i] && distance < nearestDistance) { closest = i; nearestDistance = distance; } } return closest; } private void Interact() { if (nearby == -1) return; if (nearby == Airlock) { if (RepairedMask == AllRepaired) { Phase = GamePhase.Won; Dispatch = "Inez here. Bring that exit survey back blank, like everyone else."; audioBus.Play(DomeAudio.Cue.Won); } else { Dispatch = "The airlock needs all three stations online."; audioBus.Play(DomeAudio.Cue.Denied); } return; } if (nearby >= PartNames.Length) { int previous = Carried; Carried = nearby - PartNames.Length; Dispatch = previous == -1 ? $"Take the {PartNames[Carried].ToLowerInvariant()} to {StationNames[Carried]}." : $"Swapped parts. The {PartNames[previous].ToLowerInvariant()} went back to its stand."; audioBus.Play(DomeAudio.Cue.Pickup); return; } if (IsRepaired(nearby)) return; if (Carried != nearby) { Dispatch = $"Find the {PartNames[nearby].ToLowerInvariant()} for {StationNames[nearby]}."; audioBus.Play(DomeAudio.Cue.Denied); return; } RepairedMask |= 1 << nearby; Carried = -1; Dispatch = RepairedMask == AllRepaired ? "Interlock cleared. Head back to the airlock." : Repairs[nearby]; audioBus.Play(DomeAudio.Cue.Repair); } private void ShowStoryChapter() { storyProps ??= DomeStoryProps.Create(environmentRoot, environment); storyProps.Hide(); for (int i = 0; i < story.Total; i++) { DomeStory.Task task = story.Target(i); Vector2 point = task.Point; Vector3 position = new(point.x, environment.SurfaceY(point) + 0.025f, point.y); storyProps.Show(i, task.Kind, position, DomeStory.Accent); } } private void InteractWithStory() { DomeStory.Task task = story.Target(nearby); StoryAction result = story.Interact(nearby, out string feedback); Dispatch = feedback; if (result == StoryAction.Blocked) { audioBus.Play(DomeAudio.Cue.Denied); return; } storyProps.Complete(nearby); if (result == StoryAction.ChapterFinished && story.IsLastChapter) { Phase = GamePhase.Won; audioBus.Play(DomeAudio.Cue.Won); return; } if (result == StoryAction.ChapterFinished) { story.Advance(); ShowStoryChapter(); } bool pickup = task.Kind == StoryPropKind.Supply || task.Kind == StoryPropKind.Litter; audioBus.Play(pickup ? DomeAudio.Cue.Pickup : DomeAudio.Cue.Repair); } private void FrameDome() { bool allowInput = Phase == GamePhase.Viewer || (Phase == GamePhase.Playing && !MapOpen); cameraRig.Frame(domeCamera, environment, environmentRoot, world.Player, Phase == GamePhase.Title, Overview, Phase == GamePhase.Viewer, allowInput); } private void DrawMarkers() { markerStyle ??= new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold, clipping = TextClipping.Overflow }; float scale = Mathf.Clamp(Screen.height / 720f, 0.75f, 2.1f); markerStyle.fontSize = Mathf.RoundToInt(12f * scale); Color previousColor = GUI.color; if (InStory) { for (int i = 0; i < story.Total; i++) { if (!story.IsOpen(i) && nearby != i) continue; DomeStory.Task task = story.Target(i); DrawMarker(task.Point, 1.85f, task.Name, DomeStory.Accent, nearby == i, scale); } GUI.color = previousColor; return; } for (int i = 0; i < PointRanges.Length; i++) { if (i == Airlock) { bool open = RepairedMask == AllRepaired; DrawMarker(i, open ? "Airlock open" : "Airlock", open ? PartColors[2] : new Color32(178, 196, 204, 255), scale); } else if (i < PartNames.Length) { DrawMarker(i, StationNames[i], PartColors[i], scale); } else { int part = i - PartNames.Length; if (Carried != part && !IsRepaired(part)) DrawMarker(i, PartNames[part], PartColors[part], scale); } } GUI.color = previousColor; } private void DrawMarker(int index, string label, Color accent, float scale) { DrawMarker(DomeEnvironment.Site(index), MarkerHeights[index], label, accent, index == nearby, scale); } private void DrawMarker(Vector2 point, float height, string label, Color accent, bool active, float scale) { Vector3 worldPoint = environmentRoot.TransformPoint(new Vector3(point.x, environment.SurfaceY(point) + height, point.y)); Vector3 projected = domeCamera.WorldToScreenPoint(worldPoint); if (projected.z <= 0f || projected.x < 2f || projected.x > Screen.width - 2f || projected.y < 2f || projected.y > Screen.height - 2f) return; float width = Mathf.Max(52f, label.Length * 7 + 20f) * scale; Rect rect = new(projected.x - width * 0.5f, Screen.height - projected.y - 9f * scale, width, 20f * scale); GUI.color = active ? accent : new Color32(27, 39, 49, 235); GUI.DrawTexture(rect, Texture2D.whiteTexture); GUI.color = accent; GUI.DrawTexture(new Rect(rect.x, rect.y, 3f * scale, rect.height), Texture2D.whiteTexture); GUI.color = active ? new Color32(27, 39, 49, 255) : new Color32(241, 249, 244, 255); GUI.Label(rect, label, markerStyle); } } }