source: nagslang/game_object.py@ 209:ad1d3de210cd

Last change on this file since 209:ad1d3de210cd was 209:ad1d3de210cd, checked in by Stefano Rivera <stefano@…>, 8 years ago

Drop compatibility imports, and allow an optional module on classnames

File size: 7.3 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):
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):
106 self.widget.draw(surface)
107
108
109class GameObject(object):
110 """A representation of a thing in the game world.
111
112 This has a rendery thing, physicsy things and maybe some other things.
113 """
114
115 zorder = ZORDER_LOW
116
117 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
118 self.physicser = physicser
119 physicser.set_game_object(self)
120 self.physicser.add_to_space()
121 self.renderer = renderer
122 renderer.set_game_object(self)
123 self.puzzler = puzzler
124 if puzzler is not None:
125 puzzler.set_game_object(self)
126 self.overlay = overlay
127 if overlay is not None:
128 self.overlay.set_game_object(self)
129
130 def get_space(self):
131 return self.physicser.get_space()
132
133 def get_shape(self):
134 return self.physicser.get_shape()
135
136 def get_render_position(self, surface):
137 return self.physicser.get_render_position(surface)
138
139 def get_render_angle(self):
140 return self.physicser.get_angle()
141
142 def render(self, surface):
143 return self.renderer.render(surface)
144
145 def animate(self):
146 self.renderer.animate()
147
148 def collide_with_protagonist(self):
149 """Called as a `pre_solve` collision callback with the protagonist.
150
151 You can return `False` to ignore the collision, anything else
152 (including `None`) to process the collision as normal.
153 """
154 return True
155
156
157class FloorSwitch(GameObject):
158 zorder = ZORDER_FLOOR
159
160 def __init__(self, space, position):
161 body = make_body(None, None, position)
162 self.shape = pymunk.Circle(body, 30)
163 self.shape.collision_type = COLLISION_TYPE_SWITCH
164 self.shape.sensor = True
165 super(FloorSwitch, self).__init__(
166 SingleShapePhysicser(space, self.shape),
167 render.ImageStateRenderer({
168 True: resources.get_image('objects', 'sensor_on.png'),
169 False: resources.get_image('objects', 'sensor_off.png'),
170 }),
171 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
172 )
173
174
175class Note(GameObject):
176 zorder = ZORDER_FLOOR
177
178 def __init__(self, space, position, message):
179 body = make_body(None, None, position)
180 self.shape = pymunk.Circle(body, 30)
181 self.shape.sensor = True
182 super(Note, self).__init__(
183 SingleShapePhysicser(space, self.shape),
184 render.ImageRenderer(resources.get_image('objects', 'note.png')),
185 puzzle.CollidePuzzler(),
186 TextOverlay(message),
187 )
188
189
190class FloorLight(GameObject):
191 zorder = ZORDER_FLOOR
192
193 def __init__(self, space, position, state_source):
194 body = make_body(None, None, position)
195 self.shape = pymunk.Circle(body, 10)
196 self.shape.collision_type = COLLISION_TYPE_SWITCH
197 self.shape.sensor = True
198 super(FloorLight, self).__init__(
199 SingleShapePhysicser(space, self.shape),
200 render.ImageStateRenderer({
201 True: resources.get_image('objects', 'light_on.png'),
202 False: resources.get_image('objects', 'light_off.png'),
203 }),
204 puzzle.StateProxyPuzzler(state_source),
205 )
206
207
208class Box(GameObject):
209 def __init__(self, space, position):
210 body = make_body(10, 10000, position, damping=0.5)
211 self.shape = pymunk.Poly(
212 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
213 self.shape.friction = 0.5
214 self.shape.collision_type = COLLISION_TYPE_BOX
215 super(Box, self).__init__(
216 SingleShapePhysicser(space, self.shape),
217 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
218 )
219
220
221class Door(GameObject):
222 zorder = ZORDER_FLOOR
223
224 def __init__(self, space, position, destination, dest_pos, key_state=None):
225 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
226 self.shape = pymunk.Poly(
227 body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
228 self.shape.collision_type = COLLISION_TYPE_DOOR
229 self.shape.sensor = True
230 self.destination = destination
231 self.dest_pos = tuple(dest_pos)
232 if key_state is None:
233 puzzler = puzzle.YesPuzzler()
234 else:
235 puzzler = puzzle.StateProxyPuzzler(key_state)
236 super(Door, self).__init__(
237 SingleShapePhysicser(space, self.shape),
238 render.ImageRenderer(resources.get_image('objects', 'door.png')),
239 puzzler,
240 )
241
242 def collide_with_protagonist(self):
243 if self.puzzler.get_state():
244 DoorEvent.post(self.destination, self.dest_pos)
Note: See TracBrowser for help on using the repository browser.