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 *
RevLine 
[674]1#!/usr/bin/env python2
[343]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/>.
[477]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."""
[668]20import logging
21import sys
[343]22
[672]23from scripts.dialogueparsers import YamlDialogueParser
[668]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
[343]31class Player(object):
32    """
33    Mock player object that always has complete quests
34    """
35    def __init__(self):
[350]36        self.inventory = set(['beer'])
[590]37        self.people_i_know = set()
[343]38
39    def meet(self,npc):
[590]40        if npc in self.people_i_know:
[343]41            raise RuntimeError("I already know %s" % npc)
[590]42        self.people_i_know.add(npc)
[350]43
[343]44    def met(self,npc):
[590]45        return npc in self.people_i_know
[350]46
47
48class Beer(object):
49    quality = 3
50
51
[343]52class Box(object):
53    """
54    Mock box object than can be opened or closed
55    This would normally be a container
56    """
[674]57    # States for the mock Box object
58    OPENED, CLOSED = range(2)
59   
[343]60    def __init__(self):
[674]61        self.state = self.CLOSED
[350]62
[343]63    def isOpen(self):
[674]64        return self.state == self.OPENED
[350]65
[343]66    def isClosed(self):
[674]67        return self.state == self.CLOSED
[350]68
[343]69    def open(self):
[674]70        self.state = self.OPENED
[343]71        return self.state
[350]72
[343]73    def close(self):
[674]74        self.state = self.CLOSED
[343]75        return self.state
[350]76
[668]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:
[350]89            break
[668]90   
91    return chosen_response
[350]92
[668]93def main(dialogue_filepath):
[343]94    pc = Player()
95    box = Box()
[668]96    quest = QuestEngine('quests')
[350]97    beer = Beer()
[673]98   
99    parser = YamlDialogueParser()
[668]100    with file(dialogue_filepath, 'r') as dialogue_file:
[673]101        dialogue = parser.load(dialogue_file)
[668]102   
103    game_state = {
[350]104        'quest': quest,
[343]105        'pc': pc,
[350]106        'box': box,
107        'beer': beer
[343]108    }
109
[668]110    DialogueEngine.initiateDialogue(dialogue, game_state)
[673]111    npc_name = dialogue.npc_name
[668]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)
[343]119
120if __name__ == "__main__":
[674]121    print("1 - Dialogue sample\n2 - Quest Sample")
[350]122    choice = input("> ")
123    if choice == 1:
124        dialogue_file = 'dialogue/drunkard.yaml'
125    elif choice == 2:
126        dialogue_file = 'dialogue/quest_sample.yaml'
[343]127    if len(sys.argv) > 1:
128        dialogue_file = sys.argv[1]
[350]129
[343]130    main(dialogue_file)
Note: See TracBrowser for help on using the repository browser.