Пример #1
0
        private void DrawHook(object sender, WorldState.DrawEventArgs args)
        {
            WorldState  world      = sender as WorldState;
            SpriteBatch batch      = args.Batch;
            Vector2     screenSize = args.ScreenSize;

            Texture2D bg = _state.Assets.Get <Texture2D>("ui/inventory.png");

            batch.Texture(new Vector2(0, 270 - bg.Height + (bg.Height + 20) * (float)_offsetY.Value), bg, Color.White);

            Font font = _state.Assets.Get <Font>("fonts/bitcell.ttf");

            if (_state is CombatState)
            {
                batch.Text(font,
                           16,
                           "Inventory",
                           new Vector2(16, 8 + (bg.Height + 20) * (float)_offsetY.Value),
                           Color.White);
            }
            else
            {
                int x = 16;

                batch.Text(font,
                           16,
                           "[" + InputMap.FindMapping(InputAction.Inventory).Key + "] Inventory",
                           new Vector2(x, 8 + (bg.Height + 20) * (float)_offsetY.Value),
                           Color.White);

                x += (int)font.Measure(16, "[" + InputMap.FindMapping(InputAction.Inventory).Key + "] Inventory").X + 6;

                // only the player uses the inventory for now
                batch.Text(font,
                           16,
                           "[" + InputMap.FindMapping(InputAction.Spells).Key + "] Spells",
                           new Vector2(x, 8 + (bg.Height + 20) * (float)_offsetY.Value),
                           Color.White * 0.6f);

                x += (int)font.Measure(16, "[" + InputMap.FindMapping(InputAction.Spells).Key + "] Spells").X + 6;

                batch.Text(font,
                           16,
                           "[" + InputMap.FindMapping(InputAction.Player).Key + "] " + _entity.Name,
                           new Vector2(x, 8 + (bg.Height + 20) * (float)_offsetY.Value),
                           Color.White * 0.6f);
            }

            Rectangle itemsListRectangle = new Rectangle(16, 31, 171, 222);
            Vector2   transformLT        = Vector2.Transform(itemsListRectangle.Location.ToVector2(), batch.Transform);
            Vector2   transformRB        = Vector2.Transform(new Vector2(itemsListRectangle.Right, itemsListRectangle.Bottom), batch.Transform);

            Vector3    scale, translation;
            Quaternion rot;

            batch.Transform.Decompose(out scale, out rot, out translation);
            batch.Transform = batch.Transform * Matrix.CreateTranslation(0, (int)-_scroll * scale.X, 0);

            batch.Scissor = new Rectangle(
                (int)transformLT.X,
                (int)(transformLT.Y + (bg.Height + 20) * (float)_offsetY.Value * scale.X),
                (int)(transformRB.X - transformLT.X),
                (int)(transformRB.Y - transformLT.Y));

            itemsListRectangle.Y      += 6;
            itemsListRectangle.Height -= 12;

            if (_entity.Inventory.OccupiedSlots == 0)
            {
                Vector2 measure = font.Measure(16, "No items in inventory");
                batch.Text(font, 16, "No items in inventory", itemsListRectangle.Center.ToVector2() - measure / 2 + new Vector2(0, (bg.Height + 20) * (float)_offsetY.Value), Color.White * 0.4f);
            }
            else
            {
                for (int i = 0; i < _entity.Inventory.OccupiedSlots; i++)
                {
                    Inventory.ItemSlotReference reference = _sortedList[i];
                    int   y        = itemsListRectangle.Y + i * 15 + (int)((bg.Height + 20) * _offsetY.Value);
                    bool  selected = _selectedSlot == i;
                    float alpha    = selected ? 0.7f + (float)Math.Sin(_state.Game.Time * 10) * 0.1f : 0.4f;

                    batch.Texture(
                        new Vector2(itemsListRectangle.X + 6, y),
                        _state.Assets.Get <Texture2D>(reference.Item.SmallIcon),
                        Color.White * alpha);

                    batch.Text(font,
                               16,
                               reference.Item.Name,
                               new Vector2(itemsListRectangle.X + 18, y - 8),
                               Color.White * alpha);

                    int statusX = itemsListRectangle.X + itemsListRectangle.Width - 6 - 8;

                    if (reference.Item.EquippableIn != InventoryEquipSlot.None)
                    {
                        bool drawn = false;

                        foreach (InventoryEquipSlot slot in Enum.GetValues(typeof(InventoryEquipSlot)))
                        {
                            if (slot != InventoryEquipSlot.None && _entity.Inventory[slot].Slot == reference.Slot)
                            {
                                batch.Texture(new Vector2(statusX, y), _state.Assets.Get <Texture2D>("icons/equipped.png"), Color.White * 0.5f);
                                drawn = true;
                                break;
                            }
                        }

                        if (!drawn)
                        {
                            batch.Texture(new Vector2(statusX, y), _state.Assets.Get <Texture2D>("icons/not_equipped.png"), Color.White * 0.5f);
                        }

                        statusX -= 10;
                    }

                    // unusable
                    if (_state is CombatState && !(reference.Item is CombatItem))
                    {
                        batch.Texture(new Vector2(statusX, y), _state.Assets.Get <Texture2D>("icons/unusable.png"), Color.White * 0.5f);
                        statusX -= 10;
                    }
                }
            }

            batch.Transform = batch.Transform * Matrix.CreateTranslation(0, (int)_scroll * scale.X, 0);
            batch.Scissor   = null;

            Rectangle infoRectangle = new Rectangle(itemsListRectangle.Right + 12, itemsListRectangle.Top - 6 + (int)((bg.Height + 20) * (float)_offsetY.Value), 261, 223);

            infoRectangle.Y += 40;

            if (_entity.Inventory.OccupiedSlots > 0 && _selectedSlot != -1)
            {
                Inventory.ItemSlotReference reference = _sortedList[_selectedSlot];

                batch.Texture(
                    new Vector2(infoRectangle.X + infoRectangle.Width / 2 - 16, infoRectangle.Y),
                    _state.Assets.Get <Texture2D>(reference.Item.Icon),
                    Color.White);

                Vector2 measure = font.Measure(16, reference.Item.Name);
                batch.Text(font, 16, reference.Item.Name, new Vector2(infoRectangle.X + infoRectangle.Width / 2 - measure.X / 2, infoRectangle.Y + 30), Color.White);

                int y = 44;
                foreach (string s in reference.Item.Description.Split('\n'))
                {
                    measure = font.Measure(16, s);
                    batch.Text(font, 16, s, new Vector2(infoRectangle.X + infoRectangle.Width / 2 - measure.X / 2, infoRectangle.Y + y), Color.White * 0.7f);

                    y += 12;
                }

                if (reference.Item is ArmorItem)
                {
                    Rectangle blockRectangle = new Rectangle(infoRectangle.X + infoRectangle.Width / 2 - 140 / 2, infoRectangle.Y + 90, 140, 16);

                    batch.Rectangle(blockRectangle, Color.White * 0.1f);
                    blockRectangle.Width = (int)(140 * ((float)(reference.Item as ArmorItem).ArmorValue / 100));
                    batch.Rectangle(blockRectangle, Color.White * 0.3f);
                    blockRectangle.Width = 140;

                    batch.Text(font, 16,
                               ((reference.Item as ArmorItem).Protection == ArmorItem.ProtectionType.Magical ? "Magical" : "Physical") +
                               " Armor: +" +
                               (reference.Item as ArmorItem).ArmorValue,
                               new Vector2(blockRectangle.X + 4, blockRectangle.Y - 4), Color.White);
                }

                if (reference.Item is WeaponItem)
                {
                    string text = "Damage: " +
                                  (reference.Item as WeaponItem).DamageRange.Item1 + "-" + (reference.Item as WeaponItem).DamageRange.Item2 +
                                  " (scales with " + ((reference.Item as WeaponItem).UsesAgility ? "agility" : "strength") + ")";
                    measure = font.Measure(16, text);

                    batch.Text(font, 16, text, new Vector2(infoRectangle.X + infoRectangle.Width / 2 - measure.X / 2, infoRectangle.Y + 90 - 8), Color.White);
                }

                int x = 0;
                batch.Text(font, 16, "Actions", new Vector2(infoRectangle.X + x + 10, infoRectangle.Y + infoRectangle.Height - 72 - 8), Color.White);

                if (_state is CombatState && !(_sortedList[_selectedSlot].Item is CombatItem))
                {
                    batch.Text(font, 16, "This item is not usable in combat", new Vector2(infoRectangle.X + x + 10, infoRectangle.Y + infoRectangle.Height - 60 - 8), Color.White * 0.6f);
                }
                else
                {
                    foreach (PaneAction action in _actions)
                    {
                        bool selected = _rightPaneSelected && _selectedAction == _actions.IndexOf(action);
                        //float alpha = selected ? 0.8f + (float)Math.Sin(_state.Game.Time * 10) * 0.2f : 0.6f;
                        float alpha = selected ? 0.8f : 0.6f;

                        batch.Text(font, 16, action.Name, new Vector2(infoRectangle.X + x + 10, infoRectangle.Y + infoRectangle.Height - 60 - 8), Color.White * alpha);
                        if (selected)
                        {
                            batch.Texture(new Vector2(infoRectangle.X + x, infoRectangle.Y + infoRectangle.Height - 60), _state.Assets.Get <Texture2D>("ui/arrow.png"), Color.White);
                        }

                        x += (int)font.Measure(16, action.Name).X + 16;
                    }
                }
            }
        }
Пример #2
0
        private IEnumerator <ICoroutineOperation> ChangeSelectedSlot(int selectedSlot)
        {
            if (selectedSlot != _selectedSlot)
            {
                _selectedSlot = -1;

                int       y               = 37 + Math.Max(0, selectedSlot * 15);
                int       height          = (_entity.Inventory.OccupiedSlots * 15);
                Rectangle itemsListBounds = new Rectangle(16, 37, 171, 210);

                float scrollStart  = _scroll;
                float scrollTarget = -1;

                if (itemsListBounds.Bottom <= y + 15 - (int)_scroll)
                {
                    scrollTarget = _scroll;

                    while (itemsListBounds.Bottom <= y + 15 - (int)scrollTarget)
                    {
                        scrollTarget++;
                    }
                }
                if (itemsListBounds.Top > y - (int)_scroll)
                {
                    scrollTarget = _scroll;

                    while (itemsListBounds.Top > y - (int)scrollTarget)
                    {
                        scrollTarget--;
                    }
                }

                if (scrollTarget != -1)
                {
                    scrollTarget = MathHelper.Clamp(scrollTarget, 0, height - itemsListBounds.Height - 6);
                }

                if (scrollTarget == -1 || Math.Abs(scrollTarget - scrollStart) == 1)
                {
                    yield return(Wait.Seconds(_state.Game, 0.06));
                }
                else
                {
                    double startTime = _state.Game.Time;

                    while (_state.Game.Time < startTime + (Math.Abs(scrollTarget - scrollStart) > 100 ? 0.5 : 0.1))
                    {
                        _scroll = MathHelper.Lerp(scrollStart, scrollTarget, (float)(_state.Game.Time - startTime) * (1f / (Math.Abs(scrollTarget - scrollStart) > 100 ? 0.5f : 0.1f)));
                        yield return(null);
                    }

                    _scroll = scrollTarget;
                }
            }

            _selectedSlot   = selectedSlot;
            _selectedAction = 0;

            _actions.Clear();

            if (_selectedSlot != -1)
            {
                _actions.Add(new PaneAction("Duplicate", (a, ent, item, ui) =>
                {
                    ent.Inventory.Push(item.Item);
                    UpdateSortedList();
                }));

                _actions.Add(new PaneAction("Drop", (a, ent, item, ui) =>
                {
                    foreach (InventoryEquipSlot islot in Enum.GetValues(typeof(InventoryEquipSlot)))
                    {
                        if (islot != InventoryEquipSlot.None && ent.Inventory[islot].Slot == item.Slot)
                        {
                            ent.Inventory[islot] = ent.Inventory[Inventory.EMPTY_SLOT];
                        }
                    }

                    ent.Inventory.PopAt(item.Slot);
                    UpdateSortedList();

                    int slot         = ui._selectedSlot;
                    ui._selectedSlot = -1;

                    if (slot >= ent.Inventory.OccupiedSlots)
                    {
                        ent.World.Coroutine.AddExisting(ChangeSelectedSlot(slot - 1));
                    }
                    else
                    {
                        ent.World.Coroutine.AddExisting(ChangeSelectedSlot(slot));
                    }
                }));

                Inventory.ItemSlotReference i = _sortedList[_selectedSlot];
                foreach (InventoryEquipSlot slot in Enum.GetValues(typeof(InventoryEquipSlot)))
                {
                    if (slot != InventoryEquipSlot.None && i.Item.EquippableIn.HasFlag(slot))
                    {
                        string text = "Equip ";
                        switch (slot)
                        {
                        case InventoryEquipSlot.Head:
                            text += "on head";
                            break;

                        case InventoryEquipSlot.Chest:
                            text += "chest";
                            break;

                        case InventoryEquipSlot.Leggings:
                            text += "leggings";
                            break;

                        case InventoryEquipSlot.Boots:
                            text += "boots";
                            break;

                        case InventoryEquipSlot.Weapon:
                            text += "weapon";
                            break;

                        case InventoryEquipSlot.Ring:
                            text += "ring";
                            break;
                        }

                        if (i.Inventory[slot].Slot == i.Slot)
                        {
                            text = text.
                                   Replace("Equip", "Unequip").
                                   Replace("on ", "from ");
                        }

                        _actions.Add(new PaneAction(text, (a, ent, item, ui) =>
                        {
                            if (ent.Inventory[slot].Slot == item.Slot)
                            {
                                ent.Inventory[slot] = ent.Inventory[Inventory.EMPTY_SLOT];
                            }
                            else
                            {
                                ent.Inventory[slot] = ent.Inventory[item.Slot];

                                foreach (InventoryEquipSlot slot_2 in Enum.GetValues(typeof(InventoryEquipSlot)))
                                {
                                    if (slot_2 != InventoryEquipSlot.None && slot_2 != slot && ent.Inventory[slot_2].Slot == item.Slot)
                                    {
                                        ent.Inventory[slot_2] = ent.Inventory[Inventory.EMPTY_SLOT];
                                    }
                                }
                            }
                        }));
                    }
                }
            }
        }