diff --git a/Assets/scenes/minimal.scene b/Assets/scenes/minimal.scene index 9659acf..faeeff1 100644 --- a/Assets/scenes/minimal.scene +++ b/Assets/scenes/minimal.scene @@ -22,6 +22,16 @@ "prefab": "prefabs/player.prefab" }, "SpawnPoints": [], + "StartServer": false + }, + { + "__type": "Sandbox.KPTLConnect", + "__guid": "93c56284-67a1-4c60-bbd4-648af2963ca7", + "PlayerPrefab": { + "_type": "gameobject", + "prefab": "prefabs/player.prefab" + }, + "SpawnPoints": [], "StartServer": true } ] diff --git a/Code/KPTLConnect.cs b/Code/KPTLConnect.cs new file mode 100644 index 0000000..00c5637 --- /dev/null +++ b/Code/KPTLConnect.cs @@ -0,0 +1,96 @@ +using System; +using System.Threading.Tasks; +using Sandbox.Utility; + +namespace Sandbox; + +[Title( "Connect" )] +[Category( "KPTL" )] +[Icon( "electrical_services" )] +public sealed class KPTLConnect : Component, Component.INetworkListener +{ + [Property] public bool StartServer { get; set; } = true; + + [Property] public GameObject PlayerPrefab { get; set; } + + [Property] public List SpawnPoints { get; set; } + + protected override async Task OnLoad() + { + if ( Scene.IsEditor ) + return; + + if ( StartServer && !Networking.IsActive ) + { + LoadingScreen.Title = "Creating Lobby"; + await Task.DelayRealtimeSeconds( 0.1f ); + Networking.CreateLobby(); + } else + { + string lobbyId = await Http.RequestStringAsync( "https://sbox.koptilnya.xyz/string" ); + Networking.Connect(lobbyId); + } + } + + /// + /// A client is fully connected to the server. This is called on the host. + /// + public void OnActive( Connection channel ) + { + Log.Info( $"Player '{channel.DisplayName}' has joined the game" ); + + if ( !PlayerPrefab.IsValid() ) + return; + + // + // Find a spawn location for this player + // + var startLocation = FindSpawnLocation().WithScale( 1 ); + + // Spawn this object and make the client the owner + var player = PlayerPrefab.Clone( startLocation, name: $"Player - {channel.DisplayName}" ); + player.NetworkSpawn( channel ); + + if ( Networking.IsHost ) + { + Task.RunInThreadAsync( UpdateLobbyId ); + } + } + + private async Task UpdateLobbyId() + { + await Task.DelayRealtimeSeconds( 2f ); + + var lobbyList = await Networking.QueryLobbies(); + + await Http.RequestAsync( $"https://sbox.koptilnya.xyz/string?text={lobbyList[0].LobbyId}", "POST" ); + } + + /// + /// Find the most appropriate place to respawn + /// + Transform FindSpawnLocation() + { + // + // If they have spawn point set then use those + // + if ( SpawnPoints is not null && SpawnPoints.Count > 0 ) + { + return Random.Shared.FromList( SpawnPoints, default ).Transform.World; + } + + // + // If we have any SpawnPoint components in the scene, then use those + // + var spawnPoints = Scene.GetAllComponents().ToArray(); + if ( spawnPoints.Length > 0 ) + { + return Random.Shared.FromArray( spawnPoints ).Transform.World; + } + + // + // Failing that, spawn where we are + // + return Transform.World; + } +} diff --git a/Code/Kal.cs b/Code/Kal.cs index 332b9d7..415b255 100644 --- a/Code/Kal.cs +++ b/Code/Kal.cs @@ -49,6 +49,8 @@ public sealed class Kal : Component Camera.SetParent(GameObject); var cameraComponent = Camera.Components.Create(); cameraComponent.ZFar = 32768f; + + // Sandbox.Game. } protected override void OnFixedUpdate() diff --git a/kakozuzo_2.sbproj b/kakozuzo_2.sbproj index e18e16e..19f54f2 100644 --- a/kakozuzo_2.sbproj +++ b/kakozuzo_2.sbproj @@ -24,6 +24,6 @@ "CsProjName": "", "StartupScene": "scenes/minimal.scene", "MapStartupScene": "scenes/minimal.scene", - "LaunchMode": "QuickPlay" + "LaunchMode": "Launcher" } } \ No newline at end of file