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
RevLine 
[81]1import pymunk
[93]2import pymunk.pygame_util
[81]3
[201]4from nagslang import puzzle
[207]5from nagslang import render
[107]6from nagslang.constants import (
[162]7 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
[201]8 ZORDER_FLOOR, COLLISION_TYPE_DOOR)
[155]9from nagslang.resources import resources
[180]10from nagslang.events import DoorEvent
[196]11from nagslang.widgets.text import LabelWidget
[81]12
[82]13
[59]14class Physicser(object):
[93]15 def __init__(self, space):
[123]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()
[93]26
27 def add_to_space(self):
[215]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)
[59]32
[93]33 def remove_from_space(self):
[215]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)
[59]38
[93]39 def get_render_position(self, surface):
[215]40 pos = self.get_shape().body.position
41 return pymunk.pygame_util.to_pygame(pos, surface)
[63]42
[93]43 def get_angle(self):
[215]44 return self.get_shape().body.angle
45
[217]46 def get_velocity(self):
47 return self.get_shape().body.velocity
48
[216]49 def _get_position(self):
[215]50 return self.get_shape().body.position
51
[216]52 def _set_position(self, position):
[215]53 self.get_shape().body.position = position
[93]54
[216]55 position = property(_get_position, _set_position)
56
[93]57 def apply_impulse(self, j, r=(0, 0)):
[215]58 return self.get_shape().body.apply_impulse(j, r)
[59]59
60
61class SingleShapePhysicser(Physicser):
[93]62 def __init__(self, space, shape):
63 super(SingleShapePhysicser, self).__init__(space)
[59]64 self._shape = shape
[186]65 shape.physicser = self
[59]66
[123]67 def get_shape(self):
68 return self._shape
69
[59]70
[133]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)
[145]80 body.position = tuple(position)
[133]81 if damping is not None:
82 body.damping = damping
83 body.velocity_func = damping_velocity_func
84 return body
85
86
[191]87class Overlay(object):
88 def set_game_object(self, game_object):
89 self.game_object = game_object
90
[211]91 def render(self, surface, display_offset):
[191]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
[196]101 self.widget = LabelWidget((20, 20), self.text)
[191]102
[211]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)
[191]110 self.widget.draw(surface)
111
112
[59]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
[162]119 zorder = ZORDER_LOW
120
[191]121 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
[93]122 self.physicser = physicser
[123]123 physicser.set_game_object(self)
[93]124 self.physicser.add_to_space()
[59]125 self.renderer = renderer
[123]126 renderer.set_game_object(self)
[81]127 self.puzzler = puzzler
[123]128 if puzzler is not None:
129 puzzler.set_game_object(self)
[191]130 self.overlay = overlay
131 if overlay is not None:
132 self.overlay.set_game_object(self)
[59]133
[123]134 def get_space(self):
135 return self.physicser.get_space()
136
137 def get_shape(self):
138 return self.physicser.get_shape()
139
[93]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()
[59]145
146 def render(self, surface):
[123]147 return self.renderer.render(surface)
[81]148
[143]149 def animate(self):
150 self.renderer.animate()
151
[217]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
[186]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 """
[192]165 return True
[186]166
[81]167
168class FloorSwitch(GameObject):
[162]169 zorder = ZORDER_FLOOR
170
[93]171 def __init__(self, space, position):
[145]172 body = make_body(None, None, position)
[81]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__(
[93]177 SingleShapePhysicser(space, self.shape),
[207]178 render.ImageStateRenderer({
[162]179 True: resources.get_image('objects', 'sensor_on.png'),
180 False: resources.get_image('objects', 'sensor_off.png'),
181 }),
[201]182 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
[81]183 )
184
[106]185
[191]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),
[207]195 render.ImageRenderer(resources.get_image('objects', 'note.png')),
[201]196 puzzle.CollidePuzzler(),
[191]197 TextOverlay(message),
198 )
199
200
[106]201class FloorLight(GameObject):
[162]202 zorder = ZORDER_FLOOR
203
[106]204 def __init__(self, space, position, state_source):
[145]205 body = make_body(None, None, position)
[106]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),
[207]211 render.ImageStateRenderer({
[162]212 True: resources.get_image('objects', 'light_on.png'),
213 False: resources.get_image('objects', 'light_off.png'),
214 }),
[201]215 puzzle.StateProxyPuzzler(state_source),
[106]216 )
[133]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)])
[208]224 self.shape.friction = 0.5
[133]225 self.shape.collision_type = COLLISION_TYPE_BOX
226 super(Box, self).__init__(
227 SingleShapePhysicser(space, self.shape),
[207]228 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]229 )
[176]230
231
232class Door(GameObject):
233 zorder = ZORDER_FLOOR
234
[186]235 def __init__(self, space, position, destination, dest_pos, key_state=None):
[176]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)
[186]243 if key_state is None:
[201]244 puzzler = puzzle.YesPuzzler()
[186]245 else:
[201]246 puzzler = puzzle.StateProxyPuzzler(key_state)
[176]247 super(Door, self).__init__(
248 SingleShapePhysicser(space, self.shape),
[207]249 render.ImageRenderer(resources.get_image('objects', 'door.png')),
[186]250 puzzler,
[176]251 )
252
[188]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.