using Godot; using Godot.Collections; using System.Linq; using System.Text; namespace Gmtk24 { public partial class Hud : Control { [Export] public Player Player; [Export] public Label HoverText; [Export] public bool InteractionPaused = false; [Export] public Container CameraHeightContainer; [Export] public Slider CameraHeightSlider; [Export] public Label CornerPrompt; private Interactible Hovered; private Dictionary ButtonNames = new(); public override void _Process(double delta) { if (Player == null) return; GodotObject collider = null; if (Input.MouseMode == Input.MouseModeEnum.Captured) { if (Player.Eye.Current) collider = Player.LookingAt.GetCollider(); } else { var results = Util.RaycastFromMouse(GetViewport().GetCamera3D()); collider = results?.Collider; } if (!InteractionPaused) { if (collider is Interactible interactible) { if (interactible != Hovered) { Hovered?.SetHovered(false); interactible.SetHovered(true); } Hovered = interactible; HoverText.Text = $"{FormButtonNames("interact")}: {interactible.InteractName}"; } else { HoverText.Text = ""; } } else { Hovered?.SetHovered(false); Hovered = null; if (Player.Table.Orbit.HeldBlock != null) { var release = FormButtonNames("release_block"); var place = FormButtonNames("place_block"); var rotate_right = FormButtonNames("rotate_block_right"); var rotate_left = FormButtonNames("rotate_block_left"); HoverText.Text = $"{place}: Place Block\n{release}: Throw Block\nRotate Block: ({rotate_right}/{rotate_left})"; } else { HoverText.Text = ""; } } if (!Player.Table.Orbit.IsEnabled) { var names = FormButtonNames("toggle_table"); if (Player.Table.IsEnabled) CornerPrompt.Text = $"{names}: {"Recall Table"}"; else CornerPrompt.Text = $"{names}: {"Spawn Table"}"; } else { if (Player.Table.Orbit.HeldBlock == null) { var zoom_out = FormButtonNames("zoom_orbit_out"); var zoom_in = FormButtonNames("zoom_orbit_out"); var drag = FormButtonNames("drag_orbit"); CornerPrompt.Text = $"Zoom: {zoom_in}/{zoom_out}\nDrag: {drag}"; } else { CornerPrompt.Text = ""; } } } public override void _UnhandledInput(InputEvent @event) { if (@event.IsAction("interact") && Hovered != null) { Hovered.HandleInput(@event); GetViewport().SetInputAsHandled(); } } private string FormButtonNames(string actionName) { if (!ButtonNames.ContainsKey(actionName)) { ButtonNames.Add(actionName, InputMap.ActionGetEvents(actionName) .Select(e => e.AsText().Replace(" (Physical)", "")) .Aggregate((a, b) => $"{a}, {b}")); } return ButtonNames[actionName]; } public void ResetPromptCache() { ButtonNames.Clear(); } } }