Ejemplo n.º 1
0
    public bool ShowDialogue(string agentName, string dialogue, bool tryAbove, bool autoClose, EventDialogue masterEvent = null,
                             QuestionRole questionRole = QuestionRole.None)
    {
        GameObject     boxObject          = null;
        TextBox        boxComponent       = null;
        int            boxSpeakerID       = BaseAgent.INVALID_AGENT_ID;
        Vector2        boxDimensions      = Vector2.zero;
        Vector2        boxOffset          = Vector2.zero;
        Vector3        boxPosition        = Vector3.zero;
        bool           boxPositionValid   = false;
        BaseAgent      agentComponent     = null;
        GameObject     boxTailObject      = null;
        SpriteRenderer boxTailRenderer    = null;
        Vector2        tailOffset         = Vector2.zero;
        int            optionCurrentIndex = -1;

        if (!QuestionRoleAvailable(questionRole))
        {
            return(false);
        }
        else
        {
            if (questionRole != QuestionRole.None)
            {
                autoClose = false;
            }
        }
        if ((textBoxParent != null) && (symbolDatabase != null) && (dialogue != null) && (cameraObject != null))
        {
            if (stage != null)
            {
                boxSpeakerID = stage.GetAgentID(agentName, ref agentComponent);
            }
            if (boxSpeakerID == BaseAgent.INVALID_AGENT_ID)
            {
                /*halmeida - this is not a character's dialogue text, so it is system info text.*/
                boxObject    = new GameObject("SystemTextBox");
                boxComponent = boxObject.AddComponent <TextBox>();
                boxComponent.SetBoxLimits(systemBoxLimits.x, systemBoxLimits.y);
                boxComponent.SetEssentials(symbolDatabase, bodySpriteSystem, dialogue, 0f, TextBox.TextAlignment.AlignLeft, autoClose,
                                           BOX_OPENING_SPEED);
            }
            else
            {
                boxObject    = new GameObject("DialogueTextBox");
                boxComponent = boxObject.AddComponent <DialogueBox>();
                boxComponent.SetBoxLimits(dialogueBoxLimits.x, dialogueBoxLimits.y);
                boxComponent.SetEssentials(symbolDatabase, bodySpriteDialogue, dialogue, TextBox.DEFAULT_TEXT_SPEED,
                                           TextBox.TextAlignment.AlignLeft, autoClose, BOX_OPENING_SPEED);
                if (agentComponent != null)
                {
                    boxComponent.SetSpeakerName(agentComponent.GetCurrentName());
                    if (questionRole != QuestionRole.Option)
                    {
                        boxComponent.SetOrnamentPortrait(agentComponent.agentPortrait);
                    }
                }
                if (questionRole == QuestionRole.None)
                {
                    boxTailObject          = new GameObject("DialogueBoxTail");
                    boxTailRenderer        = boxTailObject.AddComponent <SpriteRenderer>();
                    boxTailRenderer.sprite = tailSpriteDialogue;
                }
            }
            boxComponent.Build();
            boxDimensions = boxComponent.GetBoxWorldDimensions();
            switch (questionRole)
            {
            case QuestionRole.None:
                boxPosition = GetBoxPositionForAgent(boxSpeakerID, boxDimensions, tryAbove, ref boxPositionValid, ref tailOffset);
                break;

            case QuestionRole.Enunciate:
                boxPosition = GetBoxPositionForEnunciate(boxDimensions, ref boxPositionValid);

                /*halmeida - with the dimensions of the enunciate set, I can already determine the area available
                 * for the options.*/
                optionAreaHeight  = 2f * enunciateBoxTopOffset;
                optionAreaHeight *= ((enunciateBoxTopOffset < 0) ? -1f : 1f);
                optionAreaHeight -= boxDimensions.y;
                if (optionsToAllocate > 0)
                {
                    optionAreaHeight = optionAreaHeight / optionsToAllocate;
                }
                optionHalfAreaHeight = optionAreaHeight / 2f;
                optionBoxTopOffset   = enunciateBoxTopOffset - boxDimensions.y;
                /*halmeida - enunciate text boxes cannot react to any input.*/
                boxComponent.BlockReactions();
                enunciateBoxComponent = boxComponent;
                optionAwaitedIndex    = 0;
                break;

            case QuestionRole.Option:
                boxPosition = GetBoxPositionForOption(optionAwaitedIndex, boxDimensions, ref boxPositionValid);
                /*halmeida - option text boxes can only react to precise input over them.*/
                boxComponent.AllowReactions(true);
                optionCurrentIndex = optionAwaitedIndex;
                optionAwaitedIndex++;
                break;
            }
            if (boxPositionValid)
            {
                boxObject.transform.SetParent(textBoxParent.transform, false);

                /*halmeida - since the textBoxParent is at x = 0 and y = 0, I can use the local position of its
                 * children as if it was their position. Changing just the local position is faster.*/
                boxObject.transform.localPosition = boxPosition;
                if (boxTailObject != null)
                {
                    boxTailObject.transform.SetParent(boxObject.transform, false);
                    boxTailObject.transform.localPosition = new Vector3(tailOffset.x, tailOffset.y, boxComponent.GetBoxToTextDepth());
                    if (tailOffset.y > 0f)
                    {
                        boxTailObject.transform.rotation = Quaternion.Euler(0f, 0f, 180f);
                    }
                }
                boxComponent.Open();
                UsefulFunctions.IncreaseArray <GameObject>(ref boxObjects, boxObject);
                UsefulFunctions.IncreaseArray <TextBox>(ref boxComponents, boxComponent);
                UsefulFunctions.IncreaseArray <EventDialogue>(ref boxMasterEvents, masterEvent);
                UsefulFunctions.IncreaseArray <QuestionRole>(ref boxQuestionRoles, questionRole);
                UsefulFunctions.IncreaseArray <int>(ref boxOptionIndexes, optionCurrentIndex);
                UsefulFunctions.IncreaseArray <int>(ref boxSpeakerIDs, boxSpeakerID);
                UsefulFunctions.IncreaseArray <bool>(ref boxTryAboves, tryAbove);
                UsefulFunctions.IncreaseArray <GameObject>(ref boxTailObjects, boxTailObject);
                UsefulFunctions.IncreaseArray <SpriteRenderer>(ref boxTailRenderers, boxTailRenderer);
                return(true);
            }
            boxComponent.Clear();
            GameObject.Destroy(boxObject);
            if (boxTailObject != null)
            {
                GameObject.Destroy(boxTailObject);
            }
        }
        return(false);
    }