source: trunk/game/dialogue_demo.py @ 410

Revision 396, 4.9 KB checked in by zenbitz, 10 years ago (diff)

dialogue fixes

  • Property svn:executable set to *
RevLine 
[343]1#!/usr/bin/env 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
[350]19from scripts import quest_engine
[343]20import yaml
21import sys
22
23"""
24A very simple demonstration of the dialogue engine in effect. It mocks up a
25simple user object and allows the dialogue engine to interact with it via
26callbacks we provide.
27"""
28
29class Player(object):
30    """
31    Mock player object that always has complete quests
32    """
33    def __init__(self):
[350]34        self.inventory = set(['beer'])
[343]35        self.peopleIknow = set()
36
37    def meet(self,npc):
38        if npc in self.peopleIknow:
39            raise RuntimeError("I already know %s" % npc)
40        self.peopleIknow.add(npc)
[350]41
[343]42    def met(self,npc):
43        return npc in self.peopleIknow
[350]44
45
46class Beer(object):
47    quality = 3
48
49
[343]50class Box(object):
51    """
52    Mock box object than can be opened or closed
53    This would normally be a container
54    """
55    def __init__(self):
56        self.state = 'closed'
[350]57
[343]58    def isOpen(self):
59        if self.state == 'open':
60            return self.state
61        else:
62            return False
[350]63
[343]64    def isClosed(self):
65        if self.state == 'closed':
66             return self.state
67        else:
68             return False
[350]69
[343]70    def open(self):
71        self.state = 'open'
72        return self.state
[350]73
[343]74    def close(self):
75        self.state = 'closed'
76        return self.state
[350]77
78
[343]79def main(dialogue_file):
80
81    # set up some closures
82    def say_cb(state, text):
83        print "NPC says:", text
84
85    def get_reply(responses):
86        for i, response in enumerate(responses):
87            print "%d. %s" % (i, response)
[350]88
89        while True:
90            try:
91                print "\nChoose a response: ",
92                val = int(sys.stdin.readline().strip())
93            except ValueError:
94                print "That's not a valid value, amigo"
95                continue
96            break
97
[343]98        print "you picked %s" % (val,)
99        return val
100
101    def start_quest(state, quest):
102        print "You've picked up the '%s' quest!" % quest
[350]103        state['quest'].addQuest(quest)
[343]104
[350]105    def complete_quest(state, quest_id):
106        print "You've finished the quest %s" % quest_id
107        state['quest'].finishQuest(quest_id)
108
109    def delete_quest(state, quest_id):
110        print "You've deleted quest %s" % quest_id
111        state['quest'].deleteQuest(quest_id)
112
113    def increase_value(state, quest_id, variable, value):
114        print "Increased %s by %i"%(variable,value)
115        state['quest'][quest_id].increaseValue(variable,value)
116
117    def decrease_value(state, quest_id, variable, value):
118        print "Decreased %s by %i"%(variable,value)
119        state['quest'][quest_id].decreaseValue(variable,value)
120
121    def set_value(state,quest_id, variable, value):
[396]122        print "Set %s to %s"%(variable,value)
[350]123        state['quest'][quest_id].setValue(variable,value)
124
[343]125    def meet(state, npc):
126        print "You've met %s!" % npc
127        state['pc'].meet(npc)
[350]128
[343]129    def get_stuff(state, thing):
130        if thing not in state['pc'].inventory:
131            state['pc'].inventory.add(thing)
132            print "You've now have the %s" % thing
[350]133
[343]134    def take_stuff(state,thing):
135        if thing in state['pc'].inventory:
136            state['pc'].inventory.remove(thing)
137            print "You no longer have the %s" % thing
138
139    callbacks = {
140        "say": say_cb,
141        "start_quest": start_quest,
142        "complete_quest": complete_quest,
[350]143        "delete_quest": delete_quest,
144        "increase_value": increase_value,
145        "decrease_value": decrease_value,
146        "set_value": set_value,
[343]147        "meet": meet,
148        "get_stuff" : get_stuff,
149        "take_stuff" : take_stuff
150    }
151
152    pc = Player()
153    box = Box()
[350]154    quest = quest_engine.QuestEngine()
155    beer = Beer()
156
[343]157    state = {
[350]158        'quest': quest,
[343]159        'pc': pc,
[350]160        'box': box,
161        'beer': beer
[343]162    }
163
164    dialog = dialogue.DialogueEngine(dialogue_file, callbacks, state)
165    responses = dialog.run()
166    while responses:
167        choice = get_reply(responses)
168        responses = dialog.reply(choice)
169
170if __name__ == "__main__":
[350]171    print "1 - Dialogue sample\n2 - Quest Sample"
172    choice = input("> ")
173    if choice == 1:
174        dialogue_file = 'dialogue/drunkard.yaml'
175    elif choice == 2:
176        dialogue_file = 'dialogue/quest_sample.yaml'
[343]177    if len(sys.argv) > 1:
178        dialogue_file = sys.argv[1]
[350]179
[343]180    main(dialogue_file)
Note: See TracBrowser for help on using the repository browser.