Пример #1
0
        public void DrawInventory(SpriteBatch sb, List <InventoryItem> itemsPlayer, ICraftingObject cftObj)
        {
            Vector2 itemDrawLoc = itemDrawLocStart;

            menuOpen = true;
            craftObj = cftObj;

            List <InventoryItem> items = itemsPlayer;

            // draw item stat panel
            Texture2D textureItemStat = new Texture2D(_graphics, 140, 50);

            Color[] csdata = new Color[50 * 140];
            for (int j = 0; j < csdata.Length; ++j)
            {
                csdata[j] = Color.Gray;
            }
            textureItemStat.SetData(csdata);
            Vector2 itemStatLoc = new Vector2(itemDrawLoc.X, itemDrawLoc.Y - 80);

            itemMenuButtonLocations["itemStat"] = new Rectangle((int)itemStatLoc.X, (int)itemStatLoc.Y, textureItemStat.Width, textureItemStat.Height);
            sb.Begin();
            sb.Draw(textureItemStat, itemStatLoc, Color.Gray);
            sb.End();


            craftableItemsChecked = SearchCraftingRecipes(itemsPlayer, craftObj.GetCraftSet());

            int textureHW = 64;

            // draw slots
            for (int i = 0; i < craftableItemsChecked.Count; i++)
            {
                // draw slots
                Texture2D textureSlot = new Texture2D(_graphics, textureHW, textureHW);
                Color[]   data        = new Color[64 * 64];
                Color     color       = Color.DarkGray;
                if (i == selectedIndex)
                {
                    color = Color.Crimson;
                }
                for (int j = 0; j < data.Length; ++j)
                {
                    data[j] = color;
                }
                textureSlot.SetData(data);

                Rectangle slotLoc = new Rectangle((int)itemDrawLoc.X, (int)itemDrawLoc.Y, textureHW, textureHW);
                slotLocations[i] = slotLoc;
                sb.Begin();
                sb.Draw(textureSlot, itemDrawLoc, Color.DarkGray);
                sb.End();

                itemDrawLoc.X += 70;
                if (itemDrawLoc.X > location.X + GetWidth() / 2 - 50)
                {
                    itemDrawLoc.X  = itemDrawLocStart.X;
                    itemDrawLoc.Y += 70;
                }
            }

            // draw items
            itemDrawLoc = itemDrawLocStart;
            for (int i = 0; i < craftableItemsChecked.Count; i++)
            {
                var     item = craftableItemsChecked[i];
                Vector2 offsetLocation;
                Vector2 itemLoc = itemDrawLoc;
                // track sprite scale
                if (!saveItemSpriteScale.ContainsKey(item))
                {
                    saveItemSpriteScale[item] = item.spriteScale;
                }

                if (item is IHandHeld) // handhelds display action frames so scaling them will make them too tiny and offset
                {
                    item.spriteScale  = 1.3f;
                    offsetLocation    = new Vector2(itemLoc.X + textureHW / 3, itemLoc.Y + textureHW / 3);
                    item.currRowFrame = 0;
                }
                else
                {
                    item.spriteScale = (float)(itemDisplaySizePix / item.targetRectangle.Width);
                    offsetLocation   = new Vector2(itemLoc.X + textureHW / 2, itemLoc.Y + textureHW / 1.7f); // move Y down a little to leave room for stack number display
                }

                item.location = offsetLocation;

                item.currColumnFrame = 0;
                item.currRowFrame    = 0;
                item.Draw(sb, null);

                sb.Begin();
                //name display
                if (i == selectedIndex)
                {
                    sb.DrawString(font, item.itemKey, itemStatLoc, Color.Black);
                }
                sb.End();

                itemDrawLoc.X += 70;
                if (itemDrawLoc.X > location.X + GetWidth() / 2 - 50)
                {
                    itemDrawLoc.X  = itemDrawLocStart.X;
                    itemDrawLoc.Y += 70;
                }
            }

            // draw cursor
            sb.Begin();
            sb.Draw(cursor, cursorPos, Color.White);
            sb.End();

            // reset itemDrawLoc
            itemDrawLoc = itemDrawLocStart;
        }
Пример #2
0
        public void Update(KeyboardState kstate, GameTime gameTime, Camera cam)
        {
            if (menuOpen)
            {
                timeRClicked += gameTime.ElapsedGameTime.Milliseconds;
                timeLClicked += gameTime.ElapsedGameTime.Milliseconds;

                cursorPos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
                Rectangle cursorRect = new Rectangle((int)cursorPos.X, (int)cursorPos.Y, cursor.Width, cursor.Height);

                selectedIndex = -1;
                int i = 0;
                foreach (var slot in slotLocations.Values)
                {
                    if (slot.Intersects(cursorRect))
                    {
                        selectedIndex = i;
                        // TODO: have this start timer for crafting (do some sort of animation) and then create item when timer is done. OR have a sound effect
                        if (Mouse.GetState().LeftButton == ButtonState.Pressed && !(timeLClicked < 400)) // click timer
                        {
                            bool canCreateItem = false;
                            if (craftableItemsChecked.Count > 0)
                            {
                                var item = craftableItemsChecked[selectedIndex]; // get item from menu icons

                                if (emptySpotAvailable || (item.stackable && playerInvCanStackItem[item.bbKey]))
                                {
                                    canCreateItem = true;
                                }

                                else
                                {
                                    // check to see if the ingredients used will free up a spot
                                    foreach (KeyValuePair <string, int> ingredient in ingredientsAmountDifferences[item.bbKey])
                                    {
                                        if (ingredient.Value <= 0)
                                        {
                                            canCreateItem = true;
                                            break;
                                        }
                                    }
                                }

                                InventoryItem itemCreated = null;
                                if (canCreateItem)
                                {
                                    // remove ingredients from player inv
                                    foreach (var itm in inventoryOfPlayer.inventory)
                                    {
                                        if (itm == null)
                                        {
                                            continue;
                                        }
                                        foreach (var ing in Mappings.ItemMappings.CraftingRecipes[craftObj.GetCraftSet()][item.bbKey])
                                        {
                                            if (itm.bbKey.Equals(ing.Key))
                                            {
                                                itm.amountStacked -= ing.Value;
                                            }
                                        }
                                    }

                                    // create inv item and add to players inv
                                    itemCreated = ItemUtility.CreateInventoryItem(item.bbKey, inventoryOfPlayer.teamType, inventoryOfPlayer.regionKey, item.location, _content, _graphics);
                                    craftObj.GetCraftingQueue().Enqueue(itemCreated);


                                    /*if (inventoryOfPlayer.AddInventoryItem(itemCreated))
                                     * {
                                     *  itemCreated.inInventory = true;
                                     *  itemCreated.onGround = false;
                                     * }*/

                                    timeLClicked = 0;
                                }
                            }
                        }
                    }
                    i++;
                }
            }
            menuOpen = false;
        }