private void Update() { gameStateUpdateTimer += Time.deltaTime; gameResearchHashUpdateTimer += Time.deltaTime; productionStatisticsUpdateTimer += Time.deltaTime; if (gameStateUpdateTimer > GAME_STATE_UPDATE_INTERVAL) { gameStateUpdateTimer = 0; SendPacket(new GameStateUpdate() { State = new GameState(TimeUtils.CurrentUnixTimestampMilliseconds(), GameMain.gameTick) }); } if (gameResearchHashUpdateTimer > GAME_RESEARCH_UPDATE_INTERVAL) { gameResearchHashUpdateTimer = 0; if (GameMain.data.history.currentTech != 0) { TechState state = GameMain.data.history.techStates[GameMain.data.history.currentTech]; SendPacket(new GameHistoryResearchUpdatePacket(GameMain.data.history.currentTech, state.hashUploaded)); } } if (productionStatisticsUpdateTimer > STATISTICS_UPDATE_INTERVAL) { productionStatisticsUpdateTimer = 0; StatisticsManager.SendBroadcastIfNeeded(); } PacketProcessor.ProcessPacketQueue(); }
private void Update() { // Wait for the entire buffer to be full before starting to interpolate the player position if (snapshotBuffer[0].Timestamp == 0) { return; } double past = (1000 / (double)LocalPlayerMovement.SEND_RATE) * (snapshotBuffer.Length - 1); double now = TimeUtils.CurrentUnixTimestampMilliseconds(); double renderTime = now - past; for (int i = 0; i < snapshotBuffer.Length - 1; ++i) { var t1 = snapshotBuffer[i].Timestamp; var t2 = snapshotBuffer[i + 1].Timestamp; if (renderTime <= t2 && renderTime >= t1) { var total = t2 - t1; var reminder = renderTime - t1; var ratio = total > 0 ? reminder / total : 1; // We interpolate to the appropriate position between our 2 known snapshot MoveInterpolated(snapshotBuffer[i], snapshotBuffer[i + 1], (float)ratio); break; } else if (i == snapshotBuffer.Length - 2 && renderTime > t2) { // This will skip interpolation and will snap to the most recent position. MoveInterpolated(snapshotBuffer[i], snapshotBuffer[i + 1], 1); } } }
public void ProcessPacket(GameStateUpdate packet, NebulaConnection conn) { GameState state = packet.State; // We offset the tick received to account for the time it took to receive the packet long timeOffset = TimeUtils.CurrentUnixTimestampMilliseconds() - packet.State.timestamp; long tickOffsetSinceSent = (long)System.Math.Round(timeOffset / (GameMain.tickDeltaTime * 1000)); state.gameTick += tickOffsetSinceSent; SimulatedWorld.UpdateGameState(state); }
private void Update() { gameStateUpdateTimer += Time.deltaTime; if (gameStateUpdateTimer > 1) { SendPacket(new GameStateUpdate() { State = new GameState(TimeUtils.CurrentUnixTimestampMilliseconds(), GameMain.gameTick) }); } PacketProcessor.ProcessPacketQueue(); }
public void UpdatePosition(PlayerMovement movement) { if (!rootTransform) { return; } for (int i = 0; i < snapshotBuffer.Length - 1; ++i) { snapshotBuffer[i] = snapshotBuffer[i + 1]; } snapshotBuffer[snapshotBuffer.Length - 1] = new Snapshot() { Timestamp = TimeUtils.CurrentUnixTimestampMilliseconds(), Position = movement.Position.ToUnity(), Rotation = Quaternion.Euler(movement.Rotation.ToUnity()), BodyRotation = Quaternion.Euler(movement.BodyRotation.ToUnity()), }; }