Inventory management focused game written in Godot / C#
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.2 KiB

public partial class PickupController : Node3D
11 months ago
{
public Item CurrentItem { get; private set; }
public bool HasItemsHeld => GetChildCount() > 0;
Node3D _placementPreview;
11 months ago
[Export] public Camera3D Camera { get; set; }
[Export] public float PickupDistance { get; set; } = 2.0f;
Player _player;
11 months ago
Node3D _world;
public override void _Ready()
11 months ago
{
_player = GetParent<Player>();
11 months ago
// TODO: Find a better way to find the world.
_world = GetNode<Node3D>("/root/Game/Workshop");
11 months ago
}
public override void _UnhandledInput(InputEvent @event)
{
if (!_player.IsLocal) return;
11 months ago
EnsureCurrentItemValid();
if (CurrentItem == null) return;
if (@event.IsActionPressed("interact_pickup")) {
if (!HasItemsHeld) {
// Create clone of the item's model, use it as placement preview.
_placementPreview = (Node3D)CurrentItem.Model.Duplicate(0);
_placementPreview.Name = "PlacementPreview";
_placementPreview.TopLevel = true;
_placementPreview.Visible = false;
SetMeshLayerOutline(_placementPreview, OutlineMode.Exclusive);
AddChild(_placementPreview);
11 months ago
// Parent item to the `PickupController`.
var prevRot = CurrentItem.GlobalRotation;
CurrentItem.GetParent().RemoveChild(CurrentItem);
AddChild(CurrentItem);
SetMeshLayerOutline(CurrentItem.Model, OutlineMode.Disable);
11 months ago
CurrentItem.Position = Vector3.Zero;
CurrentItem.GlobalRotation = prevRot;
CurrentItem.CollisionLayer &= (uint)~PhysicsLayer.Static;
// CurrentItem.Freeze = true;
GetViewport().SetInputAsHandled();
}
} else if (@event.IsActionPressed("interact_place")) {
if (HasItemsHeld) {
11 months ago
// Parent item back to the world.
var prevTransform = CurrentItem.GlobalTransform;
CurrentItem.CollisionLayer |= (uint)PhysicsLayer.Static;
// CurrentItem.Freeze = false;
RemoveChild(CurrentItem);
_world.AddChild(CurrentItem);
CurrentItem.GlobalTransform = _placementPreview.Visible
? _placementPreview.GlobalTransform
: prevTransform;
RemoveChild(_placementPreview);
_placementPreview.QueueFree();
_placementPreview = null;
11 months ago
GetViewport().SetInputAsHandled();
}
}
}
public override void _PhysicsProcess(double delta)
{
if (!_player.IsLocal) return;
11 months ago
EnsureCurrentItemValid();
if (HasItemsHeld) {
11 months ago
if ((RayToMouseCursor() is RayResult ray) && (ray.Collider is Grid)) {
// Snao rotation to nearest axis.
// FIXME: This needs to snap relative to the grid rotation.
var globalRot = CurrentItem.GlobalRotation;
globalRot.X = Snapped(globalRot.X, Tau / 4);
globalRot.Y = Snapped(globalRot.Y, Tau / 4);
globalRot.Z = Snapped(globalRot.Z, Tau / 4);
_placementPreview.GlobalRotation = globalRot;
11 months ago
// Snap the position to the grid.
var halfSize = (Vector3)CurrentItem.Size * Grid.StepSize / 2;
var pos = ray.Position + halfSize * (ray.Normal * _placementPreview.GlobalTransform.Basis);
11 months ago
pos = pos.Snapped(Grid.StepSize * Vector3.One); // FIXME: This does global snapping only
_placementPreview.GlobalPosition = pos;
_placementPreview.Visible = true;
} else {
_placementPreview.Visible = false;
11 months ago
}
} else {
var interactable = RayToMouseCursor()?.Collider;
// Remove the outline from the previously looked-at item.
if (CurrentItem != null) SetMeshLayerOutline(CurrentItem.Model, OutlineMode.Disable);
11 months ago
// If the ray hits anything and the object hit is an item, set it as current.
CurrentItem = interactable as Item;
// Add the outline to the currently looked-at item.
if (CurrentItem != null) SetMeshLayerOutline(CurrentItem.Model, OutlineMode.Enable);
11 months ago
}
}
void EnsureCurrentItemValid()
{
if (CurrentItem == null) return;
if (!IsInstanceValid(CurrentItem)) {
CurrentItem = null;
if (_placementPreview != null) {
RemoveChild(_placementPreview);
_placementPreview.QueueFree();
_placementPreview = null;
}
11 months ago
}
}
RayResult RayToMouseCursor()
{
var mouse = GetViewport().GetMousePosition();
var from = Camera.ProjectRayOrigin(mouse);
var to = from + Camera.ProjectRayNormal(mouse) * PickupDistance;
var query = PhysicsRayQueryParameters3D.Create(from, to);
query.CollisionMask = (uint)PhysicsLayer.Interactable;
query.CollideWithAreas = true;
// Exclude the `CurrentItem` from collision checking if it's being held.
query.Exclude = HasItemsHeld ? [ CurrentItem.GetRid() ] : [];
11 months ago
var result = GetWorld3D().DirectSpaceState.IntersectRay(query);
return (result.Count > 0) ? new(result) : null;
}
class RayResult(Dictionary dict)
{
public CollisionObject3D Collider { get; } = dict["collider"].As<CollisionObject3D>();
public Vector3 Position { get; } = (Vector3)dict["position"];
public Vector3 Normal { get; } = (Vector3)dict["normal"];
}
enum OutlineMode { Disable, Enable, Exclusive }
static void SetMeshLayerOutline(Node3D parent, OutlineMode mode)
{
var children = parent.FindChildren("*", "MeshInstance3D", owned: false);
foreach (var mesh in children.Cast<MeshInstance3D>())
switch (mode) {
case OutlineMode.Disable: mesh.Layers &= ~(uint)RenderLayer.Outline; break;
case OutlineMode.Enable: mesh.Layers |= (uint)RenderLayer.Outline; break;
case OutlineMode.Exclusive: mesh.Layers = (uint)RenderLayer.Outline; break;
}
}
11 months ago
}