source: nagslang/collectable.py@ 536:d64c416b2d1c

Last change on this file since 536:d64c416b2d1c was 536:d64c416b2d1c, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Three seconds.

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