source: nagslang/game_object.py@ 221:0c0d5919f70a

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

Better rendering and movement detection.

File size: 7.5 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
[218]120 is_moving = False # `True` if a movement animation should play.
[162]121
[191]122 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
[93]123 self.physicser = physicser
[123]124 physicser.set_game_object(self)
[93]125 self.physicser.add_to_space()
[59]126 self.renderer = renderer
[123]127 renderer.set_game_object(self)
[81]128 self.puzzler = puzzler
[123]129 if puzzler is not None:
130 puzzler.set_game_object(self)
[191]131 self.overlay = overlay
132 if overlay is not None:
133 self.overlay.set_game_object(self)
[59]134
[123]135 def get_space(self):
136 return self.physicser.get_space()
137
138 def get_shape(self):
139 return self.physicser.get_shape()
140
[93]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()
[59]146
147 def render(self, surface):
[123]148 return self.renderer.render(surface)
[81]149
[143]150 def animate(self):
151 self.renderer.animate()
152
[186]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 """
[192]159 return True
[186]160
[81]161
162class FloorSwitch(GameObject):
[162]163 zorder = ZORDER_FLOOR
164
[93]165 def __init__(self, space, position):
[145]166 body = make_body(None, None, position)
[81]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__(
[93]171 SingleShapePhysicser(space, self.shape),
[207]172 render.ImageStateRenderer({
[162]173 True: resources.get_image('objects', 'sensor_on.png'),
174 False: resources.get_image('objects', 'sensor_off.png'),
175 }),
[201]176 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
[81]177 )
178
[106]179
[191]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),
[207]189 render.ImageRenderer(resources.get_image('objects', 'note.png')),
[201]190 puzzle.CollidePuzzler(),
[191]191 TextOverlay(message),
192 )
193
194
[106]195class FloorLight(GameObject):
[162]196 zorder = ZORDER_FLOOR
197
[106]198 def __init__(self, space, position, state_source):
[145]199 body = make_body(None, None, position)
[106]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),
[207]205 render.ImageStateRenderer({
[162]206 True: resources.get_image('objects', 'light_on.png'),
207 False: resources.get_image('objects', 'light_off.png'),
208 }),
[201]209 puzzle.StateProxyPuzzler(state_source),
[106]210 )
[133]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)])
[208]218 self.shape.friction = 0.5
[133]219 self.shape.collision_type = COLLISION_TYPE_BOX
220 super(Box, self).__init__(
221 SingleShapePhysicser(space, self.shape),
[207]222 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]223 )
[176]224
225
226class Door(GameObject):
227 zorder = ZORDER_FLOOR
228
[186]229 def __init__(self, space, position, destination, dest_pos, key_state=None):
[176]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)
[186]237 if key_state is None:
[201]238 puzzler = puzzle.YesPuzzler()
[186]239 else:
[201]240 puzzler = puzzle.StateProxyPuzzler(key_state)
[176]241 super(Door, self).__init__(
242 SingleShapePhysicser(space, self.shape),
[207]243 render.ImageRenderer(resources.get_image('objects', 'door.png')),
[186]244 puzzler,
[176]245 )
246
[188]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.