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)
Ejemplo n.º 2
0
 def __init__(self, ID, agent_layer=None, inventory=None, text="Its you. Who would've thought that?", **kwargs):
     if inventory == None:
         inventory = Inventory()
         inventory.placeItem(CarryableItem(ID=456, name="Dagger123"))
         inventory.placeItem(CarryableItem(ID=555, name="Beer"))
         inventory.placeItem(CarryableItem(ID=616, name="Pamphlet", image="/gui/inv_images/inv_pamphlet.png"))
     CharacterBase.__init__(self, ID, agent_layer, inventory, text, **kwargs)
     self.people_i_know = set()
     self.attributes.append("PC")
Ejemplo n.º 3
0
Archivo: actors.py Proyecto: m64/PEG
class PlayerCharacter (GameObject, Living, CharStats):
    """
    PC class
    """
    def __init__ (self, ID, agent_layer = None, inventory = None, **kwargs):
        GameObject.__init__( self, ID, **kwargs )
        Living.__init__( self, **kwargs )
        CharStats.__init__( self, **kwargs )

        self.is_PC = True
        
        # PC _has_ an inventory, he _is not_ one
        if inventory == None:
            self.inventory = Inventory()
            self.inventory.placeItem(CarryableItem(ID=456, name="Dagger123"))
            self.inventory.placeItem(CarryableItem(ID=555, name="Beer"))
        else:
            self.inventory = inventory
        self.peopleIknow = set()

        self.state = _AGENT_STATE_NONE
        self.layer_id = agent_layer.getId()
        self.createBehaviour(agent_layer)
    
    def meet(self, npc):
        """Record that the PC has met a certain NPC
        @type npc: str
        @param npc: The NPC's name or id"""
        if npc in self.peopleIknow:
            raise RuntimeError("I already know %s" % npc)
        self.peopleIknow.add(npc)

    def met(self, npc):
        """Indicate whether the PC has met this npc before
        @type npc: str
        @param npc: The NPC's name or id
        @return: None"""
        return npc in self.peopleIknow

    def createBehaviour(self, layer):
        """Creates the behaviour for this actor.
            @return None """
        self.behaviour = PCBehaviour(self, layer)
    
    def setup(self):
        """@return: None"""
        self.behaviour.attachToLayer(self.ID)

    def start(self):
        """@return: None"""
        self.behaviour.idle()
    
    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 teleport(self, location):
        """Teleports a PC instantly to the given location.
           @type location: fife.Location
           @param location: Target coordinates for PC.
           @return: None"""
        self.state = _AGENT_STATE_IDLE
        self.behaviour.nextAction = None 
        self.behaviour.agent.setLocation(location)

    def approach(self, location, action = None):
        """Approaches a location and then perform an action (if set).
           @type loc: fife.Location
           @param loc: the location to approach
           @type action: Action
           @param action: The action to schedule for execution after the approach.
           @return: None"""
        self.state = _AGENT_STATE_APPROACH
        self.behaviour.nextAction = action
        boxLocation = tuple([int(float(i)) for i in location])
        l = fife.Location(self.behaviour.agent.getLocation())
        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
        self.behaviour.agent.move('run', l, self.behaviour.speed+1)