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
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):
[59]28 raise NotImplementedError()
29
[93]30 def remove_from_space(self):
[59]31 raise NotImplementedError()
32
[93]33 def get_render_position(self, surface):
[63]34 raise NotImplementedError()
35
[93]36 def get_angle(self):
37 raise NotImplementedError()
38
39 def apply_impulse(self, j, r=(0, 0)):
[59]40 raise NotImplementedError()
41
42
43class SingleShapePhysicser(Physicser):
[93]44 def __init__(self, space, shape):
45 super(SingleShapePhysicser, self).__init__(space)
[59]46 self._shape = shape
[186]47 shape.physicser = self
[59]48
[123]49 def get_shape(self):
50 return self._shape
51
[93]52 def add_to_space(self):
[123]53 self.get_space().add(self._shape)
[93]54 if not self._shape.body.is_static:
[123]55 self.get_space().add(self._shape.body)
[59]56
[93]57 def remove_from_space(self):
[123]58 self.get_space().remove(self._shape)
[93]59 if not self._shape.body.is_static:
[123]60 self.get_space().remove(self._shape.body)
[59]61
[93]62 def get_render_position(self, surface):
[59]63 pos = self._shape.body.position
64 return pymunk.pygame_util.to_pygame(pos, surface)
[63]65
[93]66 def get_angle(self):
[63]67 return self._shape.body.angle
[59]68
[93]69 def apply_impulse(self, j, r=(0, 0)):
70 return self._shape.body.apply_impulse(j, r)
71
[59]72
[133]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)
[145]82 body.position = tuple(position)
[133]83 if damping is not None:
84 body.damping = damping
85 body.velocity_func = damping_velocity_func
86 return body
87
88
[191]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
[196]103 self.widget = LabelWidget((20, 20), self.text)
[191]104
105 def render(self, surface):
106 self.widget.draw(surface)
107
108
[59]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
[162]115 zorder = ZORDER_LOW
116
[191]117 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
[93]118 self.physicser = physicser
[123]119 physicser.set_game_object(self)
[93]120 self.physicser.add_to_space()
[59]121 self.renderer = renderer
[123]122 renderer.set_game_object(self)
[81]123 self.puzzler = puzzler
[123]124 if puzzler is not None:
125 puzzler.set_game_object(self)
[191]126 self.overlay = overlay
127 if overlay is not None:
128 self.overlay.set_game_object(self)
[59]129
[123]130 def get_space(self):
131 return self.physicser.get_space()
132
133 def get_shape(self):
134 return self.physicser.get_shape()
135
[93]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()
[59]141
142 def render(self, surface):
[123]143 return self.renderer.render(surface)
[81]144
[143]145 def animate(self):
146 self.renderer.animate()
147
[186]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 """
[192]154 return True
[186]155
[81]156
157class FloorSwitch(GameObject):
[162]158 zorder = ZORDER_FLOOR
159
[93]160 def __init__(self, space, position):
[145]161 body = make_body(None, None, position)
[81]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__(
[93]166 SingleShapePhysicser(space, self.shape),
[207]167 render.ImageStateRenderer({
[162]168 True: resources.get_image('objects', 'sensor_on.png'),
169 False: resources.get_image('objects', 'sensor_off.png'),
170 }),
[201]171 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
[81]172 )
173
[106]174
[191]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),
[207]184 render.ImageRenderer(resources.get_image('objects', 'note.png')),
[201]185 puzzle.CollidePuzzler(),
[191]186 TextOverlay(message),
187 )
188
189
[106]190class FloorLight(GameObject):
[162]191 zorder = ZORDER_FLOOR
192
[106]193 def __init__(self, space, position, state_source):
[145]194 body = make_body(None, None, position)
[106]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),
[207]200 render.ImageStateRenderer({
[162]201 True: resources.get_image('objects', 'light_on.png'),
202 False: resources.get_image('objects', 'light_off.png'),
203 }),
[201]204 puzzle.StateProxyPuzzler(state_source),
[106]205 )
[133]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)])
[208]213 self.shape.friction = 0.5
[133]214 self.shape.collision_type = COLLISION_TYPE_BOX
215 super(Box, self).__init__(
216 SingleShapePhysicser(space, self.shape),
[207]217 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]218 )
[176]219
220
221class Door(GameObject):
222 zorder = ZORDER_FLOOR
223
[186]224 def __init__(self, space, position, destination, dest_pos, key_state=None):
[176]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)
[186]232 if key_state is None:
[201]233 puzzler = puzzle.YesPuzzler()
[186]234 else:
[201]235 puzzler = puzzle.StateProxyPuzzler(key_state)
[176]236 super(Door, self).__init__(
237 SingleShapePhysicser(space, self.shape),
[207]238 render.ImageRenderer(resources.get_image('objects', 'door.png')),
[186]239 puzzler,
[176]240 )
241
[188]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.