source: trunk/game/dialogue_demo.py @ 477

Revision 477, 4.9 KB checked in by maximinus_parpg, 10 years ago (diff)

More clean up of the code.
Finshed first pass.
So should be at least stands compliant.

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