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
+93
View File
@@ -0,0 +1,93 @@
using Godot;
using System;
using System.Collections.Generic;
public partial class Alert : Control {
Options _Options;
Reference _Reference;
Queue<string> AlertQueue = new Queue<string>();
public override void _Ready() {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
_Reference.Alert = this;
}
// This needs to notify the user
public void PerformAlert(string NotificationText ) {
if ( _Options.isServer ) {
GD.Print( NotificationText );
} else {
AlertQueue.Enqueue( NotificationText );
}
}
Panel working = null;
float CurrentStep;
float PauseCounter;
int task = 0;
public override void _Process( double delta ) {
switch( task ) {
// Create the label
case 0: {
if (AlertQueue.Count > 0 ) {
working = new Panel();
working.Size = new Vector2( 300, 50 );
AddChild( working );
working.Position = new Vector2( 0, -50 );
Label text = new Label();
text.Text = AlertQueue.Dequeue();
text.Size = new Vector2( 300, 50 );
text.HorizontalAlignment = HorizontalAlignment.Center;
text.VerticalAlignment = VerticalAlignment.Center;
working.AddChild( text );
CurrentStep = 0;
PauseCounter = 0;
task = 1;
}
break;
}
// Bring down the label
case 1: {
if ( CurrentStep < _Options.AlertTime / 2 ) {
CurrentStep += Convert.ToSingle( delta );
float alertTime = _Options.AlertTime / 2;
working.Position = new Vector2( 0, Mathf.Lerp( -50, 25, CurrentStep / alertTime ) );
} else {
task = 2;
}
break;
}
// Pause the label
case 2: {
PauseCounter += Convert.ToSingle( delta );
if (PauseCounter >= _Options.AlertPauseTime) {
task = 3;
}
break;
}
// Bring Up the label
case 3: {
if ( CurrentStep > 0 ) {
CurrentStep -= Convert.ToSingle( delta );
float alertTime = _Options.AlertTime / 2;
working.Position = new Vector2( 0, Mathf.Lerp( -50, 25, CurrentStep / alertTime ) );
} else {
working.Free();
working = null;
task = 0;
}
break;
}
}
}
}
+29
View File
@@ -0,0 +1,29 @@
using System;
using Godot;
public partial class Bullet : RigidBody3D {
Reference _Reference;
Options _Options;
public int Creator = -1;
public float Damage = 1;
public override void _Ready() {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
}
public void _on_body_entered( Node body ) {
if (Multiplayer.GetUniqueId() == 1){
if ( body is Humanoid ) {
Humanoid player = (Humanoid)body;
_Reference.GameHandler.TakeDamage(Damage, Creator, Convert.ToInt32( player.Name.ToString().Split(new char[]{','})[0] ));
_Reference.Alert.PerformAlert("Bullet his player with ID: " + body.GetMultiplayerAuthority());
}
}
this.QueueFree();
}
}
+16
View File
@@ -0,0 +1,16 @@
using Godot;
using System;
public partial class DayCycle : DirectionalLight3D {
Options _Options;
Reference _Reference;
public override void _Process(double delta) {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
if ( _Options.isServer ){
Rotation = Rotation + new Vector3(Convert.ToSingle(delta),0,0);
}
}
}
+349
View File
@@ -0,0 +1,349 @@
using Godot;
using System;
using System.Collections.Generic;
public partial class Humanoid : CharacterBody3D {
Options _Options;
Reference _Reference;
[Export]
public AnimationTree Animator;
[Export]
public AnimQuickState CurrentAnimation;
[Export]
public Vector2 InputDirection;
UIMode CurrentUIMode = UIMode.Game;
(UIMode, Control)[] UIFrames;
public ProgressBar HealthBar;
ProgressBar StaminaBar;
public ITool EquippedTool;
Node Backpack;
public string PlayerName = "";
public float Health = 100;
public float MaxHealth = 100;
public float Stamina = 100;
public float MaxStamina = 100;
public float StaminaDrain = 5;
public float StaminaCharge = 5;
public float JumpStamina = 10;
public bool Running = false;
public float JumpVelocity = 5.5f;
public float RotationSpeed = 1f;
public float FallDistanceForDamage = 15;
public float FallDamageScale = 5f;
public Ability Ability;
public float Gravity = ProjectSettings.GetSetting( "physics/3d/default_gravity" ).AsSingle();
public bool mouseLock = true;
public float Speed {
get {
if( Running ) {
return 9f;
}
return 5f;
}
}
public override void _EnterTree() {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
string[] Data = Name.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string PID = Data[0];
string AID = Data[1];
PlayerName = Data[2];
this.SetMultiplayerAuthority( Convert.ToInt32(PID), true );
if( IsMultiplayerAuthority() ) {
_Reference.Humanoid = this;
_Options.isAlive = true;
_Reference.Humanoid = this;
_Reference.Camera = GetChild<Camera3D>( 0 );
_Reference.Camera.GetChild<AudioListener3D>(0).MakeCurrent();
_Reference.Camera.Current = true;
UIFrames = new (UIMode, Control) [] {
( UIMode.Game, GetNode<Control>( "/root/GameRoot/UI/GameUI/Game" ) ),
( UIMode.Inventory, GetNode<Control>( "/root/GameRoot/UI/GameUI/Inventory" ) ),
( UIMode.Pause, GetNode<Control>( "/root/GameRoot/UI/SettingsFrame" ) )
};
HealthBar = GetNode<ProgressBar>( "/root/GameRoot/UI/GameUI/Game/HealthBar" );
StaminaBar = GetNode<ProgressBar>( "/root/GameRoot/UI/GameUI/Game/StaminaBar" );
Input.MouseMode = Input.MouseModeEnum.ConfinedHidden;
SetUIMode( CurrentUIMode );
HealthBar.MaxValue = MaxHealth;
HealthBar.Value = Health;
StaminaBar.MaxValue = MaxStamina;
StaminaBar.Value = Stamina;
EquipTool( 0 );
List<Vector3> spawnPoints = new List<Vector3>();
foreach (Node3D cur in _Reference.PlayerSpawnPoints.GetChildren()){
spawnPoints.Add(cur.GlobalPosition);
}
int chosenSpawn = new Random(DateTime.Now.Millisecond).Next(0, spawnPoints.Count);
this.SetDeferred("global_position", spawnPoints[chosenSpawn]);
Ability _Ability = (Ability)Convert.ToInt32(AID);
if (_Ability == Ability.Vitality){
MaxHealth *= 1.25f;
Health = MaxHealth;
} else if ( _Ability == Ability.Stamina ){
MaxStamina *= 1.25f;
StaminaCharge *= 1.25f;
Stamina = MaxStamina;
} else if ( _Ability == Ability.Strength ){
_Reference.Inventory.UpdateMaxWeight(500);
}
GetNode<Node3D>("PlayerModel").Visible = false;
}
}
public enum AnimQuickState{
Idle,
Walk,
Run,
Jump
}
Vector3 GetMovement( float deltaTime ) {
// Copy current velocity off the CharacterBody3d
Vector3 velocity = Velocity;
if( CurrentUIMode == UIMode.Game ) {
// Add the gravity.
if ( IsOnFloor() ) {
if ( Input.GetActionStrength( "Jump" ) == 1 && Stamina >= JumpStamina ) {
CurrentAnimation = AnimQuickState.Jump;
Stamina -= JumpStamina;
velocity.Y = JumpVelocity;
}
} else {
CurrentAnimation = AnimQuickState.Idle;
velocity.Y -= Gravity * deltaTime;
}
if (Running && Stamina <= 0){
Running = false;
}
// Get the input direction and handle the movement/deceleration.
Vector2 inputDir = Input.GetVector("Left", "Right", "Forward", "Backward");
Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if( direction != Vector3.Zero ) { // If there was input
velocity.X = direction.X * Speed;
velocity.Z = direction.Z * Speed;
if (Running){
Stamina -= deltaTime * StaminaDrain;
StaminaBar.Value = Stamina;
StaminaBar.MaxValue = MaxStamina;
CurrentAnimation = AnimQuickState.Run;
InputDirection = inputDir;
}else{
if (Stamina < MaxStamina){
Stamina += deltaTime * StaminaCharge / 2;
StaminaBar.Value = Stamina;
StaminaBar.MaxValue = MaxStamina;
}
CurrentAnimation = AnimQuickState.Walk;
InputDirection = inputDir;
}
} else { // If there was no input
velocity.X = Mathf.MoveToward( Velocity.X, 0, Speed );
velocity.Z = Mathf.MoveToward( Velocity.Z, 0, Speed );
if (Stamina < MaxStamina){
Stamina += deltaTime * StaminaCharge;
StaminaBar.Value = Stamina;
StaminaBar.MaxValue = MaxStamina;
}
CurrentAnimation = AnimQuickState.Idle;
}
} else {
velocity = new Vector3 (
Mathf.MoveToward( Velocity.X, 0, Speed /6 ),
velocity.Y - (Gravity * deltaTime),
Mathf.MoveToward( Velocity.Z, 0, Speed /6 )
);
if (Stamina < MaxStamina){
Stamina += deltaTime * StaminaCharge;
StaminaBar.Value = Stamina;
StaminaBar.MaxValue = MaxStamina;
}
CurrentAnimation = AnimQuickState.Idle;
}
return velocity;
}
public void EquipTool(int BackpackIndex ) {
if( Backpack == null ) {
Backpack = GetNode<Node>( "/root/GameRoot/UI/GameUI/Game/ToolBar" );
}
var Children = Backpack.GetChildren();
if( Children.Count > BackpackIndex ) {
foreach( Label cur in Children ) {
cur.Modulate = new Color( 1, 1, 1 );
}
if (EquippedTool != null ) {
EquippedTool.UnEquipped();
}
// If same button is pressed put tool away
Label UILabel = Backpack.GetChild<Label>( BackpackIndex );
UILabel.Modulate = new Color(1,0,1);
EquippedTool = ( ITool )UILabel.GetChild(0);
EquippedTool.Equiped();
}
}
enum UIMode {
Game,
Inventory,
Pause
}
void SetUIMode(UIMode mode) {
if (CurrentUIMode == UIMode.Inventory ) {
foreach( var cur in UIFrames ) {
if( cur.Item1 == UIMode.Inventory ) {
Inventory inv = (Inventory)cur.Item2;
if( inv._splitFrame != null ) {
inv._splitFrame.Free();
inv._splitFrame = null;
}
}
}
}
CurrentUIMode = mode;
foreach (var cur in UIFrames) {
if (cur.Item1 == mode) {
cur.Item2.Visible = true;
} else {
cur.Item2.Visible = false;
}
}
if (mode == UIMode.Game) {
Input.MouseMode = Input.MouseModeEnum.Captured;
mouseLock = true;
} else {
Input.MouseMode = Input.MouseModeEnum.Visible;
mouseLock = false;
}
}
public override void _Input( InputEvent _event ) {
if( IsMultiplayerAuthority() ) {
if( _event is InputEventKey eventKey ) {
int KeyCode = (int)eventKey.PhysicalKeycode;
if( eventKey.Keycode == Key.Escape ) {
if( eventKey.Pressed ) {
if( CurrentUIMode != UIMode.Pause ) {
SetUIMode( UIMode.Pause );
} else {
SetUIMode( UIMode.Game );
}
}
} else if( eventKey.Keycode == Key.Tab ) {
if( eventKey.Pressed ) {
if( CurrentUIMode != UIMode.Inventory ) {
SetUIMode( UIMode.Inventory );
} else {
SetUIMode( UIMode.Game );
}
}
}
if( CurrentUIMode == UIMode.Game ) {
if( eventKey.Keycode == Key.Shift ) {
if( eventKey.Pressed && Stamina > 0 ) {
Running = true;
} else {
Running = false;
}
} else if( eventKey.Pressed && KeyCode >= 49 && KeyCode <= 57 ) {
EquipTool( KeyCode - 49 );
} else if( eventKey.Pressed ) {
if( EquippedTool != null ) {
EquippedTool.KeyPress( eventKey.Keycode );
}
} else if( !eventKey.Pressed ) {
if( EquippedTool != null ) {
EquippedTool.KeyRelease( eventKey.Keycode );
}
}
}
} else if ( _event is InputEventMouseMotion ) {
if( mouseLock ) {
InputEventMouseMotion inputEventMouseMotion = (InputEventMouseMotion)_event;
Vector2 LookChange = inputEventMouseMotion.Relative;
// Horizontal Rotation
RotateY( LookChange.X * RotationSpeed * -0.002f );
// Vertical Rotation
_Reference.Camera.RotateX( LookChange.Y * RotationSpeed * -0.002f );
if( _Reference.Camera.RotationDegrees.X < -90 ) {
_Reference.Camera.RotationDegrees = new Vector3( -90, _Reference.Camera.RotationDegrees.Y, _Reference.Camera.RotationDegrees.Z );
} else if( _Reference.Camera.RotationDegrees.X > 90 ) {
_Reference.Camera.RotationDegrees = new Vector3( 90, _Reference.Camera.RotationDegrees.Y, _Reference.Camera.RotationDegrees.Z );
}
}
} else if ( _event is InputEventMouseButton _mouseevent ) {
if( mouseLock ) {
if( _mouseevent.ButtonIndex == MouseButton.Left ) {
if( EquippedTool != null && _mouseevent.Pressed ) {
EquippedTool.LeftMouseDown();
} else if( EquippedTool != null && !_mouseevent.Pressed ) {
EquippedTool.LeftMouseUp();
}
} else if( _mouseevent.ButtonIndex == MouseButton.Right ) {
if( EquippedTool != null && _mouseevent.Pressed ) {
EquippedTool.RightMouseDown();
} else if( EquippedTool != null && !_mouseevent.Pressed ) {
EquippedTool.RightMouseUp();
}
}
}
}
}
}
void SetAnimation(){
AnimationNodeStateMachinePlayback stateMachine = (AnimationNodeStateMachinePlayback)Animator.Get("parameters/playback");
stateMachine.Travel( CurrentAnimation.ToString() );
Animator.Set("parameters/Walk/BlendSpace2D/blend_position", InputDirection);
Animator.Set("parameters/Run/BlendSpace2D/blend_position", InputDirection);
}
float LastY = -1;
public override void _PhysicsProcess( double _dT ) {
SetAnimation();
if( IsMultiplayerAuthority() ) {
float deltaTime = Convert.ToSingle( _dT );
// Fall Damage
if( IsOnFloor() ) {
if( LastY != -1 ) {
float FallDistance = LastY - GlobalPosition.Y;
if( FallDistance > FallDistanceForDamage ) {
_Reference.GameHandler.TakeDamage( (FallDistance - FallDistanceForDamage) * FallDamageScale, 0, Multiplayer.GetUniqueId() );
}
LastY = -1;
}
} else {
if( LastY == -1 ) {
LastY = GlobalPosition.Y;
}
}
// Copy the Velocity back onto the CharacterBody3d
Velocity = GetMovement( deltaTime );
// Apply Motion
MoveAndSlide();
}
}
}
+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
}
+11
View File
@@ -0,0 +1,11 @@
using Godot;
public partial class Pickup : RigidBody3D {
[Export]
public int ItemID {get; set;}
[Export]
public int Quantity {get; set;}
}
+13
View File
@@ -0,0 +1,13 @@
using Godot;
public partial class SpawnPoint : Node3D {
Options _Options;
Reference _Reference;
public override void _EnterTree() {
_Reference = GetNode<Reference>("/root/Reference");
_Reference.PlayerSpawnPoints = this.GetChild<Node3D>(0);
_Reference.ItemSpawnPoints = this.GetChild<Node3D>(1);
}
}
+120
View File
@@ -0,0 +1,120 @@
using Godot;
using Godot.Collections;
using System.Diagnostics;
public partial class SelectTool : Node3D, ITool {
public int ToolID { get{ return -1; } } // -1 for non-existant tool stat
Options _Options;
Reference _Reference;
ItemStats _Stats;
[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;
StatItem ItemStats = _Stats.stats[ ItemID ];
PickupItem item = new PickupItem( ItemID, ItemStats.ItemName, ItemStats.InventorySize, ItemStats.Weight, ItemStats.IsQuantityItem, Quantity, ItemStats.MaxQuantity, ItemStats.ToolPath );
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.Name;
_SelectToolFrame.GetChild<Label>( 1 ).Text = "Slot : " + item.GridSize.ToString();
_SelectToolFrame.GetChild<Label>( 2 ).Text = "Weight : " + item.Weight;
if (item.isQuantityItem ) {
_SelectToolFrame.GetChild<Label>( 3 ).Text = "Capacity : " + item.Quantity + "/" + item.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");
_Stats = (ItemStats)_Reference.ItemSpawnPath;
_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;
}
}
+89
View File
@@ -0,0 +1,89 @@
using Godot;
using System;
public partial class Sniper : Node, ITool {
public int ToolID { get{ return 0; } } // Match ToolID to ItemStat Index
Options _Options;
Reference _Reference;
float BulletSpawnSpacing = 2; // Distance off the front of the camera that the bullet spawn
bool isZoomed = false;
float ZoomFOV = 30;
float _normalZoom;
float HipAccuracy = 5; // Maximum offset in degrees | 15 would be 7.5 deg in each direction
float HipRecoil = 5; // Maximum offset in degrees | 5 would be 2.5 horizontally and 5 up
float AimAccuracy = 2; // Maximum offset in degrees
float AimRecoil = 2; // Maximum offset in degrees
public void Equiped() {
_Options = GetNode<Options>( "/root/Options" );
_Reference = GetNode<Reference>("/root/Reference");
_normalZoom = _Reference.Camera.Fov;
}
public void KeyPress( Key key ) {
}
public void KeyRelease( Key key ) {
}
Vector3 Accuracy() {
Random Random = new Random( DateTime.Now.Millisecond );
if( isZoomed ) {
float xAccuracy = ( ( Random.NextSingle() * 2 ) - 1 ) * AimAccuracy / 2;
float yAccuracy = ( ( Random.NextSingle() * 2 ) - 1 ) * AimAccuracy / 2;
return new Vector3( xAccuracy, yAccuracy, 0 );
} else {
float xAccuracy = ( ( Random.NextSingle() * 2 ) - 1 ) * HipAccuracy / 2;
float yAccuracy = ( ( Random.NextSingle() * 2 ) - 1 ) * HipAccuracy / 2;
return new Vector3( xAccuracy, yAccuracy, 0 );
}
}
void Recoil() {
Random Random = new Random( DateTime.Now.Millisecond );
Vector2 recoil;
if ( isZoomed ) {
float xRecoil = ( ( Random.NextSingle() * 2 ) - 1 ) * AimRecoil / 2;
float yRecoil = Random.NextSingle() * AimRecoil;
recoil = new Vector2( xRecoil, yRecoil );
} else {
float xRecoil = ( ( Random.NextSingle() * 2 ) - 1 ) * HipRecoil / 2;
float yRecoil = Random.NextSingle() * HipRecoil;
recoil = new Vector2( xRecoil, yRecoil );
}
_Reference.Camera.RotateX( 1 ); // IE Up and Down
_Reference.Humanoid.RotateX( 1 ); // IE Left and Right
}
public void LeftMouseDown() {
Vector3 LookVector = -_Reference.Camera.GlobalBasis.Z;
_Reference.GameHandler.Shoot(ToolID, LookVector, 200);
//Recoil();
}
public void LeftMouseUp() {
}
public void RightMouseDown() {
isZoomed = true;
_Reference.Camera.Fov = ZoomFOV;
}
public void RightMouseUp() {
isZoomed = false;
_Reference.Camera.Fov = _normalZoom;
}
public void UnEquipped() {
}
}
+13
View File
@@ -0,0 +1,13 @@
using Godot;
public interface ITool {
public int ToolID{ get; }
public void Equiped();
public void UnEquipped();
public void LeftMouseDown();
public void LeftMouseUp();
public void RightMouseDown();
public void RightMouseUp();
public void KeyPress( Key key );
public void KeyRelease( Key key );
}