source: nagslang/game_object.py@ 104:1be3eebb87c4

Last change on this file since 104:1be3eebb87c4 was 104:1be3eebb87c4, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

More consistent debug rendering.

File size: 5.5 KB
Line 
1import math
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
8from nagslang.options import options
9
10
11class Puzzler(object):
12 def get_state(self, space):
13 raise NotImplementedError()
14
15 def notify(self):
16 pass
17
18
19class FloorSwitchPuzzler(Puzzler):
20 def __init__(self, space, shape):
21 self._space = space
22 self._shape = shape
23
24 def get_state(self):
25 for shape in self._space.shape_query(self._shape):
26 if shape.collision_type in SWITCH_PUSHERS:
27 return True
28 return False
29
30
31class Physicser(object):
32 def __init__(self, space):
33 self.space = space
34
35 def add_to_space(self):
36 raise NotImplementedError()
37
38 def remove_from_space(self):
39 raise NotImplementedError()
40
41 def get_render_position(self, surface):
42 raise NotImplementedError()
43
44 def get_angle(self):
45 raise NotImplementedError()
46
47 def apply_impulse(self, j, r=(0, 0)):
48 raise NotImplementedError()
49
50
51class SingleShapePhysicser(Physicser):
52 def __init__(self, space, shape):
53 super(SingleShapePhysicser, self).__init__(space)
54 self._shape = shape
55
56 def add_to_space(self):
57 self.space.add(self._shape)
58 if not self._shape.body.is_static:
59 self.space.add(self._shape.body)
60
61 def remove_from_space(self):
62 self.space.remove(self._shape)
63 if not self._shape.body.is_static:
64 self.space.remove(self._shape.body)
65
66 def get_render_position(self, surface):
67 pos = self._shape.body.position
68 return pymunk.pygame_util.to_pygame(pos, surface)
69
70 def get_angle(self):
71 return self._shape.body.angle
72
73 def apply_impulse(self, j, r=(0, 0)):
74 return self._shape.body.apply_impulse(j, r)
75
76
77class Renderer(object):
78 def __init__(self, shape):
79 self._shape = shape
80
81 def _render_shape(self, surface, pos, angle):
82 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
83 color = getattr(
84 self._shape, 'color', pygame.color.THECOLORS['lightblue'])
85 # We only explicitly draw Circle and Poly shapes. Everything else we
86 # forward to pymunk.
87 if isinstance(self._shape, pymunk.Circle):
88 centre = pymunk.pygame_util.to_pygame(
89 self._shape.body.position, surface)
90 radius = int(self._shape.radius)
91 pygame.draw.circle(surface, color, centre, radius, 2)
92 elif isinstance(self._shape, pymunk.Poly):
93 # polygon bounding box
94 points = [pymunk.pygame_util.to_pygame(p, surface)
95 for p in self._shape.get_vertices()]
96 pygame.draw.lines(surface, color, True, points, 2)
97 else:
98 pymunk.pygame_util.draw(surface, self._shape)
99
100 def render(self, surface, pos, angle):
101 if options.debug:
102 self._render_shape(surface, pos, angle)
103
104
105def image_pos(image, pos):
106 return (pos[0] - image.get_width() / 2,
107 pos[1] - image.get_height() / 2)
108
109
110class ImageRenderer(Renderer):
111 def __init__(self, shape, image):
112 super(ImageRenderer, self).__init__(shape)
113 self._image = image
114
115 def render(self, surface, pos, angle):
116 surface.blit(self._image, image_pos(self._image, pos))
117 super(ImageRenderer, self).render(surface, pos, angle)
118
119
120class FacingImageRenderer(Renderer):
121 def __init__(self, shape, left_image, right_image):
122 super(FacingImageRenderer, self).__init__(shape)
123 self._images = {
124 'left': left_image,
125 'right': right_image,
126 }
127
128 def get_image(self, angle):
129 if abs(angle) < math.pi / 2:
130 return self._images['right']
131 return self._images['left']
132
133 def render(self, surface, pos, angle):
134 image = self.get_image(angle)
135 surface.blit(image, image_pos(image, pos))
136 super(FacingImageRenderer, self).render(surface, pos, angle)
137
138
139class ShapeRenderer(Renderer):
140 def render(self, surface, pos, angle):
141 self._render_shape(surface, pos, angle)
142 super(ShapeRenderer, self).render(surface, pos, angle)
143
144
145class GameObject(object):
146 """A representation of a thing in the game world.
147
148 This has a rendery thing, physicsy things and maybe some other things.
149 """
150
151 def __init__(self, physicser, renderer, puzzler=None):
152 self.physicser = physicser
153 self.physicser.add_to_space()
154 self.renderer = renderer
155 self.puzzler = puzzler
156
157 def get_render_position(self, surface):
158 return self.physicser.get_render_position(surface)
159
160 def get_render_angle(self):
161 return self.physicser.get_angle()
162
163 def render(self, surface):
164 return self.renderer.render(
165 surface, self.get_render_position(surface),
166 self.get_render_angle())
167
168
169class FloorSwitch(GameObject):
170 def __init__(self, space, position):
171 body = pymunk.Body()
172 body.position = position
173 self.shape = pymunk.Circle(body, 30)
174 self.shape.collision_type = COLLISION_TYPE_SWITCH
175 self.shape.sensor = True
176 super(FloorSwitch, self).__init__(
177 SingleShapePhysicser(space, self.shape),
178 ShapeRenderer(self.shape),
179 FloorSwitchPuzzler(space, self.shape),
180 )
181
182 def render(self, surface):
183 if self.puzzler.get_state():
184 self.shape.color = pygame.color.THECOLORS['green']
185 else:
186 self.shape.color = pygame.color.THECOLORS['red']
187 super(FloorSwitch, self).render(surface)
Note: See TracBrowser for help on using the repository browser.