comparison nagslang/game_object.py @ 106:bce9cd8a4a8c

FloorLight, linked to a FloorSwitch.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 02 Sep 2013 13:48:24 +0200
parents 1be3eebb87c4
children b90d01e4d9d4
comparison
equal deleted inserted replaced
105:0131e4606e1a 106:bce9cd8a4a8c
6 6
7 from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH 7 from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
8 from nagslang.options import options 8 from nagslang.options import options
9 9
10 10
11 class PuzzleGlue(object):
12 """Glue that holds bits of a puzzle together.
13 """
14 def __init__(self):
15 self._components = {}
16
17 def add_component(self, name, puzzler):
18 self._components[name] = puzzler
19 puzzler.set_glue(self)
20
21 def get_state_of(self, name):
22 return self._components[name].get_state()
23
24
11 class Puzzler(object): 25 class Puzzler(object):
12 def get_state(self, space): 26 """Behaviour specific to a puzzle component.
13 raise NotImplementedError() 27 """
14 28 def set_glue(self, glue):
15 def notify(self): 29 self.glue = glue
16 pass 30
31 def get_state(self):
32 raise NotImplementedError()
17 33
18 34
19 class FloorSwitchPuzzler(Puzzler): 35 class FloorSwitchPuzzler(Puzzler):
20 def __init__(self, space, shape): 36 def __init__(self, space, shape):
21 self._space = space 37 self._space = space
24 def get_state(self): 40 def get_state(self):
25 for shape in self._space.shape_query(self._shape): 41 for shape in self._space.shape_query(self._shape):
26 if shape.collision_type in SWITCH_PUSHERS: 42 if shape.collision_type in SWITCH_PUSHERS:
27 return True 43 return True
28 return False 44 return False
45
46
47 class StateProxyPuzzler(Puzzler):
48 def __init__(self, state_source):
49 self._state_source = state_source
50
51 def get_state(self):
52 return self.glue.get_state_of(self._state_source)
29 53
30 54
31 class Physicser(object): 55 class Physicser(object):
32 def __init__(self, space): 56 def __init__(self, space):
33 self.space = space 57 self.space = space
183 if self.puzzler.get_state(): 207 if self.puzzler.get_state():
184 self.shape.color = pygame.color.THECOLORS['green'] 208 self.shape.color = pygame.color.THECOLORS['green']
185 else: 209 else:
186 self.shape.color = pygame.color.THECOLORS['red'] 210 self.shape.color = pygame.color.THECOLORS['red']
187 super(FloorSwitch, self).render(surface) 211 super(FloorSwitch, self).render(surface)
212
213
214 class FloorLight(GameObject):
215 def __init__(self, space, position, state_source):
216 body = pymunk.Body()
217 body.position = position
218 self.shape = pymunk.Circle(body, 10)
219 self.shape.collision_type = COLLISION_TYPE_SWITCH
220 self.shape.sensor = True
221 super(FloorLight, self).__init__(
222 SingleShapePhysicser(space, self.shape),
223 ShapeRenderer(self.shape),
224 StateProxyPuzzler(state_source),
225 )
226
227 def render(self, surface):
228 if self.puzzler.get_state():
229 self.shape.color = pygame.color.THECOLORS['green']
230 else:
231 self.shape.color = pygame.color.THECOLORS['red']
232 super(FloorLight, self).render(surface)