/**
         * Gets an instance of this manager
         */
        public static ConversationManager GetInstance()
        {
            if(instance == null)
            {
                instance = ManagerBase.GetManagerInstance<ConversationManager>();
            }

            return instance;
        }
Exemplo n.º 2
0
        /**
         * Event: On play node
         */
        public IEnumerator OnPlayNode(ConversationManager.Node node, ConversationManager.OnNextNodeDelegate callback)
        {
            // ----------
            // Speak node
            // ----------
            if(node is ConversationManager.SpeakNode)
            {
                ConversationManager.SpeakNode speakNode = (ConversationManager.SpeakNode)node;

                // We assume if the speaker isn't set, this speech bubble handles everyone's speech
                if(this.speaker == null || speakNode.speaker.Value == this.speaker)
                {
                    float delay = speakNode.GetDuration();
                    Pawn currentSpeaker = (Pawn)speakNode.speaker.Value;

                    this.Toggle(true);
                    this.SetSubtitle(speakNode.subtitle);
                    this.SetPortrait(currentSpeaker.portrait);

                    // Delay before next node is played
                    yield return new WaitForSeconds(delay);

                    callback(node, 0);
                }
            }

            // ----------
            // Choice node
            // ----------
            else if(node is ConversationManager.ChoiceNode)
            {
                ConversationManager.ChoiceNode choiceNode = (ConversationManager.ChoiceNode)node;

                // We assume if the speaker isn't set, this speech bubble handles everyone's speech
                if(this.speaker == null || ConversationManager.GetCurrentConversation().GetNextSpeaker(choiceNode, 0) == this.speaker)
                {
                    Pawn currentSpeaker = ConversationManager.GetCurrentConversation().GetNextSpeaker(choiceNode, 0);

                    this.Toggle(true);

                    for(int i = 0; i < choiceNode.outputs.Length; i++)
                    {
                        this.SetOption(i, choiceNode.outputs[i].label, (index) => {
                            callback(node, index);
                        });
                    }

                    this.SetPortrait(currentSpeaker.portrait);
                }
            }

            yield return null;
        }
        /**
         * Updates a specific node container
         */
        private static void UpdateNodeContainer(ConversationManager.Node node)
        {
            if(!nodeContainers.ContainsKey(node.id))
            {
                nodeContainers[node.id] = new EditorGUITool.Node();
            }

            EditorGUITool.Node nodeContainer = nodeContainers[node.id];

            nodeContainer.target = node;
            nodeContainer.title = node.id.ToString();
            nodeContainer.SetPosition(new Vector2(node.position.x + 20, node.position.y + 20));

            nodeContainer.input = new EditorGUITool.Node.Input();
            nodeContainer.input.onConnect = (EditorGUITool.Node.Input input, EditorGUITool.Node.Output output) => {
                if(output.parent != null)
                {
                    EditorGUITool.Node inputNodeContainer = input.parent;
                    ConversationManager.Node inputNode = (ConversationManager.Node)inputNodeContainer.target;

                    EditorGUITool.Node outputNodeContainer = output.parent;
                    ConversationManager.Node outputNode = (ConversationManager.Node)outputNodeContainer.target;

                    outputNode.Connect(inputNode, output.index);

                    UpdateNodeContainers();
                }
            };

            // ----------
            // Outputs
            // ----------
            nodeContainer.outputs.Clear();

            for(int i = 0; i < node.outputs.Length; i++)
            {
                EditorGUITool.Node.Output newOutput = new EditorGUITool.Node.Output();

                newOutput.onConnect = (EditorGUITool.Node.Input input, EditorGUITool.Node.Output output) => {
                    if(input.parent != null)
                    {
                        EditorGUITool.Node inputNodeContainer = input.parent;
                        ConversationManager.Node inputNode = (ConversationManager.Node)inputNodeContainer.target;

                        EditorGUITool.Node outputNodeContainer = output.parent;
                        ConversationManager.Node outputNode = (ConversationManager.Node)outputNodeContainer.target;

                        outputNode.Connect(inputNode, output.index);

                        UpdateNodeContainers();
                    }
                };

                nodeContainer.outputs.Add(newOutput);
            }

            // ----------
            // Drag event
            // ----------
            nodeContainer.onDrag = () => {
                node.position.Value = nodeContainer.GetPosition();
            };

            // ----------
            // Remove event
            // ----------
            nodeContainer.onRemove = () => {
                conversation.RemoveNode(node.id);
                UpdateNodeContainers();
            };

            // ---------
            // Render content
            // ---------
            nodeContainer.renderContent = () => {
                // ----------
                // Basic properties
                // ----------
                // Type
                int typeIndexBefore = 0;

                for(int i = 0; i < nodeTypeNames.Length; i++)
                {
                    if(node.GetType().Name == nodeTypeNames[i])
                    {
                        typeIndexBefore = i;
                    }
                }

                int typeIndexAfter = EditorGUITool.Popup("Type", typeIndexBefore, nodeTypeNames);

                if(typeIndexBefore != typeIndexAfter)
                {
                    conversation.ChangeNodeType(node.id, nodeTypeNames[typeIndexAfter]);

                    UpdateNodeContainers();
                }

                EditorGUITool.Space();

                // ----------
                // Speak node
                // ----------
                if(node is ConversationManager.SpeakNode)
                {
                    ConversationManager.SpeakNode speakNode = (ConversationManager.SpeakNode)node;

                    speakNode.speaker.Value = EditorGUITool.ObjectField<Pawn>("Speaker", speakNode.speaker.Value, true);
                    speakNode.facing.Value = EditorGUITool.ObjectField<Pawn>("Facing", speakNode.facing.Value, true);
                    speakNode.subtitle = EditorGUITool.TextField("Subtitle", speakNode.subtitle);
                    speakNode.audio.Value = EditorGUITool.ObjectField<AudioClip>("Audio", speakNode.audio.Value, false);
                }

                // ----------
                // Choice node
                // ----------
                if(node is ConversationManager.ChoiceNode)
                {
                    for(int i = 0; i < node.outputs.Length; i++)
                    {
                        node.outputs[i].label = EditorGUITool.TextField("", node.outputs[i].label);
                    }

                    EditorGUITool.Space();

                    EditorGUITool.Horizontal(() => {
                        EditorGUITool.Button("-", () => {
                            if(node.outputs.Length > 1) {
                                node.RemoveSlot(node.outputs.Length - 1);
                                UpdateNodeContainer(node);
                            }
                        });
                        EditorGUITool.Button("+", () => {
                            node.AddSlot();
                            UpdateNodeContainer(node);
                        });
                    });
                }

                // ----------
                // Quest node
                // ----------
                if(node is ConversationManager.QuestNode)
                {
                    ConversationManager.QuestNode questNode = (ConversationManager.QuestNode)node;

                    questNode.action = (ConversationManager.QuestNode.Action)EditorGUITool.Popup("Action", questNode.action);
                    questNode.quest.path = EditorGUITool.PathField("Quest", questNode.quest.path, "quest");
                }

                // ----------
                // Flag node
                // ----------
                if(node is ConversationManager.FlagNode)
                {
                    ConversationManager.FlagNode flagNode = (ConversationManager.FlagNode)node;

                    // Mode (get/set)
                    ConversationManager.FlagNode.Mode modeBefore = flagNode.mode;

                    flagNode.SetMode((ConversationManager.FlagNode.Mode)EditorGUITool.Popup("Mode", flagNode.mode));

                    if(flagNode.mode != modeBefore)
                    {
                        UpdateNodeContainer(flagNode);
                    }

                    // Flag list
                    EditorGUITool.Horizontal(() => {
                        EditorGUITool.Label("Name");
                        EditorGUITool.Label("Bool");
                    });

                    for(int i = 0; i < flagNode.flags.Count; i++)
                    {
                        FlagManager.Flag flag = flagNode.flags[i];

                        EditorGUITool.Horizontal(() => {
                            flag.name = EditorGUITool.TextField("", flag.name);
                            flag.state = EditorGUITool.BoolField("", flag.state);
                        });
                    }

                    EditorGUITool.Space();

                    // Add/remove flags
                    EditorGUITool.Horizontal(() => {
                        EditorGUITool.Button("-", () => {
                            flagNode.flags.RemoveAt(flagNode.flags.Count - 1);
                            UpdateNodeContainer(flagNode);
                        });

                        EditorGUITool.Button("+", () => {
                            flagNode.flags.Add(new FlagManager.Flag());
                            UpdateNodeContainer(flagNode);
                        });
                    });
                }

                // ----------
                // Next dialog node
                // ----------
                if(node is ConversationManager.NextDialogNode)
                {
                    ConversationManager.NextDialogNode nextDialogNode = (ConversationManager.NextDialogNode)node;

                    ConversationManager.Dialog dialog = conversation.GetDialog(nextDialogNode.dialogId);
                    string dialogName = dialog != null ? dialog.name : "";
                    string[] dialogNames = conversation.GetDialogNames();
                    int dialogIndex = Array.IndexOf(dialogNames, dialogName);

                    if(dialogIndex < 0)
                    {
                        dialogIndex = 0;
                    }

                    dialogIndex = EditorGUITool.Popup("Next dialog", dialogIndex, dialogNames);

                    nextDialogNode.dialogId = conversation.dialogs[dialogIndex].id;

                    nextDialogNode.endCurrentDialog = EditorGUITool.BoolField("End current dialog", nextDialogNode.endCurrentDialog);
                }
            };
        }
        /**
         * Updates a specific node container connection
         */
        private static void UpdateNodeContainerConnection(ConversationManager.Node node)
        {
            if(nodeContainers.ContainsKey(node.id))
            {
                EditorGUITool.Node nodeContainer = nodeContainers[node.id];

                for(int i = 0; i < node.outputs.Length; i++)
                {
                    int id = node.outputs[i].nodeId;

                    if(nodeContainers.ContainsKey(id))
                    {
                        EditorGUITool.Node connectedNodeContainer = nodeContainers[id];

                        nodeContainer.outputs[i].Connect(connectedNodeContainer.input);
                    }
                }
            }
        }
        /**
         * Event: Play node
         */
        public IEnumerator OnPlayNode(ConversationManager.Node node, ConversationManager.OnNextNodeDelegate callback)
        {
            Transform target = null;

            // ----------
            // Speak node
            // ----------
            if(node is ConversationManager.SpeakNode)
            {
                ConversationManager.SpeakNode speakNode = (ConversationManager.SpeakNode)node;

                target = speakNode.speaker.Value.transform;
            }

            // ----------
            // Choice node
            // ----------
            if(node is ConversationManager.ChoiceNode)
            {
                ConversationManager.ChoiceNode choiceNode = (ConversationManager.ChoiceNode)node;

                target = ConversationManager.GetCurrentConversation().GetNextSpeaker(choiceNode, 0).transform;
            }

            if(target != null)
            {
                this.FocusTarget(target);
            }

            yield return null;
        }