Skip to content

Commit

Permalink
sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
BBpezsgo committed Jan 5, 2025
1 parent 8aa58fc commit 555edd1
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 116 deletions.
4 changes: 4 additions & 0 deletions Assets/Components/Player.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using Unity.Burst;
using Unity.Entities;
using Unity.NetCode;

Expand All @@ -8,11 +10,13 @@ public enum PlayerConnectionState : byte
Disconnected,
}

[BurstCompile]
public struct Player : IComponentData
{
[GhostField] public int ConnectionId;
[GhostField] public PlayerConnectionState ConnectionState;
[GhostField] public int Team;
[GhostField] public float Resources;
public bool IsCoreComputerSpawned;
public Guid Guid;
}
10 changes: 10 additions & 0 deletions Assets/RPC/SessionLoginRequestRpc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.NetCode;

[BurstCompile]
public struct SessionLoginRequestRpc : IRpcCommand
{
public required FixedBytes16 Guid;
}
6 changes: 6 additions & 0 deletions Assets/RPC/SessionRegisterRequestRpc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Unity.NetCode;

public struct SessionRegisterRequestRpc : IRpcCommand
{

}
17 changes: 17 additions & 0 deletions Assets/RPC/SessionResponseRpc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Unity.Burst;
using Unity.Collections;
using Unity.NetCode;

public enum SessionStatusCode : byte
{
OK,
AlreadyLoggedIn,
InvalidGuid,
}

[BurstCompile]
public struct SessionResponseRpc : IRpcCommand
{
public required SessionStatusCode StatusCode;
public required FixedBytes16 Guid;
}
10 changes: 5 additions & 5 deletions Assets/Scenes/GameScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1331,26 +1331,26 @@ MonoBehaviour:
x: 4
y: 6
Script: builder.bbc
Team: 2
Team: 1
- Prefab: {fileID: 2099412289264216910, guid: 2433642d39f17be6aa8ad8c596976581, type: 3}
Spawn:
x: 2
y: 6
Script: builder.bbc
Team: 2
Team: 1
- Prefab: {fileID: 1994654560244389388, guid: 8cc3337a89423ebee8b83eac4f208105, type: 3}
Spawn:
x: 0
y: 6
Script: extractor.bbc
Team: 2
Team: 1
- Prefab: {fileID: 8196213229133260831, guid: b9ee901bbacbf08859e49b904b381b95, type: 3}
Spawn:
x: -2
y: 6
Script: transporter.bbc
Team: 2
Team: 2
Team: 1
Team: 1
GeneratedScript: unit-4.bbc
GeneratedCount: 0
Start: {x: -20, y: -20}
Expand Down
4 changes: 4 additions & 0 deletions Assets/Systems/Client/EntityInfoUISystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ void ISystem.OnUpdate(ref SystemState state)
.WithAll<EntityWithInfoUI>()
.WithEntityAccess())
{
#if UNITY_EDITOR && ENABLE_PROFILER
using Unity.Profiling.ProfilerMarker.AutoScope _ = __instantiateUI.Auto();
#endif

GameObject uiPrefab = SystemAPI.ManagedAPI.GetSingleton<UIPrefabs>().EntityInfo;
Unity.Mathematics.float3 spawnPosition = transform.ValueRO.Position;
Expand Down Expand Up @@ -77,7 +79,9 @@ void ISystem.OnUpdate(ref SystemState state)
.WithNone<EntityWithInfoUI>()
.WithEntityAccess())
{
#if UNITY_EDITOR && ENABLE_PROFILER
using Unity.Profiling.ProfilerMarker.AutoScope _ = __destroyUI.Auto();
#endif

Object.Destroy(uiRef.Value.gameObject);
commandBuffer.RemoveComponent<EntityInfoUIReference>(entity);
Expand Down
117 changes: 117 additions & 0 deletions Assets/Systems/Client/PlayerSystemClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;

[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct PlayerSystemClient : ISystem
{
bool _requestSent;
SessionStatusCode _sessionStatus;
Guid _guid;

void ISystem.OnCreate(ref SystemState state)
{
_requestSent = false;
_guid = default;
state.RequireForUpdate<NetworkStreamConnection>();
}

void ISystem.OnUpdate(ref SystemState state)
{
EntityCommandBuffer commandBuffer = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);

NetworkStreamConnection connection = SystemAPI.GetSingleton<NetworkStreamConnection>();

foreach (var (request, command, entity) in
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>, RefRO<SessionResponseRpc>>()
.WithEntityAccess())
{
commandBuffer.DestroyEntity(entity);

_sessionStatus = command.ValueRO.StatusCode;
_guid = Marshal.As<FixedBytes16, Guid>(command.ValueRO.Guid);

Debug.Log(string.Format("[Client] Session status: {0}\n guid: {1}", _sessionStatus, _guid));
}

if (connection.CurrentState != ConnectionState.State.Connected) return;

if (!TryGetLocalPlayer(ref state, out Player player))
{
if (!_requestSent)
{
Entity response = commandBuffer.CreateEntity();
commandBuffer.AddComponent<SendRpcCommandRequest>(response);
if (_guid == default)
{
Debug.Log(string.Format("[Client] No player found, registering"));

commandBuffer.AddComponent<SessionRegisterRequestRpc>(response, new()
{

});
}
else
{
Debug.Log(string.Format("[Client] No player found, logging in with {0}", _guid));

commandBuffer.AddComponent<SessionLoginRequestRpc>(response, new()
{
Guid = Marshal.As<Guid, FixedBytes16>(_guid)
});
}

_requestSent = true;
}
}
else
{
_requestSent = false;
}
}

public bool TryGetLocalPlayer(ref SystemState state, out Player player)
{
if (!SystemAPI.TryGetSingleton(out NetworkId networkId))
{
player = default;
return false;
}

foreach (var _player in
SystemAPI.Query<RefRO<Player>>())
{
if (_player.ValueRO.ConnectionId != networkId.Value) continue;
player = _player.ValueRO;
return true;
}

player = default;
return false;
}

public static bool TryGetLocalPlayer(out Player player)
{
using EntityQuery playersQ = ConnectionManager.ClientOrDefaultWorld.EntityManager.CreateEntityQuery(typeof(Player));
using EntityQuery connectionsQ = ConnectionManager.ClientOrDefaultWorld.EntityManager.CreateEntityQuery(typeof(NetworkId));
if (!connectionsQ.TryGetSingleton(out NetworkId networkId))
{
player = default;
return false;
}

using NativeArray<Player> players = playersQ.ToComponentDataArray<Player>(Allocator.Temp);
for (int i = 0; i < players.Length; i++)
{
if (players[i].ConnectionId != networkId.Value) continue;
player = players[i];
return true;
}

player = default;
return false;
}
}
Loading

0 comments on commit 555edd1

Please sign in to comment.