58f3874f204b87a874751d593f4360a785791a70
[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     def quit_game(self):
34         from pgzero.game import exit
35         exit()
36
37     @apply_events
38     def update(self, dt):
39         return self._scene.update(dt)
40
41     def draw(self):
42         self._scene.draw(self._app.screen)
43
44     @apply_events
45     def on_mouse_down(self, pos, button):
46         return self._scene.on_mouse_down(pos, button)
47
48     @apply_events
49     def on_mouse_up(self, pos, button):
50         return self._scene.on_mouse_up(pos, button)
51
52     @apply_events
53     def on_key_down(self, key, mod, unicode):
54         return self._scene.on_key_down(key, mod, unicode)
55
56     @apply_events
57     def on_key_up(self, key, mod):
58         return self._scene.on_key_up(key, mod)
59
60     @apply_events
61     def on_music_end(self):
62         return self._scene.on_music_end()
63
64
65 class Event:
66     """ Base class for events. """
67
68     ENGINE_METHOD = "unknown_event"
69
70     def __init__(self, *args, **kw):
71         self._args = args
72         self._kw = kw
73
74     def apply(self, engine):
75         getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
76
77
78 class ChangeSceneEvent(Event):
79     """ Change to a new scene. """
80
81     ENGINE_METHOD = "change_scene"
82
83
84 class QuitEvent(Event):
85     """ Quit the game. """
86
87     ENGINE_METHOD = "quit_game"
88
89
90 class Actors:
91     """ A list of actors. """
92
93     def __init__(self):
94         self._actors = []
95
96     def add(self, actor):
97         self._actors.append(actor)
98         return actor
99
100     def remove(self, actor):
101         self._actors.remove(actor)
102         return actor
103
104     def draw(self, screen):
105         for actor in self._actors:
106             actor.draw()  # TODO: allow an option screen to be passed in
107
108
109 class Scene:
110     """ Base class for scenes. """
111
112     def __init__(self):
113         self.actors = Actors()
114
115     def enter(self):
116         pass
117
118     def exit(self):
119         pass
120
121     def update(self, dt):
122         pass
123
124     def draw(self, screen):
125         screen.clear()
126         self.actors.draw(screen)
127
128     def on_mouse_down(self, pos, button):
129         pass
130
131     def on_mouse_up(self, pos, button):
132         pass
133
134     def on_key_down(self, key, mod, unicode):
135         pass
136
137     def on_key_up(self, key, mod):
138         pass
139
140     def on_music_end(self):
141         pass