using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeloX;
namespace Sandbox;
public sealed class VeloXNetworkManager : Component, Component.INetworkListener
{
///
/// The prefab to spawn for the player to control.
///
[Property] public GameObject PlayerPrefab { get; set; }
protected override async Task OnLoad()
{
if ( Scene.IsEditor )
return;
if ( !Networking.IsActive )
{
LoadingScreen.Title = "Creating Lobby";
await Task.DelayRealtimeSeconds( 0.1f );
Networking.CreateLobby( new() );
}
}
///
/// 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;
var player = PlayerPrefab.Clone( WorldTransform, name: $"Player - {channel.DisplayName}" );
player.NetworkSpawn( channel );
player.Components.Get().ConnectionID = channel.Id;
}
}