Esempio n. 1
0
    NovelBranch ConsumeBranch(ref List <FrameFragment> lines)
    {
        List <NovelFrame> result = new List <NovelFrame>();

        if (lines[0].token != Tokens.branch)
        {
            throw new System.Exception("Expected TOKEN branch but got " + lines[0].token);
        }

        string title = lines[0].payload;

        lines.Remove(lines[0]);

        List <FrameFragment> frames = new List <FrameFragment>();
        List <FrameFragment> post   = new List <FrameFragment>();

        bool foundExit = false;

        foreach (FrameFragment fragment in lines)
        {
            if (foundExit)
            {
                post.Add(fragment);
            }
            else
            {
                if (fragment.token == Tokens.exitbranch)
                {
                    foundExit = true;
                }
                else
                {
                    frames.Add(fragment);
                }
            }
        }

        if (foundExit == false)
        {
            throw new System.Exception("Expected token to terminate Branch " + title
                                       + " but did not find terminator. ("
                                       + ("exitbranch " + title) + ")");
        }

        while (frames.Count > 0)
        {
            NovelFrame frame = Consume(ref frames);
            if (frame is NovelCharacter)
            {
                List <NovelFrame> flattened = (frame as NovelCharacter).Flatten();
                result.AddRange(flattened);
            }
            else
            {
                result.Add(frame);
            }
        }

        lines = post;
        return(new NovelBranch(title, result));
    }
Esempio n. 2
0
    void Advance(bool doNotIncrement = false)
    {
        //Debug.Log("advance:"+doNotIncrement);
        lastToken = currentBranch.frames[currentFrame].token;
        if (!doNotIncrement)
        {
            currentFrame++;
        }

        if (currentFrame >= currentBranch.frames.Count)
        {
            running = false;
            characterGraphic.gameObject.SetActive(false);
            if (callBack != null)
            {
                callBack.Invoke();
            }
            return;
        }

        NovelFrame frame = currentBranch.frames[currentFrame];

        switch (frame.token)
        {
        case Tokens.background:
        {
            title.text        = string.Empty;
            message.text      = string.Empty;
            background.sprite = marshal.Background((string)frame.payload);
            StartTimer();
        } break;

        case Tokens.monologue:
        {
            title.text   = "monologue";
            message.text = (string)frame.payload;
            state        = RunnerStates.waitOnInput;
        } break;

        case Tokens.character:
        {
            NovelCharacter character = ((NovelCharacter)frame);
            if (character.name != "You" && character.name != "player")
            {
                characterGraphic.gameObject.SetActive(true);
                characterGraphic.sprite = marshal.Character(character.name, null);
                characterGraphic.rectTransform.sizeDelta = Scale(characterGraphic.sprite.rect.size.y, 1080,
                                                                 characterGraphic.sprite.rect.size);
            }
            title.text   = ((NovelCharacter)frame).name;
            message.text = string.Empty;
            state        = RunnerStates.waitOnTimer;
        } break;

        case Tokens.expression:
        {
            NovelExpression expression = ((NovelExpression)frame);
            if (expression.character != "You" && expression.character != "player")
            {
                characterGraphic.gameObject.SetActive(true);
                characterGraphic.sprite = marshal.Character(expression.character, (string)expression.payload);
                characterGraphic.rectTransform.sizeDelta = Scale(characterGraphic.sprite.rect.size.y, 1080,
                                                                 characterGraphic.sprite.rect.size);
            }

            title.text = ((NovelExpression)frame).character;
            StartTimer();
        } break;

        case Tokens.line:
        {
            if (lastToken == Tokens.character || lastToken == Tokens.expression || lastToken == Tokens.line)
            {                     /* Do Nothing */
            }
            else
            {
                title.text = ((NovelExpression)frame).character + " " + ((NovelExpression)frame).payload;
            }
            message.text = (string)frame.payload;
            state        = RunnerStates.waitOnInput;
        } break;

        case Tokens.hidecharacter:
        {
            characterGraphic.gameObject.SetActive(false);
            StartTimer();
        } break;

        case Tokens.directive:
        {
            marshal.ExecuteProcedure((string)frame.payload);
            state = RunnerStates.waitOnTimer;
            StartTimer();
        } break;

        case Tokens.jump:
        {
            StartTimer();
            Jump((string)frame.payload);
        } break;

        case Tokens.TEXT:
        {
            NovelText text = (NovelText)frame;
            state = RunnerStates.waitOnInput;
            phone.PostTextMessage(text.character == "You", text.character, text.line);
        } break;

        case Tokens.options:
        {
            novelAssembly.SetActive(true);
            state = RunnerStates.waitOnChoice;
            NovelOptions options = (NovelOptions)frame;

            normalDisplay.SetActive(false);
            optionDisplay.SetActive(true);

            Transform[] old = optionContent.GetComponentsInChildren <Transform>();
            Debug.Log(old.Length);

            foreach (Transform go in old)
            {
                if (go == optionContent.transform)
                {
                    continue;
                }
                go.transform.parent = null;
                GameObject.Destroy(go.gameObject);
            }

            foreach (NovelOption option in options.options)
            {
                GameObject g = GameObject.Instantiate(providedButton, optionContent.transform.position,
                                                      optionContent.transform.rotation, optionContent.transform);
                g.GetComponentInChildren <Text>().text = (string)option.payload;
                g.GetComponent <Button>().onClick.AddListener(
                    () => { Jump(option.target); EndOptions(); }                              /// CLOSURE! to the rescue!!
                    );
            }
        } break;

        default:
        {
            throw new System.Exception("Unexpected token " + frame.token + " in switch statement.");
        }
        }
    }