Changeset 521:5a8a7f17597d


Ignore:
Timestamp:
04/09/11 21:36:05 (12 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Phase:
public
Message:

Add support for 'if': '...' to dialogue choices.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • scripts/npc-test

    r483 r521  
    4040        print "%s:" % dsm.state, text
    4141        print "--"
    42         for i, choice in state.choices:
     42        for i, choice in dsm.choices():
    4343            print "%d: %s" % (i, choice)
    4444        if state.auto_next:
     
    9090        state.enter(my_locals)
    9191        state.leave(my_locals)
     92        list(state.choices(my_locals))
    9293    print "ok"
    9394
  • skaapsteker/dialogue.py

    r490 r521  
    5050        AddSpriteEvent.post(sprite)
    5151
    52     def event(self, ev):
     52    def _make_locals(self):
    5353        my_locals = {
    5454            "state": self.states,
     
    5858            "drop_item": self._drop_item,
    5959        }
     60        return my_locals
     61
     62    def choices(self):
     63        my_locals = self._make_locals()
     64        state = self.states[self.state]
     65        return state.choices(my_locals)
     66
     67    def event(self, ev):
     68        my_locals = self._make_locals()
    6069        my_locals.update(ev.items)
    6170        state = self.states[self.state]
     
    104113        self.name = name
    105114        self.text = state_src.get("text", None)
    106         self.choices = []
     115        self._choices = []
    107116        self.triggers = []
    108117
     
    110119        for i, choice in enumerate(choices):
    111120            pseudo_path = base_path + ["choice-%d" % i]
    112             self.choices.append((i, choice["text"]))
     121            if "if" in choice:
     122                choice_if = compile(choice["if"],
     123                                    "<%s>" % ":".join(pseudo_path + ["if"]),
     124                                    "eval")
     125            else:
     126                choice_if = None
     127            self._choices.append((i, choice["text"], choice_if))
    113128            next_state_code = choice.get("next", None)
    114129            if next_state_code is not None:
     
    127142        if auto_next is not None:
    128143            self.auto_next = True
    129             assert not self.choices, "%s: auto_next and choices are not compatible" % ":".join(base_path)
     144            assert not self._choices, "%s: auto_next and choices are not compatible" % ":".join(base_path)
    130145            pseudo_path = base_path + ["auto_next"]
    131146            self.triggers.append(Trigger("""auto_next""", auto_next, pseudo_path))
     
    150165        return "<%r name=%r>" % (self.__class__.__name__, self.name)
    151166
     167    def choices(self, my_locals):
     168        for i, text, choice_if in self._choices:
     169            if choice_if is None:
     170                yield i, text
     171            elif eval(choice_if, {}, my_locals.copy()):
     172                yield i, text
     173
    152174    def event(self, my_locals):
    153175        for trigger in self.triggers:
  • skaapsteker/widgets/bubble.py

    r507 r521  
    5959        else:
    6060            self._text = None
    61         options = [(text, i) for (i, text) in state.choices]
     61        options = [(text, i) for (i, text) in self.dsm.choices()]
    6262        if state.auto_next:
    6363            text = state.auto_next_text if state.auto_next_text else "Next"
Note: See TracChangeset for help on using the changeset viewer.