Files
PolyphiaGame/Scripts/Game/Tools/SelectTool.cs
T
2025-12-17 19:13:10 -08:00

122 lines
4.7 KiB
C#

using Godot;
using Godot.Collections;
public partial class SelectTool : Node3D, ITool {
public int ToolID { get{ return -1; } } // -1 for non-existant tool stat
Options _Options;
Reference _Reference;
[Export] PackedScene SelectToolFrame;
[Export] Material HoverMaterial;
Material _defaultMaterial;
MeshInstance3D _lookingAt;
Control _SelectToolFrame;
bool mouseDown = false;
bool mouseDownDebounce = false;
bool equipped = false;
public (bool, Dictionary) screenSpaceToRay( float Distance ) {
Vector2 ClickPos = GetViewport().GetMousePosition();
Vector3 from = _Reference.Camera.ProjectRayOrigin( ClickPos );
Vector3 to = from + _Reference.Camera.ProjectRayNormal( ClickPos ) * Distance;
PhysicsDirectSpaceState3D spaceState = GetWorld3D().DirectSpaceState;
Dictionary rayArray = spaceState.IntersectRay(new PhysicsRayQueryParameters3D(){ From=from, To=to });
if( rayArray.Count > 0 ) {
return (true, rayArray);
}
return (false, null);
}
void ClearLookingAt() {
if( _lookingAt != null ) {
_lookingAt.SetSurfaceOverrideMaterial( 0, _defaultMaterial );
_lookingAt = null;
_SelectToolFrame.Visible = false;
}
}
public override void _PhysicsProcess( double delta ) {
if( equipped ) {
var x = screenSpaceToRay(_Options.ClickDistance);
if( x.Item1 ) {
Node test = (Node)x.Item2["collider"];
if( test is Pickup ) {
int ItemID = ((Pickup)test).ItemID;
int Quantity = ((Pickup)test).Quantity;
GameItem item = new GameItem(ItemID);
item.GameObject = test;
if (item.ItemStats.IsQuantityItem) {
item.InventoryObject.Quantity = Quantity;
}
if( _lookingAt == null ) {
_lookingAt = test.GetChild<MeshInstance3D>( 1 );
_defaultMaterial = _lookingAt.GetSurfaceOverrideMaterial( 0 );
_lookingAt.SetSurfaceOverrideMaterial( 0, HoverMaterial );
_SelectToolFrame.Visible = true;
_SelectToolFrame.GetChild<Label>( 0 ).Text = item.ItemStats.ItemName;
_SelectToolFrame.GetChild<Label>( 1 ).Text = "Slot : " + item.ItemStats.InventorySize.ToString();
_SelectToolFrame.GetChild<Label>( 2 ).Text = "Weight : " + item.ItemStats.InventoryWeight;
if (item.ItemStats.IsQuantityItem ) {
_SelectToolFrame.GetChild<Label>( 3 ).Text = "Capacity : " + item.InventoryObject.Quantity + "/" + item.ItemStats.MaxQuantity;
_SelectToolFrame.Size = new Vector2( _SelectToolFrame.Size.X, 95 );
} else {
_SelectToolFrame.GetChild<Label>( 3 ).Text = "";
_SelectToolFrame.Size = new Vector2( _SelectToolFrame.Size.X, 72 );
}
}else if (_lookingAt != test.GetChild<MeshInstance3D>( 1 ) ) {
ClearLookingAt();
}
if( mouseDown && mouseDownDebounce ) {
mouseDownDebounce = false;
bool PickedUp = _Reference.Inventory.AddInventory(item);
if( PickedUp ) {
ClearLookingAt();
_Reference.GameHandler.ClearItemNetworked( test.Name );
}
}
} else {
ClearLookingAt();
}
} else {
ClearLookingAt();
}
}
}
public void Equiped() {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
_SelectToolFrame = SelectToolFrame.Instantiate<Control>();
GetNode( "/root/GameRoot/UI/GameUI/Game" ).AddChild( _SelectToolFrame );
equipped = true;
}
public void LeftMouseDown() {
mouseDown = true;
mouseDownDebounce = true;
}
public void LeftMouseUp() {
mouseDown = false;
mouseDownDebounce = false;
}
public void KeyPress( Key key ) {}
public void KeyRelease( Key key ) {}
public void RightMouseDown() {}
public void RightMouseUp() {}
public void UnEquipped() {
if (_SelectToolFrame != null ) {
_SelectToolFrame.Free();
}
equipped = false;
}
}