source: nagslang/level.py@ 379:e2cebabf87e8

Last change on this file since 379:e2cebabf87e8 was 379:e2cebabf87e8, checked in by Stefano Rivera <stefano@…>, 8 years ago

Collected collectables stay collected

File size: 7.9 KB
Line 
1import pygame
2import pygame.locals as pgl
3
4from nagslang import collectable
5from nagslang import game_object as go
6from nagslang import enemies
7from nagslang import puzzle
8from nagslang.utils import tile_surface
9from nagslang.resources import resources
10from nagslang.yamlish import load, dump
11
12POLY_COLORS = {
13 1: pygame.color.THECOLORS['red'],
14 2: pygame.color.THECOLORS['green'],
15 3: pygame.color.THECOLORS['yellow'],
16 4: pygame.color.THECOLORS['blue'],
17 5: pygame.color.THECOLORS['lightblue'],
18 6: pygame.color.THECOLORS['magenta'],
19}
20
21
22LINE_COLOR = pygame.color.THECOLORS['orange']
23
24
25class Level(object):
26
27 def __init__(self, name, world):
28 self.name = name
29 # defaults
30 self.x = 800
31 self.y = 600
32 self.polygons = {}
33 self.lines = []
34 self.world = world
35 self.world.level_state.setdefault(name, {})
36 self.basetile = 'tiles/floor.png'
37 self._tile_image = None
38 self._surface = None
39 self._exterior = False
40 self._glue = puzzle.PuzzleGlue()
41 self.drawables = []
42 self.overlay_drawables = []
43 self._game_objects = []
44 self._enemies = []
45
46 def _get_data(self):
47 # For overriding in tests.
48 with resources.get_file('levels', self.name) as f:
49 return load(f)
50
51 def _dump_data(self, f):
52 # For manipulation in tests.
53 dump({
54 'size': [self.x, self.y],
55 'base_tile': self.basetile,
56 'polygons': self.polygons,
57 'lines': self.lines,
58 'game_objects': self._game_objects,
59 'enemies': self._enemies,
60 }, f)
61
62 @classmethod
63 def list_levels(cls):
64 with resources.get_file('levels', 'LEVELS') as f:
65 levels = load(f)
66 return levels['levels']
67
68 def load(self, space):
69 data = self._get_data()
70 self.x, self.y = data['size']
71 self.basetile = data['base_tile']
72 for i, points in data['polygons'].iteritems():
73 self.polygons[i] = []
74 for point in points:
75 self.polygons[i].append(tuple(point))
76 self.lines = data.get('lines', [])
77 self._game_objects = data.get('game_objects', [])
78 for game_object_dict in self._game_objects:
79 self._create_game_object(space, **game_object_dict)
80 self._enemies = data.get('enemies', [])
81 for enemy_dict in self._enemies:
82 self._create_enemy(space, **enemy_dict)
83
84 def _create_game_object(self, space, classname, args, name=None):
85 modules = {
86 'collectable': collectable,
87 'game_object': go,
88 'puzzle': puzzle,
89 }
90 if '.' in classname:
91 module, classname = classname.split('.')
92 else:
93 module = 'game_object'
94 cls = getattr(modules[module], classname)
95
96 if module == 'collectable' and name in self.world.inventory:
97 return
98
99 if issubclass(cls, puzzle.Puzzler):
100 gobj = cls(*args)
101 elif issubclass(cls, go.GameObject):
102 gobj = cls(space, *args)
103 level_state = self.world.level_state[self.name]
104 stored_state = level_state.get(name, {})
105 should_save = bool(gobj.set_stored_state_dict(stored_state))
106 if should_save:
107 if name is None:
108 raise Exception(
109 "Unnamed game object wants to save state:" % (gobj,))
110 level_state[name] = stored_state
111 self.drawables.append(gobj)
112 if gobj.overlay:
113 self.overlay_drawables.append(gobj.overlay)
114 else:
115 raise TypeError(
116 "Expected a subclass of Puzzler or GameObject, got %s" % (
117 classname))
118 if name is not None:
119 self._glue.add_component(name, gobj)
120 return gobj
121
122 def _create_enemy(self, space, classname, args, name=None):
123 cls = getattr(enemies, classname)
124 if issubclass(cls, go.GameObject):
125 gobj = cls(space, self.world, *args)
126 self.drawables.append(gobj)
127 else:
128 raise TypeError(
129 "Expected a subclass of GameObject, got %s" % (
130 classname))
131 if name is not None:
132 self._glue.add_component(name, gobj)
133 return gobj
134
135 def all_closed(self):
136 """Check if all the polygons are closed"""
137 closed = True
138 messages = []
139 for index, poly in self.polygons.items():
140 if len(poly) == 0:
141 # We ignore empty polygons
142 continue
143 elif len(poly) == 1:
144 closed = False
145 messages.append("Error: polygon %s too small" % index)
146 elif poly[-1] != poly[0]:
147 closed = False
148 messages.append("Error: polygon %s not closed" % index)
149 return closed, messages
150
151 def save(self):
152 closed, _ = self.all_closed()
153 if not closed:
154 return False
155 with resources.get_file('levels', self.name, mode='w') as f:
156 self._dump_data(f)
157 return True
158
159 def get_size(self):
160 return self.x, self.y
161
162 def set_base_tile(self, new_tile):
163 self.basetile = new_tile
164 self._tile_image = None
165
166 def point_to_pygame(self, pos):
167 # Convert a point from pymunk (which is what we store)
168 # to pygame for drawing
169 return (pos[0], self.y - pos[1])
170
171 def get_walls(self):
172 walls = self.polygons.values()
173 walls.extend(self.lines)
174 return walls
175
176 def _draw_walls(self):
177 for index, polygon in self.polygons.items():
178 color = POLY_COLORS[index]
179 if len(polygon) > 1:
180 pointlist = [self.point_to_pygame(p) for p in polygon]
181 pygame.draw.lines(self._surface, color, False, pointlist, 2)
182 for line in self.lines:
183 pointlist = [self.point_to_pygame(p) for p in line]
184 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2)
185
186 def get_background(self):
187 self._draw_background()
188 self._draw_exterior()
189 # Draw polygons
190 self._draw_walls()
191 return self._surface
192
193 def _draw_exterior(self, force=False):
194 """Fill the exterior of the level with black"""
195 if self._exterior and not force:
196 return
197 white = pygame.color.THECOLORS['white']
198 black = pygame.color.THECOLORS['black']
199 surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
200 surface.fill(black)
201 for index, polygon in self.polygons.items():
202 if len(polygon) > 1:
203 pointlist = [self.point_to_pygame(p) for p in polygon]
204 # filled polygons
205 color = white
206 # If a polygon overlaps on of the existing polygons,
207 # it is treated as negative
208 # This is not a complete inversion, since any overlap
209 # triggers this (inversion is easy enough, but the
210 # behaviour doesn't seem useful)
211 # We also only check the vertexes - not breaking this
212 # assumption is left to the level designers
213 surface.lock()
214 for p in pointlist:
215 if surface.get_at(p) == white:
216 color = black
217 surface.unlock()
218 pygame.draw.polygon(surface, color, pointlist, 0)
219 self._surface.blit(surface, (0, 0), special_flags=pgl.BLEND_RGBA_MULT)
220 self._exterior = True
221
222 def _draw_background(self, force=False):
223 if self._tile_image is None:
224 self._tile_image = resources.get_image(self.basetile)
225 if self._surface is not None and not force:
226 # We assume we don't change
227 return self._surface
228 self._surface = tile_surface((self.x, self.y), self._tile_image)
229 return self._surface
Note: See TracBrowser for help on using the repository browser.