Add a list of actors to scenes.
[koperkapel.git] / koperkapel / scenes / base.py
1 """ Scene utilities. """
2
3 import functools
4
5
6 def apply_events(f):
7     """ Decorator that applies events to an engine. """
8     @functools.wraps(f)
9     def wrap(self, *args, **kw):
10         events = f(self, *args, **kw)
11         self._apply_events(events)
12     return wrap
13
14
15 class Engine:
16     """ A holder for game state. """
17
18     def __init__(self, app, scene):
19         self._app = app
20         self._scene = scene
21
22     def _apply_events(self, events):
23         if not events:
24             return
25         for ev in events:
26             ev.apply(self)
27
28     def change_scene(self, scene):
29         self._scene.exit()
30         self._scene = scene
31         self._scene.enter()
32
33     @apply_events
34     def update(self, dt):
35         return self._scene.update(dt)
36
37     def draw(self):
38         self._scene.draw(self._app.screen)
39
40     @apply_events
41     def on_mouse_down(self, pos, button):
42         return self._scene.on_mouse_down(pos, button)
43
44     @apply_events
45     def on_mouse_up(self, pos, button):
46         return self._scene.on_mouse_up(pos, button)
47
48     @apply_events
49     def on_key_down(self, key, mod, unicode):
50         return self._scene.on_key_down(key, mod, unicode)
51
52     @apply_events
53     def on_key_up(self, key, mod):
54         return self._scene.on_key_up(key, mod)
55
56     @apply_events
57     def on_music_end(self):
58         return self._scene.on_music_end()
59
60
61 class Event:
62     """ Base class for events. """
63
64     ENGINE_METHOD = "unknown_event"
65
66     def __init__(self, *args, **kw):
67         self._args = args
68         self._kw = kw
69
70     def apply(self, engine):
71         getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
72
73
74 class ChangeSceneEvent(Event):
75     """ Change to a new scene. """
76
77     ENGINE_METHOD = "change_scene"
78
79
80 class Actors:
81     """ A list of actors. """
82
83     def __init__(self):
84         self._actors = []
85
86     def add(self, actor):
87         self._actors.append(actor)
88         return actor
89
90     def remove(self, actor):
91         self._actors.remove(actor)
92         return actor
93
94     def draw(self, screen):
95         for actor in self._actors:
96             actor.draw()  # TODO: allow an option screen to be passed in
97
98
99 class Scene:
100     """ Base class for scenes. """
101
102     def __init__(self):
103         self.actors = Actors()
104
105     def enter(self):
106         pass
107
108     def exit(self):
109         pass
110
111     def update(self, dt):
112         pass
113
114     def draw(self, screen):
115         screen.clear()
116         self.actors.draw(screen)
117
118     def on_mouse_down(self, pos, button):
119         pass
120
121     def on_mouse_up(self, pos, button):
122         pass
123
124     def on_key_down(self, key, mod, unicode):
125         pass
126
127     def on_key_up(self, key, mod):
128         pass
129
130     def on_music_end(self):
131         pass