zxc
This commit is contained in:
parent
11c4bb9ebf
commit
918571e4f3
@ -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
|
||||
}
|
||||
]
|
||||
|
96
Code/KPTLConnect.cs
Normal file
96
Code/KPTLConnect.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -49,6 +49,8 @@ public sealed class Kal : Component
|
||||
Camera.SetParent(GameObject);
|
||||
var cameraComponent = Camera.Components.Create<CameraComponent>();
|
||||
cameraComponent.ZFar = 32768f;
|
||||
|
||||
// Sandbox.Game.
|
||||
}
|
||||
|
||||
protected override void OnFixedUpdate()
|
||||
|
@ -24,6 +24,6 @@
|
||||
"CsProjName": "",
|
||||
"StartupScene": "scenes/minimal.scene",
|
||||
"MapStartupScene": "scenes/minimal.scene",
|
||||
"LaunchMode": "QuickPlay"
|
||||
"LaunchMode": "Launcher"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user