public void RecieveDeltas(DeltaEnvelope deltaEnv) { if (!Role.IsInRole(EntityRole.AUTHORITY)) { //if (Role.IsInRole(EntityRole.CONTROLLER)) //{ // Console.WriteLine($"State Before Delta: {_logic.TakeSnapshot()}"); //} if (_lastValidSnapshot != null) { _logic.ApplySnapshot(_lastValidSnapshot); } //if (Role.IsInRole(EntityRole.CONTROLLER)) //{ // Console.WriteLine($"Last Valid State: {_logic.TakeSnapshot()}"); // Console.WriteLine($"Applying Delta: {deltaEnv.Deltas} From: {deltaEnv.SentTimestamp} It Is: {_sim.GetTimestamp()}"); //} _logic.ApplyDeltas(deltaEnv.Deltas); _lastValidSnapshot = _logic.TakeSnapshot(); _recordedFrameCommands = _recordedFrameCommands .Where(e => e.SentTimestamp > deltaEnv.SentTimestamp) .ToList(); var processCommands = Role.IsInRole(EntityRole.CONTROLLER); foreach (var env in _recordedFrameCommands) { if (processCommands) { _logic.ProcessCommands(env.Commands); //Console.WriteLine($"ReProcessing Commands:"); //Console.WriteLine($"Timestamp: {env.SentTimestamp}, Delta: {env.SentDelta}"); //foreach (var command in env.Commands) //{ // Console.WriteLine($"{command}"); //} } _logic.Simulate(env.SentDelta); //if (processCommands) //{ // Console.WriteLine($"State After Reprocess: {_logic.TakeSnapshot()}"); //} } //if (Role.IsInRole(EntityRole.CONTROLLER)) //{ // Console.WriteLine($"State After Simulation: {_logic.TakeSnapshot()}"); //} } }
public void Update() { var isAuthority = Role.IsInRole(EntityRole.AUTHORITY); var delta = _sim.GetDeltaTime(); if (_lastSentSnapshot == null && Role.IsInRole(EntityRole.AUTHORITY)) { _lastSentSnapshot = _logic.TakeSnapshot(); } if (!isAuthority) { var currentFrameCommands = new FrameCommands() { Commands = new List <object>(), SentDelta = delta, SentTimestamp = _sim.GetTimestamp() }; if (Role.IsInRole(EntityRole.CONTROLLER)) { //Console.WriteLine($"Delta is: {delta}"); var commands = _logic.GenerateCommands(); currentFrameCommands.Commands.Add(commands); if (!Role.IsInRole(EntityRole.AUTHORITY)) { _logic.ProcessCommands(currentFrameCommands.Commands); //Console.WriteLine($"Processing Commands:"); //foreach (var command in currentFrameCommands.Commands) //{ // Console.WriteLine($"{command}"); //} } _authorityEntity.SendFrameRecord(currentFrameCommands); } _recordedFrameCommands.Add(currentFrameCommands); } //if (Role.IsInRole(EntityRole.AUTHORITY)) //{ // _logic.ProcessCommands(_recievedEnvelopes.SelectMany(e => e.Envelope.Commands)); // _recievedEnvelopes.Clear(); //} _logic.Simulate(delta); if (Role.IsInRole(EntityRole.AUTHORITY)) { var timestamp = _sim.GetTimestamp(); var newSnapshot = _logic.TakeSnapshot(); _recordedFrameSnapshots.Add(new FrameSnapshot() { Snapshot = newSnapshot, RecordedTimestamp = timestamp }); _timeSinceLastUpdate += delta; if (_timeSinceLastUpdate > UpdatePeriod) { _timeSinceLastUpdate = 0; var env = new DeltaEnvelope() { Deltas = _logic.CalculateDeltas(_lastSentSnapshot, newSnapshot), SentTimestamp = timestamp }; foreach (var client in _clientEntities) { client.SendDeltaEnvelope(env); } _controllerEntity?.SendDeltaEnvelope(env); _lastSentSnapshot = newSnapshot; } _recordedFrameSnapshots = _recordedFrameSnapshots.Where(r => r.RecordedTimestamp > timestamp - 1000).ToList(); } }
public void SendDeltaEnvelope(DeltaEnvelope deltaEnv) { RecieveDeltas(deltaEnv); }
public void SendDeltaEnvelope(DeltaEnvelope deltaEnv) { _sim.SendMessage(new EntityMessage <DeltaEnvelope>(Id, deltaEnv)); }