source: skaapsteker/gamestate.py

Last change on this file was 612:a91b2e4400a5, checked in by Simon Cross <hodgestar@…>, 12 years ago

Fallback to using simplejson if json does not exist (this appears to be all that is needed to add Python2.5 compatibility).

File size: 3.6 KB
Line 
1try:
2 import json
3except:
4 import simplejson as json
5
6import os
7
8from . import data
9from . import options
10from .sprites.base import find_sprite
11
12
13class StateProxy(object):
14
15 def __init__(self, data, gamestate):
16 self.__dict__['_data'] = data # should be a dict
17 self.__dict__['_gamestate'] = gamestate # use sparingly
18
19 def __getattr__(self, key):
20 try:
21 value = self._data[key]
22 except KeyError:
23 raise AttributeError
24 if isinstance(value, dict):
25 return StateProxy(value, None) # surprise people
26 else:
27 return value
28
29 def __setattr__(self, key, value):
30 self._data[key] = value
31
32 def __iter__(self):
33 return self._data.iterkeys()
34
35 def __contains__(self, key):
36 return key in self._data
37
38 def copy(self):
39 return self._data.copy()
40
41 def gamestate(self):
42 return self._gamestate
43
44
45class GameState(object):
46
47 def __init__(self, game_file):
48 self._game_file = game_file
49 self.world = None
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()
58 self.data = json.loads(raw_data, encoding='utf-8')
59 self.world = StateProxy(self.data, self)
60
61 def new_game(self):
62 self.load_game(data.filepath("game.json"))
63 if options['all_tails']:
64 self.data['fox']['tails'] += [
65 "fireball",
66 "flight",
67 "invisibility",
68 "lightning",
69 "shapeshift",
70 "shield",
71 "sprint",
72 "steal",
73 ]
74 self.save_game()
75
76 def save_game(self):
77 if self.world is None:
78 return
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:
87 json.dump(self.data, open(self._game_file, "wb"), indent=4)
88 except:
89 print "Cannot create save game file."
90
91 def create_sprites(self, level):
92 sprites = []
93 for stype, key in [
94 ('items', 'items'),
95 ('npcs', 'npcs')]:
96 for sprite_name, sprite_dict in self.data[key].items():
97 sprite_dict = sprite_dict.copy()
98 sprite_dict['name'] = sprite_name
99 sprite_dict['world'] = self.world
100 if sprite_dict.pop('level') == level:
101 sprites.append(find_sprite(sprite_dict, stype))
102 return sprites
103
104 def create_item_sprite(self, item, to_level=None, to_pos=None):
105 itemdef = self.data['items'][item]
106
107 if to_level is not None:
108 itemdef['level'] = to_level
109 if to_pos is not None:
110 itemdef['pos'] = to_pos
111
112 sprite_dict = itemdef.copy()
113 sprite_dict.pop('level')
114 sprite_dict['name'] = item
115 sprite_dict['world'] = self.world
116 return find_sprite(sprite_dict, 'items')
117
118 def create_npc_sprite(self, npc, to_level=None, to_pos=None):
119 npcdef = self.data['npcs'][npc]
120
121 if to_level is not None:
122 npcdef['level'] = to_level
123 if to_pos is not None:
124 npcdef['pos'] = to_pos
125
126 sprite_dict = npcdef.copy()
127 sprite_dict.pop('level')
128 sprite_dict['name'] = npc
129 sprite_dict['world'] = self.world
130 return find_sprite(sprite_dict, 'npcs')
Note: See TracBrowser for help on using the repository browser.