gmtk24/scripts/Hud.cs

105 lines
3.7 KiB
C#
Raw Permalink Normal View History

2024-08-19 22:27:26 +02:00
using Godot;
2024-08-20 16:49:13 +02:00
using Godot.Collections;
using System.Linq;
2024-08-19 22:27:26 +02:00
using System.Text;
namespace Gmtk24 {
public partial class Hud : Control {
[Export]
public Player Player;
[Export]
2024-08-20 16:49:13 +02:00
public Label HoverText;
[Export]
public bool InteractionPaused = false;
2024-08-20 15:58:22 +02:00
[Export]
public Container CameraHeightContainer;
[Export]
public Slider CameraHeightSlider;
2024-08-20 16:49:13 +02:00
[Export]
public Label CornerPrompt;
2024-08-19 22:27:26 +02:00
private Interactible Hovered;
2024-08-20 16:49:13 +02:00
private Dictionary<string, string> ButtonNames = new();
2024-08-19 22:27:26 +02:00
public override void _Process(double delta) {
if (Player == null)
return;
2024-08-19 23:33:24 +02:00
GodotObject collider = null;
2024-08-19 22:27:26 +02:00
if (Input.MouseMode == Input.MouseModeEnum.Captured) {
2024-08-19 23:33:24 +02:00
if (Player.Eye.Current)
collider = Player.LookingAt.GetCollider();
2024-08-19 22:27:26 +02:00
} else {
2024-08-20 00:50:55 +02:00
var results = Util.RaycastFromMouse(GetViewport().GetCamera3D());
collider = results?.Collider;
2024-08-19 22:27:26 +02:00
}
2024-08-20 16:49:13 +02:00
if (!InteractionPaused) {
if (collider is Interactible interactible) {
if (interactible != Hovered) {
Hovered?.SetHovered(false);
interactible.SetHovered(true);
}
2024-08-19 23:33:24 +02:00
2024-08-20 16:49:13 +02:00
Hovered = interactible;
2024-08-19 22:27:26 +02:00
2024-08-20 16:49:13 +02:00
HoverText.Text = $"{FormButtonNames("interact")}: {interactible.InteractName}";
} else {
HoverText.Text = "";
2024-08-19 22:27:26 +02:00
}
} else {
2024-08-19 23:33:24 +02:00
Hovered?.SetHovered(false);
2024-08-19 22:27:26 +02:00
Hovered = null;
2024-08-20 16:49:13 +02:00
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 = "";
}
2024-08-19 22:27:26 +02:00
}
}
public override void _UnhandledInput(InputEvent @event) {
2024-08-20 00:50:55 +02:00
if (@event.IsAction("interact") && Hovered != null) {
2024-08-19 22:27:26 +02:00
Hovered.HandleInput(@event);
GetViewport().SetInputAsHandled();
2024-08-19 22:27:26 +02:00
}
}
2024-08-20 16:49:13 +02:00
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();
}
2024-08-19 22:27:26 +02:00
}
}