source: nagslang/game_object.py @ 133:79e1888573d3

Last change on this file since 133:79e1888573d3 was 133:79e1888573d3, checked in by Jeremy Thurgood <firxen@…>, 7 years ago

A box.

File size: 8.3 KB
Line 
1import math
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.constants import (
8    SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
9from nagslang.options import options
10
11
12class PuzzleGlue(object):
13    """Glue that holds bits of a puzzle together.
14    """
15    def __init__(self):
16        self._components = {}
17
18    def add_component(self, name, puzzler):
19        self._components[name] = puzzler
20        puzzler.set_glue(self)
21
22    def get_state_of(self, name):
23        return self._components[name].get_state()
24
25
26class Puzzler(object):
27    """Behaviour specific to a puzzle component.
28    """
29    def set_glue(self, glue):
30        self.glue = glue
31
32    def set_game_object(self, game_object):
33        self.game_object = game_object
34
35    def get_state(self):
36        raise NotImplementedError()
37
38
39class FloorSwitchPuzzler(Puzzler):
40    def get_state(self):
41        space = self.game_object.get_space()
42        for shape in space.shape_query(self.game_object.get_shape()):
43            if shape.collision_type in SWITCH_PUSHERS:
44                return True
45        return False
46
47
48class StateProxyPuzzler(Puzzler):
49    def __init__(self, state_source):
50        self._state_source = state_source
51
52    def get_state(self):
53        return self.glue.get_state_of(self._state_source)
54
55
56class Physicser(object):
57    def __init__(self, space):
58        self._space = space
59
60    def get_space(self):
61        return self._space
62
63    def set_game_object(self, game_object):
64        self.game_object = game_object
65
66    def get_shape(self):
67        raise NotImplementedError()
68
69    def add_to_space(self):
70        raise NotImplementedError()
71
72    def remove_from_space(self):
73        raise NotImplementedError()
74
75    def get_render_position(self, surface):
76        raise NotImplementedError()
77
78    def get_angle(self):
79        raise NotImplementedError()
80
81    def apply_impulse(self, j, r=(0, 0)):
82        raise NotImplementedError()
83
84
85class SingleShapePhysicser(Physicser):
86    def __init__(self, space, shape):
87        super(SingleShapePhysicser, self).__init__(space)
88        self._shape = shape
89
90    def get_shape(self):
91        return self._shape
92
93    def add_to_space(self):
94        self.get_space().add(self._shape)
95        if not self._shape.body.is_static:
96            self.get_space().add(self._shape.body)
97
98    def remove_from_space(self):
99        self.get_space().remove(self._shape)
100        if not self._shape.body.is_static:
101            self.get_space().remove(self._shape.body)
102
103    def get_render_position(self, surface):
104        pos = self._shape.body.position
105        return pymunk.pygame_util.to_pygame(pos, surface)
106
107    def get_angle(self):
108        return self._shape.body.angle
109
110    def apply_impulse(self, j, r=(0, 0)):
111        return self._shape.body.apply_impulse(j, r)
112
113
114class Renderer(object):
115    def set_game_object(self, game_object):
116        self.game_object = game_object
117
118    def _render_shape(self, surface):
119        shape = self.game_object.get_shape()
120        # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
121        color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
122        # We only explicitly draw Circle and Poly shapes. Everything else we
123        # forward to pymunk.
124        if isinstance(shape, pymunk.Circle):
125            centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
126            radius = int(shape.radius)
127            pygame.draw.circle(surface, color, centre, radius, 2)
128        elif isinstance(shape, pymunk.Poly):
129            # polygon bounding box
130            points = [pymunk.pygame_util.to_pygame(p, surface)
131                      for p in shape.get_vertices()]
132            pygame.draw.lines(surface, color, True, points, 2)
133        else:
134            pymunk.pygame_util.draw(surface, shape)
135
136    def render(self, surface):
137        pos = self.game_object.get_render_position(surface)
138        angle = self.game_object.get_render_angle()
139        if options.debug:
140            self._render_shape(surface, pos, angle)
141
142
143def image_pos(image, pos):
144    return (pos[0] - image.get_width() / 2,
145            pos[1] - image.get_height() / 2)
146
147
148class ImageRenderer(Renderer):
149    def __init__(self, image):
150        self._image = image
151
152    def render(self, surface):
153        pos = self.game_object.get_render_position(surface)
154        surface.blit(self._image, image_pos(self._image, pos))
155        super(ImageRenderer, self).render(surface)
156
157
158class FacingImageRenderer(Renderer):
159    def __init__(self, left_image, right_image):
160        self._images = {
161            'left': left_image,
162            'right': right_image,
163        }
164
165    def get_image(self, angle):
166        if abs(angle) < math.pi / 2:
167            return self._images['right']
168        return self._images['left']
169
170    def render(self, surface):
171        pos = self.game_object.get_render_position(surface)
172        image = self.get_image(self.game_object.get_render_angle())
173        surface.blit(image, image_pos(image, pos))
174        super(FacingImageRenderer, self).render(surface)
175
176
177class ShapeRenderer(Renderer):
178    def render(self, surface):
179        self._render_shape(surface)
180        super(ShapeRenderer, self).render(surface)
181
182
183class ShapeStateRenderer(ShapeRenderer):
184    """Renders the shape in a different colour depending on the state.
185
186    Requires the game object it's attached to to have a puzzler.
187    """
188    def render(self, surface):
189        if self.game_object.puzzler.get_state():
190            color = pygame.color.THECOLORS['green']
191        else:
192            color = pygame.color.THECOLORS['red']
193
194        self.game_object.get_shape().color = color
195        super(ShapeStateRenderer, self).render(surface)
196
197
198def 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
205def 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
212
213
214class GameObject(object):
215    """A representation of a thing in the game world.
216
217    This has a rendery thing, physicsy things and maybe some other things.
218    """
219
220    def __init__(self, physicser, renderer, puzzler=None):
221        self.physicser = physicser
222        physicser.set_game_object(self)
223        self.physicser.add_to_space()
224        self.renderer = renderer
225        renderer.set_game_object(self)
226        self.puzzler = puzzler
227        if puzzler is not None:
228            puzzler.set_game_object(self)
229        self.zorder = ZORDER_LOW
230
231    def get_space(self):
232        return self.physicser.get_space()
233
234    def get_shape(self):
235        return self.physicser.get_shape()
236
237    def get_render_position(self, surface):
238        return self.physicser.get_render_position(surface)
239
240    def get_render_angle(self):
241        return self.physicser.get_angle()
242
243    def render(self, surface):
244        return self.renderer.render(surface)
245
246
247class FloorSwitch(GameObject):
248    def __init__(self, space, position):
249        body = pymunk.Body()
250        body.position = position
251        self.shape = pymunk.Circle(body, 30)
252        self.shape.collision_type = COLLISION_TYPE_SWITCH
253        self.shape.sensor = True
254        super(FloorSwitch, self).__init__(
255            SingleShapePhysicser(space, self.shape),
256            ShapeStateRenderer(),
257            FloorSwitchPuzzler(),
258        )
259
260
261class FloorLight(GameObject):
262    def __init__(self, space, position, state_source):
263        body = pymunk.Body()
264        body.position = position
265        self.shape = pymunk.Circle(body, 10)
266        self.shape.collision_type = COLLISION_TYPE_SWITCH
267        self.shape.sensor = True
268        super(FloorLight, self).__init__(
269            SingleShapePhysicser(space, self.shape),
270            ShapeStateRenderer(),
271            StateProxyPuzzler(state_source),
272        )
273
274
275class 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        )
Note: See TracBrowser for help on using the repository browser.