private void OnUpdateSelf(ref UpdateSelf p) { Player player; if (players.TryGetValue(p.SenderConnection, out player)) { if (player.Index != p.Index) { throw new InvalidOperationException(); } if (player.LastUpdateTime > p.UpdateTime) { return; } player.LastUpdateTime = p.UpdateTime; player.Pos = p.Pos; player.AnimState = p.AnimState; player.AnimTime = p.AnimTime; player.IsFacingLeft = p.IsFacingLeft; player.Controllable = p.Controllable; player.IsFirePressed = p.IsFirePressed; } }
protected override void OnFixedUpdate(float timeMult) { base.OnFixedUpdate(timeMult); if (isStillLoading > 0) { isStillLoading--; if (isStillLoading <= 0) { net.Send(new LevelReady { Index = localPlayerIndex }, 2, NetDeliveryMethod.ReliableUnordered, PacketChannels.Main); } } #if DEBUG Hud.ShowDebugText("- Local Player Index: " + localPlayerIndex); Hud.ShowDebugText("- RTT: " + (int)(net.AverageRoundtripTime * 1000) + " ms / Up: " + net.Up + " / Down: " + net.Down); Hud.ShowDebugText("- Last Server Update: " + lastServerUpdateTime); Hud.ShowDebugText("- Remote Objects: " + localRemotableActors.Count + " / " + remoteActors.Count); #endif if (players.Count > 0) { lastUpdate += timeMult; if (lastUpdate < 1.4f) { return; } lastUpdate = 0f; long updateTime = (long)(NetTime.Now * 1000); UpdateSelf updateSelfPacket = players[0].CreateUpdatePacket(); updateSelfPacket.Index = localPlayerIndex; updateSelfPacket.UpdateTime = updateTime; net.Send(updateSelfPacket, 29, NetDeliveryMethod.Unreliable, PacketChannels.Main); foreach (KeyValuePair <int, IRemotableActor> pair in localRemotableActors) { UpdateRemotableActor p = new UpdateRemotableActor(); pair.Value.OnUpdateRemotableActor(ref p); p.Index = pair.Key; p.UpdateTime = updateTime; net.Send(p, 32, NetDeliveryMethod.Unreliable, PacketChannels.Main); } } }
private static void OnUpdateSelf(ref UpdateSelf p) { Player player; if (players.TryGetValue(p.SenderConnection, out player)) { if (player.Index != p.Index) { throw new InvalidOperationException(); } Vector3 pos = p.Pos; pos.X += p.Speed.X * p.SenderConnection.AverageRoundtripTime * 0.5f; pos.Y += p.Speed.Y * p.SenderConnection.AverageRoundtripTime * 0.5f; player.Pos = pos; player.Speed = p.Speed; player.AnimState = p.AnimState; player.AnimTime = p.AnimTime; player.IsFacingLeft = p.IsFacingLeft; } }