Use WorldEvent to switch level.
[koperkapel.git] / koperkapel / world.py
1 """ World and player state. """
2
3
4 class World:
5     """ World and player state. """
6
7     def __init__(self):
8         self._state = self._build_initial_state()
9
10     @property
11     def level(self):
12         return self._state["level"]
13
14     @property
15     def roaches(self):
16         return self._state["roaches"]
17
18     def _build_initial_state(self):
19         state = {}
20         state["roaches"] = [
21             self._build_roach("roachel", intelligence=3),
22             self._build_roach("roeginald", strength=3),
23             self._build_roach("roichard", quickness=3),
24         ]
25         state["level"] = {
26             "name": "level1",
27         }
28         return state
29
30     def _build_roach(self, name, **kw):
31         attributes = {
32             "intelligence": 1,
33             "strength": 1,
34             "quickness": 1,
35             "health": 5,
36         }
37         attributes.update(kw)
38         return {
39             "name": name,
40             "attributes": attributes,
41         }
42
43     def _apply_set(self, updates):
44         for name, value in updates.items():
45             parts = name.split(".")
46             obj = self._state
47             for p in parts[:-1]:
48                 obj = obj[p]
49             obj[parts[-1]] = value
50
51     def apply_event(self, action, *args, **kw):
52         if action == "set":
53             return self._apply_set(*args, **kw)
54         raise ValueError("Unknown world event action: %r" % (action,))