Beispiel #1
0
 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);
 }
Beispiel #2
0
        protected override void Update(GameTime Time)
        {
            Mouse.Update();
            Keyboard.Update(Time);
            XboxPad.Update(Time);
            Timers.Update(Time);
            Performance.Update(Time);

            Globe.Active = IsActive;
            if (XboxPad.Pressed(XboxPad.Buttons.Back) || Keyboard.Pressed(Keyboard.Keys.Escape))
            {
                Exit();
            }

            Profiler.Start("Game Update");
            switch (State)
            {
                #region MainMenu

            case States.MainMenu:
                if (Globe.Active)
                {
                    if (Keyboard.Pressed(Keyboard.Keys.F1))
                    {
                        CreateLobby("Server");
                        State = States.Game;
                    }
                    else if (Keyboard.Pressed(Keyboard.Keys.F2))
                    {
                        if (Keyboard.Holding(Keyboard.Keys.LeftShift) || Keyboard.Holding(Keyboard.Keys.RightShift))
                        {
                            MultiPlayer.Connect("Game", "127.0.0.1", 6121, Globe.Version, MpName);
                        }
                        else
                        {
                            MultiPlayer.Connect("Game", "71.3.34.68", 6121, Globe.Version, MpName);
                        }
                    }
                    else if (Keyboard.Pressed(Keyboard.Keys.F3))
                    {
                        CreateLobby("Server");
                        Camera.Position = new Vector2((Map.Width / 2f), (Map.Height / 2f));
                        State           = States.MapEditor;
                    }
                }
                break;

                #endregion

            case States.RequestMap: if (MultiPlayer.Type() == MultiPlayer.Types.Client)
                {
                    MultiPlayer.Send(MultiPlayer.Construct(Packets.RequestMap)); State = States.SyncingMap;
                }
                break;

                #region MapEditor
            case States.MapEditor:
                Map.Update(Time);
                Point MousePoint = new Point((int)(Mouse.CameraPosition.X / Tile.Width), (int)(Mouse.CameraPosition.Y / Tile.Height));
                if (Globe.Active)
                {
                    #region Camera Movement
                    if (Keyboard.Holding(Keyboard.Keys.W))
                    {
                        Camera.Position.Y -= (float)(Map.Speed.Y * Time.ElapsedGameTime.TotalSeconds);
                    }
                    if (Keyboard.Holding(Keyboard.Keys.S))
                    {
                        Camera.Position.Y += (float)(Map.Speed.Y * Time.ElapsedGameTime.TotalSeconds);
                    }
                    if (Keyboard.Holding(Keyboard.Keys.A))
                    {
                        Camera.Position.X -= (float)(Map.Speed.X * Time.ElapsedGameTime.TotalSeconds);
                    }
                    if (Keyboard.Holding(Keyboard.Keys.D))
                    {
                        Camera.Position.X += (float)(Map.Speed.X * Time.ElapsedGameTime.TotalSeconds);
                    }
                    #endregion
                    bool BackPlace = (Keyboard.Holding(Keyboard.Keys.LeftShift) || Keyboard.Holding(Keyboard.Keys.RightShift));
                    if (Mouse.ScrolledUp())
                    {
                        if (!BackPlace)
                        {
                            if (EditorForeTile > 1)
                            {
                                EditorForeTile--;
                            }
                        }
                        else
                        {
                            if (EditorBackTile > 1)
                            {
                                EditorBackTile--;
                            }
                        }
                    }
                    if (Mouse.ScrolledDown())
                    {
                        if (!BackPlace)
                        {
                            if (EditorForeTile < Mod.Fore.Values.Count)
                            {
                                EditorForeTile++;
                            }
                        }
                        else
                        {
                            if (EditorBackTile < Mod.Back.Values.Count)
                            {
                                EditorBackTile++;
                            }
                        }
                    }
                    if (Mouse.Holding(Mouse.Buttons.Left))
                    {
                        if (!BackPlace)
                        {
                            Map.PlaceFore(EditorForeTile, MousePoint.X, MousePoint.Y, null, true);
                        }
                        else
                        {
                            Map.PlaceBack(EditorBackTile, MousePoint.X, MousePoint.Y, true);
                        }
                    }
                    if (Mouse.Holding(Mouse.Buttons.Right))
                    {
                        if (!BackPlace)
                        {
                            Map.ClearFore(MousePoint.X, MousePoint.Y, true);
                        }
                        else
                        {
                            Map.ClearBack(MousePoint.X, MousePoint.Y, true);
                        }
                    }
                }
                break;
                #endregion

                #region Game

            case States.Game:
                Map.Update(Time);
                for (byte i = 0; i < Players.Length; i++)
                {
                    if (Players[i] != null)
                    {
                        Players[i].Update(Time);
                    }
                }
                if (MultiPlayer.Type("Game") == MultiPlayer.Types.Server)
                {
                    if (Timers.Tick("Positions"))
                    {
                        foreach (var Player1 in Players)
                        {
                            if (Player1 != null && (Player1.Connection != null))
                            {
                                var O = MultiPlayer.Construct("Game", Packets.Position);
                                foreach (var Player2 in Players)
                                {
                                    if ((Player2 != Player1) && (Player2 != null) && !Player2.Dead)
                                    {
                                        O.Write(Player2.Slot);
                                        O.Write(Player2.Position);
                                        O.Write(Player2.Angle);
                                    }
                                }
                                MultiPlayer.SendTo("Game", O, Player1.Connection, NetDeliveryMethod.UnreliableSequenced, 1);
                            }
                        }
                    }
                    if (GameType == GameTypes.TeamStandard)
                    {
                        if (!RoundEnded)
                        {
                            if (RoundEndWait > 0)
                            {
                                RoundEndWait -= Time.ElapsedGameTime.TotalSeconds;
                            }
                            else if ((TeamCount(1) > 0) && (TeamCount(2) > 0))
                            {
                                if ((DeadCount(1) == TeamCount(1)) && (DeadCount(2) == TeamCount(2)))
                                {
                                    EndRound(VictoryStates.Draw, 0, 0);
                                }
                                else if (DeadCount(1) == TeamCount(1))
                                {
                                    EndRound(VictoryStates.Team, 2, 0);
                                }
                                else if (DeadCount(2) == TeamCount(2))
                                {
                                    EndRound(VictoryStates.Team, 1, 0);
                                }
                            }
                        }
                        else
                        {
                            if (RoundTimer > 0)
                            {
                                RoundTimer -= Time.ElapsedGameTime.TotalSeconds;
                            }
                            else
                            {
                                NewRound();
                            }
                        }
                    }
                }
                break;

                #endregion
            }
            Profiler.Stop("Game Update");

            #region Networking

            MultiPlayer.Flush("Game");
            NetIncomingMessage I;
            while ((I = MultiPlayer.Read("Game")) != null)
            {
                var Break = false;
                switch (I.MessageType)
                {
                case NetIncomingMessageType.ConnectionApproval:
                    Read(Packets.Connection, I);
                    break;

                case NetIncomingMessageType.Data:
                    Read((Packets)I.ReadByte(), I);
                    break;

                case NetIncomingMessageType.StatusChanged:
                    var Status = ((MultiPlayer.Type("Game") == MultiPlayer.Types.Client)
                            ? (NetConnectionStatus)I.ReadByte()
                            : ((MultiPlayer.Type("Game") == MultiPlayer.Types.Server)
                                ? I.SenderConnection.Status
                                : NetConnectionStatus.None));
                    switch (Status)
                    {
                    case NetConnectionStatus.Connected:
                        if (MultiPlayer.Type("Game") == MultiPlayer.Types.Client)
                        {
                            var Hail = I.SenderConnection.RemoteHailMessage;
                            Read((Packets)Hail.ReadByte(), Hail);
                        }
                        break;

                    case NetConnectionStatus.Disconnected:
                        if ((MultiPlayer.Type("Game") == MultiPlayer.Types.Client) &&
                            (I.SenderConnection.Status == NetConnectionStatus.Disconnected))
                        {
                            QuitLobby();
                            Break = true;
                        }
                        else
                        {
                            Read(Packets.Disconnection, I);
                        }
                        break;
                    }
                    break;
                }
                if (Break)
                {
                    break;
                }
            }

            #endregion

            base.Update(Time);
        }