source: nagslang/collectable.py @ 385:51deb78cae52

Last change on this file since 385:51deb78cae52 was 385:51deb78cae52, checked in by Stefano Rivera <stefano@…>, 7 years ago

Use a result object to get new drawables back to the area

File size: 1.8 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
11class CollectibleGameObject(GameObject):
12    zorder = ZORDER_LOW
13
14    def __init__(self, space, name, shape, renderer):
15        self._name = name
16        self.collected = False
17        shape.sensor = True
18        super(CollectibleGameObject, self).__init__(
19            SingleShapePhysicser(space, shape),
20            renderer,
21            interactible=environment.Interactible(
22                environment.Action(
23                    self._collect, environment.HumanFormCondition())),
24        )
25
26    def _collect(self, protagonist):
27        protagonist.add_item(self._name)
28        self.physicser.remove_from_space()
29        self.collected = True
30
31    def update(self, dt):
32        if self.collected:
33            return Result(remove=[self])
34
35
36class Gun(CollectibleGameObject):
37    def __init__(self, space, position):
38        body = make_body(None, None, position)
39        self.shape = pymunk.Circle(body, 20)
40        super(Gun, self).__init__(
41            space, 'gun', self.shape,
42            render.ImageRenderer(resources.get_image('objects', 'gun.png')),
43        )
44
45
46class KeyCard(CollectibleGameObject):
47    def __init__(self, space, position, name):
48        body = make_body(None, None, position)
49        self.shape = pymunk.Circle(body, 20)
50        super(KeyCard, self).__init__(
51            space, name, self.shape,
52            render.ImageRenderer(
53                resources.get_image('objects', '%s.png' % (name,))),
54        )
55
56    @classmethod
57    def requires(cls):
58        return [("name", "string"), ("position", "coordinates"),
59                ("item_name", "string")]
Note: See TracBrowser for help on using the repository browser.