source: nagslang/world.py@ 276:3153196517fc

Last change on this file since 276:3153196517fc was 276:3153196517fc, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Move protagonist to the world

File size: 2.0 KB
RevLine 
[238]1# The world object
2#
3# This is a global object for tracking state across scenes and all that
4
[252]5import os
6import sys
7
[276]8import pymunk
9
[252]10from nagslang.yamlish import dump, load
[276]11from nagslang.protagonist import Protagonist
[252]12
[238]13
14class World(object):
15
16 def __init__(self):
[252]17 self.reset()
18
19 def reset(self):
[261]20 self.attacks = 0
[238]21 self.transformations = 0
22 self.kills = 0
23 self.rooms = 0
[276]24 self.protagonist = Protagonist(pymunk.Space(), (350, 300))
[238]25
[252]26 def _save_location(self):
27 app = 'nagslang'
28 if sys.platform.startswith('win'):
29 if 'APPDATA' in os.environ:
30 return os.path.join(os.environ['APPDATA'], app)
31 return os.path.join(os.path.expanduser('~'), '.' + app)
32 elif 'XDG_DATA_HOME' in os.environ:
33 return os.path.join(os.environ['XDG_DATA_HOME'], app)
34 return os.path.join(os.path.expanduser('~'), '.local', 'share', app)
35
[238]36 def save(self):
[252]37 data = {}
38 for attr in dir(self):
39 if attr.startswith('_'):
40 continue
41 value = getattr(self, attr)
42 if hasattr(value, '__name__'):
43 continue
[276]44 # Hack until we save protagonist state
45 if hasattr(value, 'animate'):
46 continue
[252]47 data[attr] = value
48
49 fn = self._save_location()
50 if not os.path.isdir(os.path.dirname(fn)):
51 os.makedirs(os.path.dirname(fn))
52 with open(fn, 'w') as f:
53 dump(data, f)
[238]54
55 def load(self):
[252]56 fn = self._save_location()
57 if not os.path.exists(fn):
58 return False
59 with open(fn) as f:
60 data = load(f)
61 for k, v in data.iteritems():
62 setattr(self, k, v)
63 return True
[238]64
65 def get_formatted_stats(self):
66 return "\n".join([
67 "Times transformed: %d" % self.transformations,
68 "Enemies killed: %d" % self.kills,
69 "Rooms entered: %d" % self.rooms
[252]70 ])
Note: See TracBrowser for help on using the repository browser.