class  TestInventory(unittest.TestCase):
    class CarryableObject (GameObject, Carryable):
        def __init__ (self, ID, **kwargs):
            GameObject.__init__(self, ID, **kwargs)
            Carryable.__init__(self, **kwargs)

    def setUp(self):
        self.item = self.CarryableObject(12, name="TestItem1")
        self.item.weight = 12
        self.item2 = self.CarryableObject(13)
        self.item2.weight = 13
        self.inventory = Inventory()

    def testPlaceTakeMove(self):
        """Test Inventory Place/Take/Move functions"""
        self.assertTrue(self.inventory.isSlotEmpty("backpack"))
        self.inventory.placeItem(self.item)
        self.assertTrue(self.item in self.inventory.getItemsInSlot("backpack").values())
        self.assertEqual(self.inventory.weight, 12)
        self.assertEqual(self.inventory.count(), 1)
        self.assertFalse(self.inventory.isSlotEmpty("backpack"))

        self.inventory.moveItemToSlot(self.item, "groin")
        self.assertFalse(self.item in self.inventory.getItemsInSlot("backpack").values())
        self.assertTrue(self.item in self.inventory.getItemsInSlot("groin").values())
        self.assertEqual(self.inventory.count(), 1)
        
        self.assertRaises(ValueError, self.inventory.moveItemToSlot, self.item2, "somewhere")
        
        self.inventory.moveItemToSlot(self.item2, "chest")
        self.assertEqual(self.inventory.count(),2)
        self.assertEqual(self.inventory.weight, 12+13)
        self.assertTrue(self.item2 in self.inventory.getItemsInSlot("chest").values())

        self.inventory.takeItem(self.item)
        self.assertEqual(self.inventory.count(),1)
        self.assertEqual(self.inventory.weight, 13)

    def testReplace(self):
        """Test Inventory items replace each other in single-item slots"""
        self.inventory.placeItem(self.item)
        self.inventory.moveItemToSlot(self.item,"neck")
        self.assertFalse(self.inventory.isSlotEmpty("neck"))
        self.assertTrue(self.item in self.inventory.getItemsInSlot("neck").values())

        self.inventory.moveItemToSlot(self.item2, "neck")
        self.assertFalse(self.inventory.isSlotEmpty("neck"))
        self.assertTrue(self.item2 in self.inventory.getItemsInSlot("neck").values())
        self.assertFalse(self.item in self.inventory.getItemsInSlot("neck").values())

    def testFind(self):
        self.inventory.placeItem(self.item)
        self.assertEqual(self.inventory.findItemByID(12), self.item)
        self.assertEqual(self.inventory.findItemByID(13), None)
        self.assertEqual(self.inventory.findItem(name="TestItem1"), self.item)
        self.assertEqual(self.inventory.findItem(name="RandomName1"), None)
        self.assertEqual(self.inventory.findItem(kind="carryable"), self.item)
        self.assertEqual(self.inventory.findItem(kind="weapon"), None)
Exemplo n.º 2
0
class CharacterBase(GameObject, Living, CharStats):
    """Base class for Characters"""

    def __init__(self, ID, agent_layer=None, inventory=None, text="", **kwargs):
        GameObject.__init__(self, ID, text=text, **kwargs)
        Living.__init__(self, **kwargs)
        CharStats.__init__(self, **kwargs)

        self.behaviour = None

        if inventory == None:
            self.inventory = Inventory()
        else:
            self.inventory = inventory

        self.state = _AGENT_STATE_NONE
        self.layer_id = agent_layer.getId()
        self.createBehaviour(agent_layer)

    def createBehaviour(self, layer):
        """Creates the behaviour for this actor.
           @return: None"""
        pass

    def setup(self):
        """@return: None"""
        self.behaviour.attachToLayer(self.ID)

    def start(self):
        """@return: None"""
        self.behaviour.idle()

    def teleport(self, location):
        """Teleports a Character instantly to the given location.
           @type location: fife.Location
           @param location: Target coordinates for Character.
           @return: None"""
        self.state = _AGENT_STATE_IDLE
        self.behaviour.nextAction = None
        self.behaviour.agent.setLocation(location)

    def give(self, item, actor):
        """Gives the specified item to the different actor. Raises an exception if the item was invalid or not found
           @type item: Carryable
           @param item: The item object to give
           @param actor: Person to give item to"""
        if item == None:
            raise ValueError("I don't have %s" % item.name)
        self.inventory.takeItem(item)
        actor.inventory.placeItem(item)

    def hasItem(self, item_type):
        """Returns wether an item is present in the players inventory or not
        @param item_type: ID of the item
        @type item_type: str
        @return: True when the item is present, False when not"""
        return self.inventory.findItem(item_type=item_type)

    def itemCount(self, item_type=""):
        """Returns number of all items or items specified by item_type 
        the player has.
        @param item_type: ID of the item, can be empty
        @type item_type: str
        @return: Number of items"""
        return self.inventory.count(item_type)

    def getLocation(self):
        """Get the NPC's position as a fife.Location object. Basically a
           wrapper.
           @rtype: fife.Location
           @return: the location of the NPC"""
        return self.behaviour.agent.getLocation()

    def run(self, location):
        """Makes the PC run to a certain location
           @type location: fife.ScreenPoint
           @param location: Screen position to run to.
           @return: None"""
        self.state = _AGENT_STATE_RUN
        self.behaviour.nextAction = None
        self.behaviour.agent.move("run", location, self.behaviour.speed + 1)

    def walk(self, location):
        """Makes the PC walk to a certain location.
           @type location: fife.ScreenPoint
           @param location: Screen position to walk to.
           @return: None"""
        self.state = _AGENT_STATE_RUN
        self.behaviour.nextAction = None
        self.behaviour.agent.move("walk", location, self.behaviour.speed - 1)

    def getStateForSaving(self):
        """Returns state for saving
        """
        ret_dict = GameObject.getStateForSaving(self)
        ret_dict["Inventory"] = self.inventory.serializeInventory()
        return ret_dict

    def _getCoords(self):
        """Get-er property function"""
        return (self.getLocation().getMapCoordinates().x, self.getLocation().getMapCoordinates().y)

    def _setCoords(self, coords):
        """Set-er property function"""
        map_coords = self.getLocation().getMapCoordinates()
        map_coords.X, map_coords.Y = float(coords[0]), float(coords[1])
        self.teleport(map_coords)

    coords = property(
        _getCoords,
        _setCoords,
        doc="Property allowing you to get and set the object's \
                coordinates via tuples",
    )