Mercurial > skaapsteker
annotate skaapsteker/gamestate.py @ 612:a91b2e4400a5
Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 11 Apr 2011 23:54:56 +0200 |
parents | aa01ca54dce2 |
children | 0675f390653c |
rev | line source |
---|---|
612
a91b2e4400a5
Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).
Simon Cross <hodgestar@gmail.com>
parents:
558
diff
changeset
|
1 try: |
a91b2e4400a5
Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).
Simon Cross <hodgestar@gmail.com>
parents:
558
diff
changeset
|
2 import json |
a91b2e4400a5
Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).
Simon Cross <hodgestar@gmail.com>
parents:
558
diff
changeset
|
3 except: |
a91b2e4400a5
Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).
Simon Cross <hodgestar@gmail.com>
parents:
558
diff
changeset
|
4 import simplejson as json |
a91b2e4400a5
Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).
Simon Cross <hodgestar@gmail.com>
parents:
558
diff
changeset
|
5 |
447 | 6 import os |
127 | 7 |
248 | 8 from . import data |
531
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
9 from . import options |
248 | 10 from .sprites.base import find_sprite |
127 | 11 |
12 | |
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 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
|
14 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 |
127 | 19 def __getattr__(self, key): |
20 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
|
21 value = self._data[key] |
127 | 22 except KeyError: |
23 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
|
24 if isinstance(value, dict): |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
25 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
|
26 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
|
27 return value |
127 | 28 |
29 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
|
30 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
|
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 __iter__(self): |
225
84b6afff51fe
Fix __iter__ for StateProxy.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
33 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
|
34 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
35 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
|
36 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
|
37 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
127
diff
changeset
|
38 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
|
39 return self._data.copy() |
127 | 40 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
41 def gamestate(self): |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
42 return self._gamestate |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
43 |
127 | 44 |
45 class GameState(object): | |
46 | |
447 | 47 def __init__(self, game_file): |
48 self._game_file = game_file | |
454
95527fd29872
More complete game load/save/restart.
Jeremy Thurgood <firxen@gmail.com>
parents:
447
diff
changeset
|
49 self.world = None |
447 | 50 |
51 def can_resume(self): | |
52 return os.path.exists(self._game_file) | |
53 | |
54 def load_game(self, game_file=None): | |
55 if game_file is None: | |
56 game_file = self._game_file | |
57 raw_data = open(game_file, "rb").read() | |
483
b48c194c4af9
Some haiku and encoding fixes.
Jeremy Thurgood <firxen@gmail.com>
parents:
479
diff
changeset
|
58 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
|
59 self.world = StateProxy(self.data, self) |
127 | 60 |
447 | 61 def new_game(self): |
62 self.load_game(data.filepath("game.json")) | |
531
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
63 if options['all_tails']: |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
64 self.data['fox']['tails'] += [ |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
65 "fireball", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
66 "flight", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
67 "invisibility", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
68 "lightning", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
69 "shapeshift", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
70 "shield", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
71 "sprint", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
72 "steal", |
5f1058593462
Remove all tails by default
Stefano Rivera <stefano@rivera.za.net>
parents:
493
diff
changeset
|
73 ] |
447 | 74 self.save_game() |
75 | |
76 def save_game(self): | |
479 | 77 if self.world is None: |
78 return | |
447 | 79 save_dir, _ = os.path.split(self._game_file) |
80 if not os.path.exists(save_dir): | |
81 try: | |
82 os.makedirs(save_dir) | |
83 except: | |
84 print "Cannot create save game directory." | |
85 return | |
86 try: | |
554
51bc11e3b693
Pretty-print save game JSON.
Jeremy Thurgood <firxen@gmail.com>
parents:
531
diff
changeset
|
87 json.dump(self.data, open(self._game_file, "wb"), indent=4) |
447 | 88 except: |
89 print "Cannot create save game file." | |
127 | 90 |
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
|
91 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
|
92 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
|
93 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
|
94 ('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
|
95 ('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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 return sprites |
447 | 103 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
104 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
|
105 itemdef = self.data['items'][item] |
447 | 106 |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
107 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
|
108 itemdef['level'] = to_level |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
109 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
|
110 itemdef['pos'] = to_pos |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
111 |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
112 sprite_dict = itemdef.copy() |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
113 sprite_dict.pop('level') |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
114 sprite_dict['name'] = item |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
115 sprite_dict['world'] = self.world |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
483
diff
changeset
|
116 return find_sprite(sprite_dict, 'items') |
558
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
117 |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
118 def create_npc_sprite(self, npc, to_level=None, to_pos=None): |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
119 npcdef = self.data['npcs'][npc] |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
120 |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
121 if to_level is not None: |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
122 npcdef['level'] = to_level |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
123 if to_pos is not None: |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
124 npcdef['pos'] = to_pos |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
125 |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
126 sprite_dict = npcdef.copy() |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
127 sprite_dict.pop('level') |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
128 sprite_dict['name'] = npc |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
129 sprite_dict['world'] = self.world |
aa01ca54dce2
Add function for warping in NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
554
diff
changeset
|
130 return find_sprite(sprite_dict, 'npcs') |