Exemplo n.º 1
0
        void AddShopPlayer(ulong userId)
        {
            if (!players.ContainsKey(userId))
            {
                players[userId] = new ShopPlayer()
                {
                    Points = 15, TotalOnlineSeconds = 0
                }
            }
            ;
        }

        #endregion

        #region UI

        void CreateItemsUI()
        {
            float gap       = 0.014f;
            float width     = 0.15f;
            float height    = 0.17f;
            float startxBox = gap;
            float startyBox = 1f - height - 0.05f;

            float xmin = startxBox;
            float ymin = startyBox;

            itemsUI = new CuiElementContainer();
            int i          = 0;
            var mainParent = itemsUI.Add(new CuiPanel()
            {
                Image = { Color = "0 0 0 0" }
            }, "rustyshop.items",
                                         "rustyshop.items.customui");

            foreach (var item in items)
            {
                var min       = $"{xmin} {ymin}";
                var max       = $"{xmin + width} {ymin + height}";
                var panelname = itemsUI.Add(new CuiPanel()
                {
                    Image         = { Color = "0 0 0 0" },
                    RectTransform = { AnchorMin = min, AnchorMax = max }
                }, mainParent);


                itemsUI.Add(new CuiButton()
                {
                    Button        = { Command = $"rustyshop.buy {i}", Color = "0.3 0.5 0.4 1" },
                    RectTransform = { AnchorMin = "0 0", AnchorMax = "1 0.25" },
                    Text          =
                    {
                        Text     =
                            $"<color=orange><color=#00ccff>{item.Amount}</color> за <color=#ffcc00>{item.Cost}</color> р.</color>",
                        FontSize = 16,
                        Align    = TextAnchor.MiddleCenter
                    }
                }, panelname);

                itemsUI.Add(new CuiElement()
                {
                    Name       = CuiHelper.GetGuid(),
                    Parent     = panelname,
                    Components =
                    {
                        new CuiRawImageComponent()
                        {
                            Png    = ImageStorage.FetchPng(item.DisplayName),
                            Sprite = "assets/content/textures/generic/fulltransparent.tga"
                        },
                        new CuiRectTransformComponent()
                        {
                            AnchorMin = "0 0.25", AnchorMax = "1 1"
                        }
                    }
                });

                xmin += width + gap;
                if (xmin + width >= 1)
                {
                    xmin  = startxBox;
                    ymin -= height + gap;
                }
                i++;
            }
        }

        void DrawUI(BasePlayer player)
        {
            var shopPlayer = players[player.userID];

            foreach (var we in itemsUI)
            {
                CuiHelper.DestroyUi(player, we.Name);
            }
            core.DrawUIWithEx(player, "RustyShop", "menu", itemsUI, shopPlayer.Points,
                              core.TimeToString(shopPlayer.TotalOnlineSeconds));
        }

        void DestroyUI(BasePlayer player)
        {
            core.DestroyUI(player, "RustyShop", "menu");
        }

        IEnumerator LoadImages()
        {
            foreach (var item in items)
            {
                yield return(CommunityEntity.ServerInstance.StartCoroutine(ImageStorage.Store(item.DisplayName, item.URL)));
            }
            CreateItemsUI();
        }