Esempio n. 1
0
 public void AddItem(PlacedItem placedItem)
 {
     PlacedItems.Add(placedItem);
     placedItem.OnRemove += () =>
     {
         if (PlacedItems.Contains(placedItem))
         {
             RemoveItem(placedItem);
         }
     };
     placedItem.Place();
 }
Esempio n. 2
0
    public void RemoveItem(PlacedItem placedItem)
    {
        if (placedItem == null || !placedItem.Removable)
        {
            return;
        }

        ItemController.AddItem(placedItem.Item);

        PlacedItems.Remove(placedItem);
        placedItem.Remove();
    }
        /// <summary>
        /// Removes placed item from the map.
        /// </summary>
        /// <param name="uid">Uid of item to be removed.</param>
        public void RemovePlacedItem(int uid)
        {
            GameMap?.RemoveObjectFromMapByUid(uid);
            foreach (EditorToolboxItem item in PlacedItems)
            {
                if (item.UID == uid)
                {
                    PlacedItems.Remove(item);
                    if (item.ItemType == EditorToolboxItemType.HUMAN_PLAYER)
                    {
                        HumanPlayerPlaced = false;
                    }
                    break;
                }
            }

            OnPropertyChanged("PlacedItems");
        }
        /// <summary>
        /// Places SelectedToolboxItem to the map. If no item is selected, nothing happnes.
        ///
        /// If the map block is occupied, coordinates are out of range, map is null or item type is unknown, exception is raised.
        ///
        /// </summary>
        /// <param name="mapX">X coordinate of map block to place this item.</param>
        /// <param name="mapY">Y coordinate of map block to place this item.</param>
        public void PlaceSelectedToolboxItem(int mapX, int mapY)
        {
            EditorToolboxItem item = SelectedToolboxItem;

            // no item selected => do nothing
            if (item == null)
            {
                return;
            }

            if (GameMap == null)
            {
                throw new Exception("Není herní mapa!");
            }
            else if (mapX < 0 || mapX >= GameMap.Width || mapY < 0 || mapY >= GameMap.Height)
            {
                throw new Exception($"[{mapX},{mapY}] není v rozsahu mapy!");
            }
            else if ((item.ItemType == EditorToolboxItemType.AI_PLAYER || item.ItemType == EditorToolboxItemType.MONSTER) && GameMap.Grid[mapX, mapY].Occupied)
            {
                throw new Exception($"Na pozici [{mapX},{mapY}] už je umístěn hráč, nebo monstrum!");
            }
            else if ((item.ItemType == EditorToolboxItemType.ITEM || item.ItemType == EditorToolboxItemType.ARMOR || item.ItemType == EditorToolboxItemType.WEAPON) && GameMap.Grid[mapX, mapY].Item != null)
            {
                throw new Exception($"Na pozici [{mapX},{mapY}] už je umístěn předmět!");
            }

            int uid = -1;

            switch (item.ItemType)
            {
            case EditorToolboxItemType.HUMAN_PLAYER:
                if (HumanPlayerPlaced)
                {
                    throw new Exception("Na hrací plochu lze umístit pouze jednoho lidského hráče!");
                }
                GameMap.Grid[mapX, mapY].Creature = new HumanPlayer("Human player", GameMap.Grid[mapX, mapY]);
                uid = GameMap.Grid[mapX, mapY].Creature.UniqueId;
                HumanPlayerPlaced = true;
                break;

            case EditorToolboxItemType.AI_PLAYER:
                GameMap.Grid[mapX, mapY].Creature = AIPlayerFactory.CreateSimpleAIPLayer("Simple AI Player", GameMap.Grid[mapX, mapY]);
                uid = GameMap.Grid[mapX, mapY].Creature.UniqueId;
                break;

            case EditorToolboxItemType.MONSTER:
                GameMap.Grid[mapX, mapY].Creature = MonsterFactory.CreateRandomMonster(GameMap.Grid[mapX, mapY]);
                uid = GameMap.Grid[mapX, mapY].Creature.UniqueId;
                break;

            case EditorToolboxItemType.ITEM:
                GameMap.Grid[mapX, mapY].Item = ItemFactory.CreateBasicItem(GameMap.Grid[mapX, mapY]);
                uid = GameMap.Grid[mapX, mapY].Item.UniqueId;
                break;

            case EditorToolboxItemType.ARMOR:
                GameMap.Grid[mapX, mapY].Item = ItemFactory.CreateLeatherArmor(GameMap.Grid[mapX, mapY]);
                uid = GameMap.Grid[mapX, mapY].Item.UniqueId;
                break;

            case EditorToolboxItemType.WEAPON:
                GameMap.Grid[mapX, mapY].Item = ItemFactory.CreateAxe(GameMap.Grid[mapX, mapY]);
                uid = GameMap.Grid[mapX, mapY].Item.UniqueId;
                break;

            default:
                throw new Exception($"Neznámý typ umisťovaného předmětu: {item.ItemType}!");
            }
            EditorToolboxItem placedItem = item.Clone();

            placedItem.UID = uid;
            PlacedItems.Add(placedItem);

            OnPropertyChanged("PlacedItems");
        }
        /// <summary>
        /// Load map to this view model.
        /// </summary>
        /// <param name="map">Map to be loaded.</param>
        public void LoadMap(Map map)
        {
            // try to load all map items and if it's ok, place it, otherwise
            // throw exception, this way the old map will still remain in editor
            List <EditorToolboxItem> tmpPlacedItems = new List <EditorToolboxItem>();

            foreach (MapBlock mb in map.Grid)
            {
                if (mb.Creature != null)
                {
                    EditorToolboxItemType type;
                    if (mb.Creature is Monster)
                    {
                        type = EditorToolboxItemType.MONSTER;
                    }
                    else if (mb.Creature is HumanPlayer)
                    {
                        type = EditorToolboxItemType.HUMAN_PLAYER;
                    }
                    else if (mb.Creature is AbstractPlayer)
                    {
                        type = EditorToolboxItemType.AI_PLAYER;
                    }
                    else
                    {
                        throw new Exception($"Neznámý typ bytosti {mb.Creature.GetType()}!");
                    }
                    tmpPlacedItems.Add(new EditorToolboxItem()
                    {
                        UID = mb.Creature.UniqueId, Name = mb.Creature.Name, ItemType = type
                    });
                }

                if (mb.Item != null)
                {
                    EditorToolboxItemType type;
                    if (mb.Item is AbstractWeapon)
                    {
                        type = EditorToolboxItemType.WEAPON;
                    }
                    else if (mb.Item is AbstractArmor)
                    {
                        type = EditorToolboxItemType.ARMOR;
                    }
                    else if (mb.Item is AbstractInventoryItem)
                    {
                        type = EditorToolboxItemType.ITEM;
                    }
                    else
                    {
                        throw new Exception($"Neznámý typ předmětu {mb.Item.GetType()}!");
                    }
                    tmpPlacedItems.Add(new EditorToolboxItem()
                    {
                        UID = mb.Item.UniqueId, Name = mb.Item.Name, ItemType = type
                    });
                }
            }

            PlacedItems.Clear();
            tmpPlacedItems.ForEach(placedItem => PlacedItems.Add(placedItem));
            GameMap              = map;
            MapWidth             = map.Width;
            MapHeight            = map.Height;
            MapName              = map.MapName;
            GeneratePanelEnabled = true;
            DeselectToolboxItem();

            OnPropertyChanged("GeneratePanelEnabled");
            OnPropertyChanged("PlacedItems");
            OnPropertyChanged("MapWidth");
            OnPropertyChanged("Mapheight");
            OnPropertyChanged("MapName");
        }