forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoInGameServerSystem.cs
More file actions
56 lines (47 loc) · 2.2 KB
/
Copy pathGoInGameServerSystem.cs
File metadata and controls
56 lines (47 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
namespace KickBall
{
// When server receives GoInGameRequest, ready the client to receive ghosts, spawn the player character, and delete the RPC request
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct GoInGameServerSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EntityPrefabs>();
var query = SystemAPI.QueryBuilder().WithAll<GoInGameRequest, ReceiveRpcCommandRequest>().Build();
state.RequireForUpdate(query);
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var playerPrefab = SystemAPI.GetSingleton<EntityPrefabs>().Player;
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (requestSource, requestEntity) in
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>>()
.WithAll<GoInGameRequest>()
.WithEntityAccess())
{
ecb.AddComponent<NetworkStreamInGame>(requestSource.ValueRO.SourceConnection);
// spawn player
{
var networkId = SystemAPI.GetComponent<NetworkId>(requestSource.ValueRO.SourceConnection);
var player = ecb.Instantiate(playerPrefab);
ecb.SetComponent(player, new GhostOwner { NetworkId = networkId.Value });
// Set color for player based on their network ID.
var rand = Random.CreateFromIndex((uint)networkId.Value);
ecb.SetComponent(player, new Color { Value = new float4(rand.NextFloat3(), 0) });
// Add the player to the linked entity group so it is destroyed automatically on disconnect
ecb.AppendToBuffer(requestSource.ValueRO.SourceConnection, new LinkedEntityGroup { Value = player });
}
ecb.DestroyEntity(requestEntity);
}
ecb.Playback(state.EntityManager);
}
}
}