void Awake()
 {
     _simulation         = GetComponent <PlayerSimulation>();
     _coroutineScheduler = new CoroutineScheduler(initialCapacity: 1, growthStep: 1);
     _coroutineScheduler.Run(CoroutineUtils.OnSendRate(_sendRate, this, SendMessage));
     //_coroutineScheduler.Run(CoroutineUtils.EveryFrame(this, SendMessage));
 }
Beispiel #2
0
    private void SimulateOffline()
    {
        PlayerSimulation.SimulateOffline(_simConfig, _eulerBody, GetInput, 0d, 5d, Time.fixedDeltaTime);

        transform.position = _eulerBody.State.Position;
        transform.rotation = _eulerBody.State.Rotation;
    }
Beispiel #3
0
    private void Awake()
    {
        const float bufferLenghtInS = 5f;

        _simulation    = GetComponent <PlayerSimulation>();
        _inputHistory  = new CircularBuffer <PlayerInput>(Mathf.RoundToInt(60 * bufferLenghtInS));
        _serverHistory = new CircularBuffer <RigidbodyState>(50); // Todo: in external debug vizualizer component
    }
    private void FixedUpdate()
    {
        //_body.AddTorque(new Vector3(10f * Input.GetAxis("Roll"), 0f, 0f), ForceMode.Force);
        PlayerSimulation.Simulate(_simConfig, _body, RealBodyTest.SampleInput());

        _body.Integrate(Time.fixedDeltaTime);
        transform.position = _body.State.Position;
        transform.rotation = _body.State.Rotation;
    }
Beispiel #5
0
    void Awake()
    {
        Animator animator = GetComponent <Animator>();

        playerSimulation = new PlayerSimulation(transform, playerData, animator);
        groundCheck      = transform.Find("GroundCheck");
        playerLogic      = new PlayerLogic(playerData, playerSimulation, groundCheck);
        soundManager     = GameObject.FindGameObjectWithTag("SoundManager").GetComponent <SoundManager>();
        playerLogic.AttachSoundManager(soundManager);
    }
Beispiel #6
0
    private void FixedUpdate()
    {
        if (Time.time > 5f)
        {
            _unityBody.Rigidbody.isKinematic = true;
            _unityBody.enabled = false;
            return;
        }

        PlayerSimulation.Simulate(_simConfig, _unityBody, GetInput(Time.fixedTime));
    }
Beispiel #7
0
    private void Correct(IClock fixedClock)
    {
        if (_serverHistory.Count < 1)
        {
            Debug.Log("no server history available");
            return;
        }

        /* Extrapolate last received server state to catch up to client time */

        /* Todo: Save massive amounts of calculation by only simulating 1 correction tick per actual physics frame, you dummy
         * New server state? Do offline sim for however many steps it requires to catch up to local client time instantly.
         * THEN, then you only have to simulate one tick for each real-time physics tick in order to keep up. Until the new server state arrives.
         *
         * So in this method we only tick once
         * In the receive state handler we tick multiple times to catch up to local client time
         */

        var latency = _lastReceivedStateRTT / 2d;
        var timeSinceLastReceivedState = fixedClock.CurrentTime - _lastReceivedStateTimestamp;
        var timeSinceServerState       = latency + timeSinceLastReceivedState;
        var startTime = _lastReceivedStateTimestamp - latency;

        Debug.Assert(timeSinceServerState > 0.0, "timeSinceServerState > 0: " + timeSinceServerState);

        _eulerBody.State = _lastServerState;
        PlayerSimulation.SimulateOffline(_simulation.Config, _eulerBody, t => _inputHistory.Lerp(t), startTime, timeSinceServerState, fixedClock.DeltaTime);
        _correctedState = _eulerBody.State;

        /* Calculate error between corrected server state and current client state */

        var currentClientState = _simulation.UnityBody.State;

        const float maxPosError = 2f;
        const float maxRotError = 45f;

        _posError = Vector3.Distance(currentClientState.Position, _correctedState.Position) / maxPosError;
        _rotError = Quaternion.Angle(currentClientState.Rotation, _correctedState.Rotation) / maxRotError;

        /* Apply corrected state over time, with use of error metrics */

        _posErrorSmooth = Mathf.Lerp(_posErrorSmooth, 0.5f + _posError, 1f / _errorSmoothing * fixedClock.DeltaTime);
        _rotErrorSmooth = Mathf.Lerp(_rotErrorSmooth, 0.5f + _rotError, 1f / _errorSmoothing * fixedClock.DeltaTime);

        var interpolatedState = RigidbodyExtensions.Lerp(
            currentClientState, _correctedState,
            _posErrorSmooth * fixedClock.DeltaTime * _errorCorrectionSpeed,
            _rotErrorSmooth * fixedClock.DeltaTime * _errorCorrectionSpeed);

        if (_applyCorrection)
        {
            interpolatedState.ApplyTo(_simulation.UnityBody.Rigidbody);
        }
    }
        public Task Initialize(MatchConfig config)
        {
            Assert.IsTrue(config.IsValid);
            PlayerSimulations = new PlayerSimulation[config.PlayerCount];
            var tasks = new List <Task>();

            for (int i = 0; i < PlayerSimulations.Length; i++)
            {
                PlayerSimulations[i] = new PlayerSimulation();
                tasks.Add(PlayerSimulations[i].Initialize(config.PlayerConfigs[i]));
            }
            return(Task.WhenAll(tasks));
        }
Beispiel #9
0
        public Task Initialize(MatchConfig config)
        {
            Assert.IsTrue(config.IsValid);
            PlayerSimulations = new PlayerSimulation[config.PlayerCount];
            var tasks = new List <Task>();

            for (int i = 0; i < PlayerSimulations.Length; i++)
            {
                PlayerSimulations[i] = new PlayerSimulation();
                tasks.Add(PlayerSimulations[i].Initialize(config.PlayerConfigs[i]));
            }

            context = Mediator.Global.CreateContext();
            context.Subscribe <PlayerResetEvent>(ResetPlayer);

            return(Task.WhenAll(tasks));
        }
Beispiel #10
0
 public PlayerLogic(PlayerData playerData, PlayerSimulation playerSimulation, Transform groundCheck)
 {
     this.playerData       = playerData;
     this.playerSimulation = playerSimulation;
     this.groundcheck      = groundCheck;
 }
Beispiel #11
0
 private void FixedUpdate()
 {
     //_body.AddTorque(new Vector3(10f * Input.GetAxis("Roll"), 0f, 0f), ForceMode.Force);
     PlayerSimulation.Simulate(_config, _body, SampleInput());
 }
Beispiel #12
0
 void Start()
 {
     _playerSimulation = new PlayerSimulation(_movementData);
 }
Beispiel #13
0
 private void Start()
 {
     playerSimulation = new PlayerSimulation();
     animator         = GetComponent <Animator>();
 }