velox_test/code/VeloXNetworkManager.cs
2025-06-12 22:51:36 +07:00

46 lines
1.1 KiB
C#

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
{
/// <summary>
/// The prefab to spawn for the player to control.
/// </summary>
[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() );
}
}
/// <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;
var player = PlayerPrefab.Clone( WorldTransform, name: $"Player - {channel.DisplayName}" );
player.NetworkSpawn( channel );
player.Components.Get<VeloXCar>().ConnectionID = channel.Id;
}
}