Ejemplo n.º 1
0
        /// <summary>
        /// Setup the game UI.
        /// </summary>
        public override void Add(ScreenManager screenManager)
        {
            base.Add(screenManager);

            // Status bar.
            sbStats = new StatusBar(Manager);
            sbStats.Init();
            sbStats.Bottom = Manager.ScreenHeight;
            sbStats.Left   = 0;
            sbStats.Width  = Manager.ScreenWidth;

            lblStats = new Label(Manager)
            {
                Top = 4, Left = 8, Width = Manager.ScreenWidth - 16
            };
            lblStats.Init();

            sbStats.Add(lblStats);
            Window.Add(sbStats);

            // Inventory.
            pnlInventory      = new InventoryControl(this, Manager);
            pnlInventory.Left = Manager.TargetWidth / 2 - (pnlInventory.Width / 2);
            pnlInventory.Init();
            Window.Add(pnlInventory);

            // Chat.
            txtChat = new TextBox(Manager);
            txtChat.Init();
            txtChat.Left = 8;
            txtChat.DrawFormattedText = false;
            txtChat.Bottom            = sbStats.Top - 8;
            txtChat.Width             = (int)(Manager.TargetWidth * .4f) - 16; // Remove 16 to align due to invisible scrollbar
            txtChat.Visible           = false;
            txtChat.Passive           = true;
            Window.Add(txtChat);

            lstChats = new ControlList <ChatDataControl>(Manager)
            {
                Left   = txtChat.Left,
                Width  = txtChat.Width + 16,
                Height = (int)(Manager.TargetHeight * .25f)
            };
            lstChats.Init();
            lstChats.Color          = Color.Transparent;
            lstChats.HideSelection  = true;
            lstChats.Passive        = true;
            lstChats.HideScrollbars = true;
            lstChats.Top            = txtChat.Top - lstChats.Height;
            Window.Add(lstChats);

            // Tablist.
            lstPlayers = new ControlList <PlayerListDataControl>(Manager)
            {
                Width = 256,
                Top   = 256
            };
            lstPlayers.Init();
            lstPlayers.HideSelection  = true;
            lstPlayers.Left           = Manager.TargetWidth / 2 - (lstPlayers.Width / 2);
            lstPlayers.Passive        = true;
            lstPlayers.HideScrollbars = true;
            lstPlayers.Visible        = false;
            Window.Add(lstPlayers);

            foreach (var player in Level.Players)
            {
                lstPlayers.Items.Add(new PlayerListDataControl(player, Manager, lstPlayers));
            }


            // Listen for later player joins.
            Client.Events.Network.Game.PlayerJoinReceived.AddHandler(
                args => { lstPlayers.Items.Add(new PlayerListDataControl(args.Player, Manager, lstPlayers)); });

            // Listen for ping updates for players.
            Client.Events.Network.Game.PingUpdateReceived.AddHandler(args =>
            {
                foreach (var ping in args.Pings)
                {
                    var control =
                        (PlayerListDataControl)
                        lstPlayers.Items.FirstOrDefault(i => ((PlayerListDataControl)i).User.UUID == ping.Key);
                    control?.ChangePing(ping.Value);
                }
            });


            // Hackish way to get chats to start at the bottom.
            for (var i = 0; i < (Manager.TargetHeight * 0.25f) / 18; i++)
            {
                lstChats.Items.Add(new ChatDataControl("", Manager, lstChats, this));
            }

            Client.Events.Network.Game.ChatReceived.AddHandler(args => { AddChat(args.Message, Manager, lstChats); });

            // Level event handlers.
            Client.Events.Game.Level.BlockPlaced.AddHandler(args =>
            {
                // Directly access the tile array, as we don't want to send two BlockPlaced events, as the tile indexer will
                // automatically call the event and send a network message.
                if (args.Level != null)
                {
                    args.Level.Tiles.Tiles[args.X, args.Y, args.Z] = new Tile(args.Type);
                }
            });
        }