source: nagslang/collectable.py @ 528:811481b20689

Last change on this file since 528:811481b20689 was 528:811481b20689, checked in by Simon Cross <hodgestar@…>, 7 years ago

Add support for collectables.

File size: 2.0 KB
Line 
1import pymunk
2
3from nagslang import environment
4from nagslang import render
5from nagslang.constants import ZORDER_LOW
6from nagslang.game_object import (GameObject, SingleShapePhysicser, Result,
7                                  make_body)
8from nagslang.resources import resources
9
10
11def get_editable_game_objects():
12    classes = []
13    for cls_name, cls in globals().iteritems():
14        if isinstance(cls, type) and hasattr(cls, 'requires'):
15            classes.append((cls_name, cls))
16    return classes
17
18
19class CollectibleGameObject(GameObject):
20    zorder = ZORDER_LOW
21
22    def __init__(self, space, name, shape, renderer):
23        self._name = name
24        self.collected = False
25        shape.sensor = True
26        super(CollectibleGameObject, self).__init__(
27            SingleShapePhysicser(space, shape),
28            renderer,
29            interactible=environment.Interactible(
30                environment.Action(
31                    self._collect, environment.HumanFormCondition())),
32        )
33
34    def _collect(self, protagonist):
35        protagonist.add_item(self._name)
36        self.physicser.remove_from_space()
37        self.collected = True
38
39    def update(self, dt):
40        if self.collected:
41            return Result(remove=[self])
42
43
44class Gun(CollectibleGameObject):
45    def __init__(self, space, position):
46        body = make_body(None, None, position)
47        self.shape = pymunk.Circle(body, 20)
48        super(Gun, self).__init__(
49            space, 'gun', self.shape,
50            render.ImageRenderer(resources.get_image('objects', 'gun.png')),
51        )
52
53
54class KeyCard(CollectibleGameObject):
55    def __init__(self, space, position, name):
56        body = make_body(None, None, position)
57        self.shape = pymunk.Circle(body, 20)
58        super(KeyCard, self).__init__(
59            space, name, self.shape,
60            render.ImageRenderer(
61                resources.get_image('objects', '%s.png' % (name,))),
62        )
63
64    @classmethod
65    def requires(cls):
66        return [("name", "string"), ("position", "coordinates"),
67                ("item_name", "string")]
Note: See TracBrowser for help on using the repository browser.