source: trunk/game/dialogue_demo.py @ 674

Revision 674, 3.8 KB checked in by technomage, 9 years ago (diff)

Patch by Technomage

  • Modified the sha-bang line (#!) of the top-level entry scripts to search for python2 rather than python, since some linux systems now default to python 3.x which would break PARPG on those systems.
  • Some general cleanup of the dialogue_demo.py script.
  • Property svn:executable set to *
Line 
1#!/usr/bin/env python2
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"""A very simple demonstration of the dialogue engine in effect. It mocks up a
18   simple user object and allows the dialogue engine to interact with it via
19   callbacks we provide."""
20import logging
21import sys
22
23from scripts.dialogueparsers import YamlDialogueParser
24from scripts.dialogueengine import DialogueEngine
25from scripts.quest_engine import QuestEngine
26
27def setup_logging():
28    logging.basicConfig(filename='dialogue_demo.log')
29setup_logging()
30
31class Player(object):
32    """
33    Mock player object that always has complete quests
34    """
35    def __init__(self):
36        self.inventory = set(['beer'])
37        self.people_i_know = set()
38
39    def meet(self,npc):
40        if npc in self.people_i_know:
41            raise RuntimeError("I already know %s" % npc)
42        self.people_i_know.add(npc)
43
44    def met(self,npc):
45        return npc in self.people_i_know
46
47
48class Beer(object):
49    quality = 3
50
51
52class Box(object):
53    """
54    Mock box object than can be opened or closed
55    This would normally be a container
56    """
57    # States for the mock Box object
58    OPENED, CLOSED = range(2)
59   
60    def __init__(self):
61        self.state = self.CLOSED
62
63    def isOpen(self):
64        return self.state == self.OPENED
65
66    def isClosed(self):
67        return self.state == self.CLOSED
68
69    def open(self):
70        self.state = self.OPENED
71        return self.state
72
73    def close(self):
74        self.state = self.CLOSED
75        return self.state
76
77def getReply(dialogue_responses):
78    while (True):
79        print('Available responses:')
80        for index, response in enumerate(dialogue_responses):
81            print('{0} - {1}'.format(index, response.text))
82        try:
83            chosen_response_n = int(raw_input('Choose a response number> '))
84            chosen_response = dialogue_responses[chosen_response_n]
85        except ValueError, IndexError:
86            print('invalid response, please enter an integer between 0 and {0}'
87                  .format(len(dialogue_responses) - 1))
88        else:
89            break
90   
91    return chosen_response
92
93def main(dialogue_filepath):
94    pc = Player()
95    box = Box()
96    quest = QuestEngine('quests')
97    beer = Beer()
98   
99    parser = YamlDialogueParser()
100    with file(dialogue_filepath, 'r') as dialogue_file:
101        dialogue = parser.load(dialogue_file)
102   
103    game_state = {
104        'quest': quest,
105        'pc': pc,
106        'box': box,
107        'beer': beer
108    }
109
110    DialogueEngine.initiateDialogue(dialogue, game_state)
111    npc_name = dialogue.npc_name
112    while DialogueEngine.in_dialogue:
113        responses = DialogueEngine.continueDialogue()
114        current_dialogue_section = DialogueEngine.getCurrentDialogueSection()
115        dialogue_text = current_dialogue_section.text.replace('\n', '\n    ')
116        print('{0}: {1}'.format(npc_name, dialogue_text))
117        chosen_reply = getReply(responses)
118        DialogueEngine.reply(chosen_reply)
119
120if __name__ == "__main__":
121    print("1 - Dialogue sample\n2 - Quest Sample")
122    choice = input("> ")
123    if choice == 1:
124        dialogue_file = 'dialogue/drunkard.yaml'
125    elif choice == 2:
126        dialogue_file = 'dialogue/quest_sample.yaml'
127    if len(sys.argv) > 1:
128        dialogue_file = sys.argv[1]
129
130    main(dialogue_file)
Note: See TracBrowser for help on using the repository browser.