This commit is contained in:
opti1337 2024-10-29 23:25:36 +03:00
parent 11c4bb9ebf
commit 918571e4f3
4 changed files with 109 additions and 1 deletions

View File

@ -22,6 +22,16 @@
"prefab": "prefabs/player.prefab" "prefab": "prefabs/player.prefab"
}, },
"SpawnPoints": [], "SpawnPoints": [],
"StartServer": false
},
{
"__type": "Sandbox.KPTLConnect",
"__guid": "93c56284-67a1-4c60-bbd4-648af2963ca7",
"PlayerPrefab": {
"_type": "gameobject",
"prefab": "prefabs/player.prefab"
},
"SpawnPoints": [],
"StartServer": true "StartServer": true
} }
] ]

96
Code/KPTLConnect.cs Normal file
View File

@ -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<GameObject> 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);
}
}
/// <summary>
/// A client is fully connected to the server. This is called on the host.
/// </summary>
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" );
}
/// <summary>
/// Find the most appropriate place to respawn
/// </summary>
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<SpawnPoint>().ToArray();
if ( spawnPoints.Length > 0 )
{
return Random.Shared.FromArray( spawnPoints ).Transform.World;
}
//
// Failing that, spawn where we are
//
return Transform.World;
}
}

View File

@ -49,6 +49,8 @@ public sealed class Kal : Component
Camera.SetParent(GameObject); Camera.SetParent(GameObject);
var cameraComponent = Camera.Components.Create<CameraComponent>(); var cameraComponent = Camera.Components.Create<CameraComponent>();
cameraComponent.ZFar = 32768f; cameraComponent.ZFar = 32768f;
// Sandbox.Game.
} }
protected override void OnFixedUpdate() protected override void OnFixedUpdate()

View File

@ -24,6 +24,6 @@
"CsProjName": "", "CsProjName": "",
"StartupScene": "scenes/minimal.scene", "StartupScene": "scenes/minimal.scene",
"MapStartupScene": "scenes/minimal.scene", "MapStartupScene": "scenes/minimal.scene",
"LaunchMode": "QuickPlay" "LaunchMode": "Launcher"
} }
} }