Example #1
0
        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()}");
                //}
            }
        }
        private LocalEntity CreateLocalEntity(IEntityLogic logic, EntityAdded added)
        {
            var entity = new LocalEntity(added.EntityId, logic, this, GetRoleForSim(Id, added), added.UpdatePeriod);

            _entities[entity.Id] = entity;
            logic.ApplySnapshot(added.InitialSnapshot);
            return(entity);
        }