Mercurial > skaapsteker
annotate skaapsteker/gamestate.py @ 454:95527fd29872
More complete game load/save/restart.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 20:47:35 +0200 |
parents | 4bec05fed6c7 |
children | fb9258d66137 |
rev | line source |
---|---|
447 | 1 import os |
127 | 2 import json |
3 | |
248 | 4 from . import data |
5 from .sprites.base import find_sprite | |
127 | 6 |
7 | |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
8 class StateProxy(object): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
9 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
10 def __init__(self, data): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
11 self.__dict__['_data'] = data # should be a dict |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
12 |
127 | 13 def __getattr__(self, key): |
14 try: | |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
15 value = self._data[key] |
127 | 16 except KeyError: |
17 raise AttributeError | |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
18 if isinstance(value, dict): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
19 return StateProxy(value) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
20 else: |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
21 return value |
127 | 22 |
23 def __setattr__(self, key, value): | |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
24 self._data[key] = value |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
25 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
26 def __iter__(self): |
225
84b6afff51fe
Fix __iter__ for StateProxy.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
27 return self._data.iterkeys() |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
28 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
29 def __contains__(self, key): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
30 return key in self._data |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
31 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
32 def copy(self): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
33 return self._data.copy() |
127 | 34 |
35 | |
36 class GameState(object): | |
37 | |
447 | 38 def __init__(self, game_file): |
39 self._game_file = game_file | |
454
95527fd29872
More complete game load/save/restart.
Jeremy Thurgood <firxen@gmail.com>
parents:
447
diff
changeset
|
40 self.world = None |
447 | 41 |
42 def can_resume(self): | |
43 return os.path.exists(self._game_file) | |
44 | |
45 def load_game(self, game_file=None): | |
46 if game_file is None: | |
47 game_file = self._game_file | |
48 raw_data = open(game_file, "rb").read() | |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
49 self.data = json.loads(raw_data) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
50 self.world = StateProxy(self.data) |
127 | 51 |
447 | 52 def new_game(self): |
53 self.load_game(data.filepath("game.json")) | |
54 self.save_game() | |
55 | |
56 def save_game(self): | |
57 save_dir, _ = os.path.split(self._game_file) | |
58 if not os.path.exists(save_dir): | |
59 try: | |
60 os.makedirs(save_dir) | |
61 except: | |
62 print "Cannot create save game directory." | |
63 return | |
64 try: | |
65 json.dump(self.data, open(self._game_file, "wb")) | |
66 except: | |
67 print "Cannot create save game file." | |
127 | 68 |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
69 def create_sprites(self, level): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
70 sprites = [] |
193
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
189
diff
changeset
|
71 for stype, key in [ |
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
189
diff
changeset
|
72 ('items', 'items'), |
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
189
diff
changeset
|
73 ('npcs', 'npcs')]: |
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
189
diff
changeset
|
74 for sprite_name, sprite_dict in self.data[key].items(): |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
75 sprite_dict = sprite_dict.copy() |
193
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
189
diff
changeset
|
76 sprite_dict['name'] = sprite_name |
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
189
diff
changeset
|
77 sprite_dict['world'] = self.world |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
78 if sprite_dict.pop('level') == level: |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
79 sprites.append(find_sprite(sprite_dict, stype)) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
80 return sprites |
447 | 81 |
82 |