source: trunk/game/dialogue-demo.py @ 331

Revision 317, 2.7 KB checked in by barra_parpg, 10 years ago (diff)

Patch by sirren & mvbarracuda:

  • New player character animations
  • Changed PC object id to player for consistency reasons (agentxmlgen.py uses the folder name for the object id)
  • Renamed parpg-editor >> parpg_editor (use underscores as space characters!)
  • SVL eol-style >> native fixes
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18from scripts import dialogue
19import yaml
20import sys
21
22"""
23A very simple demonstration of the dialogue engine in effect. It mocks up a
24simple user object and allows the dialogue engine to interact with it via
25callbacks we provide.
26"""
27
28class Player(object):
29    """
30    Mock player object that always has complete quests
31    """
32    def __init__(self):
33        self.current_quests = set()
34        self.finished_quests = set()
35
36    def canAcceptQuest(self, quest):
37        return     quest not in self.finished_quests \
38               and quest not in self.current_quests
39
40    def hasSatisfiedQuest(self, quest):
41        return quest in self.current_quests
42
43    def startQuest(self, quest):
44        if quest in self.current_quests:
45            raise RuntimeError("Already have quest, %s" % quest)
46        self.current_quests.add(quest)
47
48    def completeQuest(self, quest):
49        self.finished_quests.add(quest)
50        self.current_quests.remove(quest)
51
52def main():
53
54    # set up some closures
55    def say_cb(state, text):
56        print "NPC says:", text
57
58    def get_reply(responses):
59        for i, response in enumerate(responses):
60            print "%d. %s" % (i, response)
61        print "\nChoose a response: ",
62        val = int(sys.stdin.readline().strip())
63        print "you picked %s" % (val,)
64        return val
65
66    def start_quest(state, quest):
67        print "You've picked up the '%s' quest!" % quest,
68        state['pc'].startQuest(quest)
69
70    def complete_quest(state, quest):
71        print "You've finished the '%s' quest" % quest
72        state['pc'].completeQuest(quest)
73
74    callbacks = {
75        "say": say_cb,
76        "start_quest": start_quest,
77        "complete_quest": complete_quest
78    }
79
80    pc = Player()
81
82    state = {
83        'quests': {},
84        'pc': pc
85    }
86
87    dialog = dialogue.DialogueEngine('dialogue/sample.yaml', callbacks, state)
88    responses = dialog.run()
89    while responses:
90        choice = get_reply(responses)
91        responses = dialog.reply(choice)
92
93if __name__ == "__main__":
94    main()
Note: See TracBrowser for help on using the repository browser.