using UnityEngine; namespace DomeShift { public enum GamePhase { Title, Playing, Paused, Won, Lost } public enum PlayableCharacter { Bud, Doyle } [DisallowMultipleComponent] [RequireComponent(typeof(DomeEnvironment))] public sealed class DomeGame : MonoBehaviour { 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 DomeEnvironment environment; private Transform environmentRoot; private DomeWorld world; 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; public GamePhase Phase { get; private set; } = GamePhase.Title; public PlayableCharacter Character { get; private set; } = PlayableCharacter.Bud; public bool Exploring { get; private set; } 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 string Prompt { get { 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(); environmentRoot = environment.BuildForGame(); world = DomeWorld.Create(environmentRoot, environment); world.SetCharacter(Character); 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 (Exploring && 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; } 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(false); } public void StartExplore() { EnterDome(true); } private void EnterDome(bool explore) { ResetRun(); Exploring = explore; Overview = false; SetCutaway(true); Dispatch = explore ? $"{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 && Exploring)) return; ResetRun(); Exploring = false; Overview = false; SetCutaway(false); Phase = GamePhase.Title; } public void Restart() { EnterDome(Exploring); } 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; world.Tick(Vector2.zero, Carried, RepairedMask, worldTime); } private void ResetRun() { TimeLeft = RoundLength; RepairedMask = 0; Carried = -1; nearby = -1; worldTime = 0f; MapOpen = false; Dispatch = ""; 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.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 (!Exploring) TimeLeft = Mathf.Max(0f, TimeLeft - Time.deltaTime); if (!Exploring && 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(Vector2.ClampMagnitude(input, 1f) * (WalkSpeed * Mathf.Min(Time.deltaTime, 0.08f))); nearby = ClosestPoint(); if (Input.GetKeyDown(KeyCode.E)) { Interact(); nearby = 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 void AnimateWorld(Vector2 motion) { worldTime += Time.deltaTime; world.Tick(motion, Carried, RepairedMask, 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) { 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 FrameDome() { bool title = Phase == GamePhase.Title; float aspect = Mathf.Max(domeCamera.aspect, 0.1f); if (title || Overview) { float shift = title ? Mathf.Min(environment.Width * 0.18f, aspect * 9f) : 0f; Vector3 center = environmentRoot.position + new Vector3(-shift, environment.RoofHeight * 0.23f, -1f); float projectedWidth = environment.Width * 0.5f + environment.Depth * 0.09f + 4.5f + shift; domeCamera.transform.position = center + new Vector3(15f, 67f, -79f); domeCamera.transform.LookAt(center); domeCamera.orthographicSize = Mathf.Max(environment.Depth * 0.35f + environment.RoofHeight * 0.62f + 3f, projectedWidth / aspect); return; } Vector3 lookAt = world.Player.position + new Vector3(0f, 0.5f, 1.15f); domeCamera.transform.position = lookAt + new Vector3(0f, 16f, -20f); domeCamera.transform.LookAt(lookAt); domeCamera.orthographicSize = Mathf.Max(9.4f, 11.5f / aspect); } 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; 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) { Vector2 point = DomeEnvironment.Site(index); Vector3 worldPoint = environmentRoot.TransformPoint(new Vector3(point.x, environment.SurfaceY(point) + MarkerHeights[index], 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); bool active = index == nearby; 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); } } }