source: trunk/game/scripts/dialogue.py @ 683

Revision 680, 5.4 KB checked in by technomage, 9 years ago (diff)

Patch by Technomage

  • Updated/wrote code documentation for the core dialogue subsystem modules dialogue.py, dialogueengine.py, dialogueactions.py, and dialogueparsers.py.
  • Updated/created flowcharts and UML diagrams explaining the new DialogueEngine? and how it functions.
  • Made a few minor changes to the dialoguegui.py module to make the class logger less visible.
  • Removed the "this is a sample dialogue file" crud from the old_man.yaml test dialogue file.
  • Property svn:eol-style set to native
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"""
18Provides classes used to contain and organize dialogue data for use within
19in-game dialogues between the player character and NPCs.
20"""
21try:
22    from collections import OrderedDict
23except ImportError:
24    # Python version 2.4-2.6 doesn't have the OrderedDict
25    from scripts.common.ordereddict import OrderedDict
26
27class Dialogue(object):
28    """
29    Represents a complete dialogue and acts as a container for the dialogue
30    data belonging to a particular NPC.
31    """
32    __slots__ = ['npc_name', 'avatar_path', 'start_section_id', 'sections']
33   
34    def __init__(self, npc_name, avatar_path, start_section_id,
35                 dialogue_sections=None):
36        """
37        Initialize a new L{Dialogue} instance.
38       
39        @param npc_name: name displayed for the NPC in the dialogue.
40        @type npc_name: basestring
41        @param avatar_path: path to the image that should be displayed as the
42            NPC's avatar.
43        @type avatar_path: basestring
44        @param start_section_id: ID of the L{DialogueSection} that should be
45            displayed when the dialogue is first initiated.
46        @type start_section_id: basestring
47        @param dialogue_sections: sections of dialogue that make up this
48            L{Dialogue} instance.
49        @type dialogue_sections: list of L{DialogueSections<DialogueSection>}
50        """
51        self.npc_name = npc_name
52        self.avatar_path = avatar_path
53        self.start_section_id = start_section_id
54        self.sections = OrderedDict()
55        if (dialogue_sections is not None):
56            for section in dialogue_sections:
57                self.sections[section.id] = section
58   
59    def __str__(self):
60        """Return the string representation of a L{Dialogue} instance."""
61        string_representation = (
62            ('Dialogue(npc_id={0.npc_name}, avatar_path={0.avatar_path}, '
63             'start_section_id={0.start_section_id}, ...)').format(self)
64        )
65        return string_representation
66
67
68class DialogueNode(object):
69    """
70    Abstract base class that represents a node or related group of attributes
71    within a Dialogue.
72    """
73    def __init__(self, text, actions=None):
74        """
75        Initialize a new L{DialogueNode} instance.
76       
77        @param text: textual content of the L{DialogueNode}.
78        @type text: basestring
79        @param actions: dialogue actions associated with the L{DialogueNode}.
80        @type actions: list of L{DialogueActions<DialogueAction>}
81        """
82        self.text = text
83        self.actions = actions or []
84
85
86class DialogueSection(DialogueNode):
87    """DialogueNode that represents a distinct section of the dialogue."""
88    __slots__ = ['id', 'text', 'actions', 'responses']
89   
90    def __init__(self, id, text, responses=None, actions=None):
91        """
92        Initialize a new L{DialogueSection} instance.
93       
94        @param id: named used to uniquely identify the L{DialogueSection}
95            within a L{Dialogue}.
96        @type id: basestring
97        @param text: text displayed as the NPC's part of the L{Dialogue}.
98        @type text: basestring
99        @param responses: possible responses that the player can choose from.
100        @type responses: list of L{DialogueResponses<DialogueResponse>}
101        @param actions: dialogue actions that should be executed when the
102            L{DialogueSection} is reached.
103        @type actions: list of L{DialogueActions<DialogueAction>}
104        """
105        DialogueNode.__init__(self, text=text, actions=actions)
106        self.id = id
107        if (responses is not None):
108            self.responses = list(responses)
109
110
111class DialogueResponse(DialogueNode):
112    """
113    L{DialogueNode} that represents one possible player response to a
114    particular L{DialogueSection}.
115    """
116    __slots__ = ['text', 'actions', 'condition', 'next_section_id']
117   
118    def __init__(self, text, next_section_id, actions=None,
119                 condition=None):
120        """
121        Initialize a new L{DialogueResponse} instance.
122       
123        @param text: text displayed as the content of the player's response.
124        @type text: basestring
125        @param next_section_id: ID of the L{DialogueSection} that should be
126            jumped to if this response is chosen by the player.
127        @type next_section_id: basestring
128        @param actions: dialogue actions that should be executed if this
129            response is chosen by the player.
130        @type actions: list of L{DialogueActions}
131        @param condition: Python expression that when evaluated determines
132            whether the L{DialogueResponse} should be displayed to the player
133            as a valid response.
134        @type condition: basestring
135        """
136        DialogueNode.__init__(self, text=text, actions=actions)
137        self.condition = condition
138        self.next_section_id = next_section_id
Note: See TracBrowser for help on using the repository browser.