/// <summary> /// Adds item to inventory. If the inventory is full nothing happens. Use IsInventoryFull() method first to check /// whether there's room for another item in inventory. /// </summary> /// <param name="item">Item to be added.</param> public void AddItemToInventory(AbstractInventoryItem item) { if (IsInventoryFull()) { return; } Inventory.Add(item); }
/// <summary> /// Removes item from inventory and returns it. /// </summary> /// <param name="index">Idenx of the item to be removed. If the index is out of range, null is returned.</param> /// <returns>Item removed from inventory.</returns> public AbstractInventoryItem RemoveItemFromInventory(int index) { if (index < 0 || index >= Inventory.Count) { return(null); } AbstractInventoryItem item = Inventory[index]; Inventory.RemoveAt(index); return(item); }
/// <summary> /// Renders item as a path. /// </summary> /// <param name="item">Item to be rendered.</param> /// <param name="x">Top left corner x-coordinate of map block.</param> /// <param name="y">Top left corner y-coordinate of map block.</param> /// <param name="blockSize">Height and width of the block.</param> /// <returns>Item rendered as a path.</returns> private Path RenderItem(AbstractInventoryItem item, double x, double y, double blockSize) { return(RenderPath(configuration.ItemPath, x, y, blockSize / 3, Color.FromRgb(0, 0, 0), null)); }
public void TestSerializeMaze() { int w = 4; int h = 4; Map map = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); map.MapName = "Test map"; // add creatures to map Monster origMonster = new Monster("Test monster", map.Grid[0, 0], 4, 3654123, 87621235); map.AddCreature(origMonster); SimpleAIPlayer aiPlayer = new SimpleAIPlayer("Test player", map.Grid[3, 2]); map.AddCreature(aiPlayer); HumanPlayer hPlayer = new HumanPlayer("Příliš žluťoučký kůň úpěl ďábelské ódy", map.Grid[1, 3]) { BaseHitPoints = 98432156, BaseAttack = 112348, BaseDeffense = 41226987 }; map.AddCreature(hPlayer); // add items to map AbstractWeapon weapon = ItemFactory.CreateAxe(map.Grid[1, 3]); map.AddItem(weapon); AbstractArmor armor = ItemFactory.CreateLeatherArmor(map.Grid[1, 1]); map.AddItem(armor); AbstractInventoryItem item = new BasicItem("Příliš žluťoučký kůň úpěl ďábelské ódy.!?_/()':123456789<>&@{}[]", map.Grid[2, 2], 514) { UniqueId = 6284 }; map.AddItem(item); // serialize - deserialize IMapSerializer <byte[], byte[]> byteSerializer = new BinaryMapSerializer(); byte[] serializedMap = byteSerializer.Serialize(map); Map deserializedMap = byteSerializer.Deserialize(serializedMap); // check map Assert.AreEqual(map.MapName, deserializedMap.MapName, "Wrong map name!"); Assert.AreEqual(w, deserializedMap.Width, "Wrong width after deserialization!"); Assert.AreEqual(h, deserializedMap.Width, "Wrong height after deserialization!"); Assert.AreEqual(map.WinningBlock.X, deserializedMap.WinningBlock.X, "Wrong x coordinate of winning block!"); Assert.AreEqual(map.WinningBlock.Y, deserializedMap.WinningBlock.Y, "Wrong Y coordinate of winning block!"); // check map blocks for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { MapBlock origBlock = map.Grid[i, j]; MapBlock testedBlock = deserializedMap.Grid[i, j]; foreach (Direction dir in DirectionMethods.GetAllDirections()) { Assert.AreEqual(origBlock.EntranceInDirection(dir).IsOpen(), testedBlock.EntranceInDirection(dir).IsOpen(), $"Wrong entrance in direction {dir} in block [{i},{j}]."); } } } // check creatures Monster m = (Monster)deserializedMap.Grid[0, 0].Creature; CheckCreature(origMonster, m); SimpleAIPlayer p = (SimpleAIPlayer)deserializedMap.Grid[3, 2].Creature; CheckCreature(aiPlayer, p); HumanPlayer hp = (HumanPlayer)deserializedMap.Grid[1, 3].Creature; CheckCreature(hPlayer, hp); // check items AbstractWeapon weap = (AbstractWeapon)map.Grid[1, 3].Item; CheckItem(weap, weapon); AbstractArmor arm = (AbstractArmor)map.Grid[1, 1].Item; CheckItem(arm, armor); AbstractInventoryItem itm = (AbstractInventoryItem)map.Grid[2, 2].Item; CheckItem(item, itm); }