source: nagslang/game_object.py@ 207:42e8993c31fd

Last change on this file since 207:42e8993c31fd was 207:42e8993c31fd, checked in by Stefano Rivera <stefano@…>, 8 years ago

Break out Renderers

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