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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
447
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
1 import os
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2 import json
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3
248
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 225
diff changeset
4 from . import data
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 225
diff changeset
5 from .sprites.base import find_sprite
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14 def __getattr__(self, key):
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 except KeyError:
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
23
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
39
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
40 class GameState(object):
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
41
447
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
42 def __init__(self, game_file):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
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
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
45
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
46 def can_resume(self):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
47 return os.path.exists(self._game_file)
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
48
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
49 def load_game(self, game_file=None):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
50 if game_file is None:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
51 game_file = self._game_file
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
55
447
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
56 def new_game(self):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
57 self.load_game(data.filepath("game.json"))
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
58 self.save_game()
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
59
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
60 def save_game(self):
479
fb9258d66137 New Main Menu
Stefano Rivera <stefano@rivera.za.net>
parents: 454
diff changeset
61 if self.world is None:
fb9258d66137 New Main Menu
Stefano Rivera <stefano@rivera.za.net>
parents: 454
diff changeset
62 return
447
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
63 save_dir, _ = os.path.split(self._game_file)
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
64 if not os.path.exists(save_dir):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
65 try:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
66 os.makedirs(save_dir)
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
67 except:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
68 print "Cannot create save game directory."
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
69 return
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
70 try:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
71 json.dump(self.data, open(self._game_file, "wb"))
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
72 except:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
73 print "Cannot create save game file."
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
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
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
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')