source: nagslang/game_object.py@ 211:434b5a3aaaff

Last change on this file since 211:434b5a3aaaff was 211:434b5a3aaaff, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Pass display offset to overlays for when the display is larger than the level

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