private void CreateChoices(SceneTransitionRequest str) { var animGroup = new AnimGroup(); var curTime = Time.time; if (_choiceButtons != null) { foreach (var btn in _choiceButtons) { Destroy(btn); } } _choiceButtons = new Button[str.TransitionChoices.Length]; for (int i = 0; i < str.TransitionChoices.Length; ++i) { var choiceButton = Instantiate <Button>(ChoiceButtonPrototype, UIContainer.transform); var choice = str.TransitionChoices[i]; choiceButton.gameObject.SetActive(true); var choiceButtonCG = choiceButton.GetComponent <CanvasGroup>(); choiceButtonCG.alpha = 0.0f; animGroup.AddAnimation(new FadeCGAnimation(choiceButtonCG, curTime + i * FadeInDuration, FadeInDuration, FadeInCurve, 1.0f)); choiceButton.image.rectTransform.anchoredPosition = ChoiceButtonPrototype.image.rectTransform.anchoredPosition + new Vector2(0.0f, -i * (ChoiceButtonPrototype.image.rectTransform.rect.height + 40)); choiceButton.onClick.AddListener(delegate { _choiceHandler(choice.ChoiceID); }); var choiceButtonText = choiceButton.GetComponentInChildren <Text>(); choiceButtonText.text = choice.ChoiceText; _choiceButtons[i] = choiceButton; } _pendingAnimations.Enqueue(animGroup); }
private void TransitionMainText(SceneTransitionRequest str, Image anchorTarget) { var animGroup = new AnimGroup(); var textFadeOut = new FadeAnimation(PhraseText, Time.time, FadeOutDuration, FadeOutCurve, 0.0f); animGroup.AddAnimation(textFadeOut); var lastAnimFinish = textFadeOut.TimeEnd; var defaultTextBoxSize = new Vector2(anchorTarget.rectTransform.rect.width - 30, anchorTarget.rectTransform.rect.height - 35); var requiredHeight = GetDesiredTextHeight(PhraseText, str.TransitionPhrase, defaultTextBoxSize); if (PhraseBackground.rectTransform.anchoredPosition != anchorTarget.rectTransform.anchoredPosition || !Mathf.Approximately(requiredHeight, PhraseText.rectTransform.rect.height)) { var textBoxResize = new RectAnimation(PhraseBackground.rectTransform, textFadeOut.TimeEnd, TransitionDuration, TransitionCurve, anchorTarget.rectTransform); // Fixup for correct size textBoxResize.TargetSize.y = requiredHeight + 35; animGroup.AddAnimation(textBoxResize); lastAnimFinish = textBoxResize.TimeEnd; } var textChange = new SetTextAnimation(PhraseText, lastAnimFinish, str.TransitionPhrase); animGroup.AddAnimation(textChange); var textFadeIn = new FadeAnimation(PhraseText, lastAnimFinish, FadeInDuration, FadeInCurve, 1.0f); animGroup.AddAnimation(textFadeIn); _pendingAnimations.Enqueue(animGroup); }
private void TransitionBackground(SceneTransitionRequest str) { var animGroup = new AnimGroup(); BackgroundTransition.sprite = _backgroundPool.GetBackgroundSprite(str.TransitionBackground.ToLower()); animGroup.AddAnimation(new FadeAnimation(BackgroundTransition, Time.time, FadeInDuration, FadeInCurve, 1.0f)) .AddAnimation(new SetSpriteAnimation(Background, Time.time + FadeInDuration, BackgroundTransition.sprite)) .AddAnimation(new FadeAnimation(BackgroundTransition, Time.time + FadeInDuration, 0.001f, FadeOutCurve, 0.0f)); _pendingAnimations.Enqueue(animGroup); }
/** * Transition scene to another state */ public bool Transition(SceneTransitionRequest str) { HideChoices(); if (!_willAcceptTransitions) { return(false); } if (str.TransitionBackground != null) { TransitionBackground(str); } if (str.TransitionSpeaker == null) { HideActor(); TransitionMainText(str, DescriptionBackgroundAnchor); } else { ShowActor(str); ActorImage.sprite = _actorPool.GetActorSprite(str.TransitionSpeaker, str.TransitionSpeakerEmotion); if (str.TransitionChoices == null) { // Enable generic tap target if there are no choices TapTarget.gameObject.SetActive(true); if (str.TransitionSpeaker.Contains("Player")) { TransitionMainText(str, PlayerPhraseBackgroundAnchor); } else { TransitionMainText(str, NPCPhraseBackgroundAnchor); } } else { // Disable generic tap target TapTarget.gameObject.SetActive(false); TransitionMainText(str, ChoiceBackgroundAnchor); CreateChoices(str); } } return(true); }
private void ShowActor(SceneTransitionRequest str) { var timeOffset = 0.0f; // If the actor name is already on screen, change it gracefully if (_actorGroup.alpha > 0.99f) { if (ActorName.text != str.TransitionSpeaker) { var animGroup = new AnimGroup(); animGroup.AddAnimation(new FadeAnimation(ActorName, Time.time, FadeOutDuration, FadeOutCurve, 0.0f)) .AddAnimation(new SetTextAnimation(ActorName, Time.time + FadeOutDuration, str.TransitionSpeaker)) .AddAnimation(new FadeAnimation(ActorName, Time.time + FadeOutDuration, FadeInDuration, FadeInCurve, 1.0f)); timeOffset = FadeOutDuration + FadeInDuration; _pendingAnimations.Enqueue(animGroup); } } else { // Change the name without any animation, the fade in from the canvas group will do the rest ActorName.text = str.TransitionSpeaker; _pendingAnimations.Enqueue(new AnimGroup().AddAnimation(new FadeCGAnimation(_actorGroup, Time.time + timeOffset, FadeInDuration, FadeInCurve, 1.0f))); } }
/** * Create a scene transition, based on the current state */ private SceneTransitionRequest CreateSceneTransition(SceneTransitionRequest prevRequest = null) { var transitionBuilder = new SceneTransitionRequest.Builder(); if (prevRequest != null) { // Copy fileds from the previous transition // FIXME: right now we actually only care about background changes... transitionBuilder.SetBackground(prevRequest.TransitionBackground); } var storyText = _story.currentText; var storyTags = _story.currentTags; // Parse tags first, they will come in handy later var actorEmotion = "default"; var canUseDefender = false; var defenderCost = 0; foreach (var tag in storyTags) { var tagComponents = tag.Split(':'); if (tagComponents.Length > 1) { if (tagComponents[0].Equals("defenderAvailable")) { canUseDefender = tagComponents[1].Trim().Equals("true"); } else if (tagComponents[0].Equals("defenderCost")) { defenderCost = int.Parse(tagComponents[1]); } } else { // Keyless tags are interpreted as emotions actorEmotion = tag; } } // Parse story text, possibly handling edge cases like location changes var splitText = storyText.Split(':'); if (splitText.Length > 1) { if (splitText[0].Equals("Location")) { // Location change. We should probably read the next line and re-run our function. transitionBuilder.SetBackground(splitText[1].Trim()); _story.Continue(); return(CreateSceneTransition(transitionBuilder.Build())); } else { if (splitText[0].Equals(_playerActorName)) { var speaker = GetGender() == 0 ? _playerBoyName : _playerGirlName; transitionBuilder.SetSpeaker(speaker, actorEmotion); transitionBuilder.SetPhrase(RebuildString(splitText, 1).Trim()); } else if (_registeredActors.Contains(splitText[0])) { var speaker = splitText[0]; transitionBuilder.SetSpeaker(speaker, actorEmotion); transitionBuilder.SetPhrase(RebuildString(splitText, 1).Trim()); } else { // No one is actually speaking, the colon is just part of the text transitionBuilder.SetPhrase(storyText.Trim()); } } } else { // Set text only transitionBuilder.SetPhrase(storyText.Trim()); } // Parse choices (if there are any) foreach (var choice in _story.currentChoices) { var splitChoice = choice.text.Split('@'); var choiceType = "neutral"; if (splitChoice.Length > 1) { choiceType = splitChoice[1]; } var storyChoice = new StoryChoice(choice.index, splitChoice[0], choiceType); transitionBuilder.AddChoice(storyChoice); } return(transitionBuilder.Build()); }