protected override void OnExiting(object sender, EventArgs args) { Multiplayer.QuitLobby(); base.OnExiting(sender, args); }
protected override void Update(GameTime time) { Performance.UpdateFPS.Record(1 / time.ElapsedGameTime.TotalSeconds); Timers.Update(time); Globe.IsActive = IsActive; Mouse.Update(); Keyboard.Update(time); XboxPad.Update(time); if (XboxPad.Pressed(XboxPad.Buttons.Back) || Keyboard.Pressed(Keyboard.Keys.Escape) || Quit) { Exit(); } if (Keyboard.Pressed(Keyboard.Keys.F3)) { Profiler.Enabled = !Profiler.Enabled; } Profiler.Start("Game Update"); #region Menu/Connecting if (Frame == Frames.Menu) { UpdateMenuWorld(time); if (MenuState == MenuStates.UsernameEntry) { if (IsActive) { BlinkTimer -= time.ElapsedGameTime.TotalSeconds; if (BlinkTimer <= 0) { BlinkTimer += .6; } var name = Settings.Get("Name").AcceptInput(String.InputFlags.NoLeadingSpaces | String.InputFlags.NoRepeatingSpaces, 20); Settings.Set("Name", name); if (Keyboard.Pressed(Keyboard.Keys.Enter) && !name.IsNullOrEmpty()) { MenuState = MenuStates.HostConnect; } } } else if (MenuState == MenuStates.HostConnect) { if (Mouse.Press(Mouse.Buttons.Left)) { Vector2 scale = Scale * .75f, size = _orbisFont.MeasureString("Host") * scale; var button = new Rectangle((int)(Screen.BackBufferWidth / 2f - size.X / 2f), (int)(Screen.BackBufferHeight / 2f - size.Y), (int)size.X, (int)size.Y); if (new Rectangle(Mouse.X, Mouse.Y, 1, 1).Intersects(button)) { Multiplayer.CreateLobby(Settings.Get("Name")); GenStarted = false; Frame = Frames.LoadGame; } scale = Scale * .75f; size = _orbisFont.MeasureString("Connect") * scale; button = new Rectangle((int)(Screen.BackBufferWidth / 2f - size.X / 2f), (int)(Screen.BackBufferHeight / 2f + size.Y * .25f), (int)size.X, (int)size.Y); if (new Rectangle(Mouse.X, Mouse.Y, 1, 1).Intersects(button)) { MenuState = MenuStates.IPEntry; } } } else if (MenuState == MenuStates.IPEntry) { if (IsActive) { BlinkTimer -= time.ElapsedGameTime.TotalSeconds; if (BlinkTimer <= 0) { BlinkTimer += .6; } var ip = Settings.Get("IP").AcceptInput( String.InputFlags.NoLeadingPeriods | String.InputFlags.NoLetters | String.InputFlags.NoSpecalCharacters | String.InputFlags.NoSpaces | String.InputFlags.AllowPeriods | String.InputFlags.NoRepeatingPeriods | String.InputFlags.AllowColons | String.InputFlags.NoRepeatingColons | String.InputFlags.NoLeadingPeriods, 21); Settings.Set("IP", ip); if (Keyboard.Pressed(Keyboard.Keys.Enter) && !ip.IsNullOrEmpty()) { Network.Connect(Settings.Get("IP").Split(':')[0], Settings.Get("IP").Contains(":") ? Convert.ToInt32(Settings.Get("IP").Split(':')[1]) : Multiplayer.Port, new Network.Packet(null, Settings.Get("Name"))); Frame = Frames.Connecting; } else if (Keyboard.Pressed(Keyboard.Keys.Tab)) { MenuState = MenuStates.HostConnect; } } } Network.Update(); } else if (Frame == Frames.Connecting) { UpdateMenuWorld(time); BlinkTimer -= time.ElapsedGameTime.TotalSeconds; if (BlinkTimer <= 0) { BlinkTimer += 1; } Network.Update(); } #endregion #region LoadGame/Game else if (Frame == Frames.LoadGame) { UpdateMenuWorld(time); BlinkTimer -= time.ElapsedGameTime.TotalSeconds; if (BlinkTimer <= 0) { BlinkTimer += 1; } if (Network.IsNullOrServer) { if (!GenStarted) { LoadingText = null; GenDone = false; var thread = new Thread(() => { GameWorld = World.Generate(8400, 2400); }) { IsBackground = true }; thread.Start(); GenStarted = true; } if (GenDone) { BufferedStrings = new OrderedDictionary(); if (MenuMusicChannel.HasValue) { Sound.Terminate(MenuMusicChannel.Value); MenuMusicChannel = null; } LoadItems(); Self.Spawn(GameWorld.Spawn); GameWorld.Position = Self.WorldPosition; Frame = Frames.Game; } } Network.Update(); } else if (Frame == Frames.Game) { MouseTileX = (int)Math.Floor(Mouse.CameraPosition.X / Tile.Size); MouseTileY = (int)Math.Floor(Mouse.CameraPosition.Y / Tile.Size); Self.SelfUpdate(time); foreach (var t in Players) { t?.Update(time); } GameWorld.Position = Self.WorldPosition; if (Settings.IsDebugMode) { if (Keyboard.Pressed(Keyboard.Keys.L)) { GameWorld.Light = !GameWorld.Light; } if (Mouse.ScrolledUp()) { GameWorld.Zoom = MathHelper.Min(8, (float)Math.Round(GameWorld.Zoom + ZoomRate, 2)); } if (Mouse.ScrolledDown()) { GameWorld.Zoom = MathHelper.Max(.5f, (float)Math.Round(GameWorld.Zoom - ZoomRate, 2)); } if (Keyboard.Pressed(Keyboard.Keys.D1)) { Self.AddItem(Items["Dirt"].Clone(Keyboard.HoldingShift() ? 30 : 3)); } if (Keyboard.Pressed(Keyboard.Keys.D2)) { Self.AddItem(Items["Stone"].Clone(Keyboard.HoldingShift() ? 30 : 3)); } } for (var i = (BufferedStrings.Count - 1); i >= 0; i--) { var bString = (BufferedStrings[i] as BufferedString); bString.CalculateRectangle(_orbisFont, new Vector2(Self.WorldPosition.X, (Self.WorldPosition.Y - BufferedString.PlayerYOffset))); if (i < (BufferedStrings.Count - 1)) { if (bString.Rectangle.Intersects((BufferedStrings[i + 1] as BufferedString).Rectangle)) { bString.Offset -= (20 * (float)time.ElapsedGameTime.TotalSeconds); } else { bString.Offset += (20 * (float)time.ElapsedGameTime.TotalSeconds); } } else { bString.Offset = MathHelper.Min(0, (bString.Offset + (20 * (float)time.ElapsedGameTime.TotalSeconds))); } bString.Life -= (float)time.ElapsedGameTime.TotalSeconds; if (bString.Life <= 0) { BufferedStrings.RemoveAt(i); i--; } } Network.Update(); } #endregion Profiler.Stop("Game Update"); Textures.Dispose(); Sound.AutoTerminate(); base.Update(time); }