source: nagslang/game_object.py@ 217:d98daba73055

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

Composition-based renderers.

File size: 7.7 KB
Line 
1import pymunk
2import pymunk.pygame_util
3
4from nagslang import puzzle
5from nagslang import render
6from nagslang.constants import (
7 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
8 ZORDER_FLOOR, COLLISION_TYPE_DOOR)
9from nagslang.resources import resources
10from nagslang.events import DoorEvent
11from nagslang.widgets.text import LabelWidget
12
13
14class Physicser(object):
15 def __init__(self, space):
16 self._space = space
17
18 def get_space(self):
19 return self._space
20
21 def set_game_object(self, game_object):
22 self.game_object = game_object
23
24 def get_shape(self):
25 raise NotImplementedError()
26
27 def add_to_space(self):
28 shape = self.get_shape()
29 self.get_space().add(shape)
30 if not shape.body.is_static:
31 self.get_space().add(shape.body)
32
33 def remove_from_space(self):
34 shape = self.get_shape()
35 self.get_space().remove(shape)
36 if not shape.body.is_static:
37 self.get_space().remove(shape.body)
38
39 def get_render_position(self, surface):
40 pos = self.get_shape().body.position
41 return pymunk.pygame_util.to_pygame(pos, surface)
42
43 def get_angle(self):
44 return self.get_shape().body.angle
45
46 def get_velocity(self):
47 return self.get_shape().body.velocity
48
49 def _get_position(self):
50 return self.get_shape().body.position
51
52 def _set_position(self, position):
53 self.get_shape().body.position = position
54
55 position = property(_get_position, _set_position)
56
57 def apply_impulse(self, j, r=(0, 0)):
58 return self.get_shape().body.apply_impulse(j, r)
59
60
61class SingleShapePhysicser(Physicser):
62 def __init__(self, space, shape):
63 super(SingleShapePhysicser, self).__init__(space)
64 self._shape = shape
65 shape.physicser = self
66
67 def get_shape(self):
68 return self._shape
69
70
71def damping_velocity_func(body, gravity, damping, dt):
72 """Apply custom damping to this body's velocity.
73 """
74 damping = getattr(body, 'damping', damping)
75 return pymunk.Body.update_velocity(body, gravity, damping, dt)
76
77
78def make_body(mass, moment, position, damping=None):
79 body = pymunk.Body(mass, moment)
80 body.position = tuple(position)
81 if damping is not None:
82 body.damping = damping
83 body.velocity_func = damping_velocity_func
84 return body
85
86
87class Overlay(object):
88 def set_game_object(self, game_object):
89 self.game_object = game_object
90
91 def render(self, surface, display_offset):
92 pass
93
94 def is_visible(self):
95 return self.game_object.puzzler.get_state()
96
97
98class TextOverlay(Overlay):
99 def __init__(self, text):
100 self.text = text
101 self.widget = LabelWidget((20, 20), self.text)
102
103 def render(self, surface, display_offset):
104 x, y = 20, 20
105 if display_offset[0] < 0:
106 x += abs(display_offset[0])
107 if display_offset[1] < 0:
108 y += abs(display_offset[1])
109 self.widget.rect.topleft = (x, y)
110 self.widget.draw(surface)
111
112
113class GameObject(object):
114 """A representation of a thing in the game world.
115
116 This has a rendery thing, physicsy things and maybe some other things.
117 """
118
119 zorder = ZORDER_LOW
120
121 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
122 self.physicser = physicser
123 physicser.set_game_object(self)
124 self.physicser.add_to_space()
125 self.renderer = renderer
126 renderer.set_game_object(self)
127 self.puzzler = puzzler
128 if puzzler is not None:
129 puzzler.set_game_object(self)
130 self.overlay = overlay
131 if overlay is not None:
132 self.overlay.set_game_object(self)
133
134 def get_space(self):
135 return self.physicser.get_space()
136
137 def get_shape(self):
138 return self.physicser.get_shape()
139
140 def get_render_position(self, surface):
141 return self.physicser.get_render_position(surface)
142
143 def get_render_angle(self):
144 return self.physicser.get_angle()
145
146 def render(self, surface):
147 return self.renderer.render(surface)
148
149 def animate(self):
150 self.renderer.animate()
151
152 def is_moving(self):
153 """Returns `True` if this object is moving.
154
155 This is mostly for movement-based animation renderers to look at.
156 """
157 return self.physicser.get_velocity().length > 0
158
159 def collide_with_protagonist(self):
160 """Called as a `pre_solve` collision callback with the protagonist.
161
162 You can return `False` to ignore the collision, anything else
163 (including `None`) to process the collision as normal.
164 """
165 return True
166
167
168class FloorSwitch(GameObject):
169 zorder = ZORDER_FLOOR
170
171 def __init__(self, space, position):
172 body = make_body(None, None, 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 render.ImageStateRenderer({
179 True: resources.get_image('objects', 'sensor_on.png'),
180 False: resources.get_image('objects', 'sensor_off.png'),
181 }),
182 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
183 )
184
185
186class Note(GameObject):
187 zorder = ZORDER_FLOOR
188
189 def __init__(self, space, position, message):
190 body = make_body(None, None, position)
191 self.shape = pymunk.Circle(body, 30)
192 self.shape.sensor = True
193 super(Note, self).__init__(
194 SingleShapePhysicser(space, self.shape),
195 render.ImageRenderer(resources.get_image('objects', 'note.png')),
196 puzzle.CollidePuzzler(),
197 TextOverlay(message),
198 )
199
200
201class FloorLight(GameObject):
202 zorder = ZORDER_FLOOR
203
204 def __init__(self, space, position, state_source):
205 body = make_body(None, None, position)
206 self.shape = pymunk.Circle(body, 10)
207 self.shape.collision_type = COLLISION_TYPE_SWITCH
208 self.shape.sensor = True
209 super(FloorLight, self).__init__(
210 SingleShapePhysicser(space, self.shape),
211 render.ImageStateRenderer({
212 True: resources.get_image('objects', 'light_on.png'),
213 False: resources.get_image('objects', 'light_off.png'),
214 }),
215 puzzle.StateProxyPuzzler(state_source),
216 )
217
218
219class Box(GameObject):
220 def __init__(self, space, position):
221 body = make_body(10, 10000, position, damping=0.5)
222 self.shape = pymunk.Poly(
223 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
224 self.shape.friction = 0.5
225 self.shape.collision_type = COLLISION_TYPE_BOX
226 super(Box, self).__init__(
227 SingleShapePhysicser(space, self.shape),
228 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
229 )
230
231
232class Door(GameObject):
233 zorder = ZORDER_FLOOR
234
235 def __init__(self, space, position, destination, dest_pos, key_state=None):
236 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
237 self.shape = pymunk.Poly(
238 body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
239 self.shape.collision_type = COLLISION_TYPE_DOOR
240 self.shape.sensor = True
241 self.destination = destination
242 self.dest_pos = tuple(dest_pos)
243 if key_state is None:
244 puzzler = puzzle.YesPuzzler()
245 else:
246 puzzler = puzzle.StateProxyPuzzler(key_state)
247 super(Door, self).__init__(
248 SingleShapePhysicser(space, self.shape),
249 render.ImageRenderer(resources.get_image('objects', 'door.png')),
250 puzzler,
251 )
252
253 def collide_with_protagonist(self):
254 if self.puzzler.get_state():
255 DoorEvent.post(self.destination, self.dest_pos)
Note: See TracBrowser for help on using the repository browser.