comparison gamelib/gameboard.py @ 444:feb9b7a23ef2

Support for saving and loading games (toolbar entries commented out for lack of space).
author Simon Cross <hodgestar@gmail.com>
date Sat, 21 Nov 2009 19:08:22 +0000
parents f2a55e5e24db
children f04a2490c35f
comparison
equal deleted inserted replaced
443:4efe57fcc1d7 444:feb9b7a23ef2
13 import equipment 13 import equipment
14 import sound 14 import sound
15 import cursors 15 import cursors
16 import sprite_cursor 16 import sprite_cursor
17 import misc 17 import misc
18 import engine
19 import toolbar 18 import toolbar
19 import serializer
20 20
21 class VidWidget(gui.Widget): 21 class VidWidget(gui.Widget):
22 def __init__(self, gameboard, vid, **params): 22 def __init__(self, gameboard, vid, **params):
23 gui.Widget.__init__(self, **params) 23 gui.Widget.__init__(self, **params)
24 self.gameboard = gameboard 24 self.gameboard = gameboard
38 if e.type == MOUSEBUTTONDOWN: 38 if e.type == MOUSEBUTTONDOWN:
39 self.gameboard.use_tool(e) 39 self.gameboard.use_tool(e)
40 elif e.type == MOUSEMOTION and self.gameboard.sprite_cursor: 40 elif e.type == MOUSEMOTION and self.gameboard.sprite_cursor:
41 self.gameboard.update_sprite_cursor(e) 41 self.gameboard.update_sprite_cursor(e)
42 42
43 class GameBoard(object): 43 class GameBoard(serializer.Simplifiable):
44 44
45 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] 45 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
46 FENCE = tiles.REVERSE_TILE_MAP['fence'] 46 FENCE = tiles.REVERSE_TILE_MAP['fence']
47 WOODLAND = tiles.REVERSE_TILE_MAP['woodland'] 47 WOODLAND = tiles.REVERSE_TILE_MAP['woodland']
48 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence'] 48 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
49
50 SIMPLIFY = [
51 'chickens',
52 'buildings',
53 'foxes',
54 'cash',
55 'wood',
56 'eggs',
57 'days',
58 'killed_foxes',
59 'day', 'night',
60 ]
49 61
50 def __init__(self, main_app, level): 62 def __init__(self, main_app, level):
51 self.disp = main_app 63 self.disp = main_app
52 self.level = level 64 self.level = level
53 self.tv = tiles.FarmVid() 65 self.tv = tiles.FarmVid()
60 self.selected_tool = None 72 self.selected_tool = None
61 self.animal_to_place = None 73 self.animal_to_place = None
62 self.sprite_cursor = None 74 self.sprite_cursor = None
63 self.chickens = set() 75 self.chickens = set()
64 self.foxes = set() 76 self.foxes = set()
65 self.buildings = [] 77 self.buildings = set()
66 self._pos_cache = { 'fox' : [], 'chicken' : []} 78 self._pos_cache = { 'fox' : [], 'chicken' : []}
67 self.cash = 0 79 self.cash = 0
68 self.wood = 0 80 self.wood = 0
69 self.eggs = 0 81 self.eggs = 0
70 self.days = 0 82 self.days = 0
566 578
567 def clear_chickens(self): 579 def clear_chickens(self):
568 for chicken in self.chickens.copy(): 580 for chicken in self.chickens.copy():
569 self.remove_chicken(chicken) 581 self.remove_chicken(chicken)
570 582
583 def clear_buildings(self):
584 for building in self.buildings.copy():
585 self.remove_building(building)
586
571 def do_night_step(self): 587 def do_night_step(self):
572 """Handle the events of the night. 588 """Handle the events of the night.
573 589
574 We return True if there are no more foxes to move or all the 590 We return True if there are no more foxes to move or all the
575 foxes are safely back. This end's the night""" 591 foxes are safely back. This end's the night"""
655 def add_fox(self, fox): 671 def add_fox(self, fox):
656 self.foxes.add(fox) 672 self.foxes.add(fox)
657 self.tv.sprites.append(fox) 673 self.tv.sprites.append(fox)
658 674
659 def add_building(self, building): 675 def add_building(self, building):
660 self.buildings.append(building) 676 self.buildings.add(building)
661 self.tv.sprites.append(building, layer='buildings') 677 self.tv.sprites.append(building, layer='buildings')
662 678
663 def place_hatched_chicken(self, new_chick, building): 679 def place_hatched_chicken(self, new_chick, building):
664 try: 680 try:
665 building.add_occupant(new_chick) 681 building.add_occupant(new_chick)
705 self.tv.sprites.remove(chick) 721 self.tv.sprites.remove(chick)
706 self._update_pos_cache(chick.pos, None, 'chicken') 722 self._update_pos_cache(chick.pos, None, 'chicken')
707 723
708 def remove_building(self, building): 724 def remove_building(self, building):
709 if building in self.buildings: 725 if building in self.buildings:
710 self.buildings.remove(building) 726 self.buildings.discard(building)
711 self.tv.sprites.remove(building, layer='buildings') 727 self.tv.sprites.remove(building, layer='buildings')
712 728
713 def add_cash(self, amount): 729 def add_cash(self, amount):
714 self.cash += amount 730 self.cash += amount
715 self.toolbar.update_cash_counter(self.cash) 731 self.toolbar.update_cash_counter(self.cash)
793 809
794 def trees_left(self): 810 def trees_left(self):
795 width, height = self.tv.size 811 width, height = self.tv.size
796 return len([(x,y) for x in range(width) for y in range(height) if self.tv.get((x,y)) == self.WOODLAND]) 812 return len([(x,y) for x in range(width) for y in range(height) if self.tv.get((x,y)) == self.WOODLAND])
797 813
814 def save_game(self):
815 return serializer.simplify(self)
816
817 def restore_game(self, data):
818 if 'refid' not in data or 'class' not in data or data['class'] != self.__class__.__name__:
819 import pprint
820 pprint.pprint(data)
821 print self.__class__.__name__
822 raise ValueError("Invalid save game.")
823 newself = serializer.unsimplify(data)
824 self.clear_chickens()
825 self.clear_buildings()
826 for chicken in newself.chickens:
827 self.add_chicken(chicken)
828 for building in newself.buildings:
829 self.add_building(building)
830 for attr in self.SIMPLIFY:
831 if attr in ('chickens', 'buildings'):
832 continue
833 setattr(self, attr, getattr(newself, attr))
834
798 835
799 class TextDialog(gui.Dialog): 836 class TextDialog(gui.Dialog):
800 def __init__(self, title, text, **params): 837 def __init__(self, title, text, **params):
801 title_label = gui.Label(title) 838 title_label = gui.Label(title)
802 839