Mercurial > nagslang
comparison nagslang/game_object.py @ 81:a1b4d09e6f23
Floor switch with horrible hackery.
author | davidsharpe@lantea.local |
---|---|
date | Sun, 01 Sep 2013 22:53:37 +0200 |
parents | 7f038ee778ad |
children | 11b0017b5e4b |
comparison
equal
deleted
inserted
replaced
80:77e66eca8093 | 81:a1b4d09e6f23 |
---|---|
1 import math | 1 import math |
2 | |
3 import pygame | |
4 import pymunk | |
5 | |
6 from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH | |
7 | |
8 class Puzzler(object): | |
9 def get_state(self, space): | |
10 raise NotImplementedError() | |
11 | |
12 def notify(self): | |
13 pass | |
14 | |
15 | |
16 class FloorSwitchPuzzler(Puzzler): | |
17 def __init__(self, shape): | |
18 self.shape = shape | |
19 | |
20 def get_state(self, space): | |
21 for shape in space.shape_query(self.shape): | |
22 if shape.collision_type in SWITCH_PUSHERS: | |
23 return True | |
24 return False | |
2 | 25 |
3 | 26 |
4 class Physicser(object): | 27 class Physicser(object): |
5 def add_to_space(self, space): | 28 def add_to_space(self, space): |
6 raise NotImplementedError() | 29 raise NotImplementedError() |
96 """A representation of a thing in the game world. | 119 """A representation of a thing in the game world. |
97 | 120 |
98 This has a rendery thing, physicsy things and maybe some other things. | 121 This has a rendery thing, physicsy things and maybe some other things. |
99 """ | 122 """ |
100 | 123 |
101 def __init__(self, renderer, physicser=None): | 124 def __init__(self, renderer, physicser=None, puzzler=None): |
102 self.renderer = renderer | 125 self.renderer = renderer |
103 if physicser is None: | 126 if physicser is None: |
104 physicser = NullPhysicser() | 127 physicser = NullPhysicser() |
105 self.physicser = physicser | 128 self.physicser = physicser |
129 self.puzzler = puzzler | |
106 | 130 |
107 def add_to_space(self, space): | 131 def add_to_space(self, space): |
108 self.physicser.add_to_space(space) | 132 self.physicser.add_to_space(space) |
109 | 133 |
110 def render(self, surface): | 134 def render(self, surface): |
111 return self.renderer.render( | 135 return self.renderer.render( |
112 surface, self.physicser.render_position(surface), | 136 surface, self.physicser.render_position(surface), |
113 self.physicser.render_angle()) | 137 self.physicser.render_angle()) |
138 | |
139 | |
140 class FloorSwitch(GameObject): | |
141 def __init__(self, position): | |
142 body = pymunk.Body() | |
143 body.position = position | |
144 self.shape = pymunk.Circle(body, 30) | |
145 self.shape.collision_type = COLLISION_TYPE_SWITCH | |
146 self.shape.sensor = True | |
147 super(FloorSwitch, self).__init__( | |
148 ShapeRenderer(self.shape), | |
149 SingleShapePhysicser(self.shape), | |
150 FloorSwitchPuzzler(self.shape), | |
151 ) | |
152 | |
153 def add_to_space(self, space): | |
154 # XXX: Hacky hack. | |
155 self._space = space | |
156 | |
157 def render(self, surface): | |
158 if self.puzzler.get_state(self._space): | |
159 self.shape.color = pygame.color.THECOLORS['green'] | |
160 else: | |
161 self.shape.color = pygame.color.THECOLORS['red'] | |
162 super(FloorSwitch, self).render(surface) |