Changeset 373:8c7c5db5f0d3


Ignore:
Timestamp:
Sep 6, 2013, 8:33:51 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Message:

Change the flavour of magic in World

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/world.py

    r346 r373  
    1919
    2020    def reset(self):
    21         self.attacks = 0
    22         self.transformations = 0
    23         self.kills = 0
    24         self.rooms = 0
    2521        first_level = Level.list_levels()[0]
    2622        starting_position = (350, 300)
    27         self.protagonist = Protagonist(pymunk.Space(), self, starting_position)
    28         self.level = (first_level, starting_position)
    29         self.level_state = {}
     23        self.__dict__['_data'] = {
     24            'attacks': 0,
     25            'transformations': 0,
     26            'kills': 0,
     27            'rooms': 0,
     28            'protagonist': Protagonist(pymunk.Space(), self,
     29                                       starting_position),
     30            'level': (first_level, starting_position),
     31            'level_state': {},
     32        }
     33
     34    def __getattr__(self, name):
     35        try:
     36            return self._data[name]
     37        except KeyError:
     38            raise AttributeError()
     39
     40    def __setattr__(self, name, value):
     41        if name not in self._data:
     42            raise AttributeError("Worlds don't have a %s property" % name)
     43        self._data[name] = value
    3044
    3145    def _save_location(self):
     
    4054
    4155    def save(self):
    42         data = {}
    43         for attr in dir(self):
    44             if attr.startswith('_'):
    45                 continue
    46             value = getattr(self, attr)
    47             if hasattr(value, '__name__'):
    48                 continue
    49             # Hack until we save protagonist state
    50             if hasattr(value, 'update') and not isinstance(value, dict):
    51                 continue
    52             data[attr] = value
    53 
     56        data = self._data.copy()
     57        data['inventory'] = sorted(data.pop('protagonist').inventory)
    5458        fn = self._save_location()
    5559        if not os.path.isdir(os.path.dirname(fn)):
     
    6468        with open(fn) as f:
    6569            data = load(f)
     70        self.protagonist.inventory = set(data.pop('inventory', []))
    6671        for k, v in data.iteritems():
    6772            setattr(self, k, v)
Note: See TracChangeset for help on using the changeset viewer.