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

Revision 287, 2.7 KB checked in by orlandov, 10 years ago (diff)

Ticket #74: Patch by or1andov, Integrate dialogue GUI and engine into game.

It should now be possible to engage in a conversation with NPC characters by
selecting talk from their context menu. fixes[s:trac, t:74]

Known Issues:


  • the GUI is terrible. instead of a dialog window, we could use a better looking background image and fixed position widgets
  • requires PyYAML to be installed sudo apt-get install python-yaml easy_install PyYAML
  • we have to create a simple state model for the PC can remember tasks
  • need to make NPC's be able to remember things - need a model in NPCs for this
  • no way currently to give or take items from npc's; again, more model work. this could be done by adding another callback similar to start_quest. may be necessary for npc's to have an inventory for this
  • The dialogue engine is not hooked up to specific NPC's... it currently always loads dialogue/sample.yaml - need to have the dialogue looked up from some XML attribute
  • dialogue is not broken off if the PC moves away from the NPC
  • npc image is hardcoded, it should use the image specified in the yaml file
  • 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.