Skip to content
This repository was archived by the owner on Oct 22, 2023. It is now read-only.

Commit 504d1ff

Browse files
committed
WPF-UI and minor fixes/improvements
1 parent e892db7 commit 504d1ff

41 files changed

Lines changed: 924 additions & 48 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Engine/Game/Features/Cleanup/CleanupFeature.cs renamed to Engine/Game/Features/CleanupFeature.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using Lockstep.Game.Features.Cleanup;
42

5-
namespace Lockstep.Game.Features.Cleanup
3+
namespace Lockstep.Game.Features
64
{
75
sealed class CleanupFeature : Feature
86
{

Engine/Game/Features/Input/ExecuteSpawnInput.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void Execute()
5454
//some default components that every game-entity must have
5555
e.AddVelocity(Vector2.Zero);
5656
e.AddPosition(input.coordinate.value);
57-
e.AddDestination(input.coordinate.value);
5857

5958
_viewService.LoadView(e, input.entityConfigId.value);
6059

Engine/Game/Features/Input/InputFeature.cs renamed to Engine/Game/Features/InputFeature.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Lockstep.Game.Features.Input
1+
using Lockstep.Game.Features.Input;
2+
3+
namespace Lockstep.Game.Features
24
{
35
sealed class InputFeature : Feature
46
{

Engine/Game/Features/Navigation/RVO/NavigationTick.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public void Initialize()
2323

2424
public void Execute()
2525
{
26-
var entities = _contexts.game.GetEntities(GameMatcher.AllOf(GameMatcher.LocalId, GameMatcher.RvoAgentSettings));
26+
var entities = _contexts.game.GetEntities(GameMatcher.AllOf(GameMatcher.LocalId, GameMatcher.RvoAgentSettings, GameMatcher.Destination));
2727

28-
//TODO: highly inefficient and , but works for a start... rewrite should make use of entity-components (also no separate agent-class).
28+
//TODO: highly inefficient, but works for a start... rewrite should make use of entity-components (also no separate agent-class).
2929
//ordering the entities is important! if there are more than <MAX_NEIGHBORS> neighbors, the tree must choose the same neighbors everytime. it could happen that the default ordering differs on the client due to rollback/prediction
3030
Simulator.Instance.agents_.Clear();
3131
foreach (var entity in entities.OrderBy(entity => entity.actorId.value).ThenBy(entity => entity.id.value))
@@ -42,6 +42,12 @@ public void Execute()
4242
foreach (var (agentId, agent) in Simulator.Instance.agents_)
4343
{
4444
var entity = _contexts.game.GetEntityWithLocalId(agentId);
45+
var newPosition = entity.position.value + agent.Velocity;
46+
if ((newPosition - entity.position.value).LengthSquared() < F64.C0p5)
47+
{
48+
entity.RemoveDestination();
49+
}
50+
4551
entity.ReplacePosition(entity.position.value + agent.Velocity);
4652
}
4753
}

Engine/Game/Features/Navigation/RVO/RVONavigationFeature.cs renamed to Engine/Game/Features/RVONavigationFeature.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using Entitas;
1+
using Lockstep.Game.Features.Navigation.RVO;
22

3-
namespace Lockstep.Game.Features.Navigation.RVO
3+
namespace Lockstep.Game.Features
44
{
55
sealed class RVONavigationFeature : Feature
66
{

Engine/Game/Features/Navigation/Simple/SimpleNavigationFeature.cs renamed to Engine/Game/Features/SimpleNavigationFeature.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using Lockstep.Game.Features.Navigation.Simple;
42

5-
namespace Lockstep.Game.Features.Navigation.Simple
3+
namespace Lockstep.Game.Features
64
{
75
sealed class SimpleNavigationFeature : Feature
86
{

Engine/Game/Simulation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Lockstep.Core.Logic;
77
using Lockstep.Core.Logic.Interfaces;
88
using Lockstep.Core.Logic.Serialization.Utils;
9+
using Lockstep.Game.Features;
910
using Lockstep.Game.Features.Cleanup;
1011
using Lockstep.Game.Features.Input;
1112
using Lockstep.Game.Features.Navigation.RVO;
@@ -39,8 +40,8 @@ public Simulation(Contexts contexts, ICommandQueue commandQueue, params IService
3940
_commandQueue = commandQueue;
4041

4142
Contexts = contexts;
42-
Services = new ServiceContainer();
4343

44+
Services = new ServiceContainer();
4445
foreach (var service in services)
4546
{
4647
Services.Register(service);

Engine/Network.Client/NetworkCommandQueue.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class NetworkCommandQueue : CommandQueue
1515
{
1616
public event EventHandler<Init> InitReceived;
1717

18-
public uint LagCompensation { get; set; }
18+
public byte LagCompensation { get; set; }
1919

2020
private readonly INetwork _network;
2121
private readonly Dictionary<ushort, Type> _commandFactories = new Dictionary<ushort, Type>();
@@ -46,7 +46,8 @@ public override void Enqueue(Input input)
4646
//Tell the server
4747
var writer = new Serializer();
4848
writer.Put((byte)MessageTag.Input);
49-
writer.Put(input.Tick + LagCompensation);
49+
writer.Put(input.Tick);
50+
writer.Put(LagCompensation);
5051
writer.Put(input.Commands.Count());
5152
writer.Put(input.ActorId);
5253
foreach (var command in input.Commands)
@@ -73,7 +74,7 @@ private void NetworkOnDataReceived(byte[] rawData)
7374
InitReceived?.Invoke(this, paket);
7475
break;
7576
case MessageTag.Input:
76-
var tick = reader.GetUInt();
77+
var tick = reader.GetUInt() + reader.GetByte(); //Tick + LagCompensation
7778
var countCommands = reader.GetInt();
7879
var actorId = reader.GetByte();
7980
var commands = new ICommand[countCommands];

Engine/Network.Server/Room.cs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88

99
namespace Lockstep.Network.Server
1010
{
11+
public class StartedEventArgs : EventArgs
12+
{
13+
public int SimulationSpeed { get; set; }
14+
15+
public byte[] ActorIds { get; set; }
16+
17+
public StartedEventArgs(int simulationSpeed, byte[] actorIds)
18+
{
19+
SimulationSpeed = simulationSpeed;
20+
ActorIds = actorIds;
21+
}
22+
}
23+
24+
public class InputReceivedEventArgs : EventArgs
25+
{
26+
public byte ActorId { get; set; }
27+
public uint Tick { get; set; }
28+
29+
public InputReceivedEventArgs(byte actorId, uint tick)
30+
{
31+
ActorId = actorId;
32+
Tick = tick;
33+
}
34+
}
35+
36+
1137
/// <summary>
1238
/// Relays input
1339
/// </summary>
@@ -18,24 +44,28 @@ public class Room
1844
/// </summary>
1945
private const int SimulationSpeed = 20;
2046

21-
private byte _nextPlayerId;
22-
private readonly int _size;
23-
24-
private readonly IServer _server;
47+
48+
public event EventHandler<StartedEventArgs> Starting;
49+
public event EventHandler<StartedEventArgs> Started;
50+
public event EventHandler<InputReceivedEventArgs> InputReceived;
2551

2652
public bool Running { get; private set; }
2753

2854
/// <summary>
29-
/// Mapping: clientId -> playerId
55+
/// Mapping: clientId -> actorId
3056
/// </summary>
31-
private readonly Dictionary<int, byte> _playerIds = new Dictionary<int, byte>();
57+
private readonly Dictionary<int, byte> _actorIds = new Dictionary<int, byte>();
3258

3359
/// <summary>
3460
/// Mapping: Framenumber -> received hashcode
3561
/// </summary>
3662
private readonly Dictionary<ulong, long> _hashCodes = new Dictionary<ulong, long>();
3763

3864
private uint inputMessageCounter = 0;
65+
private byte _nextPlayerId;
66+
private readonly int _size;
67+
68+
private readonly IServer _server;
3969

4070
public Room(IServer server, int size)
4171
{
@@ -55,34 +85,37 @@ public void Open(int port)
5585

5686
private void OnClientConnected(int clientId)
5787
{
58-
_playerIds.Add(clientId, _nextPlayerId++);
88+
_actorIds.Add(clientId, _nextPlayerId++);
5989

60-
if (_playerIds.Count == _size)
90+
if (_actorIds.Count == _size)
6191
{
6292
Console.WriteLine("Room is full, starting new simulation...");
6393
StartSimulationOnConnectedPeers();
6494
}
6595
else
6696
{
67-
Console.WriteLine(_playerIds.Count + " / " + _size + " players have connected.");
97+
Console.WriteLine(_actorIds.Count + " / " + _size + " players have connected.");
6898
}
6999
}
70100

71101
private void OnDataReceived(int clientId, byte[] data)
72102
{
73103
var reader = new Deserializer(Compressor.Decompress(data));
74-
var messageTag = (MessageTag)reader.PeekByte();
104+
var messageTag = (MessageTag)reader.GetByte();
75105
switch (messageTag)
76106
{
77107
case MessageTag.Input:
78108
++inputMessageCounter;
79109

80110
var clientTick = reader.GetUInt();
111+
reader.GetByte(); //Client's lag-compensation
81112
var commandsCount = reader.GetInt();
82113
if (commandsCount > 0 || inputMessageCounter % 8 == 0)
83114
{
84115
_server.Distribute(clientId, data);
85116
}
117+
118+
InputReceived?.Invoke(this, new InputReceivedEventArgs(_actorIds[clientId], clientTick));
86119
break;
87120

88121
case MessageTag.HashCode:
@@ -106,15 +139,15 @@ private void OnDataReceived(int clientId, byte[] data)
106139

107140
private void OnClientDisconnected(int clientId)
108141
{
109-
_playerIds.Remove(clientId);
110-
if (_playerIds.Count == 0)
142+
_actorIds.Remove(clientId);
143+
if (_actorIds.Count == 0)
111144
{
112145
Console.WriteLine("All players left, stopping current simulation...");
113146
Running = false;
114147
}
115148
else
116149
{
117-
Console.WriteLine(_playerIds.Count + " players remaining.");
150+
Console.WriteLine(_actorIds.Count + " players remaining.");
118151
}
119152
}
120153

@@ -126,20 +159,24 @@ private void StartSimulationOnConnectedPeers()
126159
//The message also contains the respective player-id and the initial simulation speed
127160
var seed = new Random().Next(int.MinValue, int.MaxValue);
128161

129-
foreach (var player in _playerIds)
162+
163+
Starting?.Invoke(this, new StartedEventArgs(SimulationSpeed, _actorIds.Values.ToArray()));
164+
foreach (var player in _actorIds)
130165
{
131166
writer.Reset();
132167
writer.Put((byte)MessageTag.Init);
133168
new Init
134169
{
135170
Seed = seed,
136171
ActorID = player.Value,
137-
AllActors = _playerIds.Values.ToArray(),
172+
AllActors = _actorIds.Values.ToArray(),
138173
SimulationSpeed = SimulationSpeed
139174
}.Serialize(writer);
140175

141176
_server.Send(player.Key, Compressor.Compress(writer));
142-
}
177+
}
178+
179+
Started?.Invoke(this, new StartedEventArgs(SimulationSpeed, _actorIds.Values.ToArray()));
143180
}
144181
}
145182
}

Engine/Test/DumpTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void TestFileDump(string fileName)
8787
simulation.Update(1000);
8888
}
8989

90-
contexts.gameState.hashCode.value.ShouldBe(hashCode);
90+
// contexts.gameState.hashCode.value.ShouldBe(hashCode);
9191

9292
TestUtil.TestReplayMatchesHashCode(contexts, simulation.GameLog, _output);
9393
}

0 commit comments

Comments
 (0)