Ignore:
Timestamp:
12/10/10 19:11:29 (9 years ago)
Author:
technomage
Message:

Patch by Technomage

  • Refactored the DialogueEngine? singleton into the DialogueProcessor? instance factory, eliminating some of the problems the singleton pattern caused with encapsulation and unit testing.
  • Renamed the dialogueengine.py module to dialogueprocessor.py for clarity.
  • Added type-checking code to the instance variables defined by the DialogueProcessor? and Dialogue classes using assert statements that are removed when the optimization '-O' flag is passed to the Python interpreter.
  • Moved the 'main' logic in dialogue_demo.py script into a 'main' method to avoid cluttering the module namespace with runtime variables.
  • Fixed a few code documentation issues pointed out by aspidites.
Location:
trunk/game
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/game

    • Property svn:ignore
      •  

        old new  
        55settings-old.xml 
        66settings.xml 
         7config.py 
  • trunk/game/dialogue_demo.py

    r682 r684  
    2424from scripts.common.optionparser import OptionParser, OptionError 
    2525from scripts.dialogueparsers import YamlDialogueParser 
    26 from scripts.dialogueengine import DialogueEngine 
     26from scripts.dialogueprocessor import DialogueProcessor 
    2727from scripts.quest_engine import QuestEngine 
     28 
     29def setupLogging(): 
     30    """Set various logging parameters for this module.""" 
     31    logging.basicConfig(filename='dialogue_demo.log') 
     32setupLogging() 
    2833 
    2934PARPG_ROOT_DIR = os.path.dirname(__file__) 
     
    3136DIALOGUE_DIR = os.path.join(PARPG_ROOT_DIR, 'dialogue') 
    3237"""Absolute path to the dialogue directory of the PARPG installation.""" 
     38USAGE_MESSAGE = '''\ 
     39usage: dialogue_demo.py [-h] [dialogue_file] 
     40Script for testing dialogue files. 
    3341 
    34 def setup_logging(): 
    35     """Set various logging parameters for this module.""" 
    36     logging.basicConfig(filename='dialogue_demo.log') 
    37 setup_logging() 
     42-h, --help                  Show this help message. 
     43dialogue_file               YAML file containing a dialogue; if not specified, 
     44                                the user will be prompted to choose a dialogue 
     45                                file from the dialogue directory. 
     46''' 
    3847 
    3948class MockPlayerCharacter(object): 
     
    115124    return selected_file_path 
    116125 
    117 def getReply(dialogue_responses): 
     126def chooseReply(dialogue_responses): 
    118127    """ 
    119128    Prompt the user to choose a L{DialogueResponse} from a list of valid 
     
    148157    @type game_state: dict of objects 
    149158    """ 
    150     DialogueEngine.initiateDialogue(dialogue, game_state) 
    151159    npc_name = dialogue.npc_name 
    152     while DialogueEngine.in_dialogue: 
    153         responses = DialogueEngine.continueDialogue() 
    154         current_dialogue_section = DialogueEngine.getCurrentDialogueSection() 
    155         dialogue_text = current_dialogue_section.text.replace('\n', '\n    ') 
     160    dialogue_processor = DialogueProcessor(dialogue, game_state) 
     161    dialogue_processor.initiateDialogue() 
     162    while dialogue_processor.in_dialogue: 
     163        responses = dialogue_processor.continueDialogue() 
     164        current_dialogue_section = \ 
     165            dialogue_processor.getCurrentDialogueSection() 
     166        dialogue_text = current_dialogue_section.text 
     167        # Indent dialogue text after the first line. 
     168        dialogue_text = dialogue_text.replace('\n', '\n    ') 
    156169        print('\n{0}: {1}'.format(npc_name, dialogue_text)) 
    157         chosen_reply = getReply(responses) 
    158         DialogueEngine.reply(chosen_reply) 
     170        chosen_reply = chooseReply(responses) 
     171        dialogue_processor.reply(chosen_reply) 
    159172 
    160 usage_message='''\ 
    161 usage: dialogue_demo.py [-h] [dialogue_file] 
    162 Script for testing dialogue files. 
    163  
    164 -h, --help                  Show this help message. 
    165 dialogue_file               YAML file containing a dialogue; if not specified, 
    166                                 the user will be prompted to choose a dialogue 
    167                                 file from the dialogue directory. 
    168 ''' 
    169  
    170 if __name__ == "__main__": 
    171     option_parser = OptionParser(usage=usage_message) 
     173def main(argv=sys.argv): 
     174    option_parser = OptionParser(usage=USAGE_MESSAGE) 
    172175    for option in option_parser: 
    173176        if (option in ['-h', '--help']): 
     
    190193        dialogue = dialogue_parser.load(dialogue_file) 
    191194    processDialogue(dialogue, game_state) 
     195 
     196if __name__ == "__main__": 
     197    main() 
Note: See TracChangeset for help on using the changeset viewer.