source: nagslang/game_object.py @ 229:329b3044ddef

Last change on this file since 229:329b3044ddef was 229:329b3044ddef, checked in by Jeremy Thurgood <firxen@…>, 7 years ago

Much better facing renderers.

File size: 7.7 KB
Line 
1import pymunk
2import pymunk.pygame_util
3
4from nagslang import puzzle
5from nagslang import render
6from nagslang.constants import (
7    SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
8    ZORDER_FLOOR, COLLISION_TYPE_DOOR)
9from nagslang.resources import resources
10from nagslang.events import DoorEvent
11
12
13class Physicser(object):
14    def __init__(self, space):
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()
25
26    def add_to_space(self):
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)
31
32    def remove_from_space(self):
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)
37
38    def get_render_position(self, surface):
39        pos = self.get_shape().body.position
40        return pymunk.pygame_util.to_pygame(pos, surface)
41
42    def get_angle(self):
43        return self.get_shape().body.angle
44
45    def get_velocity(self):
46        return self.get_shape().body.velocity
47
48    def _get_position(self):
49        return self.get_shape().body.position
50
51    def _set_position(self, position):
52        self.get_shape().body.position = position
53
54    position = property(_get_position, _set_position)
55
56    def apply_impulse(self, j, r=(0, 0)):
57        return self.get_shape().body.apply_impulse(j, r)
58
59
60class SingleShapePhysicser(Physicser):
61    def __init__(self, space, shape):
62        super(SingleShapePhysicser, self).__init__(space)
63        self._shape = shape
64        shape.physicser = self
65
66    def get_shape(self):
67        return self._shape
68
69
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)
79    body.position = tuple(position)
80    if damping is not None:
81        body.damping = damping
82        body.velocity_func = damping_velocity_func
83    return body
84
85
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
92    zorder = ZORDER_LOW
93    is_moving = False  # `True` if a movement animation should play.
94
95    def __init__(self, physicser, renderer, puzzler=None, overlay=None):
96        self.physicser = physicser
97        physicser.set_game_object(self)
98        self.physicser.add_to_space()
99        self.renderer = renderer
100        renderer.set_game_object(self)
101        self.puzzler = puzzler
102        if puzzler is not None:
103            puzzler.set_game_object(self)
104        self.overlay = overlay
105        if overlay is not None:
106            self.overlay.set_game_object(self)
107
108    def get_space(self):
109        return self.physicser.get_space()
110
111    def get_shape(self):
112        return self.physicser.get_shape()
113
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()
119
120    def get_facing_direction(self):
121        """Used by rendererd that care what direction an object is facing.
122        """
123        return None
124
125    def render(self, surface):
126        return self.renderer.render(surface)
127
128    def animate(self):
129        self.renderer.animate()
130
131    def collide_with_protagonist(self):
132        """Called as a `pre_solve` collision callback with the protagonist.
133
134        You can return `False` to ignore the collision, anything else
135        (including `None`) to process the collision as normal.
136        """
137        return True
138
139
140class FloorSwitch(GameObject):
141    zorder = ZORDER_FLOOR
142
143    def __init__(self, space, position):
144        body = make_body(None, None, position)
145        self.shape = pymunk.Circle(body, 30)
146        self.shape.collision_type = COLLISION_TYPE_SWITCH
147        self.shape.sensor = True
148        super(FloorSwitch, self).__init__(
149            SingleShapePhysicser(space, self.shape),
150            render.ImageStateRenderer({
151                True: resources.get_image('objects', 'sensor_on.png'),
152                False: resources.get_image('objects', 'sensor_off.png'),
153            }),
154            puzzle.CollidePuzzler(*SWITCH_PUSHERS),
155        )
156
157
158class Note(GameObject):
159    zorder = ZORDER_FLOOR
160
161    def __init__(self, space, position, message):
162        body = make_body(None, None, position)
163        self.shape = pymunk.Circle(body, 30)
164        self.shape.sensor = True
165        super(Note, self).__init__(
166            SingleShapePhysicser(space, self.shape),
167            render.ImageRenderer(resources.get_image('objects', 'note.png')),
168            puzzle.CollidePuzzler(),
169            render.TextOverlay(message),
170        )
171
172
173class FloorLight(GameObject):
174    zorder = ZORDER_FLOOR
175
176    def __init__(self, space, position, state_source):
177        body = make_body(None, None, position)
178        self.shape = pymunk.Circle(body, 10)
179        self.shape.collision_type = COLLISION_TYPE_SWITCH
180        self.shape.sensor = True
181        super(FloorLight, self).__init__(
182            SingleShapePhysicser(space, self.shape),
183            render.ImageStateRenderer({
184                True: resources.get_image('objects', 'light_on.png'),
185                False: resources.get_image('objects', 'light_off.png'),
186            }),
187            puzzle.StateProxyPuzzler(state_source),
188        )
189
190
191class Box(GameObject):
192    def __init__(self, space, position):
193        body = make_body(10, 10000, position, damping=0.5)
194        self.shape = pymunk.Poly(
195            body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
196        self.shape.friction = 0.5
197        self.shape.collision_type = COLLISION_TYPE_BOX
198        super(Box, self).__init__(
199            SingleShapePhysicser(space, self.shape),
200            render.ImageRenderer(resources.get_image('objects', 'crate.png')),
201        )
202
203
204class Door(GameObject):
205    zorder = ZORDER_FLOOR
206
207    def __init__(self, space, position, destination, dest_pos, key_state=None):
208        body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5)
209        self.shape = pymunk.Poly(
210            body, [(-32, -32), (32, -32), (32, 32), (-32, 32)])
211        self.shape.collision_type = COLLISION_TYPE_DOOR
212        self.shape.sensor = True
213        self.destination = destination
214        self.dest_pos = tuple(dest_pos)
215        if key_state is None:
216            puzzler = puzzle.YesPuzzler()
217        else:
218            puzzler = puzzle.StateProxyPuzzler(key_state)
219        super(Door, self).__init__(
220            SingleShapePhysicser(space, self.shape),
221            render.ImageRenderer(resources.get_image('objects', 'door.png')),
222            puzzler,
223        )
224
225    def collide_with_protagonist(self):
226        if self.puzzler.get_state():
227            DoorEvent.post(self.destination, self.dest_pos)
228
229
230class Bulkhead(GameObject):
231    zorder = ZORDER_FLOOR
232
233    def __init__(self, space, end1, end2, key_state=None):
234        body = make_body(None, None, (0, 0))
235        self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 3)
236        self.shape.collision_type = COLLISION_TYPE_DOOR
237        if key_state is None:
238            puzzler = puzzle.YesPuzzler()
239        else:
240            puzzler = puzzle.StateProxyPuzzler(key_state)
241        super(Bulkhead, self).__init__(
242            SingleShapePhysicser(space, self.shape),
243            render.ShapeStateRenderer(),
244            puzzler,
245        )
246
247    def collide_with_protagonist(self):
248        if self.puzzler.get_state():
249            # Reject the collision, we can walk through.
250            return False
251        return True
Note: See TracBrowser for help on using the repository browser.