Пример #1
0
        public void Run()
        {
            int tickMillieconds = (int)(1000 * this._fixedTick);

            var state = new FixedUpdateState
            {
                WaitHandle = new AutoResetEvent(initialState: false),

                Diagnostics = new FixedUpdateState.DiagnosticsState
                {
                    ExceedTickCountReportingCountdown = FixedUpdateState.DiagnosticsState.GetCountdownFromFixedTick(this._fixedTick),
                    ExceedTickCount  = 0,
                    TotalTickElapsed = 0,
                    Stopwatch        = new Stopwatch(),
                }
            };

            // TODO: Timer is limited to system clock resolution. The max (worst) seems to be 15.625 ms which
            // is maybe sufficient (64 ticks/s). See ClockRes SysInternal tool.
            //
            // In the future look into using Windows multi-media (winmm.dll) timers:
            //  https://www.codeproject.com/Articles/17474/Timer-surprises-and-how-to-avoid-them
            using (var stateTimer = new Timer(
                       callback: Update,
                       state: state,
                       dueTime: 0,
                       period: tickMillieconds))
            {
                _logger.Info($"World started: Id={this.InstanceId:x8}, period={tickMillieconds}ms");

                state.WaitHandle.WaitOne();
            }

            _logger.Info($"World stopped: Id={this.InstanceId:x8}");
        }
Пример #2
0
 public AnyState AnyState(string name, Condition switchCondition,
                          EnterState enter             = null, UpdateState update = null,
                          FixedUpdateState fixedUpdate = null, ExitState exit     = null
                          )
 {
     return(stateMachine.AnyState(name, switchCondition, enter, update, fixedUpdate, exit));
 }
Пример #3
0
        private void PostUpdateDiagnostics(FixedUpdateState state)
        {
            state.Diagnostics.Stopwatch.Stop();

            state.Diagnostics.TotalTickElapsed += (int)state.Diagnostics.Stopwatch.ElapsedMilliseconds;

            if (state.Diagnostics.Stopwatch.ElapsedMilliseconds > FixedUpdateState.DiagnosticsState.GetMsFromFixedTick(this._fixedTick))
            {
                state.Diagnostics.ExceedTickCount++;

                state.Diagnostics.LastExceedElasped = (int)state.Diagnostics.Stopwatch.ElapsedMilliseconds;
            }

            if (--state.Diagnostics.ExceedTickCountReportingCountdown == 0)
            {
                state.Diagnostics.ExceedTickCountReportingCountdown = FixedUpdateState.DiagnosticsState.GetCountdownFromFixedTick(this._fixedTick);

                if (state.Diagnostics.ExceedTickCount > 0)
                {
                    this._logger.Error($"Fixed update exceeded tick speed: count={state.Diagnostics.ExceedTickCount}, framesCounted={state.Diagnostics.ExceedTickCountReportingCountdown}, fixedTick={FixedUpdateState.DiagnosticsState.GetMsFromFixedTick(this._fixedTick)}ms, lastExceedElapsed={state.Diagnostics.LastExceedElasped}ms, avgElapsed={state.Diagnostics.TotalTickElapsed / state.Diagnostics.ExceedTickCountReportingCountdown}ms");

                    state.Diagnostics.ExceedTickCount = 0;
                }

                state.Diagnostics.TotalTickElapsed = 0;
            }
        }
Пример #4
0
        // -- //

        public State(StateMachine stateMachine, string name, EnterState enter, UpdateState update, FixedUpdateState fixedUpdate, ExitState exit)
        {
            this.stateMachine = stateMachine;
            this.name         = name;
            this.enter        = enter;
            this.update       = update;
            this.fixedUpdate  = fixedUpdate;
            this.exit         = exit;
        }
Пример #5
0
        // -- //

        public State AddState(string name, EnterState enter = null,
                              UpdateState update            = null, FixedUpdateState fixedUpdate = null,
                              ExitState exit = null
                              )
        {
            State newState = new State(this, name, enter, update, fixedUpdate, exit);

            if (currentState == null)
            {
                currentState = newState;
            }

            states.Add(name, newState);

            return(newState);
        }
Пример #6
0
        public State To(string name, Condition switchCondition,
                        EnterState enter             = null, UpdateState update = null,
                        FixedUpdateState fixedUpdate = null, ExitState exit     = null
                        )
        {
            State stateToAdd = stateMachine.GetState(name);

            if (stateToAdd == null)
            {
                stateToAdd = stateMachine.AddState(name, enter, update, fixedUpdate, exit);
            }

            StateOutput output = new StateOutput();

            output.condition = switchCondition;
            output.toState   = stateToAdd;

            outputStates.Add(output);

            return(output.toState);
        }
Пример #7
0
        public AnyState AnyState(string name, Condition switchCondition,
                                 EnterState enter             = null, UpdateState update = null,
                                 FixedUpdateState fixedUpdate = null, ExitState exit     = null
                                 )
        {
            AnyState stateToAdd = GetState(name) as AnyState;

            if (stateToAdd == null)
            {
                stateToAdd = new AnyState(this, name, enter, update, fixedUpdate, exit);
            }

            StateOutput output = new StateOutput();

            output.condition = switchCondition;
            output.toState   = stateToAdd;

            anyStates.Add(output);
            states.Add(name, stateToAdd);

            return(stateToAdd);
        }
Пример #8
0
        // -- //

        public AnyState(StateMachine stateMachine, string name, EnterState enter, UpdateState update, FixedUpdateState fixedUpdate, ExitState exit)
            : base(stateMachine, name, enter, update, fixedUpdate, exit)
        {
        }
Пример #9
0
 private static void PreUpdateDiagnostics(FixedUpdateState state)
 {
     state.Diagnostics.Stopwatch.Restart();
 }