source: nagslang/game_object.py@ 223:4197350b9897

Last change on this file since 223:4197350b9897 was 222:cc5f2a5ac501, checked in by Stefano Rivera <stefano@…>, 8 years ago

Overlays belong in render

File size: 6.8 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
[81]11
[82]12
[59]13class Physicser(object):
[93]14 def __init__(self, space):
[123]15 self._space = space
16
17 def get_space(self):
18 return self._space
19
20 def set_game_object(self, game_object):
21 self.game_object = game_object
22
23 def get_shape(self):
24 raise NotImplementedError()
[93]25
26 def add_to_space(self):
[215]27 shape = self.get_shape()
28 self.get_space().add(shape)
29 if not shape.body.is_static:
30 self.get_space().add(shape.body)
[59]31
[93]32 def remove_from_space(self):
[215]33 shape = self.get_shape()
34 self.get_space().remove(shape)
35 if not shape.body.is_static:
36 self.get_space().remove(shape.body)
[59]37
[93]38 def get_render_position(self, surface):
[215]39 pos = self.get_shape().body.position
40 return pymunk.pygame_util.to_pygame(pos, surface)
[63]41
[93]42 def get_angle(self):
[215]43 return self.get_shape().body.angle
44
[217]45 def get_velocity(self):
46 return self.get_shape().body.velocity
47
[216]48 def _get_position(self):
[215]49 return self.get_shape().body.position
50
[216]51 def _set_position(self, position):
[215]52 self.get_shape().body.position = position
[93]53
[216]54 position = property(_get_position, _set_position)
55
[93]56 def apply_impulse(self, j, r=(0, 0)):
[215]57 return self.get_shape().body.apply_impulse(j, r)
[59]58
59
60class SingleShapePhysicser(Physicser):
[93]61 def __init__(self, space, shape):
62 super(SingleShapePhysicser, self).__init__(space)
[59]63 self._shape = shape
[186]64 shape.physicser = self
[59]65
[123]66 def get_shape(self):
67 return self._shape
68
[59]69
[133]70def damping_velocity_func(body, gravity, damping, dt):
71 """Apply custom damping to this body's velocity.
72 """
73 damping = getattr(body, 'damping', damping)
74 return pymunk.Body.update_velocity(body, gravity, damping, dt)
75
76
77def make_body(mass, moment, position, damping=None):
78 body = pymunk.Body(mass, moment)
[145]79 body.position = tuple(position)
[133]80 if damping is not None:
81 body.damping = damping
82 body.velocity_func = damping_velocity_func
83 return body
84
85
[59]86class GameObject(object):
87 """A representation of a thing in the game world.
88
89 This has a rendery thing, physicsy things and maybe some other things.
90 """
91
[162]92 zorder = ZORDER_LOW
[218]93 is_moving = False # `True` if a movement animation should play.
[162]94
[191]95 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
[93]96 self.physicser = physicser
[123]97 physicser.set_game_object(self)
[93]98 self.physicser.add_to_space()
[59]99 self.renderer = renderer
[123]100 renderer.set_game_object(self)
[81]101 self.puzzler = puzzler
[123]102 if puzzler is not None:
103 puzzler.set_game_object(self)
[191]104 self.overlay = overlay
105 if overlay is not None:
106 self.overlay.set_game_object(self)
[59]107
[123]108 def get_space(self):
109 return self.physicser.get_space()
110
111 def get_shape(self):
112 return self.physicser.get_shape()
113
[93]114 def get_render_position(self, surface):
115 return self.physicser.get_render_position(surface)
116
117 def get_render_angle(self):
118 return self.physicser.get_angle()
[59]119
120 def render(self, surface):
[123]121 return self.renderer.render(surface)
[81]122
[143]123 def animate(self):
124 self.renderer.animate()
125
[186]126 def collide_with_protagonist(self):
127 """Called as a `pre_solve` collision callback with the protagonist.
128
129 You can return `False` to ignore the collision, anything else
130 (including `None`) to process the collision as normal.
131 """
[192]132 return True
[186]133
[81]134
135class FloorSwitch(GameObject):
[162]136 zorder = ZORDER_FLOOR
137
[93]138 def __init__(self, space, position):
[145]139 body = make_body(None, None, position)
[81]140 self.shape = pymunk.Circle(body, 30)
141 self.shape.collision_type = COLLISION_TYPE_SWITCH
142 self.shape.sensor = True
143 super(FloorSwitch, self).__init__(
[93]144 SingleShapePhysicser(space, self.shape),
[207]145 render.ImageStateRenderer({
[162]146 True: resources.get_image('objects', 'sensor_on.png'),
147 False: resources.get_image('objects', 'sensor_off.png'),
148 }),
[201]149 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
[81]150 )
151
[106]152
[191]153class Note(GameObject):
154 zorder = ZORDER_FLOOR
155
156 def __init__(self, space, position, message):
157 body = make_body(None, None, position)
158 self.shape = pymunk.Circle(body, 30)
159 self.shape.sensor = True
160 super(Note, self).__init__(
161 SingleShapePhysicser(space, self.shape),
[207]162 render.ImageRenderer(resources.get_image('objects', 'note.png')),
[201]163 puzzle.CollidePuzzler(),
[222]164 render.TextOverlay(message),
[191]165 )
166
167
[106]168class FloorLight(GameObject):
[162]169 zorder = ZORDER_FLOOR
170
[106]171 def __init__(self, space, position, state_source):
[145]172 body = make_body(None, None, position)
[106]173 self.shape = pymunk.Circle(body, 10)
174 self.shape.collision_type = COLLISION_TYPE_SWITCH
175 self.shape.sensor = True
176 super(FloorLight, self).__init__(
177 SingleShapePhysicser(space, self.shape),
[207]178 render.ImageStateRenderer({
[162]179 True: resources.get_image('objects', 'light_on.png'),
180 False: resources.get_image('objects', 'light_off.png'),
181 }),
[201]182 puzzle.StateProxyPuzzler(state_source),
[106]183 )
[133]184
185
186class Box(GameObject):
187 def __init__(self, space, position):
188 body = make_body(10, 10000, position, damping=0.5)
189 self.shape = pymunk.Poly(
190 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
[208]191 self.shape.friction = 0.5
[133]192 self.shape.collision_type = COLLISION_TYPE_BOX
193 super(Box, self).__init__(
194 SingleShapePhysicser(space, self.shape),
[207]195 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]196 )
[176]197
198
199class Door(GameObject):
200 zorder = ZORDER_FLOOR
201
[186]202 def __init__(self, space, position, destination, dest_pos, key_state=None):
[176]203 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
204 self.shape = pymunk.Poly(
205 body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
206 self.shape.collision_type = COLLISION_TYPE_DOOR
207 self.shape.sensor = True
208 self.destination = destination
209 self.dest_pos = tuple(dest_pos)
[186]210 if key_state is None:
[201]211 puzzler = puzzle.YesPuzzler()
[186]212 else:
[201]213 puzzler = puzzle.StateProxyPuzzler(key_state)
[176]214 super(Door, self).__init__(
215 SingleShapePhysicser(space, self.shape),
[207]216 render.ImageRenderer(resources.get_image('objects', 'door.png')),
[186]217 puzzler,
[176]218 )
219
[188]220 def collide_with_protagonist(self):
221 if self.puzzler.get_state():
222 DoorEvent.post(self.destination, self.dest_pos)
Note: See TracBrowser for help on using the repository browser.