source: nagslang/game_object.py@ 216:f23ab2dd6ce8

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

Clunkier properties to make pyflakes happy.

File size: 7.4 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
[216]46 def _get_position(self):
[215]47 return self.get_shape().body.position
48
[216]49 def _set_position(self, position):
[215]50 self.get_shape().body.position = position
[93]51
[216]52 position = property(_get_position, _set_position)
53
[93]54 def apply_impulse(self, j, r=(0, 0)):
[215]55 return self.get_shape().body.apply_impulse(j, r)
[59]56
57
58class SingleShapePhysicser(Physicser):
[93]59 def __init__(self, space, shape):
60 super(SingleShapePhysicser, self).__init__(space)
[59]61 self._shape = shape
[186]62 shape.physicser = self
[59]63
[123]64 def get_shape(self):
65 return self._shape
66
[59]67
[133]68def damping_velocity_func(body, gravity, damping, dt):
69 """Apply custom damping to this body's velocity.
70 """
71 damping = getattr(body, 'damping', damping)
72 return pymunk.Body.update_velocity(body, gravity, damping, dt)
73
74
75def make_body(mass, moment, position, damping=None):
76 body = pymunk.Body(mass, moment)
[145]77 body.position = tuple(position)
[133]78 if damping is not None:
79 body.damping = damping
80 body.velocity_func = damping_velocity_func
81 return body
82
83
[191]84class Overlay(object):
85 def set_game_object(self, game_object):
86 self.game_object = game_object
87
[211]88 def render(self, surface, display_offset):
[191]89 pass
90
91 def is_visible(self):
92 return self.game_object.puzzler.get_state()
93
94
95class TextOverlay(Overlay):
96 def __init__(self, text):
97 self.text = text
[196]98 self.widget = LabelWidget((20, 20), self.text)
[191]99
[211]100 def render(self, surface, display_offset):
101 x, y = 20, 20
102 if display_offset[0] < 0:
103 x += abs(display_offset[0])
104 if display_offset[1] < 0:
105 y += abs(display_offset[1])
106 self.widget.rect.topleft = (x, y)
[191]107 self.widget.draw(surface)
108
109
[59]110class GameObject(object):
111 """A representation of a thing in the game world.
112
113 This has a rendery thing, physicsy things and maybe some other things.
114 """
115
[162]116 zorder = ZORDER_LOW
117
[191]118 def __init__(self, physicser, renderer, puzzler=None, overlay=None):
[93]119 self.physicser = physicser
[123]120 physicser.set_game_object(self)
[93]121 self.physicser.add_to_space()
[59]122 self.renderer = renderer
[123]123 renderer.set_game_object(self)
[81]124 self.puzzler = puzzler
[123]125 if puzzler is not None:
126 puzzler.set_game_object(self)
[191]127 self.overlay = overlay
128 if overlay is not None:
129 self.overlay.set_game_object(self)
[59]130
[123]131 def get_space(self):
132 return self.physicser.get_space()
133
134 def get_shape(self):
135 return self.physicser.get_shape()
136
[93]137 def get_render_position(self, surface):
138 return self.physicser.get_render_position(surface)
139
140 def get_render_angle(self):
141 return self.physicser.get_angle()
[59]142
143 def render(self, surface):
[123]144 return self.renderer.render(surface)
[81]145
[143]146 def animate(self):
147 self.renderer.animate()
148
[186]149 def collide_with_protagonist(self):
150 """Called as a `pre_solve` collision callback with the protagonist.
151
152 You can return `False` to ignore the collision, anything else
153 (including `None`) to process the collision as normal.
154 """
[192]155 return True
[186]156
[81]157
158class FloorSwitch(GameObject):
[162]159 zorder = ZORDER_FLOOR
160
[93]161 def __init__(self, space, position):
[145]162 body = make_body(None, None, position)
[81]163 self.shape = pymunk.Circle(body, 30)
164 self.shape.collision_type = COLLISION_TYPE_SWITCH
165 self.shape.sensor = True
166 super(FloorSwitch, self).__init__(
[93]167 SingleShapePhysicser(space, self.shape),
[207]168 render.ImageStateRenderer({
[162]169 True: resources.get_image('objects', 'sensor_on.png'),
170 False: resources.get_image('objects', 'sensor_off.png'),
171 }),
[201]172 puzzle.CollidePuzzler(*SWITCH_PUSHERS),
[81]173 )
174
[106]175
[191]176class Note(GameObject):
177 zorder = ZORDER_FLOOR
178
179 def __init__(self, space, position, message):
180 body = make_body(None, None, position)
181 self.shape = pymunk.Circle(body, 30)
182 self.shape.sensor = True
183 super(Note, self).__init__(
184 SingleShapePhysicser(space, self.shape),
[207]185 render.ImageRenderer(resources.get_image('objects', 'note.png')),
[201]186 puzzle.CollidePuzzler(),
[191]187 TextOverlay(message),
188 )
189
190
[106]191class FloorLight(GameObject):
[162]192 zorder = ZORDER_FLOOR
193
[106]194 def __init__(self, space, position, state_source):
[145]195 body = make_body(None, None, position)
[106]196 self.shape = pymunk.Circle(body, 10)
197 self.shape.collision_type = COLLISION_TYPE_SWITCH
198 self.shape.sensor = True
199 super(FloorLight, self).__init__(
200 SingleShapePhysicser(space, self.shape),
[207]201 render.ImageStateRenderer({
[162]202 True: resources.get_image('objects', 'light_on.png'),
203 False: resources.get_image('objects', 'light_off.png'),
204 }),
[201]205 puzzle.StateProxyPuzzler(state_source),
[106]206 )
[133]207
208
209class Box(GameObject):
210 def __init__(self, space, position):
211 body = make_body(10, 10000, position, damping=0.5)
212 self.shape = pymunk.Poly(
213 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
[208]214 self.shape.friction = 0.5
[133]215 self.shape.collision_type = COLLISION_TYPE_BOX
216 super(Box, self).__init__(
217 SingleShapePhysicser(space, self.shape),
[207]218 render.ImageRenderer(resources.get_image('objects', 'crate.png')),
[133]219 )
[176]220
221
222class Door(GameObject):
223 zorder = ZORDER_FLOOR
224
[186]225 def __init__(self, space, position, destination, dest_pos, key_state=None):
[176]226 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
227 self.shape = pymunk.Poly(
228 body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
229 self.shape.collision_type = COLLISION_TYPE_DOOR
230 self.shape.sensor = True
231 self.destination = destination
232 self.dest_pos = tuple(dest_pos)
[186]233 if key_state is None:
[201]234 puzzler = puzzle.YesPuzzler()
[186]235 else:
[201]236 puzzler = puzzle.StateProxyPuzzler(key_state)
[176]237 super(Door, self).__init__(
238 SingleShapePhysicser(space, self.shape),
[207]239 render.ImageRenderer(resources.get_image('objects', 'door.png')),
[186]240 puzzler,
[176]241 )
242
[188]243 def collide_with_protagonist(self):
244 if self.puzzler.get_state():
245 DoorEvent.post(self.destination, self.dest_pos)
Note: See TracBrowser for help on using the repository browser.