Skip to content

Commit

Permalink
Optimize for builds and client player movement fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
gamedolphin committed Nov 20, 2019
1 parent 4a391a1 commit e335832
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 129 deletions.
16 changes: 8 additions & 8 deletions RealmsCode/Assets/Scripts/Client/ClientSystemsGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public static bool IsInSystem(Type component, Type system, ref int depth)
[DisableAutoCreation]
public class ClientInitializationSystemGroup : ComponentSystemGroup
{
private BeginInitializationEntityCommandBufferSystem m_beginBarrier;
private EndInitializationEntityCommandBufferSystem m_endBarrier;
private BeginPresentationEntityCommandBufferSystem m_beginBarrier;
private EndPresentationEntityCommandBufferSystem m_endBarrier;

protected override void OnCreate()
{
m_beginBarrier = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
m_endBarrier = World.GetOrCreateSystem<EndInitializationEntityCommandBufferSystem>();
m_beginBarrier = World.GetOrCreateSystem<BeginPresentationEntityCommandBufferSystem>();
m_endBarrier = World.GetOrCreateSystem<EndPresentationEntityCommandBufferSystem>();

foreach (var system in World.Active.Systems)
{
Expand Down Expand Up @@ -110,13 +110,13 @@ public override void SortSystemUpdateList()
[AlwaysUpdateSystem]
public class ClientPresentationSystemGroup : ComponentSystemGroup
{
private BeginPresentationEntityCommandBufferSystem m_beginBarrier;
private EndPresentationEntityCommandBufferSystem m_endBarrier;
private BeginInitializationEntityCommandBufferSystem m_beginBarrier;
private EndInitializationEntityCommandBufferSystem m_endBarrier;

protected override void OnCreate()
{
m_beginBarrier = World.GetOrCreateSystem<BeginPresentationEntityCommandBufferSystem>();
m_endBarrier = World.GetOrCreateSystem<EndPresentationEntityCommandBufferSystem>();
m_beginBarrier = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
m_endBarrier = World.GetOrCreateSystem<EndInitializationEntityCommandBufferSystem>();

foreach(var system in World.Active.Systems)
{
Expand Down
12 changes: 11 additions & 1 deletion RealmsCode/Assets/Scripts/Client/Debug/ClientRandomWalkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@
[DisableAutoCreation]
public class ClientRandomWalkSystem : ClientInputSystem
{
private const float WALK_TIME = 0.2f;
private float currentTime = 0;
private float2 currentDirection;

protected override float2 GetInput()
{
return new float2(UnityEngine.Random.Range(-1, 2), UnityEngine.Random.Range(-1, 2));
if(Time.time - currentTime > WALK_TIME)
{
currentTime = Time.time;
currentDirection = new float2(UnityEngine.Random.Range(-1, 2), UnityEngine.Random.Range(-1, 2));
}

return currentDirection;
}
}
16 changes: 12 additions & 4 deletions RealmsCode/Assets/Scripts/Client/Network/ClientNetworkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ public struct ServerStateCommand : IComponentData { }
[DisableAutoCreation]
[AlwaysUpdateSystem]
[UpdateAfter(typeof(ClientInputSystem))]
public class ClientNetworkSystem : ComponentSystem, INetEventListener
public class ClientNetworkSystem : ComponentSystem, INetEventListener, INetLogger
{
private NetManager client;
private NetPeer server;
private byte[] temp = new byte[1024];
private byte[] temp = new byte[5120];

protected override void OnCreate()
{
base.OnCreate();
NetDebug.Logger = this;
client = new NetManager(this);
client.Start();
}

protected override void OnUpdate()
{
client.PollEvents();
{
HandleConnection();
HandleInputs();
client.PollEvents();
}

private void HandleConnection()
Expand Down Expand Up @@ -79,6 +80,7 @@ protected override void OnDestroy()
public void OnPeerConnected(NetPeer peer)
{
server = peer;
Debug.Log("Connected to server!");
}

public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
Expand All @@ -88,6 +90,7 @@ public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)

public void OnNetworkError(IPEndPoint endPoint, SocketError socketError)
{
Debug.Log("[CLIENT] error " + socketError);
}

public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
Expand All @@ -114,4 +117,9 @@ public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
public void OnConnectionRequest(ConnectionRequest request)
{
}

public void WriteNet(NetLogLevel level, string str, params object[] args)
{
Debug.LogFormat(str, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ public void Execute(Entity entity, int index,
[ReadOnly] ref LatestPlayerState latestState,
[ReadOnly] ref PlayerSpeed pSpeed,
ref Translation translation)
{
{
var pos = latestState.pState.Position;
var targetPos = float3(pos.x, pos.y, pos.z);

translation.Value = lerp(translation.Value, targetPos, pSpeed.Speed*deltaTime);
var delta = targetPos - translation.Value;
var len2 = dot(delta, delta);
var step = pSpeed.Speed * deltaTime;
if(len2 < step * step)
{
translation.Value = lerp(translation.Value, targetPos, step);
}
else
{
var direction = delta / sqrt(len2);
translation.Value += direction * step;
}
}
}

Expand Down
51 changes: 1 addition & 50 deletions RealmsCode/Assets/Scripts/Common/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,10 @@
using UnityEngine;
using Zenject;

[System.Serializable]
public class NetworkSettings
{
public string Name;
public string Ip;
public int Port;
}

public struct NetworkSettingsComponent : IComponentData
{
public NativeString64 Name;
public NativeString64 Ip;
public int Port;
}

public class GameManager : IInitializable
{
[Inject]
private NetworkSettings networkSettings;

[Inject]
private WorldManager worldManager;

[Inject]
private WorldSettings worldSettings;

public void Initialize()
{
worldManager.Initialize();

if (worldSettings.HasServer)
{
var world = worldManager.ServerWorld;
var entity = world.EntityManager.CreateEntity();
world.EntityManager.AddComponentData(entity, new NetworkSettingsComponent
{
// server doesnot care about ip or name
Port = networkSettings.Port
});
}

if (worldSettings.HasClient)
{
foreach (var world in worldManager.ClientWorlds)
{
var entity = world.EntityManager.CreateEntity();
world.EntityManager.AddComponentData(entity, new NetworkSettingsComponent
{
Name = new NativeString64(networkSettings.Name),
Ip = new NativeString64(networkSettings.Ip),
Port = networkSettings.Port
});
}
}
{
}
}
9 changes: 9 additions & 0 deletions RealmsCode/Assets/Scripts/Common/Input/InputData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ public override string ToString()
{
return $"{Right} {Left} {Up} {Down}";
}

public InputData(InputData data)
{
Index = data.Index;
Right = data.Right;
Left = data.Left;
Up = data.Up;
Down = data.Down;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ protected override void OnUpdate()
Entities.ForEach((Entity e, ref ServerInputCommand command) =>
{
// this only happens on the server
PostUpdateCommands.DestroyEntity(e);
EntityManager.DestroyEntity(e);
if (players.TryGetValue(command.playerID, out Entity player))
{
AddInput(player, command.inputData);
AddInput(player, new InputData(command.inputData));
}
});

Expand Down Expand Up @@ -95,7 +95,9 @@ private Entity GeneratePlayer (CreatePlayer cp)
{
EntityManager.AddBuffer<ClientInput>(entity);
}
#if UNITY_EDITOR
EntityManager.SetName(entity, $"Player{cp.Id}");
#endif
players[cp.Id] = entity;
EntityManager.DestroyEntity(p);
if (cp.OwnPlayer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public override void InstallBindings()
InitializeMessagePack();
Container.BindInstance(settings).AsSingle();
Container.BindInterfacesAndSelfTo<WorldManager>().AsSingle().NonLazy();
Container.BindInstance(networkSettings).WhenInjectedInto<GameManager>();
Container.BindInterfacesAndSelfTo<GameManager>().AsSingle().NonLazy();
Container.BindInstance(networkSettings).WhenInjectedInto<WorldManager>();
}

private void InitializeMessagePack()
Expand Down
23 changes: 17 additions & 6 deletions RealmsCode/Assets/Scripts/Server/Network/ServerNetworkSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define DEBUG
using Unity.Entities;
using LiteNetLib;
using UnityEngine;
Expand All @@ -9,28 +10,32 @@
using Zenject;
using Unity.Transforms;


public struct ServerInputCommand : IComponentData
{
public int playerID;
public InputData inputData;
}

[DisableAutoCreation]
public class ServerNetworkSystem : ComponentSystem, INetEventListener
[AlwaysUpdateSystem]
[UpdateBefore((typeof(PlayerLifecyleSystem)))]
public class ServerNetworkSystem : ComponentSystem, INetEventListener, INetLogger
{
[Inject]
private WorldSettings settings;

private NetManager server;
private readonly byte[] temp = new byte[1024];
private readonly byte[] temp = new byte[5120];

private List<NetPeer> clientList = new List<NetPeer>();
private EntityQuery stateQuery => GetEntityQuery(typeof(SerializedServerState));

protected override void OnCreate()
{
NetDebug.Logger = this;
base.OnCreate();
server = new NetManager(this);
server = new NetManager(this);
}

protected override void OnUpdate()
Expand Down Expand Up @@ -97,8 +102,8 @@ public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMetho
reader.GetBytes(temp, available);
var inputData = MessagePackSerializer.Deserialize<InputData>(temp);
// update the simulation
var entity = PostUpdateCommands.CreateEntity();
PostUpdateCommands.AddComponent(entity, new ServerInputCommand
var entity = EntityManager.CreateEntity();
EntityManager.AddComponentData(entity, new ServerInputCommand
{
playerID = (int)peer.Tag,
inputData = inputData
Expand All @@ -124,5 +129,11 @@ public void OnConnectionRequest(ConnectionRequest request)
int hashcode = dataReader.GetInt();
string name = dataReader.GetString();
peer.Tag = hashcode;
}
Debug.Log("RECEIVED CONNECTINO REQUEST");
}

public void WriteNet(NetLogLevel level, string str, params object[] args)
{
Debug.LogFormat(str, args);
}
}
Loading

0 comments on commit e335832

Please sign in to comment.