Пример #1
0
        public void TakeOff(Equipment equipment)
        {
            var slots = Slots.Where(x => x.Item2.Equipment == equipment);

            foreach (var tuple in slots)
            {
                tuple.Item2.Equipment = null;
                var itemEntity = Holder.Items.FirstOrDefault(x => x.Id == equipment.ParentEntityId);
                if (itemEntity == null)
                {
                    Holder.Items.Add(Game.GetEntity(equipment.ParentEntityId));
                }
            }
        }
Пример #2
0
        public InventoryScreen(NamelessGame game)
        {
            this.game = game;

            Panel = new Panel()
            {
                Width  = game.GetActualWidth(),
                Height = game.GetActualCharacterHeight(),
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Top
            };
            ReturnToGame = new ImageTextButton()
            {
                GridRow    = 1,
                GridColumn = 1,
                ContentHorizontalAlignment = HorizontalAlignment.Center,
                ContentVerticalAlignment   = VerticalAlignment.Center,
                Text = "Back",
                VerticalAlignment   = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Right,
                Width  = 200,
                Height = 50
            };
            ReturnToGame.Click += OnClickReturnToGame;

            var grid = new Grid()
            {
                VerticalAlignment = VerticalAlignment.Stretch, ColumnSpacing = 3, RowSpacing = 2
            };

            grid.RowsProportions.Add(new Proportion(ProportionType.Fill));
            grid.RowsProportions.Add(new Proportion(ProportionType.Pixels, 50));

            EquipmentBox = new Table()
            {
                GridColumn        = 0, GridRow = 0, HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                BorderThickness   = new Thickness(1),
                Border            = new SolidBrush(Color.White)
            };
            ItemBox = new Table()
            {
                GridColumn        = 1, GridRow = 0, HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                BorderThickness   = new Thickness(1),
                Border            = new SolidBrush(Color.White)
            };


            //FillItems(game);


            grid.Widgets.Add(EquipmentBox);
            grid.Widgets.Add(ItemBox);
            grid.Widgets.Add(ReturnToGame);
            Panel.Widgets.Add(grid);

            game.Desktop.Widgets.Add(Panel);

            SelectedTable = ItemBox;

            ItemChoiceDialog = new ChoiceDialog(
                new ChoiceOption()
            {
                Id = ItemDialogActions.Equip, Text = "Equip"
            },
                new ChoiceOption()
            {
                Id = ItemDialogActions.Drop, Text = "Drop"
            },
                new ChoiceOption()
            {
                Id = ItemDialogActions.Cancel, Text = "Cancel"
            }
                );
            ItemChoiceDialog.Title = "Item commands";


            EquipmentChoiceDialog = new ChoiceDialog(
                new ChoiceOption()
            {
                Id = EquipmentDialogActions.Unequip, Text = "Unequip"
            },
                new ChoiceOption()
            {
                Id = EquipmentDialogActions.Drop, Text = "Drop"
            },
                new ChoiceOption()
            {
                Id = EquipmentDialogActions.Cancel, Text = "Cancel"
            }
                );
            EquipmentChoiceDialog.Title = "Equipment commands";

            ItemBox.OnItemClick += (TableItem selectedItem) =>
            {
                int selectedIndex = ItemBox.Items.IndexOf(selectedItem);
                if (selectedIndex > 0)
                {
                    this.SelectedItem = selectedItem;

                    var itemEntity = (IEntity)selectedItem.Tag;
                    FillItemChoiceDialog(itemEntity);
                    OpenDialog(ItemChoiceDialog, game);
                }
            };

            EquipmentBox.OnItemClick += (TableItem selectedItem) =>
            {
                int selectedIndex = EquipmentBox.Items.IndexOf(selectedItem);
                if (selectedIndex > 0)
                {
                    this.SelectedItem = selectedItem;
                    OpenDialog(EquipmentChoiceDialog, game);
                }
            };

            EquipmentChoiceDialog.OptionsTable.OnItemClick += (TableItem selectedItemOptions) =>
            {
                if (SelectedItem.Tag == null)
                {
                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                    {
                        CloseDialog(EquipmentChoiceDialog);
                    });
                    return;
                }

                var playerEntity = game.PlayerEntity;
                var slot         = (Slot)SelectedItem.Tag;
                var itemsHolder  = playerEntity.GetComponentOfType <ItemsHolder>();
                var equipment    = playerEntity.GetComponentOfType <EquipmentSlots>();

                var equipmentItem = equipment.Slots.First(x => x.Item1 == slot).Item2.Equipment;

                if (equipmentItem == null)
                {
                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                    {
                        CloseDialog(EquipmentChoiceDialog);
                    });
                    return;
                }

                var chosenItem    = (ChoiceOption)selectedItemOptions.Tag;
                var dialogActions = (EquipmentDialogActions)chosenItem.Id;
                switch (dialogActions)
                {
                case EquipmentDialogActions.Drop:

                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                    {
                        equipment.TakeOff(equipmentItem);
                        var position = playerEntity.GetComponentOfType <Position>();
                        var command  = new DropItemCommand(new List <IEntity>()
                        {
                            game.GetEntity(equipment.ParentEntityId)
                        }, itemsHolder, position.Point);
                        namelessGame.Commander.EnqueueCommand(command);
                        invScreenSystem.ScheduleUpdate();
                    });


                    break;

                case EquipmentDialogActions.Unequip:
                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                    {
                        equipment.TakeOff(equipmentItem);
                        invScreenSystem.ScheduleUpdate();
                    });
                    break;

                case EquipmentDialogActions.Cancel:
                    break;

                default:
                    break;
                }

                Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                {
                    CloseDialog(EquipmentChoiceDialog);
                });
            };

            ItemChoiceDialog.Closed += (sender, args) => { SelectTable(ItemBox); dialogOpened = false; };

            ItemChoiceDialog.OptionsTable.OnItemClick += (TableItem selectedItemOptions) =>
            {
                var playerEntity = game.PlayerEntity;
                var itemEntity   = (IEntity)SelectedItem.Tag;
                var itemsHolder  = playerEntity.GetComponentOfType <ItemsHolder>();
                var equipment    = playerEntity.GetComponentOfType <EquipmentSlots>();

                var equipmentItem = itemEntity.GetComponentOfType <Equipment>();

                var chosenItem = (ChoiceOption)selectedItemOptions.Tag;


                ItemDialogActions itemDialogActions = (ItemDialogActions)chosenItem.Id;
                switch (itemDialogActions)
                {
                case ItemDialogActions.DropAmount:
                {
                    AmountDialog = new AmountDialog();
                    AmountDialog.ShowModal(game.Desktop);
                    AmountDialog.Amount.OnTouchDown();
                    AmountDialog.Closed += (sender, args) =>
                    {
                        if (AmountDialog.Result)
                        {
                            var position      = playerEntity.GetComponentOfType <Position>();
                            var amount        = AmountDialog.Amount.Text == null ? 0 : int.Parse(AmountDialog.Amount.Text);
                            var itemComponent = itemEntity.GetComponentOfType <Item>();
                            if (amount >= itemComponent.Amount)
                            {
                                CloseDialog(ItemChoiceDialog);
                                Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                                    {
                                        var command = new DropItemCommand(new List <IEntity>()
                                        {
                                            itemEntity
                                        }, itemsHolder,
                                                                          position.Point);
                                        namelessGame.Commander.EnqueueCommand(command);
                                        invScreenSystem.ScheduleUpdate();
                                    });
                            }
                            else if (amount < 1)
                            {
                            }
                            else
                            {
                                var clonedEntity = itemEntity.CloneEntity();

                                //game.EntitiesToAdd.Add(clonedEntity);

                                var clonedItemComponent = clonedEntity.GetComponentOfType <Item>();

                                itemComponent.Amount      -= amount;
                                clonedItemComponent.Amount = amount;



                                Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                                    {
                                        CloseDialog(ItemChoiceDialog);
                                        var command = new DropItemCommand(new List <IEntity>()
                                        {
                                            clonedEntity
                                        }, itemsHolder,
                                                                          position.Point);
                                        namelessGame.Commander.EnqueueCommand(command);

                                        invScreenSystem.ScheduleUpdate();
                                    });
                            }
                        }
                        AmountDialog = null;
                    };
                }
                break;

                case ItemDialogActions.Drop:
                {
                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                        {
                            var position = playerEntity.GetComponentOfType <Position>();
                            var command  = new DropItemCommand(new List <IEntity>()
                            {
                                itemEntity
                            }, itemsHolder,
                                                               position.Point);
                            namelessGame.Commander.EnqueueCommand(command);
                            invScreenSystem.ScheduleUpdate();
                            CloseDialog(ItemChoiceDialog);
                        });
                }
                break;

                case ItemDialogActions.Equip:
                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                    {
                        List <Slot> slotsEquipTo;
                        slotsEquipTo = (List <Slot>)chosenItem.Data;
                        equipment.Equip(equipmentItem, slotsEquipTo);
                        invScreenSystem.ScheduleUpdate();
                        CloseDialog(ItemChoiceDialog);
                    });

                    break;

                case ItemDialogActions.Cancel:
                    Actions.Add((InventoryScreenSystem invScreenSystem, NamelessGame namelessGame) =>
                    {
                        CloseDialog(ItemChoiceDialog);
                    });
                    break;

                default:
                    break;
                }
            };

            EquipmentChoiceDialog.Closed += (sender, args) => { SelectTable(EquipmentBox);
                                                                dialogOpened = false; };

            OnCloseDialog += () => { FillItems(this.game); };
        }
Пример #3
0
        public void FillItems(NamelessGame game)
        {
            var selectedIndex = SelectedTable?.Items.IndexOf(SelectedItem);


            EquipmentBox.Items.Clear();
            ItemBox.Items.Clear();

            var playerEntity = game.PlayerEntity;

            var itemsHolder = playerEntity.GetComponentOfType <ItemsHolder>();
            var equipment   = playerEntity.GetComponentOfType <EquipmentSlots>();

            char hotkey = Char.MinValue;

            var headerEquipmentItem = new TableItem(3);

            headerEquipmentItem.Cells[0].Widgets.Add(new Label()
            {
                Text = "Hotkey", HorizontalAlignment = HorizontalAlignment.Center
            });
            headerEquipmentItem.Cells[1].Widgets.Add(new Label()
            {
                Text = "Slot",
            });
            headerEquipmentItem.Cells[2].Widgets.Add(new Label()
            {
                Text = "Name",
            });
            EquipmentBox.Items.Add(headerEquipmentItem);
            {
                int i = 0;
                foreach (var equipmentSlot in equipment.Slots)
                {
                    if (i == 0)
                    {
                        hotkey = HotkeyHelper.alphabet.First();
                    }
                    else
                    {
                        hotkey = HotkeyHelper.GetNextKey(hotkey);
                    }

                    var         eq   = equipmentSlot.Item2.Equipment;
                    Description desc = null;
                    if (eq != null)
                    {
                        var itemEntity = game.GetEntity(eq.ParentEntityId);
                        desc = itemEntity.GetComponentOfType <Description>();
                    }
                    var text = desc != null ? desc.Name : "Nothing";

                    var tableItem = new TableItem(3);
                    tableItem.Hotkey = hotkey;
                    tableItem.Tag    = equipmentSlot.Item1;
                    tableItem.Cells[0].Widgets.Add(new Label()
                    {
                        Text = hotkey.ToString(), HorizontalAlignment = HorizontalAlignment.Center
                    });
                    tableItem.Cells[1].Widgets.Add(new Label()
                    {
                        Text = equipmentSlot.Item1.ToString(),
                    });
                    tableItem.Cells[2].Widgets.Add(new Label()
                    {
                        Text = text
                    });
                    EquipmentBox.Items.Add(tableItem);
                    i++;
                }
            }

            var headerItem = new TableItem(6);

            headerItem.Cells[0].Widgets.Add(new Label()
            {
                Text = "Hotkey", HorizontalAlignment = HorizontalAlignment.Center
            });
            headerItem.Cells[1].Widgets.Add(new Label()
            {
                Text = "Name",
            });
            headerItem.Cells[2].Widgets.Add(new Label()
            {
                Text = "Quality",
            });
            headerItem.Cells[3].Widgets.Add(new Label()
            {
                Text = "Weight",
            });
            headerItem.Cells[4].Widgets.Add(new Label()
            {
                Text = "Type",
            });
            headerItem.Cells[5].Widgets.Add(new Label()
            {
                Text = "Amount",
            });
            ItemBox.Items.Add(headerItem);

            List <IEntity> list = itemsHolder.Items;

            for (int i = 0; i < list.Count; i++)
            {
                IEntity entity = list[i];

                hotkey = HotkeyHelper.GetNextKey(hotkey);

                Description desc = entity.GetComponentOfType <Description>();
                Item        item = entity.GetComponentOfType <Item>();

                var tableItem = new TableItem(6);
                tableItem.Tag    = entity;
                tableItem.Hotkey = hotkey;
                tableItem.Cells[0].Widgets.Add(new Label()
                {
                    Text = hotkey.ToString(), HorizontalAlignment = HorizontalAlignment.Center
                });
                tableItem.Cells[1].Widgets.Add(new Label()
                {
                    Text = desc.Name,
                });
                tableItem.Cells[2].Widgets.Add(new Label()
                {
                    Text = item.Quality.ToString(),
                });
                tableItem.Cells[3].Widgets.Add(new Label()
                {
                    Text = (item.Weight * item.Amount).ToString(),
                });
                tableItem.Cells[4].Widgets.Add(new Label()
                {
                    Text = item.Type.ToString(),
                });
                tableItem.Cells[5].Widgets.Add(new Label()
                {
                    Text = item.Amount.ToString(),
                });
                ItemBox.Items.Add(tableItem);
            }


            if (selectedIndex >= SelectedTable?.Items.Count)
            {
                if (SelectedTable != null)
                {
                    SelectedTable.SelectedIndex = 0;
                    SelectedItem = SelectedTable.SelectedItem;
                }
            }
            else
            {
                if (SelectedTable != null)
                {
                    SelectedTable.SelectedIndex = selectedIndex;
                    SelectedItem = SelectedTable.SelectedItem;
                }
            }
        }