예제 #1
0
 def test_item_ownership(self):
     item = my_game_item.GameItem()
     item.AddStateChange(1, 0)
     self.item_mapper.AddItem("A", item)
     item.AddStateChange(3, 2)
     stored_item = self.item_mapper.GetItem("A")
     # Test this is not a default item.
     self.assertNotEqual(stored_item, my_game_item.GameItem())
     self.assertNotEqual(stored_item, item)
     self.assertEqual(item.UseItem(3), 2)
     self.assertEqual(stored_item.UseItem(3), 3)
예제 #2
0
 def setUp(self):
     self.game_item = my_game_item.GameItem()
     self.game_item.name = "hammer"
     self.game_item.reusable = True
     self.game_item.AddStateChange(1, 0)
     self.game_item.AddStateChange(2, 0)
     self.game_item.AddStateChange(3, 1)
예제 #3
0
    def test_add_item(self):
        item_1 = my_game_item.GameItem()
        item_1.AddStateChange(1, 0)
        self.item_mapper.AddItem("A", item_1)
        item_2 = my_game_item.GameItem()
        item_2.AddStateChange(2, 0)
        self.item_mapper.AddItem("A", item_2)
        item_3 = my_game_item.GameItem()
        item_3.AddStateChange(3, 0)
        self.item_mapper.AddItem("B", item_3)

        stored_item = self.item_mapper.GetItem("A")
        self.assertEqual(stored_item.UseItem(1), 1)
        self.assertEqual(stored_item.UseItem(2), 0)

        stored_item = self.item_mapper.GetItem("B")
        self.assertEqual(stored_item.UseItem(3), 0)
        self.assertEqual(stored_item.UseItem(2), 2)

        stored_item = self.item_mapper.GetItem("C")
        self.assertEqual(stored_item.UseItem(3), 3)
예제 #4
0
    def ParseItem(self, line_parts):
        """Parse [ITEMS] section of config file.

        Assumes lines of the form:
          <name>:<colon-separated list of state-changing effects>
        where state-changing effect is of the form "old_state_id>new_state_id".

        Args:
          line_parts:  An array of the config file line, split on ":".

        Raises:
          ParseError if we are unable to parse a state-changing effect specified.
        """
        key = line_parts[0]
        item = my_game_item.GameItem()
        for effect in line_parts[1:]:
            try:
                old_state, new_state = [int(s) for s in effect.split(">")]
            except ValueError:
                raise ParseError("Error parsing effect for %s: %s", key,
                                 effect)
            item.AddStateChange(old_state, new_state)
        self._player.item_mapper.AddItem(key, item)
    def test_use_item(self):
        self._player.curr_room.AddContent("E")
        item_mapper = my_game_item.ItemMapper()

        item_a = my_game_item.GameItem()
        item_a.AddStateChange(1, 0)
        item_a.reusable = True
        item_mapper.AddItem("A", item_a)

        item_b = my_game_item.GameItem()
        item_b.AddStateChange(2, 0)
        item_b.AddStateChange(3, 1)
        item_b.reusable = True
        item_mapper.AddItem("B", item_b)

        item_c = my_game_item.GameItem()
        item_c.reusable = True
        item_mapper.AddItem("C", item_c)

        item_d = my_game_item.GameItem()
        item_mapper.AddItem("C", item_d)

        item_e = my_game_item.GameItem()
        item_e.AddStateChange(1, 0)
        item_e.reusable = False
        item_mapper.AddItem("E", item_e)

        self._player.item_mapper = item_mapper
        self._player.max_inventory_size = 4
        self._player.AddItem("A")
        self._player.AddItem("B")
        self._player.AddItem("C")
        self._player.AddItem("E")
        self.assertEqual(self._player.inventory, ["A", "B", "C", "E"])

        # No item in inventory.
        self.assertFalse(self._player.UseItem("D")[0])

        # C is reusable, but does nothing.
        self.assertFalse(self._player.UseItem("C")[0])
        self.assertEqual(self._player.inventory, ["A", "B", "C", "E"])

        # B is reusable, but does nothing in state 0.
        self.assertFalse(self._player.UseItem("B")[0])
        self.assertEqual(self._player.inventory, ["A", "B", "C", "E"])

        # E is not reusable and does nothing in state 0.
        self.assertFalse(self._player.UseItem("E")[0])
        self.assertEqual(self._player.inventory, ["A", "B", "C"])

        self._player.curr_room.state = 1
        # A is reusable and moves the room from state 1 to 0.
        self.assertTrue(self._player.UseItem("A")[0])
        self.assertEqual(self._player.inventory, ["A", "B", "C"])
        self.assertEqual(self._player.curr_room.state, 0)

        self._player.curr_room.state = 3
        # B is reusable and moves the room from state 3 to 1.
        self.assertTrue(self._player.UseItem("B")[0])
        self.assertEqual(self._player.inventory, ["A", "B", "C"])
        # A is reusable and moves the room from state 1 to 0.
        self.assertEqual(self._player.curr_room.state, 1)
        self.assertTrue(self._player.UseItem("A")[0])
        self.assertEqual(self._player.inventory, ["A", "B", "C"])
        self.assertEqual(self._player.curr_room.state, 0)
예제 #6
0
 def test_default(self):
     self.assertEqual(self.item_mapper.all_items, {})
     # GameItem.__eq__ should allow this default item to be equal.
     self.assertEqual(self.item_mapper.GetItem("A"),
                      my_game_item.GameItem())