Mercurial > skaapsteker
annotate skaapsteker/gamestate.py @ 493:cb2060f8316f
Tweak gamestate to fail earlier on unknown items.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 22:41:38 +0200 |
parents | 0eade58a71b9 |
children | 5f1058593462 |
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 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
10 def __init__(self, data, gamestate): |
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
|
11 self.__dict__['_data'] = data # should be a dict |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
12 self.__dict__['_gamestate'] = gamestate # use sparingly |
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
|
13 |
127 | 14 def __getattr__(self, key): |
15 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
|
16 value = self._data[key] |
127 | 17 except KeyError: |
18 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
|
19 if isinstance(value, dict): |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
20 return StateProxy(value, None) # surprise people |
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
|
21 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
|
22 return value |
127 | 23 |
24 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
|
25 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
|
26 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
27 def __iter__(self): |
225
84b6afff51fe
Fix __iter__ for StateProxy.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
28 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
|
29 |
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 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
|
31 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
|
32 |
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 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
|
34 return self._data.copy() |
127 | 35 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
36 def gamestate(self): |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
37 return self._gamestate |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
38 |
127 | 39 |
40 class GameState(object): | |
41 | |
447 | 42 def __init__(self, game_file): |
43 self._game_file = game_file | |
454
95527fd29872
More complete game load/save/restart.
Jeremy Thurgood <firxen@gmail.com>
parents:
447
diff
changeset
|
44 self.world = None |
447 | 45 |
46 def can_resume(self): | |
47 return os.path.exists(self._game_file) | |
48 | |
49 def load_game(self, game_file=None): | |
50 if game_file is None: | |
51 game_file = self._game_file | |
52 raw_data = open(game_file, "rb").read() | |
483
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
479
diff
changeset
|
53 self.data = json.loads(raw_data, encoding='utf-8') |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
54 self.world = StateProxy(self.data, self) |
127 | 55 |
447 | 56 def new_game(self): |
57 self.load_game(data.filepath("game.json")) | |
58 self.save_game() | |
59 | |
60 def save_game(self): | |
479 | 61 if self.world is None: |
62 return | |
447 | 63 save_dir, _ = os.path.split(self._game_file) |
64 if not os.path.exists(save_dir): | |
65 try: | |
66 os.makedirs(save_dir) | |
67 except: | |
68 print "Cannot create save game directory." | |
69 return | |
70 try: | |
71 json.dump(self.data, open(self._game_file, "wb")) | |
72 except: | |
73 print "Cannot create save game file." | |
127 | 74 |
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 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
|
76 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
|
77 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
|
78 ('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
|
79 ('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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 return sprites |
447 | 87 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
88 def create_item_sprite(self, item, to_level=None, to_pos=None): |
493
cb2060f8316f
Tweak gamestate to fail earlier on unknown items.
Simon Cross <hodgestar@gmail.com>
parents:
490
diff
changeset
|
89 itemdef = self.data['items'][item] |
447 | 90 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
91 if to_level is not None: |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
92 itemdef['level'] = to_level |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
93 if to_pos is not None: |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
94 itemdef['pos'] = to_pos |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
95 |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
96 sprite_dict = itemdef.copy() |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
97 sprite_dict.pop('level') |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
98 sprite_dict['name'] = item |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
99 sprite_dict['world'] = self.world |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
100 return find_sprite(sprite_dict, 'items') |