init commit

This commit is contained in:
2025-05-12 18:07:43 -07:00
commit b410345c18
1023 changed files with 57521 additions and 0 deletions
+550
View File
@@ -0,0 +1,550 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// Remove all the global positions from this file
public partial class Inventory : Panel {
Options _Options;
Reference _Reference;
[Export] PackedScene SplitFrame;
Vector2I InventorySize = new Vector2I(4, 15);
int MaxWeight;
List<(InventoryItem, Panel)> _Inventory;
Vector2 _GridSize;
ProgressBar _WeightBar;
HeldItem _itemBeingHeld;
HeldItem __h;
HeldItem _lastRightClickedItem {
get {
return __h;
}
set {
if (_splitFrame != null) {
_splitFrame.Free();
_splitFrame = null;
}
__h = value;
}
}
public Control _splitFrame;
public override void _EnterTree() {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
_Reference.Inventory = this;
_Inventory = new List<(InventoryItem, Panel)>();
MaxWeight = 400;
Control GridBackdrop = GetChild(0).GetChild<Control>( 0 );
_WeightBar = GetChild<ProgressBar>( 1 );
// Scale Scrollbar
Vector2 FrameSize = GetChild<Control>(0).Size;
GridBackdrop.CustomMinimumSize = new Vector2( GetChild<Control>(0).Size.X - 8 , InventorySize.Y * FrameSize.X / InventorySize.X );
GridBackdrop.Size = GridBackdrop.CustomMinimumSize;
_GridSize = new Vector2( GridBackdrop.Size.X / InventorySize.X, GridBackdrop.Size.Y / InventorySize.Y );
_WeightBar.MaxValue = MaxWeight;
// Horizontal Grid Bars
for (int y=0; y< InventorySize.Y - 1; y++) {
HSeparator cur = new HSeparator{
Position = new Vector2(0, _GridSize.Y * (y + 1) - 2),
Size = new Vector2(GridBackdrop.Size.X, 4)
};
GetChild(0).GetChild(0).AddChild( cur );
}
// Vertical Grid Bars
for (int x=0; x< InventorySize.X - 1; x++) {
VSeparator cur = new VSeparator{
Position = new Vector2(_GridSize.X * (x + 1) - 2, 0),
Size = new Vector2(4, GridBackdrop.Size.Y)
};
GetChild(0).GetChild(0).AddChild( cur );
}
GetWeight();
}
public void UpdateMaxWeight(int maxWeight){
MaxWeight = maxWeight;
_WeightBar.MaxValue = MaxWeight;
GetWeight();
}
//------------------------------//
// Public Functions //
//------------------------------//
public int GetWeight() {
int _w = 0;
foreach( (InventoryItem, Control) cur in _Inventory ) {
_w += cur.Item1.Weight;
}
_WeightBar.Value = _w;
_WeightBar.GetChild<Label>( 0 ).Text = _w.ToString() + " / " + MaxWeight.ToString();
return _w;
}
public void UpdateItem( InventoryItem item ) {
foreach( (InventoryItem, Panel) cur in _Inventory ) {
if( item.Guid == cur.Item1.Guid ) {
cur.Item1.Quantity = item.Quantity;
cur.Item2.GetChild<MenuButton>( 0 ).Text = item.Name + "\n" +
item.Quantity + " / " + item.MaxQuantity;
break;
}
}
}
public bool AddInventory( InventoryItem Item ) {
Item.Rotated = false;
if( GetWeight() + Item.Weight > MaxWeight ) {
_Reference.Alert.PerformAlert( "Max weight!" );
return false;
}
for( int rotation = 0; rotation <= 1; rotation++ ) {
// Try both rotations of the block
Vector2I tempSize = Item.GridSize;
if( rotation == 1 ) {
Item.Rotated = true;
tempSize = new Vector2I( Item.GridSize.Y, Item.GridSize.X );
}
// Go through the inventory grid minus overflow from the size of the item were trying to place
for( int yindex = 0; yindex <= (InventorySize.Y - tempSize.Y); yindex++ ) {
for( int xindex = 0; xindex <= (InventorySize.X - tempSize.X); xindex++ ) {
if( CheckBlock( tempSize, new Vector2I( xindex, yindex ), null ) ) {
// We found our space
Panel ItemBackDrop = new Panel();
GetChild( 0 ).GetChild( 0 ).AddChild( ItemBackDrop );
ItemBackDrop.Size = new Vector2( _GridSize.X * tempSize.X - 10, _GridSize.Y * tempSize.Y - 10 );
ItemBackDrop.Position = new Vector2( xindex * _GridSize.X + 5, yindex * _GridSize.Y + 5 );
ItemBackDrop.GuiInput += frameInput;
MenuButton dropDown = new MenuButton();
if( Item.isQuantityItem ) {
dropDown.Text = Item.Name + "\n" +
Item.Quantity + " / " + Item.MaxQuantity;
} else {
dropDown.Text = Item.Name;
}
dropDown.SetAnchorsPreset( LayoutPreset.FullRect );
ItemBackDrop.AddChild( dropDown );
dropDown.ButtonMask = MouseButtonMask.Right;
dropDown.GuiInput += frameInput;
dropDown.GetPopup().IndexPressed += DropdownPressed;
foreach( string cur in Enum.GetNames( typeof( InventoryOptions ) ) ) {
if( cur == "Split" && !Item.isQuantityItem ) {
continue;
}
dropDown.GetPopup().AddItem( cur );
}
Item.GridPosition = new Vector2I( xindex, yindex );
_Inventory.Add( (Item, ItemBackDrop) );
GetWeight();
// Add Tool if its a tool
if (!string.IsNullOrEmpty(Item.ToolPath)){
PackedScene toolCopy = GD.Load<PackedScene>(Item.ToolPath);
Label tool = toolCopy.Instantiate<Label>();
// Place tool into toolbar in the correct spot and shift everything down to zero if need be
Node[] toolbarChildren = _Reference.ToolBar.GetChildren().ToArray();
for( int i=0; i<toolbarChildren.Count(); i++ ){
Label cur = (Label)toolbarChildren[i];
if ( cur.Text.Substring(1, 2) == ". " ){
cur.Text = (i+1).ToString() + ". " + cur.Text.Substring(3);
}
}
Debug.WriteLine( tool.Text.Substring(1, 2) );
if ( tool.Text.Substring(1, 2) == ". " ){
tool.Text = (toolbarChildren.Count()+1).ToString() + ". " + tool.Text.Substring(3);
} else {
tool.Text = (toolbarChildren.Count()+1).ToString() + ". " + tool.Text;
}
_Reference.ToolBar.AddChild( tool );
}
return true;
}
}
}
}
_Reference.Alert.PerformAlert( "No Space" );
// No space was found
return false;
}
public void DropInventory( InventoryItem item, Panel frame ) {
string[] PickupItems = _Reference.ItemSpawner._SpawnableScenes;
PackedScene ps = GD.Load<PackedScene>(PickupItems[item.ItemID]);
Pickup prefab = ps.Instantiate<Pickup>();
prefab.GlobalTransform = _Reference.Humanoid.GlobalTransform;
prefab.Quantity = item.Quantity;
_Reference.ItemSpawnPath.AddChild( prefab );
RemoveInventory( item, frame );
}
public void RemoveInventory( InventoryItem item, Panel frame ) {
foreach(Label cur in _Reference.ToolBar.GetChildren()){
if ( cur.GetChild(0) is ITool ){
ITool x = (ITool)cur.GetChild(0);
if ( x.ToolID == item.ItemID ){
if(_Reference.Humanoid.EquippedTool == x){
_Reference.Humanoid.EquipTool(0);
}
cur.QueueFree();
break;
}
}
}
_Inventory.Remove( (item, frame) ) ;
frame.Free();
GetWeight();
}
//------------------------------//
// Mouse Functions //
//------------------------------//
void MouseDown( Vector2 mousePos ) {
(InventoryItem, Panel)? clicked = GetItemFromScreen( mousePos );
if( clicked.HasValue ) {
_itemBeingHeld = new HeldItem {
MouseStart = mousePos,
MouseOffset = clicked.Value.Item2.Position - mousePos,
Frame = clicked.Value.Item2,
Item = clicked.Value.Item1
};
}
}
void MouseMove( Vector2 mousePos ) {
if( _itemBeingHeld != null ) {
Vector2I TempSize = _itemBeingHeld.Item.GridSize;
if( _itemBeingHeld.Item.Rotated ) {
TempSize = new Vector2I( _itemBeingHeld.Item.GridSize.Y, _itemBeingHeld.Item.GridSize.X );
}
Vector2 newFramePos = mousePos + _itemBeingHeld.MouseOffset;
if( newFramePos.X < 0 ) {
newFramePos.X = 0;
}
if( newFramePos.Y < 0 ) {
newFramePos.Y = 0;
}
if( newFramePos.X > _GridSize.X * InventorySize.X - (_GridSize.X * TempSize.X) ) {
newFramePos.X = _GridSize.X * InventorySize.X - (_GridSize.X * TempSize.X);
}
if( newFramePos.Y > _GridSize.Y * InventorySize.Y - (_GridSize.Y * TempSize.Y) ) {
newFramePos.Y = _GridSize.Y * InventorySize.Y - (_GridSize.Y * TempSize.Y);
}
_itemBeingHeld.Frame.Position = newFramePos;
}
}
void MouseUp( Vector2 mousePos ) {
if( _itemBeingHeld != null ) {
Vector2I TempSize = _itemBeingHeld.Item.GridSize;
if( _itemBeingHeld.Item.Rotated ) {
TempSize = new Vector2I( _itemBeingHeld.Item.GridSize.Y, _itemBeingHeld.Item.GridSize.X );
}
Vector2 FinalGrid = new Vector2(
Mathf.Round((mousePos.X + _itemBeingHeld.MouseOffset.X) / _GridSize.X),
Mathf.Round((mousePos.Y + _itemBeingHeld.MouseOffset.Y) / _GridSize.Y)
);
if( FinalGrid.X < 0 ) { FinalGrid.X = 0; }
if( FinalGrid.Y < 0 ) { FinalGrid.Y = 0; }
if( FinalGrid.X > (InventorySize.X - TempSize.X) ) { FinalGrid.X = InventorySize.X - TempSize.X; }
if( FinalGrid.Y > (InventorySize.Y - TempSize.Y) ) { FinalGrid.Y = InventorySize.Y - TempSize.Y; }
if( CheckBlock( TempSize, new Vector2I( Convert.ToInt32( FinalGrid.X ), Convert.ToInt32( FinalGrid.Y )), _itemBeingHeld.Item ) ) { // Check fitment
_itemBeingHeld.Item.GridPosition = new Vector2I( Convert.ToInt32( FinalGrid.X ), Convert.ToInt32( FinalGrid.Y ) );
_itemBeingHeld.Frame.Position = _GridSize * FinalGrid + new Vector2( 5, 5 );
} else {
(InventoryItem, Panel)? itemAtSpot = GetItemFromScreen( mousePos );
if( itemAtSpot.Value.Item1.isQuantityItem && itemAtSpot.Value.Item1.ItemID == _itemBeingHeld.Item.ItemID ) {
// Try Combine
int MaxQuantity = _itemBeingHeld.Item.MaxQuantity;
int Total = itemAtSpot.Value.Item1.Quantity + _itemBeingHeld.Item.Quantity;
if( Total > MaxQuantity ) {
int remainder = Total - MaxQuantity;
itemAtSpot.Value.Item1.Quantity = MaxQuantity;
_itemBeingHeld.Item.Quantity = remainder;
_itemBeingHeld.Frame.Position = new Vector2(
_itemBeingHeld.Item.GridPosition.X * _GridSize.X + 5,
_itemBeingHeld.Item.GridPosition.Y * _GridSize.Y + 5
);
} else {
itemAtSpot.Value.Item1.Quantity = Total;
MenuButton button = itemAtSpot.Value.Item2.GetChild<MenuButton>(0);
button.Text = itemAtSpot.Value.Item1.Name + "\n" +
itemAtSpot.Value.Item1.Quantity + " / " + itemAtSpot.Value.Item1.MaxQuantity;
RemoveInventory( _itemBeingHeld.Item, _itemBeingHeld.Frame );
}
} else {
_itemBeingHeld.Frame.Position = new Vector2(
_itemBeingHeld.Item.GridPosition.X * _GridSize.X + 5,
_itemBeingHeld.Item.GridPosition.Y * _GridSize.Y + 5
);
}
}
_itemBeingHeld = null;
}
}
void RightMouseDown( Vector2 mousePos ) {
(InventoryItem, Panel)? clicked = GetItemFromScreen( mousePos );
if( clicked.HasValue ) {
_lastRightClickedItem = new HeldItem {
Frame = clicked.Value.Item2,
Item = clicked.Value.Item1,
MouseStart = mousePos,
};
}
}
//------------------------------//
// Screen Space Math //
//------------------------------//
(InventoryItem, Panel)? GetItemFromScreen( Vector2 screenPos ) {
Vector2 ClickedGrid = new Vector2( Mathf.Floor(screenPos.X / _GridSize.X), Mathf.Floor(screenPos.Y / _GridSize.Y) );
foreach( (InventoryItem, Panel) cur in _Inventory ) {
Vector2I TempSize = cur.Item1.GridSize;
if( cur.Item1.Rotated ) {
TempSize = new Vector2I( cur.Item1.GridSize.Y, cur.Item1.GridSize.X );
}
if( ClickedGrid.X >= cur.Item1.GridPosition.X && ClickedGrid.X <= (cur.Item1.GridPosition.X + TempSize.X - 1) &&
ClickedGrid.Y >= cur.Item1.GridPosition.Y && ClickedGrid.Y <= (cur.Item1.GridPosition.Y + TempSize.Y - 1) ) {
return cur;
}
}
return null;
}
Vector2 TranslateGlobalToFramePosition( Vector2 globalPos ) {
ScrollContainer InventoryFrame = GetChild<ScrollContainer>( 0 );
Vector2 FrameGlobalPos = InventoryFrame.GlobalPosition;
Vector2 ScrollAmount = new Vector2( InventoryFrame.ScrollHorizontal, InventoryFrame.ScrollVertical );
Vector2 localPos = globalPos - FrameGlobalPos + ScrollAmount;
return localPos;
}
bool CheckBlock( Vector2I BlockSize, Vector2I BlockGridPostion, InventoryItem Ignore ) {
for( int y = 0; y < BlockSize.Y; y++ ) {
for( int x = 0; x < BlockSize.X; x++ ) {
foreach( (InventoryItem, Control) cur in _Inventory ) {
// Check Ignore
if( Ignore != null && cur.Item1.Guid == Ignore.Guid ) {
continue;
}
// Properly rotate block
Vector2I tempSize;
if( !cur.Item1.Rotated ) {
tempSize = cur.Item1.GridSize;
} else {
tempSize = new Vector2I( cur.Item1.GridSize.Y, cur.Item1.GridSize.X );
}
// Check fit
Vector2I curTopLeft = cur.Item1.GridPosition;
Vector2I curBotRight = new Vector2I( cur.Item1.GridPosition.X + tempSize.X - 1, cur.Item1.GridPosition.Y + tempSize.Y - 1 );
if( (x + BlockGridPostion.X) >= curTopLeft.X && (x + BlockGridPostion.X) <= curBotRight.X &&
(y + BlockGridPostion.Y) >= curTopLeft.Y && (y + BlockGridPostion.Y) <= curBotRight.Y ) {
return false;
}
}
}
}
return true;
}
void frameInput( InputEvent iEvent ) {
if ( iEvent is InputEventMouseButton mouseButton) {
if( mouseButton.ButtonIndex == MouseButton.Left ) {
if( mouseButton.Pressed ) {
MouseDown( TranslateGlobalToFramePosition(mouseButton.GlobalPosition) );
} else {
MouseUp( TranslateGlobalToFramePosition( mouseButton.GlobalPosition ) );
}
} else if( mouseButton.ButtonIndex == MouseButton.Right ) {
if ( mouseButton.Pressed ) {
RightMouseDown( TranslateGlobalToFramePosition( mouseButton.GlobalPosition ) );
}
}
} else if( iEvent is InputEventMouseMotion mouseMotion) {
MouseMove( TranslateGlobalToFramePosition( mouseMotion.GlobalPosition ) );
}
}
//------------------------------//
// Right Click Functions //
//------------------------------//
(bool, Vector2I) TryRotate( InventoryItem item ) {
Vector2I tempSize;
if( item.Rotated ) {
tempSize = item.GridSize;
} else {
tempSize = new Vector2I( item.GridSize.Y, item.GridSize.X );
}
// Go through the inventory grid minus overflow from the size of the item were trying to place
for( int yindex = 0; yindex <= (InventorySize.Y - tempSize.Y); yindex++ ) {
for( int xindex = 0; xindex <= (InventorySize.X - tempSize.X); xindex++ ) {
if( CheckBlock( tempSize, new Vector2I( xindex, yindex ), item ) ) {
return (true, new Vector2I(xindex, yindex));
}
}
}
return (false, Vector2I.Zero);
}
string GetNumbers( String InputString ) {
String Result = "";
string Numbers = "0123456789";
int i = 0;
for( i = 0; i < InputString.Length; i++ ) {
if( Numbers.Contains( InputString.ElementAt( i ) ) ) {
Result += InputString.ElementAt( i );
}
}
return Result;
}
void SplitFrame_Split() {
int quantity = Convert.ToInt32( _splitFrame.GetChild<LineEdit>( 1 ).Text );
InventoryItem x = _lastRightClickedItem.Item;
if (quantity <= x.Quantity ) {
PickupItem test = new PickupItem( x.ItemID, x.Name, x.GridSize, x.Weight, x.isQuantityItem, quantity, x.MaxQuantity );
if( AddInventory( test ) ) {
_lastRightClickedItem.Item.Quantity -= quantity;
UpdateItem( _lastRightClickedItem.Item );
} else {
_Reference.Alert.PerformAlert( "Not enough space to split" );
}
}
_splitFrame.Free();
_splitFrame = null;
}
void SplitFrame_Cancel() {
_splitFrame.Free();
_splitFrame = null;
}
void SplitFrame_TextChanged( string NewText ) {
string output = GetNumbers( NewText );
if ( string.IsNullOrEmpty( output ) ) {
output = "0";
}
int test = Convert.ToInt32(output);
if (test < 0) {
test = 0;
}else if (test > _lastRightClickedItem.Item.MaxQuantity) {
test = _lastRightClickedItem.Item.MaxQuantity;
}
_splitFrame.GetChild<LineEdit>( 1 ).Text = test.ToString();
_splitFrame.GetChild<LineEdit>( 1 ).CaretColumn = 50;
}
void DropdownPressed( long index ) {
InventoryOptions pressed = (InventoryOptions)index;
if( pressed == InventoryOptions.Drop ) {
DropInventory( _lastRightClickedItem.Item, _lastRightClickedItem.Frame );
} else if( pressed == InventoryOptions.Rotate ) {
(bool, Vector2I) tryRotate = TryRotate( _lastRightClickedItem.Item );
if( tryRotate.Item1 ) {
_lastRightClickedItem.Item.Rotated = !_lastRightClickedItem.Item.Rotated;
Vector2 tempSize;
if( _lastRightClickedItem.Item.Rotated ) {
tempSize = new Vector2( _lastRightClickedItem.Item.GridSize.Y, _lastRightClickedItem.Item.GridSize.X );
} else {
tempSize = _lastRightClickedItem.Item.GridSize;
}
_lastRightClickedItem.Frame.Size = new Vector2( _GridSize.X * tempSize.X - 10, _GridSize.Y * tempSize.Y - 10 );
_lastRightClickedItem.Frame.Position = new Vector2( tryRotate.Item2.X * _GridSize.X + 5, tryRotate.Item2.Y * _GridSize.Y + 5 );
_lastRightClickedItem.Item.GridPosition = tryRotate.Item2;
} else {
_Reference.Alert.PerformAlert( "Not enough space to rotate");
}
} else if( pressed == InventoryOptions.Split ) {
_splitFrame = SplitFrame.Instantiate<Control>();
GetNode( "/root/Game/UI/" ).AddChild( _splitFrame );
_splitFrame.GetChild<Button>( 2 ).ButtonDown += SplitFrame_Split;
_splitFrame.GetChild<Button>( 3 ).ButtonDown += SplitFrame_Cancel;
_splitFrame.GetChild<LineEdit>( 1 ).TextChanged += SplitFrame_TextChanged;
}
}
}
//------------------------------//
// Data Objects //
//------------------------------//
class HeldItem {
public Vector2 MouseStart;
public Vector2 MouseOffset;
public InventoryItem Item;
public Panel Frame;
}
public class PickupItem : InventoryItem {
public Guid Guid { get; }
public int ItemID { get; }
public string Name { get; }
public Vector2I GridPosition { get; set; }
public bool Rotated { get; set; }
public Vector2I GridSize { get; }
public int Weight {get;}
public bool isQuantityItem { get; }
public int Quantity { get; set; }
public int MaxQuantity { get; set; }
public string ToolPath{ get; }
public PickupItem( int _ItemID, string _Name, Vector2I _GridSize, int _Weight, bool _isQuantityItem, int _Quanitity = 0, int _MaxQuantity = 0, string _ToolPath = "" ) {
Guid = Guid.NewGuid();
ItemID = _ItemID;
Name = _Name;
GridSize = _GridSize;
Weight = _Weight;
isQuantityItem = _isQuantityItem;
Quantity = _Quanitity;
MaxQuantity = _MaxQuantity;
ToolPath = _ToolPath;
}
}
public interface InventoryItem {
public Guid Guid { get; }
public int ItemID { get; }
public string Name { get; }
public Vector2I GridPosition { get; set; }
public bool Rotated { get; set; }
public Vector2I GridSize { get; }
public int Weight { get; }
public bool isQuantityItem { get; }
public int Quantity { get; set; }
public int MaxQuantity { get; set; }
public string ToolPath { get; }
}
enum InventoryOptions {
Drop,
Rotate,
Split
}