source: nagslang/game_object.py @ 60:34a87ec12124

Last change on this file since 60:34a87ec12124 was 60:34a87ec12124, checked in by Jeremy Thurgood <firxen@…>, 7 years ago

Fix long line.

File size: 1.9 KB
Line 
1
2class Physicser(object):
3    def add_to_space(self, space):
4        raise NotImplementedError()
5
6    def remove_from_space(self, space):
7        raise NotImplementedError()
8
9    def render_position(self):
10        raise NotImplementedError()
11
12
13class NullPhysicser(Physicser):
14    def add_to_space(self, space):
15        pass
16
17    def remove_from_space(self, space):
18        pass
19
20    def render_position(self):
21        return (0, 0)
22
23
24class SingleShapePhysicser(Physicser):
25    def __init__(self, shape):
26        self._shape = shape
27
28    def add_to_space(self, space):
29        space.add(self._shape, self._shape.body)
30
31    def remove_from_space(self, space):
32        space.remove(self._shape, self._shape.body)
33
34    def render_position(self, surface):
35        pos = self._shape.body.position
36        import pymunk.pygame_util
37        return pymunk.pygame_util.to_pygame(pos, surface)
38        return (int(pos.x), int(pos.y))
39
40
41class Renderer(object):
42    def render(self, surface, pos):
43        raise NotImplementedError()
44
45
46class ImageRenderer(Renderer):
47    def __init__(self, image):
48        self._image = image
49
50    def render(self, surface, pos):
51        surface.blit(self._image, pos)
52
53
54class ShapeRenderer(Renderer):
55    def __init__(self, shape):
56        self._shape = shape
57
58    def render(self, surface, pos):
59        import pymunk.pygame_util
60        pymunk.pygame_util.draw(surface, self._shape)
61
62
63class GameObject(object):
64    """A representation of a thing in the game world.
65
66    This has a rendery thing, physicsy things and maybe some other things.
67    """
68
69    def __init__(self, renderer, physicser=None):
70        self.renderer = renderer
71        if physicser is None:
72            physicser = NullPhysicser()
73        self.physicser = physicser
74
75    def add_to_space(self, space):
76        self.physicser.add_to_space(space)
77
78    def render(self, surface):
79        return self.renderer.render(
80            surface, self.physicser.render_position(surface))
Note: See TracBrowser for help on using the repository browser.