Changeset 540


Ignore:
Timestamp:
05/07/10 20:14:52 (10 years ago)
Author:
beliar
Message:

Inventories can now be set in the map file
Items values read from the object database (objects/ObjectDatabase.yaml)
Again, values set in the map override those in the database.
Sample inventory for PC added to map.xml (same as that in init of PlayerCharacter?)

For now only the CarryableItem? type is supported. Once we decide how to implement items that do more than just sit in the inventory we need to change it.

Location:
trunk/game
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/local_loaders/xmlmap.py

    r530 r540  
    2121 
    2222from scripts.common.utils import parseBool 
    23  
    2423from fife import fife  
    2524try: 
     
    3130from fife.extensions import serializers 
    3231import time 
     32 
    3333 
    3434FORMAT = '1.0' 
     
    282282 
    283283    def parseInstances(self, layer_elt, layer): 
     284        from scripts.inventory import Inventory 
    284285        inst_elt = layer_elt.find('instances') 
    285286 
     
    344345                id = '' 
    345346            else: 
    346                 id = str(id) 
    347  
     347                id = str(id)             
     348             
    348349            inst = layer.createInstance(object, \ 
    349350                                        fife.ExactModelCoordinate(x,y,z), \ 
     
    392393                inst_dict["target_y"] = instance.get('target_y') 
    393394 
    394                 self.data.createObject(layer, inst_dict, inst) 
     395                inventory_el = instance.find("inventory") or \ 
     396                    instance.find("inv")                 
     397                if inventory_el is not None: 
     398                    inventory = Inventory() 
     399                    inv_objects = inventory_el.findall("object") 
     400                    for inv_object in inv_objects: 
     401                        attributes = inv_object.attrib.copy() 
     402                        self.data.createInventoryObject(inventory, attributes) 
     403                    inst_dict["inventory"] = inventory 
     404                self.data.createMapObject(layer, inst_dict, inst) 
    395405                 
    396406    def parseCameras(self, map_elt, map): 
  • trunk/game/maps/map.xml

    r539 r540  
    53555355                        <i id="crate02" is_open="False" locked="False" name="A dirty old crate" o="crate" object_type="WoodenCrate" r="0" target_map="None" target_map_name="None" target_x="None" target_y="None" text="A crate that is covered in grime and dirt. There doesn&apos;t seem to be anything remarkable about it" x="-2.0" y="-11.0" z="0.0" /> 
    53565356                        <i o="guard_tower" r="315" x="4.0" y="-17.0" z="0.0" /> 
    5357                         <i id="PC" is_open="None" locked="None" name="None" o="player" object_type="PlayerCharacter" r="0" target_map="None" target_map_name="None" target_x="None" target_y="None" text="None" x="2.0" y="-8.0" z="0.0" /> 
     5357                        <i id="PC" is_open="None" locked="None" name="None" o="player" object_type="PlayerCharacter" r="0" target_map="None" target_map_name="None" target_x="None" target_y="None" text="None" x="2.0" y="-8.0" z="0.0" >  
     5358                                <inventory> 
     5359                                        <object id="456" object_type="Dagger123" /> 
     5360                                        <object id="555" object_type="Beer" /> 
     5361                                        <object id="616" object_type="Pamphlet" /> 
     5362                                </inventory> 
     5363                        </i> 
    53585364                        <i id="shanty-door01" is_open="False" locked="None" o="shanty-door" object_type="ShantyDoor" r="0" target_map="maps/map2.xml" target_map_name="map2" target_x="10.0" target_y="10.0" x="-6.0" y="-13.0" z="0.0" /> 
    53595365                        <i o="container_house01" r="0" x="-1.0" y="2.0" z="0.0" /> 
  • trunk/game/objects/ObjectDatabase.yaml

    r539 r540  
    1313  text: "Looks like the entrance to the building." 
    1414  gfx: "shanty-door" 
     15 
     16Dagger123: 
     17  name: "Dagger123" 
     18 
     19Beer: 
     20  name: "Beer" 
     21 
     22Pamphlet: 
     23  name: "Pamphlet" 
     24  image: "/gui/inv_images/inv_pamphlet.png" 
     25   
  • trunk/game/scripts/engine.py

    r538 r540  
    105105        # In most maps we'll create the PC Instance internally. In these 
    106106        # cases we need a target position 
    107              
    108     def createObject (self, layer, attributes, instance): 
     107         
     108 
     109    def checkAttributes(self, attributes): 
     110        """Checks for attributes that where not given in the map file 
     111        and fills them with values from the object database 
     112        @param attributes: attributes to check 
     113        @type attributes: Dictionary 
     114        @return: The modified attributes"""         
     115        if attributes.has_key("object_type"): 
     116            class_name = attributes.pop("object_type") 
     117        else: 
     118            class_name = attributes["type"] 
     119        if self.object_db.has_key(class_name): 
     120            db_attributes = self.object_db[class_name].copy() 
     121            for key in db_attributes.keys(): 
     122                if attributes.has_key(key): 
     123                    attributes[key] = attributes[key] or db_attributes[key] 
     124                else: 
     125                    attributes[key] = db_attributes[key] 
     126        return attributes 
     127     
     128    def createInventoryObject(self, container, attributes): 
     129        """Create an inventory object and place it into a container 
     130           @type container: base.Container 
     131           @param container: Container where the item is on 
     132           @type attributes: Dictionary 
     133           @param attributes: Dictionary of all object attributes 
     134           @return: None""" 
     135        # create the extra data 
     136        extra = {} 
     137        extra['engine'] = self 
     138        attributes = self.checkAttributes(attributes) 
     139         
     140        """TODO: Change when we use other and or more classes  
     141        for inventory objects""" 
     142        from objects.composed import CarryableItem 
     143        info = {} 
     144        info.update(attributes) 
     145        info.update(extra) 
     146        ID = info.pop("id") 
     147         
     148        obj = CarryableItem(ID = ID, **info) 
     149        #obj = createObject(attributes, extra) 
     150        container.placeItem(obj) 
     151         
     152    def createMapObject (self, layer, attributes, instance): 
    109153        """Create an object and add it to the current map. 
    110154           @type layer: fife.Layer 
     
    117161        # create the extra data 
    118162        extra = {} 
    119         extra['agent_layer'] = layer 
     163        if layer is not None: 
     164            extra['agent_layer'] = layer 
    120165        extra['engine'] = self 
    121         class_name = attributes["type"] 
    122         if self.object_db.has_key(class_name): 
    123             db_attributes = self.object_db[class_name].copy() 
    124             for key in db_attributes.keys(): 
    125                 attributes[key] = attributes[key] or db_attributes[key] 
     166        attributes = self.checkAttributes(attributes) 
    126167         
    127168        obj = createObject(attributes, extra) 
Note: See TracChangeset for help on using the changeset viewer.