source: nagslang/game_object.py@ 215:325c317cbfa1

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

Better protagonist physicser.

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