source: nagslang/collectable.py@ 378:8069c9be1c3e

Last change on this file since 378:8069c9be1c3e was 378:8069c9be1c3e, checked in by Stefano Rivera <stefano@…>, 8 years ago

Create a nagslang.collectable module for collectables

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