source: nagslang/game_object.py@ 218:9e2ef2f15035

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

Better rendering and movement detection.

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