comparison nagslang/game_object.py @ 133:79e1888573d3

A box.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 02 Sep 2013 18:05:03 +0200
parents c3af35561494
children f36a7075d9a0
comparison
equal deleted inserted replaced
132:e1ef3727b7f8 133:79e1888573d3
3 import pygame 3 import pygame
4 import pymunk 4 import pymunk
5 import pymunk.pygame_util 5 import pymunk.pygame_util
6 6
7 from nagslang.constants import ( 7 from nagslang.constants import (
8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, ZORDER_LOW) 8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
9 from nagslang.options import options 9 from nagslang.options import options
10 10
11 11
12 class PuzzleGlue(object): 12 class PuzzleGlue(object):
13 """Glue that holds bits of a puzzle together. 13 """Glue that holds bits of a puzzle together.
172 image = self.get_image(self.game_object.get_render_angle()) 172 image = self.get_image(self.game_object.get_render_angle())
173 surface.blit(image, image_pos(image, pos)) 173 surface.blit(image, image_pos(image, pos))
174 super(FacingImageRenderer, self).render(surface) 174 super(FacingImageRenderer, self).render(surface)
175 175
176 176
177 class ShapeStateRenderer(Renderer): 177 class ShapeRenderer(Renderer):
178 def render(self, surface):
179 self._render_shape(surface)
180 super(ShapeRenderer, self).render(surface)
181
182
183 class ShapeStateRenderer(ShapeRenderer):
178 """Renders the shape in a different colour depending on the state. 184 """Renders the shape in a different colour depending on the state.
179 185
180 Requires the game object it's attached to to have a puzzler. 186 Requires the game object it's attached to to have a puzzler.
181 """ 187 """
182 def render(self, surface): 188 def render(self, surface):
184 color = pygame.color.THECOLORS['green'] 190 color = pygame.color.THECOLORS['green']
185 else: 191 else:
186 color = pygame.color.THECOLORS['red'] 192 color = pygame.color.THECOLORS['red']
187 193
188 self.game_object.get_shape().color = color 194 self.game_object.get_shape().color = color
189 self._render_shape(surface)
190 super(ShapeStateRenderer, self).render(surface) 195 super(ShapeStateRenderer, self).render(surface)
196
197
198 def damping_velocity_func(body, gravity, damping, dt):
199 """Apply custom damping to this body's velocity.
200 """
201 damping = getattr(body, 'damping', damping)
202 return pymunk.Body.update_velocity(body, gravity, damping, dt)
203
204
205 def make_body(mass, moment, position, damping=None):
206 body = pymunk.Body(mass, moment)
207 body.position = position
208 if damping is not None:
209 body.damping = damping
210 body.velocity_func = damping_velocity_func
211 return body
191 212
192 213
193 class GameObject(object): 214 class GameObject(object):
194 """A representation of a thing in the game world. 215 """A representation of a thing in the game world.
195 216
247 super(FloorLight, self).__init__( 268 super(FloorLight, self).__init__(
248 SingleShapePhysicser(space, self.shape), 269 SingleShapePhysicser(space, self.shape),
249 ShapeStateRenderer(), 270 ShapeStateRenderer(),
250 StateProxyPuzzler(state_source), 271 StateProxyPuzzler(state_source),
251 ) 272 )
273
274
275 class Box(GameObject):
276 def __init__(self, space, position):
277 body = make_body(10, 10000, position, damping=0.5)
278 self.shape = pymunk.Poly(
279 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
280 self.shape.collision_type = COLLISION_TYPE_BOX
281 super(Box, self).__init__(
282 SingleShapePhysicser(space, self.shape),
283 ShapeRenderer(),
284 )