init commit
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
using System;
|
||||
using Godot;
|
||||
|
||||
public partial class GameHandler : Node3D {
|
||||
Reference _Reference;
|
||||
Options _Options;
|
||||
|
||||
[Export]
|
||||
public PackedScene[] MapList;
|
||||
|
||||
[Export]
|
||||
public PackedScene Player;
|
||||
|
||||
[Export]
|
||||
public PackedScene Sun;
|
||||
|
||||
public bool serverRunning = false;
|
||||
|
||||
public override void _Ready() {
|
||||
_Options = GetNode<Options>( "/root/Options" );
|
||||
_Reference = GetNode<Reference>("/root/Reference");
|
||||
_Reference.GameHandler = this;
|
||||
|
||||
_Reference.GameUI = GetNode<Control>("/root/GameRoot/UI/GameUI");
|
||||
_Reference.Lighting = GetNode<WorldEnvironment>("/root/GameRoot/Lighting");
|
||||
_Reference.ItemSpawner = GetNode<MultiplayerSpawner>("/root/GameRoot/ItemSpawner");
|
||||
_Reference.ItemSpawnPath = GetNode<Node>("/root/GameRoot/PickupItems");
|
||||
_Reference.ToolBar = GetNode<Control>("/root/GameRoot/UI/GameUI/Game/ToolBar");
|
||||
|
||||
string[] CMDS = OS.GetCmdlineArgs();
|
||||
bool temp_launch_as_server = false;
|
||||
|
||||
for( int i = 0; i < CMDS.Length; i++ ) {
|
||||
if( CMDS [i].ToLower() == "--map" | CMDS [i].ToLower() == "/map" ) {
|
||||
_Options.SelectedMap = Convert.ToInt32( CMDS [i + 1] );
|
||||
} else if( CMDS [i].ToLower() == "--port" | CMDS [i].ToLower() == "/port" ) {
|
||||
_Options.Port = Convert.ToInt32( CMDS [i + 1] );
|
||||
} else if( CMDS [i].ToLower() == "--players" | CMDS [i].ToLower() == "/players" ) {
|
||||
_Options.maxPlayers = Convert.ToInt32( CMDS [i + 1] );
|
||||
} else if( CMDS [i].ToLower() == "--host" | CMDS [i].ToLower() == "/host" ) {
|
||||
_Options.Host = CMDS [i + 1];
|
||||
} else if( CMDS [i].ToLower() == "server" ) {
|
||||
temp_launch_as_server = true;
|
||||
}
|
||||
}
|
||||
|
||||
if( OS.HasFeature( "dedicated_server" ) || DisplayServer.GetName() == "headless" || temp_launch_as_server ) {
|
||||
StartServer();
|
||||
}
|
||||
}
|
||||
|
||||
public void StartServer(){
|
||||
_Options.isServer = true;
|
||||
_Options.connection = new ENetMultiplayerPeer();
|
||||
_Options.connection.CreateServer( _Options.Port, _Options.maxPlayers );
|
||||
Multiplayer.MultiplayerPeer = _Options.connection;
|
||||
|
||||
_Options.connection.PeerConnected += peerConnected;
|
||||
_Options.connection.PeerDisconnected += peerDisconnected;
|
||||
|
||||
Node3D MapSelected = _Reference.GameHandler.MapList[_Options.SelectedMap].Instantiate<Node3D>();
|
||||
MapSelected.SetMultiplayerAuthority(Multiplayer.GetUniqueId());
|
||||
MapSelected.Name = "Workspace";
|
||||
_Reference.Workspace = MapSelected;
|
||||
_Reference.GameHandler.AddChild( MapSelected );
|
||||
|
||||
// Spawn the sun
|
||||
AddChild( Sun.Instantiate<Node3D>() );
|
||||
|
||||
ResetItemDrops();
|
||||
serverRunning = true;
|
||||
}
|
||||
public void StopServer(){
|
||||
serverRunning = false;
|
||||
}
|
||||
|
||||
|
||||
public void StopClient(){
|
||||
_Options.connection.PeerConnected -= peerConnected;
|
||||
_Options.connection.PeerDisconnected -= peerDisconnected;
|
||||
_Reference.Workspace.Multiplayer.ConnectedToServer -= ConnectedToServer;
|
||||
_Reference.Workspace.Multiplayer.ConnectionFailed -= ConnectionFailed;
|
||||
_Reference.Workspace.Multiplayer.ServerDisconnected -= DisconnectedFromServer;
|
||||
}
|
||||
|
||||
public void StartClient(){
|
||||
_Options.connection = new ENetMultiplayerPeer();
|
||||
_Options.connection.CreateClient( _Options.Host, _Options.Port );
|
||||
Multiplayer.MultiplayerPeer = _Options.connection;
|
||||
|
||||
Multiplayer.ConnectedToServer += ConnectedToServer;
|
||||
Multiplayer.ConnectionFailed += ConnectionFailed;
|
||||
Multiplayer.ServerDisconnected += DisconnectedFromServer;
|
||||
}
|
||||
|
||||
public void RemovePlayer(int id){
|
||||
if (HasNode(id.ToString())) {
|
||||
GetNode(id.ToString()).QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
double timeElapsed = 0;
|
||||
void ResetItemDrops(){
|
||||
foreach( Node3D cur in _Reference.ItemSpawnPath.GetChildren()){
|
||||
cur.QueueFree();
|
||||
}
|
||||
foreach( Node3D cur in _Reference.ItemSpawnPoints.GetChildren() ){
|
||||
string[] PickupItems = _Reference.ItemSpawner._SpawnableScenes;
|
||||
int chosenItem = new Random(DateTime.Now.Millisecond).Next(0, PickupItems.Length);
|
||||
PackedScene ps = GD.Load<PackedScene>(PickupItems[chosenItem]);
|
||||
Node3D prefab = ps.Instantiate<Node3D>();
|
||||
prefab.Name = Guid.NewGuid().ToString();
|
||||
_Reference.ItemSpawnPath.AddChild( prefab );
|
||||
prefab.GlobalPosition = cur.GlobalPosition;
|
||||
}
|
||||
}
|
||||
public override void _Process(double delta) {
|
||||
if(serverRunning){
|
||||
timeElapsed += delta;
|
||||
if (timeElapsed > _Options.timeBetweenDropsReset * 60){
|
||||
ResetItemDrops();
|
||||
timeElapsed = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Called on server
|
||||
public void peerConnected( long peerID ) {
|
||||
|
||||
}
|
||||
public void peerDisconnected( long peerID ) {
|
||||
RemovePlayer(Convert.ToInt32(peerID));
|
||||
}
|
||||
|
||||
// Called on client
|
||||
public void ConnectedToServer() {
|
||||
_Reference.Alert.PerformAlert( "Connected to server successfully" );
|
||||
}
|
||||
public void ConnectionFailed() {
|
||||
_Reference.Alert.PerformAlert( "Connection failed" );
|
||||
}
|
||||
public void DisconnectedFromServer() {
|
||||
_Reference.Alert.PerformAlert( "Disconnected from server" );
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/// RPC's
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
[Rpc( MultiplayerApi.RpcMode.AnyPeer, CallLocal = true, TransferChannel = 1, TransferMode = MultiplayerPeer.TransferModeEnum.Reliable)]
|
||||
void _takeDamage(float DMG, int Creator, int Hit){
|
||||
// Take Damage
|
||||
Humanoid HitUser = null;
|
||||
foreach(Node cur in _Reference.GameHandler.GetChildren()){
|
||||
if ( cur.Name.ToString().Split(new char[]{','})[0] == Hit.ToString() ){
|
||||
HitUser = (Humanoid)cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Humanoid CreatorUser = null;
|
||||
foreach(Node cur in _Reference.GameHandler.GetChildren()){
|
||||
if ( cur.Name.ToString().Split(new char[]{','})[0] == Creator.ToString() ){
|
||||
CreatorUser = (Humanoid)cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
HitUser.Health -= DMG;
|
||||
|
||||
// If Player Died
|
||||
if( HitUser.Health <= 0 ) {
|
||||
// Stuff to do on the died players computer
|
||||
if (HitUser.GetMultiplayerAuthority() == Multiplayer.GetUniqueId()){
|
||||
Input.MouseMode = Input.MouseModeEnum.Visible;
|
||||
_Reference.Alert.PerformAlert("You died from Player : " + CreatorUser.PlayerName);
|
||||
}
|
||||
// Stuff to do on the killers computer
|
||||
else if (CreatorUser.GetMultiplayerAuthority() == Multiplayer.GetUniqueId()){
|
||||
_Reference.Alert.PerformAlert("You killed Player : " + HitUser.PlayerName);
|
||||
}
|
||||
// Stuff to do on Server
|
||||
else if ( Multiplayer.GetUniqueId() == 1 ){
|
||||
|
||||
}
|
||||
// Stuff to perform for everybody
|
||||
// Award the player points
|
||||
// Notify Player of kill
|
||||
// Reset Dead player
|
||||
}
|
||||
|
||||
// Update UI For Hit Player
|
||||
if (HitUser.GetMultiplayerAuthority() == Multiplayer.GetUniqueId()){
|
||||
HitUser.HealthBar.MaxValue = HitUser.MaxHealth;
|
||||
HitUser.HealthBar.Value = Convert.ToDouble( HitUser.Health );
|
||||
}
|
||||
}
|
||||
public void TakeDamage(float DMG, int Creator, int Hit){
|
||||
Rpc("_takeDamage", DMG, Creator, Hit);
|
||||
}
|
||||
|
||||
[Rpc( MultiplayerApi.RpcMode.AnyPeer, CallLocal = false, TransferChannel = 1, TransferMode = MultiplayerPeer.TransferModeEnum.Reliable)]
|
||||
void _Shoot(int Creator, int itemID, Vector3 initialPosition, Vector3 ImpulseVector){
|
||||
if (Multiplayer.GetUniqueId() == 1){
|
||||
PackedScene ps = GD.Load<PackedScene>("res://Prefab/bullet.tscn");
|
||||
Bullet physicsBullet = ps.Instantiate<Bullet>();
|
||||
physicsBullet.Name = Guid.NewGuid().ToString();
|
||||
physicsBullet.Creator = Creator;
|
||||
physicsBullet.Damage = ((ItemStats)_Reference.ItemSpawnPath).stats[itemID].ToolDamage ;
|
||||
GetNode<Node3D>("/root/GameRoot/Game").AddChild(physicsBullet);
|
||||
physicsBullet.SetDeferred("global_position", initialPosition);
|
||||
physicsBullet.CallDeferred("apply_impulse", ImpulseVector);
|
||||
}
|
||||
}
|
||||
public void Shoot(int ToolID, Vector3 LookVector, float ImpulseForce){
|
||||
Rpc("_Shoot", Multiplayer.GetUniqueId(), ToolID, _Reference.Humanoid.GlobalPosition + LookVector * 4, LookVector * ImpulseForce);
|
||||
}
|
||||
|
||||
[Rpc( MultiplayerApi.RpcMode.AnyPeer, CallLocal = false, TransferChannel = 1, TransferMode = MultiplayerPeer.TransferModeEnum.Reliable)]
|
||||
void _SpawnPlayer( int UniqueID, string PlayerName, int AbilityIndex ){
|
||||
if (Multiplayer.IsServer()){
|
||||
Humanoid playerCharacter = _Reference.GameHandler.Player.Instantiate<Humanoid>();
|
||||
playerCharacter.Name = UniqueID.ToString() + "," + AbilityIndex + "," + PlayerName;
|
||||
playerCharacter.PlayerName = PlayerName;
|
||||
AddChild(playerCharacter, true);
|
||||
playerCharacter.SetMultiplayerAuthority( UniqueID );
|
||||
}
|
||||
}
|
||||
public void SpawnPlayer( int UniqueID, string PlayerName, Ability _Ability ){
|
||||
Rpc( "_SpawnPlayer", UniqueID, PlayerName, (int)_Ability );
|
||||
}
|
||||
|
||||
[Rpc( MultiplayerApi.RpcMode.AnyPeer, CallLocal = true, TransferChannel = 1, TransferMode = MultiplayerPeer.TransferModeEnum.Reliable)]
|
||||
void _clearItemNetworked(string ItemName){
|
||||
Node3D item = _Reference.ItemSpawnPath.GetNode<Node3D>(ItemName);
|
||||
if (item != null){
|
||||
try{
|
||||
item.QueueFree();
|
||||
}catch{}
|
||||
}
|
||||
}
|
||||
public void ClearItemNetworked( string ItemName ){
|
||||
Rpc("_clearItemNetworked", ItemName);
|
||||
}
|
||||
|
||||
public class HumanoidStats {
|
||||
public int HumanoidOwner { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public int MaxHealth { get; set; }
|
||||
public int Health { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class MainMenu : Control {
|
||||
Options _Options;
|
||||
Reference _Reference;
|
||||
|
||||
public override void _Ready() {
|
||||
_Reference = GetNode<Reference>("/root/Reference");
|
||||
_Options = GetNode<Options>( "/root/Options" );
|
||||
_Reference.MainMenu = this;
|
||||
}
|
||||
|
||||
void _on_settings_button_down() {
|
||||
_Reference.SettingsFrame.ToggleSettingsVisibility();
|
||||
}
|
||||
|
||||
public async void _on_join_game_pressed() {
|
||||
SettingsFile sf = _Options._SettingsFile;
|
||||
GameSettings gs = sf.Game;
|
||||
string userN = gs.UserName;
|
||||
string passW = gs.SessionToken;
|
||||
(bool, Account) User = await _Reference.MistoxNet.TrySession( userN, passW );
|
||||
if( User.Item1 ) {
|
||||
_Reference.Lighting.Environment.SsrEnabled = sf.Display.ScreenSpaceReflections;
|
||||
_Reference.Lighting.Environment.SsaoEnabled = sf.Display.ScreenSpaceAmbiantOcclusion;
|
||||
_Reference.Lighting.Environment.SdfgiEnabled = sf.Display.GlobalIllumination;
|
||||
_Reference.GameHandler.Visible = true;
|
||||
_Reference.GameUI.Visible = true;
|
||||
Visible = false;
|
||||
_Options.Host = GetNode<TextEdit>("Host").Text;
|
||||
_Reference.GameHandler.StartClient();
|
||||
this.Visible = false;
|
||||
_Reference.PreGame.Visible = true;
|
||||
|
||||
} else {
|
||||
_Reference.Alert.PerformAlert( "Login failed" );
|
||||
_Reference.SessionHandler.GetParent<Control>().Visible = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void _on_start_server_pressed(){
|
||||
_Reference.GameHandler.StartServer();
|
||||
_Reference.MainMenu.Visible = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class PreGame : Control {
|
||||
|
||||
Options _Options;
|
||||
Reference _Reference;
|
||||
Label _AbilityLabel;
|
||||
public Ability _Ability = Ability.None;
|
||||
|
||||
[Export]
|
||||
public PackedScene Backpack;
|
||||
|
||||
public override void _Ready() {
|
||||
_Reference = GetNode<Reference>("/root/Reference");
|
||||
_Options = GetNode<Options>( "/root/Options" );
|
||||
_Reference.PreGame = this;
|
||||
_AbilityLabel = this.GetChild<Label>(0);
|
||||
_AbilityLabel.Text = "Current Ability : " + _Ability.ToString();
|
||||
}
|
||||
|
||||
public void _on_ability_pressed() {
|
||||
int AbilityIndex = (int)_Ability;
|
||||
_Ability = ( Ability )(AbilityIndex + 1);
|
||||
if( AbilityIndex >= Enum.GetValues( typeof( Ability ) ).Length - 1 ) {
|
||||
_Ability = 0;
|
||||
}
|
||||
_AbilityLabel.Text = "Current Ability : " + _Ability.ToString();
|
||||
}
|
||||
|
||||
public void _on_player_spawn_pressed(){
|
||||
Inventory inv = Backpack.Instantiate<Inventory>();
|
||||
GetNode<Control>("/root/GameRoot/UI/GameUI").AddChild(inv);
|
||||
inv.Name = "Inventory";
|
||||
|
||||
_Reference.GameHandler.SpawnPlayer( Multiplayer.GetUniqueId(), _Options._SettingsFile.Game.UserName, _Ability );
|
||||
_Reference.PreGame.Visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Ability {
|
||||
None, // No boost perk
|
||||
Vitality, // Gain slightly more health
|
||||
Strength, // Gain extra weight carrying | moves faster with more stuff
|
||||
Stamina, // Run Faster
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Godot;
|
||||
|
||||
public partial class SessionHandler : Panel {
|
||||
|
||||
Options _Options;
|
||||
Reference _Reference;
|
||||
|
||||
[Export] LineEdit UsernameBox;
|
||||
[Export] LineEdit PasswordBox;
|
||||
|
||||
public override void _Ready() {
|
||||
_Options = GetNode<Options>( "/root/Options" );
|
||||
_Reference = GetNode<Reference>("/root/Reference");
|
||||
_Reference.SessionHandler = this;
|
||||
}
|
||||
|
||||
public async void onLogin() {
|
||||
(bool, Account) User = await _Reference.MistoxNet.TryLogin( UsernameBox.Text, PasswordBox.Text );
|
||||
if( User.Item1 ) {
|
||||
_Reference.Alert.PerformAlert( "Login Success" );
|
||||
_Options._SettingsFile.Game.UserName = User.Item2.UserName;
|
||||
_Options._SettingsFile.Game.SessionToken = User.Item2.PasswordHash;
|
||||
_Options._SettingsFile.Save();
|
||||
GetParent<Control>().Visible = false;
|
||||
} else {
|
||||
_Reference.Alert.PerformAlert("Login Failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void onRegister() {
|
||||
OS.ShellOpen("https://www.mistox.net/account/register");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
public partial class Settings : TabContainer {
|
||||
|
||||
Control _GameSettings;
|
||||
Control _DisplaySettings;
|
||||
Control _AudioSettings;
|
||||
Control _InputSettings;
|
||||
|
||||
Options _Options;
|
||||
Reference _Reference;
|
||||
|
||||
public override void _Ready() {
|
||||
_Reference = GetNode<Reference>("/root/Reference");
|
||||
_Options = GetNode<Options>( "/root/Options" );
|
||||
_Reference.SettingsFrame = this;
|
||||
|
||||
// Get Locations For Settings
|
||||
_GameSettings = GetChild( ( int )Tabs.Game ).GetChild( 0 ).GetChild<Control>( 0 );
|
||||
_DisplaySettings = GetChild( ( int )Tabs.Display ).GetChild( 0 ).GetChild<Control>( 0 );
|
||||
_AudioSettings = GetChild( ( int )Tabs.Audio ).GetChild( 0 ).GetChild<Control>( 0 );
|
||||
_InputSettings = GetChild( ( int )Tabs.Input ).GetChild( 0 ).GetChild<Control>( 0 );
|
||||
|
||||
if( !_Options.isServer ) {
|
||||
|
||||
// Load Settings
|
||||
_Options._SettingsFile = new SettingsFile( "Settings.json" );
|
||||
SetSettings();
|
||||
|
||||
// Game Settings
|
||||
|
||||
// Display Settings
|
||||
Action<DisplayServer.WindowMode> wm = ( DisplayServer.WindowMode mode ) => { _Options._SettingsFile.Display.WindowMode = mode; };
|
||||
BuildSetting( "Window Mode", _DisplaySettings, _Options._SettingsFile.Display.WindowMode, wm );
|
||||
Action<float> rs = ( float mode ) => { _Options._SettingsFile.Display.ScreenScalingFactor = mode; };
|
||||
BuildSetting( "Resolution Scale", _DisplaySettings, _Options._SettingsFile.Display.ScreenScalingFactor, rs );
|
||||
Action<int> mf = ( int mode ) => { _Options._SettingsFile.Display.MaxFPS = mode; };
|
||||
BuildSetting( "Max FPS", _DisplaySettings, _Options._SettingsFile.Display.MaxFPS, mf );
|
||||
Action<DisplayServer.VSyncMode> vs = ( DisplayServer.VSyncMode mode ) => { _Options._SettingsFile.Display.VSync = mode; };
|
||||
BuildSetting( "VSync", _DisplaySettings, _Options._SettingsFile.Display.VSync, vs );
|
||||
Action<Viewport.Scaling3DModeEnum> ss = ( Viewport.Scaling3DModeEnum mode ) => { _Options._SettingsFile.Display.ScreenScaling = mode; };
|
||||
BuildSetting( "Screen Scaling", _DisplaySettings, _Options._SettingsFile.Display.ScreenScaling, ss );
|
||||
Action<float> fs = (float mode) => { _Options._SettingsFile.Display.FSRSharpness = mode; };
|
||||
BuildSetting( "FSR Sharpness", _DisplaySettings, _Options._SettingsFile.Display.FSRSharpness, fs );
|
||||
Action<Viewport.Msaa> msaa = ( Viewport.Msaa mode ) => { _Options._SettingsFile.Display.Msaa = mode; };
|
||||
BuildSetting( "MSAA", _DisplaySettings, _Options._SettingsFile.Display.Msaa, msaa );
|
||||
Action<Viewport.ScreenSpaceAAEnum> ssaa = ( Viewport.ScreenSpaceAAEnum mode ) => { _Options._SettingsFile.Display.ScreenSpaceAA = mode; };
|
||||
BuildSetting( "SSAA", _DisplaySettings, _Options._SettingsFile.Display.ScreenSpaceAA, ssaa );
|
||||
Action<bool> taa = ( bool mode ) => { _Options._SettingsFile.Display.TAA = mode; };
|
||||
BuildSetting( "TAA", _DisplaySettings, _Options._SettingsFile.Display.TAA, taa );
|
||||
Action<bool> SSR = ( bool mode ) => { _Options._SettingsFile.Display.ScreenSpaceReflections = mode; };
|
||||
BuildSetting( "Screen Space Reflections", _DisplaySettings, _Options._SettingsFile.Display.ScreenSpaceReflections, SSR );
|
||||
Action<bool> SSIL = ( bool mode ) => { _Options._SettingsFile.Display.ScreenSpaceIndirectLighting = mode; };
|
||||
BuildSetting( "Indirect Lighting", _DisplaySettings, _Options._SettingsFile.Display.ScreenSpaceIndirectLighting, SSIL );
|
||||
Action<bool> SSAO = ( bool mode ) => { _Options._SettingsFile.Display.ScreenSpaceAmbiantOcclusion = mode; };
|
||||
BuildSetting( "Ambiant Occlusion", _DisplaySettings, _Options._SettingsFile.Display.ScreenSpaceAmbiantOcclusion, SSAO );
|
||||
Action<bool> GI = ( bool mode ) => { _Options._SettingsFile.Display.GlobalIllumination = mode; };
|
||||
BuildSetting( "Global Ilumination", _DisplaySettings, _Options._SettingsFile.Display.GlobalIllumination, GI );
|
||||
|
||||
|
||||
// Audio Settings
|
||||
|
||||
// Input Settings
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool settingsVisible = true;
|
||||
public void ToggleSettingsVisibility() {
|
||||
if( _Options.isAlive ) {
|
||||
|
||||
} else {
|
||||
GetNode<Control>( "/root/GameRoot/UI/SettingsFrame" ).Visible = settingsVisible;
|
||||
|
||||
GetNode<Control>( "/root/GameRoot/UI/MainMenu/ButtonList" ).Visible = !settingsVisible;
|
||||
GetNode<Control>( "/root/GameRoot/UI/MainMenu/SelectedAbility" ).Visible = !settingsVisible;
|
||||
settingsVisible = !settingsVisible;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void OnSaveButtonDown() {
|
||||
SetSettings();
|
||||
_Options._SettingsFile.Save();
|
||||
_Reference.Alert.PerformAlert( "Settings Saved" );
|
||||
if ( !settingsVisible ) {
|
||||
ToggleSettingsVisibility();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCancelButtonDown() {
|
||||
if ( !settingsVisible ) {
|
||||
ToggleSettingsVisibility();
|
||||
}
|
||||
}
|
||||
|
||||
void BuildSetting<T>( string SettingName, Control Parent, T DefaultSetting, Action<T> SettingEvent ) {
|
||||
Control SettingFrameControl = new Control{
|
||||
CustomMinimumSize = new Vector2(0, 50)
|
||||
};
|
||||
Panel Background = new Panel{
|
||||
LayoutMode = 1
|
||||
};
|
||||
Background.SetAnchorsPreset( LayoutPreset.TopWide );
|
||||
Background.SetSize( new Vector2( 780, 50 ) );
|
||||
Background.Position = new Vector2( 10, 10 );
|
||||
SettingFrameControl.AddChild( Background );
|
||||
HBoxContainer container = new HBoxContainer{
|
||||
LayoutMode = 1
|
||||
};
|
||||
container.SetAnchorsPreset( LayoutPreset.FullRect );
|
||||
Background.AddChild( container );
|
||||
Label SettingTitle = new Label{
|
||||
Text = SettingName,
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Uppercase = true,
|
||||
CustomMinimumSize = new Vector2(390, 0)
|
||||
};
|
||||
container.AddChild( SettingTitle );
|
||||
if( typeof( T ) == typeof( float ) ) {
|
||||
// Slider 0 - 2
|
||||
HSlider slider = new HSlider{
|
||||
CustomMinimumSize = new Vector2(390, 50),
|
||||
MinValue = 0,
|
||||
MaxValue = 20,
|
||||
TickCount = 21,
|
||||
Rounded = true,
|
||||
Value = Convert.ToSingle(DefaultSetting) * 10
|
||||
};
|
||||
slider.DragEnded += (bool changed) => {
|
||||
SettingEvent((T)(object)( Convert.ToSingle( slider.Value ) / 10 ));
|
||||
};
|
||||
container.AddChild( slider );
|
||||
} else if( typeof( T ) == typeof( int ) ) {
|
||||
// Number Input
|
||||
LineEdit numberEdit = new LineEdit{
|
||||
CustomMinimumSize = new Vector2(390, 50),
|
||||
Text = DefaultSetting.ToString()
|
||||
};
|
||||
numberEdit.TextChanged += ( string newText ) => {
|
||||
if ( int.TryParse( newText, out int value ) ) {
|
||||
SettingEvent((T)(object)( value ));
|
||||
} else {
|
||||
numberEdit.Text = DefaultSetting.ToString();
|
||||
}
|
||||
};
|
||||
container.AddChild( numberEdit );
|
||||
} else if( typeof( T ).IsEnum ) {
|
||||
OptionButton menuButton = new OptionButton{
|
||||
CustomMinimumSize = new Vector2(390, 0),
|
||||
Text = DefaultSetting.ToString()
|
||||
};
|
||||
foreach (var settingOption in Enum.GetValues( typeof( T ) ) ) {
|
||||
menuButton.AddItem( settingOption.ToString() );
|
||||
}
|
||||
menuButton.Selected = Convert.ToInt32( DefaultSetting );
|
||||
menuButton.ItemSelected += ( long index ) => {
|
||||
SettingEvent((T)(object)index);
|
||||
};
|
||||
container.AddChild( menuButton );
|
||||
} else if ( typeof( T ) == typeof( bool ) ) {
|
||||
// check box
|
||||
CheckBox checkBox = new CheckBox{
|
||||
CustomMinimumSize = new Vector2(390, 50),
|
||||
ToggleMode = true,
|
||||
ButtonPressed = Convert.ToBoolean(DefaultSetting)
|
||||
};
|
||||
checkBox.Toggled += (bool value) => {
|
||||
checkBox.ButtonPressed = value;
|
||||
SettingEvent((T)(object)value );
|
||||
};
|
||||
container.AddChild( checkBox );
|
||||
}
|
||||
Parent.AddChild( SettingFrameControl );
|
||||
}
|
||||
|
||||
void SetSettings() {
|
||||
// Display Settings
|
||||
DisplayServer.WindowSetMode( _Options._SettingsFile.Display.WindowMode );
|
||||
Engine.MaxFps = _Options._SettingsFile.Display.MaxFPS;
|
||||
DisplayServer.WindowSetVsyncMode( _Options._SettingsFile.Display.VSync );
|
||||
GetViewport().Scaling3DMode = _Options._SettingsFile.Display.ScreenScaling;
|
||||
GetViewport().FsrSharpness = _Options._SettingsFile.Display.FSRSharpness;
|
||||
GetViewport().Scaling3DScale = _Options._SettingsFile.Display.ScreenScalingFactor;
|
||||
GetViewport().Msaa2D = _Options._SettingsFile.Display.Msaa;
|
||||
GetViewport().Msaa3D = _Options._SettingsFile.Display.Msaa;
|
||||
GetViewport().ScreenSpaceAA = _Options._SettingsFile.Display.ScreenSpaceAA;
|
||||
GetViewport().UseTaa = _Options._SettingsFile.Display.TAA;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum Tabs {
|
||||
Game = 0,
|
||||
Display = 1,
|
||||
Audio = 2,
|
||||
Input = 3
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SettingsFile {
|
||||
string FilePath = string.Empty;
|
||||
public GameSettings Game { get; set; }
|
||||
public DisplaySettings Display { get; set; }
|
||||
public AudioSettings Audio { get; set; }
|
||||
public InputSettings Input { get; set; }
|
||||
public SettingsFile( string Path ) {
|
||||
FilePath = "user://" + Path;
|
||||
using (Godot.FileAccess file = Godot.FileAccess.Open(FilePath, Godot.FileAccess.ModeFlags.Read)) {
|
||||
if ( file != null ) {
|
||||
string content = file.GetAsText();
|
||||
SettingsFile temp = JsonConvert.DeserializeObject<SettingsFile>(content);
|
||||
Game = temp.Game != null ? temp.Game : new GameSettings();
|
||||
Display = temp.Display != null ? temp.Display : new DisplaySettings();
|
||||
Audio = temp.Audio != null ? temp.Audio : new AudioSettings();
|
||||
Input = temp.Input != null ? temp.Input : new InputSettings();
|
||||
}else{
|
||||
Game = new GameSettings();
|
||||
Display = new DisplaySettings();
|
||||
Audio = new AudioSettings();
|
||||
Input = new InputSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Save() {
|
||||
using (Godot.FileAccess file = Godot.FileAccess.Open( FilePath, Godot.FileAccess.ModeFlags.Write ) ) {
|
||||
file.StoreString( JsonConvert.SerializeObject( this, Formatting.Indented ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Settings File Settings
|
||||
///
|
||||
|
||||
public class GameSettings {
|
||||
public string UserName { get; set; } = String.Empty;
|
||||
public string SessionToken { get; set; } = String.Empty;
|
||||
}
|
||||
|
||||
public class DisplaySettings {
|
||||
public DisplayServer.WindowMode WindowMode { get; set; }
|
||||
public float ScreenScalingFactor { get; set; }
|
||||
public int MaxFPS { get; set; }
|
||||
public DisplayServer.VSyncMode VSync { get; set; }
|
||||
public Viewport.Scaling3DModeEnum ScreenScaling { get; set; }
|
||||
public float FSRSharpness { get; set; }
|
||||
public Viewport.Msaa Msaa { get; set; }
|
||||
public Viewport.ScreenSpaceAAEnum ScreenSpaceAA { get; set; }
|
||||
public bool TAA { get; set; }
|
||||
public bool ScreenSpaceReflections { get; set; }
|
||||
public bool ScreenSpaceIndirectLighting { get; set; }
|
||||
public bool ScreenSpaceAmbiantOcclusion { get; set; }
|
||||
public bool GlobalIllumination { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class AudioSettings {
|
||||
|
||||
}
|
||||
|
||||
public class InputSettings {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user