-
Notifications
You must be signed in to change notification settings - Fork 168
/
ServerInGame.cs
193 lines (167 loc) · 7.96 KB
/
ServerInGame.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.MegacityMetro.Gameplay;
using Unity.NetCode;
using Unity.NetCode.Extensions;
using Unity.Transforms;
using UnityEngine;
#if UNITY_SERVER && !UNITY_EDITOR
using Unity.Networking.Transport;
#endif
namespace Unity.MegacityMetro
{
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateInGroup(typeof(InitializationSystemGroup), OrderLast = true)]
[CreateAfter(typeof(NetworkStreamReceiveSystem))]
public partial struct ServerInGame : ISystem
{
#region Jobs
[BurstCompile]
private partial struct GetPositionJob : IJob
{
public NativeArray<SpawnPointElement> SpawnPoints;
public NativeList<float3> UsedPositions;
public Mathematics.Random Random;
[BurstCompile]
public void Execute()
{
var availablePositions = CreateAvailablePositions();
// If all player positions have been used, reset the list
if (availablePositions.Length == 0)
{
UsedPositions.Clear();
availablePositions = CreateAvailablePositions();
}
// Choose a random position from the list of available player names
var randomIndex = Random.NextInt(0, availablePositions.Length);
var position = availablePositions[randomIndex];
UsedPositions.Add(position);
availablePositions.Dispose();
}
private NativeList<float3> CreateAvailablePositions()
{
var availablePositions = new NativeList<float3>(Allocator.TempJob);
// Get a list of spawnPoints that have not been used
foreach (var position in SpawnPoints)
{
if (!UsedPositions.Contains(position.Value))
{
availablePositions.Add(position.Value);
}
}
return availablePositions;
}
}
[BurstCompile]
partial struct UpdateConnectionPositionSystemJob : IJobEntity
{
[ReadOnly] public ComponentLookup<LocalTransform> transformLookup;
public void Execute(ref GhostConnectionPosition conPos, in CommandTarget target)
{
if (!transformLookup.HasComponent(target.targetEntity))
return;
conPos = new GhostConnectionPosition
{
Position = transformLookup[target.targetEntity].Position
};
}
}
#endregion
private NativeList<float3> m_UsedPositions;
private Mathematics.Random m_Random;
public void OnCreate(ref SystemState state)
{
var myEntity = state.EntityManager.CreateEntity();
state.EntityManager.AddBuffer<PlayerConnectedElement>(myEntity);
state.RequireForUpdate<PlayerConnectedElement>();
state.RequireForUpdate<PlayerSpawner>();
state.RequireForUpdate<SpawnPointElement>();
m_UsedPositions = new NativeList<float3>(Allocator.Persistent);
var currentTime = DateTime.Now;
var seed = currentTime.Minute + currentTime.Second + currentTime.Millisecond + 1;
m_Random = new Mathematics.Random((uint)seed);
#if UNITY_SERVER && !UNITY_EDITOR
SystemAPI.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(NetworkEndpoint.AnyIpv4.WithPort(CommandLineConfig.MultiplayEndpoint.Port));
#endif
const int tileSize = 256;
var grid = state.EntityManager.CreateEntity();
state.EntityManager.SetName(grid, "GhostImportanceSingleton");
state.EntityManager.AddComponentData(grid, new GhostDistanceData
{
TileSize = new int3(tileSize, 1024 * 8, tileSize),
TileCenter = new int3(0, 0, 0),
TileBorderWidth = new float3(5f),
});
state.EntityManager.AddComponentData(grid, new GhostImportance
{
ScaleImportanceFunction = GhostDistanceImportance.ScaleFunctionPointer,
GhostConnectionComponentType = ComponentType.ReadOnly<GhostConnectionPosition>(),
GhostImportanceDataType = ComponentType.ReadOnly<GhostDistanceData>(),
GhostImportancePerChunkDataType = ComponentType.ReadOnly<GhostDistancePartitionShared>(),
});
var clientServerTickRate = new ClientServerTickRate();
clientServerTickRate.ResolveDefaults();
clientServerTickRate.SimulationTickRate = clientServerTickRate.NetworkTickRate = RateSettings.tickRate;
clientServerTickRate.PredictedFixedStepSimulationTickRatio = RateSettings.fixedRateRatio;
state.EntityManager.CreateSingleton(clientServerTickRate);
RateSettings.ApplyFrameRate();
}
public void OnUpdate(ref SystemState state)
{
var spawnBuffer = SystemAPI.GetSingletonBuffer<SpawnPointElement>();
var prefab = SystemAPI.GetSingleton<PlayerSpawner>().Player;
var cmdBuffer = SystemAPI.GetSingletonRW<BeginSimulationEntityCommandBufferSystem.Singleton>().ValueRW.CreateCommandBuffer(state.WorldUnmanaged);
var originalTrans = state.EntityManager.GetComponentData<LocalTransform>(prefab);
var health = state.EntityManager.GetComponentData<VehicleHealth>(prefab);
foreach (var (netId, entity) in SystemAPI.Query<RefRO<NetworkId>>().WithNone<NetworkStreamInGame>()
.WithEntityAccess())
{
var playerBuffer = SystemAPI.GetSingletonBuffer<PlayerConnectedElement>(true);
var networkIdIsBusy = false;
foreach (var playerElement in playerBuffer)
{
if (playerElement.NetworkID == netId.ValueRO.Value)
{
networkIdIsBusy = true;
break;
}
}
if (networkIdIsBusy)
{
Debug.LogWarning($"The NetworkID {netId.ValueRO.Value} is busy, the player couldn't be created");
continue;
}
var spawnPointsArray = spawnBuffer.ToNativeArray(Allocator.TempJob);
var findNewPosition = new GetPositionJob
{
SpawnPoints = spawnPointsArray,
UsedPositions = m_UsedPositions,
Random = m_Random
};
state.Dependency = findNewPosition.Schedule(state.Dependency);
state.Dependency.Complete();
cmdBuffer.AddComponent<NetworkStreamInGame>(entity);
var player = cmdBuffer.Instantiate(prefab);
var networkIdValue = netId.ValueRO.Value;
cmdBuffer.SetComponent(player, new GhostOwner { NetworkId = networkIdValue });
var newTrans = originalTrans;
newTrans.Position = m_UsedPositions[m_UsedPositions.Length - 1];
cmdBuffer.SetComponent(player, newTrans);
cmdBuffer.AppendToBuffer(entity, new LinkedEntityGroup { Value = player });
cmdBuffer.SetComponent(player, health);
cmdBuffer.AddComponent<GhostConnectionPosition>(entity);
cmdBuffer.SetComponent(entity, new CommandTarget { targetEntity = player });
spawnPointsArray.Dispose();
}
var updateJob = new UpdateConnectionPositionSystemJob
{
transformLookup = SystemAPI.GetComponentLookup<LocalTransform>(true)
};
state.Dependency = updateJob.ScheduleParallel(state.Dependency);
}
}
}