X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=koperkapel%2Fworld.py;h=a07b719a41a79e5882d8fcb7b2da03dfdb8bb7b9;hb=HEAD;hp=d68294949f46a7d520275a19532737cdc02db7c5;hpb=f21ab7ca426048a66640c8651bb6b015485a9770;p=koperkapel.git diff --git a/koperkapel/world.py b/koperkapel/world.py index d682949..a07b719 100644 --- a/koperkapel/world.py +++ b/koperkapel/world.py @@ -1,6 +1,7 @@ """ World and player state. """ from .scenes.base import WorldEvent +from .roaches import build_roach class World: @@ -20,14 +21,9 @@ class World: def _build_initial_state(self): state = {} state["roaches"] = [ - self._build_roach("roachel", smart=True), - self._build_roach("roeginald", strong=True), - self._build_roach("roichard", fast=True), - self._build_roach("roupert"), - ] - state["serums"] = [ - "smart", "strong", "fast", + build_roach(self, "roupert"), ] + state["serums"] = [] state["vehicles"] = { "current": "walking", "walking": { @@ -35,41 +31,58 @@ class World: "roachel", None, "roeginald", None, None, None, ] - } + }, + "robot": {"seating": []}, + "roomba": {"seating": []}, + "quadcopter": {"seating": []}, + } + state["weapons"] = { + "current": "spit", } state["level"] = { "name": "level1", } return state - def _build_roach(self, name, health=5, **kw): - roach = { - "name": name, - "health": health, - } - roach.update(kw) - return roach + def _get_obj(self, name): + parts = name.split(".") + obj = self._state + for p in parts[:-1]: + if isinstance(obj, dict): + obj = obj[p] + elif isinstance(obj, list): + obj = obj[int(p)] + else: + raise KeyError("%r not found in world" % (name,)) + return obj, parts[-1] + + def _apply_set(self, action, updates): + for name, value in updates.items(): + obj, key = self._get_obj(name) + obj[key] = value - def _apply_set(self, updates): + def _apply_append(self, action, updates): for name, value in updates.items(): - parts = name.split(".") - obj = self._state - for p in parts[:-1]: - if isinstance(obj, dict): - obj = obj[p] - elif isinstance(obj, list): - obj = obj[int(p)] - else: - raise KeyError("%r not found in world" % (name,)) - obj[parts[-1]] = value + obj, key = self._get_obj(name) + obj.append(value) + + def _apply_pop(self, action, updates): + for name, pos in updates.items(): + obj, key = self._get_obj(name) + obj.pop(pos) + + def _apply_reset(self, action): + self._state = self._build_initial_state() + + def _apply_unknown(self, action, *args, **kw): + raise ValueError("Unknown world event action: %r" % (action,)) def proxy(self): return WorldDictProxy(self._state) def apply_event(self, action, *args, **kw): - if action == "set": - return self._apply_set(*args, **kw) - raise ValueError("Unknown world event action: %r" % (action,)) + handler = getattr(self, "_apply_%s" % (action,)) + return handler(action, *args, **kw) def _maybe_subproxy(proxy, name, value): @@ -99,8 +112,8 @@ class WorldBaseProxy: "_events": _events, }) - def _record_change(self, fullname, value): - self._events.append(WorldEvent("set", { + def _record_change(self, fullname, value, action="set"): + self._events.append(WorldEvent(action, { fullname: value })) @@ -112,10 +125,6 @@ class WorldBaseProxy: class WorldDictProxy(WorldBaseProxy): """ World dictionary proxy that records changes and produces events. """ - def items(self): - return ( - (k, _maybe_subproxy(self, k, v)) for k, v in self._state.items()) - def __setattr__(self, name, value): self._top._record_change("%s%s" % (self._prefix, name), value) @@ -130,6 +139,10 @@ class WorldDictProxy(WorldBaseProxy): def __getitem__(self, name): return self.__getattr__(name) + def items(self): + return ( + (k, _maybe_subproxy(self, k, v)) for k, v in self._state.items()) + class WorldListProxy(WorldBaseProxy): """ World list proxy that records changes and produces events. """ @@ -145,3 +158,9 @@ class WorldListProxy(WorldBaseProxy): def __bool__(self): return bool(self._state) + + def append(self, value): + self._top._record_change(self._prefix, value, action="append") + + def pop(self, pos=0): + self._top._record_change(self._prefix, pos, action="pop")