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
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
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 def __getattr__(self, key):
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16 except KeyError:
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
22
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
34
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
35
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
36 class GameState(object):
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
37
447
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
38 def __init__(self, game_file):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
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
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
41
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
42 def can_resume(self):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
43 return os.path.exists(self._game_file)
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
44
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
45 def load_game(self, game_file=None):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
46 if game_file is None:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
47 game_file = self._game_file
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
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
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
51
447
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
52 def new_game(self):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
53 self.load_game(data.filepath("game.json"))
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
54 self.save_game()
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
55
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
56 def save_game(self):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
57 save_dir, _ = os.path.split(self._game_file)
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
58 if not os.path.exists(save_dir):
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
59 try:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
60 os.makedirs(save_dir)
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
61 except:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
62 print "Cannot create save game directory."
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
63 return
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
64 try:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
65 json.dump(self.data, open(self._game_file, "wb"))
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
66 except:
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
67 print "Cannot create save game file."
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
81
4bec05fed6c7 Load and save.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
82