protected virtual void SetupPortrait(PortraitOptions options) { if (options.character.State.holder == null) { return; } SetRectTransform(options.character.State.holder, options.fromPosition); if (options.character.State.facing != options.character.PortraitsFace) { options.character.State.holder.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.State.holder.localScale = new Vector3(1f, 1f, 1f); } if (options.facing != options.character.PortraitsFace) { options.character.State.holder.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.State.holder.localScale = new Vector3(1f, 1f, 1f); } }
/// <summary> /// Hide portrait with provided options /// </summary> public virtual void Hide(PortraitOptions options) { CleanPortraitOptions(options); if (options.character.State.display == DisplayType.None) { return; } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; HidePortrait(options.character.State.portraitImage.rectTransform, duration); DoMoveTween(options); //update character state after hiding options.character.State.onScreen = false; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; options.character.State.display = DisplayType.Hide; if (stage.CharactersOnStage.Remove(options.character)) { } FinishCommand(options); }
// 设置角色Portrait的朝向 protected virtual void SetupPortrait(PortraitOptions options) { // 设置当前Portrait位置 SetRectTransform(options.character.State.portraitImage.rectTransform, options.fromPosition); // 先把图片 // 转到正方向 // PortraitsFace: 角色图片制作时候的方向 // State.facing: 上一次,或,当前头像朝向 if (options.character.State.facing != options.character.PortraitsFace) { options.character.State.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.State.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f); } // 转换成新的方向 // options:facing: 新的朝向 if (options.facing != options.character.PortraitsFace) { options.character.State.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.State.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f); } }
/// <summary> /// Hide portrait with provided options /// </summary> /// <param name="options"></param> public void Hide(PortraitOptions options) { if (options.character.state.display == DisplayType.None) { return; } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; LeanTween.alpha(options.character.state.portraitImage.rectTransform, 0f, duration).setEase(stage.fadeEaseType); DoMoveTween(options); stage.charactersOnStage.Remove(options.character); //update character state after hiding options.character.state.onScreen = false; options.character.state.portrait = options.portrait; options.character.state.facing = options.facing; options.character.state.position = options.toPosition; options.character.state.display = DisplayType.Hide; FinishCommand(options); }
/// <summary> /// Hide portrait with provided options /// </summary> public virtual void Hide(PortraitOptions options) { CleanPortraitOptions(options); if (options.character.State.display == DisplayType.None) { return; } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly, just in case doing the same for DOTween float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; options.character.State.portraitImage.DOFade(0f, duration).SetEase(stage.FadeEaseType); DoMoveTween(options); stage.CharactersOnStage.Remove(options.character); //update character state after hiding options.character.State.onScreen = false; options.character.State.portrait = options.portrait; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; options.character.State.display = DisplayType.Hide; FinishCommand(options); }
/// <summary> /// Simple character hide command /// </summary> /// <param name="character">Character to hide</param> public void Hide(Character character) { PortraitOptions options = new PortraitOptions(true); options.character = character; Hide(CleanPortraitOptions(options)); }
/// <summary> /// Simple character hide command /// </summary> /// <param name="character">Character to hide</param> public virtual void Hide(Character character) { PortraitOptions options = new PortraitOptions(true); options.character = character; Hide(options); }
/// <summary> /// Moves Character in front of other characters on stage /// </summary> public virtual void MoveToFront(Character character) { PortraitOptions options = new PortraitOptions(true); options.character = character; MoveToFront(CleanPortraitOptions(options)); }
/// <summary> /// Show portrait with the supplied portrait options /// </summary> /// <param name="options"></param> public virtual void Show(PortraitOptions options) { options = CleanPortraitOptions(options); if (options.shiftIntoPlace) { options.fromPosition = Instantiate(options.toPosition) as RectTransform; if (options.offset == PositionOffset.OffsetLeft) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x - Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y)); } else if (options.offset == PositionOffset.OffsetRight) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x + Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y)); } else { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x, options.fromPosition.anchoredPosition.y); } } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; var prevPortrait = options.character.State.portrait; if (options.character.State.portrait != null && options.character.State.portrait != options.portrait) { HidePortrait(options.character.State.portraitImage.rectTransform, duration); } options.character.State.SetPortraitImageBySprite(options.portrait); options.character.State.portraitImage.rectTransform.gameObject.SetActive(true); LeanTween.alpha(options.character.State.portraitImage.rectTransform, 1f, duration).setEase(stage.FadeEaseType).setRecursive(false); DoMoveTween(options); FinishCommand(options); if (!stage.CharactersOnStage.Contains(options.character)) { stage.CharactersOnStage.Add(options.character); } MoveToFront(options); // Update character state after showing options.character.State.onScreen = true; options.character.State.display = DisplayType.Show; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; }
/// <summary> /// Shows character at a named position in the stage /// </summary> /// <param name="character"></param> /// <param name="position">Named position on stage</param> public virtual void Show(Character character, string position) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.fromPosition = options.toPosition = stage.GetPosition(position); Show(options); }
/// <summary> /// Using all portrait options available, run any portrait command. /// </summary> /// <param name="options">Portrait Options</param> /// <param name="onComplete">The function that will run once the portrait command finishes</param> // 执行Portrait命令 public virtual void RunPortraitCommand(PortraitOptions options, Action onComplete) { waitTimer = 0f; // 判断提前结束 // 情况1 // If no character specified, do nothing if (options.character == null) { onComplete(); return; } // 判断提前结束 // 情况2 // If Replace and no replaced character specified, do nothing if (options.display == DisplayType.Replace && options.replacedCharacter == null) { onComplete(); return; } // 判断提前结束 // 情况3 // Early out if hiding a character that's already hidden if (options.display == DisplayType.Hide && !options.character.State.onScreen) { onComplete(); return; } // 清理参数 options = CleanPortraitOptions(options); options.onComplete = onComplete; switch (options.display) { case (DisplayType.Show): Show(options); break; case (DisplayType.Hide): Hide(options); break; case (DisplayType.Replace): Show(options); Hide(options.replacedCharacter, options.replacedCharacter.State.position.name); break; case (DisplayType.MoveToFront): MoveToFront(options); break; } }
/// <summary> /// Move the character to a position then hide it /// </summary> /// <param name="character">Character to hide</param> /// <param name="toPosition">Where the character will disapear to</param> public virtual void Hide(Character character, string toPosition) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.toPosition = stage.GetPosition(toPosition); options.move = true; Hide(options); }
public override void Clean() { base.Clean(); foreach (Character c in new List <Character>(CharactersOnStage)) { PortraitOptions po = new PortraitOptions(); po.character = c; po.display = DisplayType.Hide; Hide(po); } }
/// <summary> /// Shows character moving from a position to a position /// </summary> /// <param name="character"></param> /// <param name="portrait"></param> /// <param name="fromPosition">Where the character will appear</param> /// <param name="toPosition">Where the character will move to</param> public virtual void Show(Character character, string portrait, string fromPosition, string toPosition) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.portrait = character.GetPortrait(portrait); options.fromPosition = stage.GetPosition(fromPosition); options.toPosition = stage.GetPosition(toPosition); options.move = true; Show(options); }
protected virtual void DoMoveTween(Character character, RectTransform fromPosition, RectTransform toPosition, float moveDuration, Boolean waitUntilFinished) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.fromPosition = fromPosition; options.toPosition = toPosition; options.moveDuration = moveDuration; options.waitUntilFinished = waitUntilFinished; DoMoveTween(options); }
public void DoMoveTween(PortraitOptions options) { // LeanTween doesn't handle 0 duration properly float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon; // LeanTween.move uses the anchoredPosition, so all position images must have the same anchor position LeanTween.move(options.character.state.portraitImage.gameObject, options.toPosition.position, duration).setEase(stage.fadeEaseType); if (options.waitUntilFinished) { waitTimer = duration; } }
public override void OnEnter() { // // 获取到舞台 // // Selected "use default Portrait Stage" if (stage == null) { // If no default specified, try to get any portrait stage in the scene stage = FindObjectOfType <Stage>(); // If portrait stage does not exist, do nothing if (stage == null) { Continue(); return; } } // DisplayType枚举, // 设置的是否是None // If no display specified, do nothing if (IsDisplayNone(display)) { Continue(); return; } PortraitOptions options = new PortraitOptions(); // 填写Portrait命令参数 options.character = character; options.replacedCharacter = replacedCharacter; options.portrait = portrait; options.display = display; options.offset = offset; options.fromPosition = fromPosition; options.toPosition = toPosition; options.facing = facing; options.useDefaultSettings = useDefaultSettings; options.fadeDuration = fadeDuration; options.moveDuration = moveDuration; options.shiftOffset = shiftOffset; options.move = move; options.shiftIntoPlace = shiftIntoPlace; options.waitUntilFinished = waitUntilFinished; // Stage运行,Portrait命令 stage.RunPortraitCommand(options, Continue); }
protected virtual void DoMoveTween(PortraitOptions options) { CleanPortraitOptions(options); // LeanTween doesn't handle 0 duration properly, just in case doing the same for DOTween float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon; // All position images must have the same anchor position options.character.State.portraitImage.transform.DOMove(options.toPosition.position, duration).SetEase(stage.FadeEaseType); if (options.waitUntilFinished) { waitTimer = duration; } }
protected virtual void FinishCommand(PortraitOptions options) { if (options.onComplete != null) { if (!options.waitUntilFinished) { options.onComplete(); } else { StartCoroutine(WaitUntilFinished(options.fadeDuration, options.onComplete)); } } else { StartCoroutine(WaitUntilFinished(options.fadeDuration)); } }
protected virtual void DoMoveTween(PortraitOptions options) { CleanPortraitOptions(options); LeanTween.cancel(options.character.State.holder.gameObject); // LeanTween doesn't handle 0 duration properly float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon; // LeanTween.move uses the anchoredPosition, so all position images must have the same anchor position LeanTween.move(options.character.State.holder.gameObject, options.toPosition.position, duration) .setEase(stage.FadeEaseType); if (options.waitUntilFinished) { waitTimer = duration; } }
/// <summary> /// Simple show command that shows the character with an available named portrait /// </summary> /// <param name="character">Character to show</param> /// <param name="portrait">Named portrait to show for the character, i.e. "angry", "happy", etc</param> public virtual void ShowPortrait(Character character, string portrait) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.portrait = character.GetPortrait(portrait); if (character.State.position == null) { options.toPosition = options.fromPosition = stage.GetPosition("middle"); } else { options.fromPosition = options.toPosition = character.State.position; } Show(options); }
/// <summary> /// Show portrait with the supplied portrait options /// </summary> /// <param name="options"></param> public virtual void Show(PortraitOptions options) { options = CleanPortraitOptions(options); if (options.shiftIntoPlace && options.fromPosition == null) { options.fromPosition = Instantiate(options.toPosition) as RectTransform; if (options.offset == PositionOffset.OffsetLeft) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x - Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y)); } else if (options.offset == PositionOffset.OffsetRight) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x + Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y)); } else { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x, options.fromPosition.anchoredPosition.y); } } SetupPortrait(options); DoMoveTween(options); FinishCommand(options); if (!stage.CharactersOnStage.Contains(options.character)) { stage.CharactersOnStage.Add(options.character); } // Update character state after showing options.character.State.onScreen = true; options.character.State.display = DisplayType.Show; options.character.State.portrait = options.portrait; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; }
protected virtual void DoMoveTween(PortraitOptions options) { CleanPortraitOptions(options); // LeanTween doesn't handle 0 duration properly float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon; if (options.fromPosition != null && options.character.State.portraitGraphic.transform.position != options.fromPosition.position) { options.character.State.portraitGraphic.transform.position = options.fromPosition.position; } // LeanTween.move uses the anchoredPosition, so all position images must have the same anchor position LeanTween.move(options.character.State.portraitGraphic.gameObject, options.toPosition.position, duration).setEase(stage.FadeEaseType); if (options.waitUntilFinished) { options.fadeDuration = duration; } }
/// <summary> /// Hide portrait with provided options /// </summary> public virtual void Hide(PortraitOptions options) { // 清理和检查参数 CleanPortraitOptions(options); if (options.character.State.display == DisplayType.None) { return; } // 设置角色Portrait的朝向 SetupPortrait(options); // 防止0 // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; LeanTween. alpha(options.character.State.portraitImage.rectTransform, 0f, duration). setEase(stage.FadeEaseType). setRecursive(false); // 移动角色形象 // 到目标点 DoMoveTween(options); // 从列表中移除 stage.CharactersOnStage.Remove(options.character); // 更新角色的状态 //update character state after hiding options.character.State.onScreen = false; options.character.State.portrait = options.portrait; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; options.character.State.display = DisplayType.Hide; // 立即执行onComplete // 还是fade结束后,调用 FinishCommand(options); }
protected virtual void SetupPortrait(PortraitOptions options) { SetRectTransform(options.character.State.portraitImage.rectTransform, options.fromPosition); if (options.character.State.facing != options.character.PortraitsFace) { options.character.State.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.State.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f); } if (options.facing != options.character.PortraitsFace) { options.character.State.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.State.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f); } }
/// <summary> /// Hide portrait with provided options /// </summary> public virtual void Hide(PortraitOptions options) { CleanPortraitOptions(options); if (options.character.State.display == DisplayType.None) { return; } SetupPortrait(options); DoMoveTween(options); stage.CharactersOnStage.Remove(options.character); //update character state after hiding options.character.State.onScreen = false; options.character.State.portrait = options.portrait; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; options.character.State.display = DisplayType.Hide; FinishCommand(options); }
/// <summary> /// Using all portrait options available, run any portrait command. /// </summary> /// <param name="options">Portrait Options</param> /// <param name="onComplete">The function that will run once the portrait command finishes</param> public void RunPortraitCommand(PortraitOptions options, Action onComplete) { waitTimer = 0f; // If no character specified, do nothing if (options.character == null) { onComplete(); return; } // If Replace and no replaced character specified, do nothing if (options.display == DisplayType.Replace && options.replacedCharacter == null) { onComplete(); return; } // Early out if hiding a character that's already hidden if (options.display == DisplayType.Hide && !options.character.state.onScreen) { onComplete(); return; } options = CleanPortraitOptions(options); options.onComplete = onComplete; switch (options.display) { case (DisplayType.Show): Show(options); break; case (DisplayType.Hide): Hide(options); break; case (DisplayType.Replace): Show(options); Hide(options.replacedCharacter, options.replacedCharacter.state.position.name); break; case (DisplayType.MoveToFront): MoveToFront(options); break; } }
/// <summary> /// Makes sure all options are set correctly so it won't break whatever command it's sent to /// </summary> /// <param name="options"></param> /// <returns></returns> private PortraitOptions CleanPortraitOptions(PortraitOptions options) { // Use default stage settings if (options.useDefaultSettings) { options.fadeDuration = stage.fadeDuration; options.moveDuration = stage.moveDuration; options.shiftOffset = stage.shiftOffset; } // if no previous portrait, use default portrait if (options.character.state.portrait == null) { options.character.state.portrait = options.character.profileSprite; } // Selected "use previous portrait" if (options.portrait == null) { options.portrait = options.character.state.portrait; } // if no previous position, use default position if (options.character.state.position == null) { options.character.state.position = stage.defaultPosition.rectTransform; } // Selected "use previous position" if (options.toPosition == null) { options.toPosition = options.character.state.position; } if (options.replacedCharacter != null) { // if no previous position, use default position if (options.replacedCharacter.state.position == null) { options.replacedCharacter.state.position = stage.defaultPosition.rectTransform; } } // If swapping, use replaced character's position if (options.display == DisplayType.Replace) { options.toPosition = options.replacedCharacter.state.position; } // Selected "use previous position" if (options.fromPosition == null) { options.fromPosition = options.character.state.position; } // if portrait not moving, use from position is same as to position if (!options.move) { options.fromPosition = options.toPosition; } if (options.display == DisplayType.Hide) { options.fromPosition = options.character.state.position; } // if no previous facing direction, use default facing direction if (options.character.state.facing == FacingDirection.None) { options.character.state.facing = options.character.portraitsFace; } // Selected "use previous facing direction" if (options.facing == FacingDirection.None) { options.facing = options.character.state.facing; } if (options.character.state.portraitImage == null) { CreatePortraitObject(options.character, options.fadeDuration); } return options; }
/// <summary> /// Moves Character in front of other characters on stage /// </summary> public virtual void MoveToFront(PortraitOptions options) { options.character.State.holder.SetSiblingIndex(options.character.State.holder.parent.childCount); options.character.State.display = DisplayType.MoveToFront; FinishCommand(options); }
/// <summary> /// Shows character at a named position in the stage /// </summary> /// <param name="character"></param> /// <param name="position">Named position on stage</param> public void Show(Character character, string position) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.fromPosition = options.toPosition = stage.GetPosition(position); Show(options); }
private void SetupPortrait(PortraitOptions options) { SetRectTransform(options.character.state.portraitImage.rectTransform, options.fromPosition); if (options.character.state.facing != options.character.portraitsFace) { options.character.state.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.state.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f); } if (options.facing != options.character.portraitsFace) { options.character.state.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f); } else { options.character.state.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f); } }
/// <summary> /// Hide portrait with provided options /// </summary> /// <param name="options"></param> public void Hide(PortraitOptions options) { CleanPortraitOptions(options); if (options.character.state.display == DisplayType.None) { return; } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; LeanTween.alpha(options.character.state.portraitImage.rectTransform, 0f, duration).setEase(stage.fadeEaseType); DoMoveTween(options); stage.charactersOnStage.Remove(options.character); //update character state after hiding options.character.state.onScreen = false; options.character.state.portrait = options.portrait; options.character.state.facing = options.facing; options.character.state.position = options.toPosition; options.character.state.display = DisplayType.Hide; FinishCommand(options); }
/// <summary> /// Convert a Moonsharp table to portrait options /// If the table returns a null for any of the parameters, it should keep the defaults /// </summary> /// <param name="table">Moonsharp Table</param> /// <param name="stage">Stage</param> /// <returns></returns> public static PortraitOptions ConvertTableToPortraitOptions(Table table, Stage stage) { PortraitOptions options = new PortraitOptions(true); // If the table supplies a nil, keep the default options.character = table.Get("character").ToObject<Character>() ?? options.character; options.replacedCharacter = table.Get("replacedCharacter").ToObject<Character>() ?? options.replacedCharacter; if (!table.Get("portrait").IsNil()) { options.portrait = options.character.GetPortrait(table.Get("portrait").CastToString()); } if (!table.Get("display").IsNil()) { options.display = table.Get("display").ToObject<DisplayType>(); } if (!table.Get("offset").IsNil()) { options.offset = table.Get("offset").ToObject<PositionOffset>(); } if (!table.Get("fromPosition").IsNil()) { options.fromPosition = stage.GetPosition(table.Get("fromPosition").CastToString()); } if (!table.Get("toPosition").IsNil()) { options.toPosition = stage.GetPosition(table.Get("toPosition").CastToString()); } if (!table.Get("facing").IsNil()) { var facingDirection = FacingDirection.None; DynValue v = table.Get("facing"); if (v.Type == DataType.String) { if (string.Compare(v.String, "left", true) == 0) { facingDirection = FacingDirection.Left; } else if (string.Compare(v.String, "right", true) == 0) { facingDirection = FacingDirection.Right; } } else { facingDirection = table.Get("facing").ToObject<FacingDirection>(); } options.facing = facingDirection; } if (!table.Get("useDefaultSettings").IsNil()) { options.useDefaultSettings = table.Get("useDefaultSettings").CastToBool(); } if (!table.Get("fadeDuration").IsNil()) { options.fadeDuration = table.Get("fadeDuration").ToObject<float>(); } if (!table.Get("moveDuration").IsNil()) { options.moveDuration = table.Get("moveDuration").ToObject<float>(); } if (!table.Get("move").IsNil()) { options.move = table.Get("move").CastToBool(); } else if (options.fromPosition != options.toPosition) { options.move = true; } if (!table.Get("shiftIntoPlace").IsNil()) { options.shiftIntoPlace = table.Get("shiftIntoPlace").CastToBool(); } if (!table.Get("waitUntilFinished").IsNil()) { options.waitUntilFinished = table.Get("waitUntilFinished").CastToBool(); } return options; }
public void DoMoveTween(Character character, RectTransform fromPosition, RectTransform toPosition, float moveDuration, Boolean waitUntilFinished) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.fromPosition = fromPosition; options.toPosition = toPosition; options.moveDuration = moveDuration; options.waitUntilFinished = waitUntilFinished; DoMoveTween(options); }
/// <summary> /// Shows character moving from a position to a position /// </summary> /// <param name="character"></param> /// <param name="portrait"></param> /// <param name="fromPosition">Where the character will appear</param> /// <param name="toPosition">Where the character will move to</param> public void Show(Character character, string portrait, string fromPosition, string toPosition) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.portrait = character.GetPortrait(portrait); options.fromPosition = stage.GetPosition(fromPosition); options.toPosition = stage.GetPosition(toPosition); options.move = true; Show(options); }
/// <summary> /// Show portrait with the supplied portrait options /// </summary> /// <param name="options"></param> public void Show(PortraitOptions options) { options = CleanPortraitOptions(options); if (options.shiftIntoPlace) { options.fromPosition = Instantiate(options.toPosition) as RectTransform; if (options.offset == PositionOffset.OffsetLeft) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x - Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y)); } else if (options.offset == PositionOffset.OffsetRight) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x + Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y)); } else { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x, options.fromPosition.anchoredPosition.y); } } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; // Fade out a duplicate of the existing portrait image if (options.character.state.portraitImage != null) { GameObject tempGO = GameObject.Instantiate(options.character.state.portraitImage.gameObject); tempGO.transform.SetParent(options.character.state.portraitImage.transform, false); tempGO.transform.localPosition = Vector3.zero; tempGO.transform.localScale = options.character.state.position.localScale; Image tempImage = tempGO.GetComponent<Image>(); tempImage.sprite = options.character.state.portraitImage.sprite; tempImage.preserveAspect = true; tempImage.color = options.character.state.portraitImage.color; LeanTween.alpha(tempImage.rectTransform, 0f, duration).setEase(stage.fadeEaseType).setOnComplete(() => { Destroy(tempGO); }); } // Fade in the new sprite image if (options.character.state.portraitImage.sprite != options.portrait || options.character.state.portraitImage.color.a < 1f) { options.character.state.portraitImage.sprite = options.portrait; options.character.state.portraitImage.color = new Color(1f, 1f, 1f, 0f); LeanTween.alpha(options.character.state.portraitImage.rectTransform, 1f, duration).setEase(stage.fadeEaseType); } DoMoveTween(options); FinishCommand(options); if (!stage.charactersOnStage.Contains(options.character)) { stage.charactersOnStage.Add(options.character); } // Update character state after showing options.character.state.onScreen = true; options.character.state.display = DisplayType.Show; options.character.state.portrait = options.portrait; options.character.state.facing = options.facing; options.character.state.position = options.toPosition; }
/// <summary> /// Convert a Moonsharp table to portrait options /// If the table returns a null for any of the parameters, it should keep the defaults /// </summary> /// <param name="table">Moonsharp Table</param> /// <param name="stage">Stage</param> /// <returns></returns> public static PortraitOptions ConvertTableToPortraitOptions(Table table, Stage stage) { PortraitOptions options = new PortraitOptions(true); // If the table supplies a nil, keep the default options.character = table.Get("character").ToObject<Character>() ?? options.character; options.replacedCharacter = table.Get("replacedCharacter").ToObject<Character>() ?? options.replacedCharacter; if (!table.Get("portrait").IsNil()) { options.portrait = options.character.GetPortrait(table.Get("portrait").CastToString()); } if (!table.Get("display").IsNil()) { options.display = table.Get("display").ToObject<DisplayType>(); } if (!table.Get("offset").IsNil()) { options.offset = table.Get("offset").ToObject<PositionOffset>(); } if (!table.Get("fromPosition").IsNil()) { options.fromPosition = stage.GetPosition(table.Get("fromPosition").CastToString()); } if (!table.Get("toPosition").IsNil()) { options.toPosition = stage.GetPosition(table.Get("toPosition").CastToString()); } if (!table.Get("facing").IsNil()) { options.facing = table.Get("facing").ToObject<FacingDirection>(); } if (!table.Get("useDefaultSettings").IsNil()) { options.useDefaultSettings = table.Get("useDefaultSettings").CastToBool(); } if (!table.Get("fadeDuration").IsNil()) { options.fadeDuration = table.Get("fadeDuration").ToObject<float>(); } if (!table.Get("moveDuration").IsNil()) { options.moveDuration = table.Get("moveDuration").ToObject<float>(); } if (!table.Get("move").IsNil()) { options.move = table.Get("move").CastToBool(); } else if (options.fromPosition != options.toPosition) { options.move = true; } if (!table.Get("shiftIntoPlace").IsNil()) { options.shiftIntoPlace = table.Get("shiftIntoPlace").CastToBool(); } //TODO: Make the next lua command wait when this options is true if (!table.Get("waitUntilFinished").IsNil()) { options.waitUntilFinished = table.Get("waitUntilFinished").CastToBool(); } return options; }
public void DoMoveTween(PortraitOptions options) { CleanPortraitOptions(options); // LeanTween doesn't handle 0 duration properly float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon; // LeanTween.move uses the anchoredPosition, so all position images must have the same anchor position LeanTween.move(options.character.state.portraitImage.gameObject, options.toPosition.position, duration).setEase(stage.fadeEaseType); if (options.waitUntilFinished) { waitTimer = duration; } }
/// <summary> /// Show portrait with the supplied portrait options /// </summary> /// <param name="options"></param> public virtual void Show(PortraitOptions options) { options = CleanPortraitOptions(options); if (options.shiftIntoPlace) { options.fromPosition = Instantiate(options.toPosition) as RectTransform; if (options.offset == PositionOffset.OffsetLeft) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x - Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y)); } else if (options.offset == PositionOffset.OffsetRight) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x + Mathf.Abs(options.shiftOffset.x), options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y)); } else { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x, options.fromPosition.anchoredPosition.y); } } SetupPortrait(options); // LeanTween doesn't handle 0 duration properly float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon; // Fade out a duplicate of the existing portrait image if (options.character.State.portraitImage != null && options.character.State.portraitImage.overrideSprite != null) { GameObject tempGO = GameObject.Instantiate(options.character.State.portraitImage.gameObject); tempGO.transform.SetParent(options.character.State.portraitImage.transform, false); tempGO.transform.localPosition = Vector3.zero; tempGO.transform.localScale = options.character.State.position.localScale; Image tempImage = tempGO.GetComponent <Image>(); tempImage.overrideSprite = options.character.State.portraitImage.overrideSprite; tempImage.preserveAspect = true; tempImage.color = options.character.State.portraitImage.color; LeanTween.alpha(tempImage.rectTransform, 0f, duration).setEase(stage.FadeEaseType).setOnComplete(() => { Destroy(tempGO); }).setRecursive(false); } // Fade in the new sprite image if (options.character.State.portraitImage.overrideSprite != options.portrait || options.character.State.portraitImage.color.a < 1f) { options.character.State.portraitImage.overrideSprite = options.portrait; options.character.State.portraitImage.color = new Color(1f, 1f, 1f, 0f); LeanTween.alpha(options.character.State.portraitImage.rectTransform, 1f, duration).setEase(stage.FadeEaseType).setRecursive(false); } DoMoveTween(options); FinishCommand(options); if (!stage.CharactersOnStage.Contains(options.character)) { stage.CharactersOnStage.Add(options.character); } // Update character state after showing options.character.State.onScreen = true; options.character.State.display = DisplayType.Show; options.character.State.portrait = options.portrait; options.character.State.facing = options.facing; options.character.State.position = options.toPosition; }
/// <summary> /// Moves Character in front of other characters on stage /// </summary> /// <param name="character"></param> public void MoveToFront(Character character) { PortraitOptions options = new PortraitOptions(true); options.character = character; MoveToFront(CleanPortraitOptions(options)); }
/// <summary> /// Move the character to a position then hide it /// </summary> /// <param name="character"></param> /// <param name="toPosition">Where the character will disapear to</param> public void Hide(Character character, string toPosition) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.toPosition = stage.GetPosition(toPosition); options.move = true; Hide(options); }
/// <summary> /// Parse and execute a conversation string /// </summary> /// <param name="conv"></param> public IEnumerator DoConversation(string conv) { if (string.IsNullOrEmpty(conv)) { yield break; } var conversationItems = Parse(conv); if (conversationItems.Count == 0) { yield break; } // Track the current and previous parameter values Character currentCharacter = null; Sprite currentPortrait = null; RectTransform currentPosition = null; Character previousCharacter = null; // Play the conversation for (int i = 0; i < conversationItems.Count; ++i) { ConversationItem item = conversationItems[i]; if (item.Character != null) { currentCharacter = item.Character; } currentPortrait = item.Portrait; currentPosition = item.Position; SayDialog sayDialog = GetSayDialog(currentCharacter); if (sayDialog == null) { // Should never happen yield break; } sayDialog.gameObject.SetActive(true); if (currentCharacter != null && currentCharacter != previousCharacter) { sayDialog.SetCharacter(currentCharacter); } var stage = Stage.GetActiveStage(); if (stage != null && currentCharacter != null && (currentPortrait != currentCharacter.state.portrait || currentPosition != currentCharacter.state.position)) { var portraitOptions = new PortraitOptions(true); portraitOptions.display = item.Hide ? DisplayType.Hide : DisplayType.Show; portraitOptions.character = currentCharacter; portraitOptions.fromPosition = currentCharacter.state.position; portraitOptions.toPosition = currentPosition; portraitOptions.portrait = currentPortrait; // Do a move tween if the character is already on screen and not yet at the specified position if (currentCharacter.state.onScreen && currentPosition != currentCharacter.state.position) { portraitOptions.move = true; } if (item.Hide) { stage.Hide(portraitOptions); } else { stage.Show(portraitOptions); } } if (stage == null && currentPortrait != null) { sayDialog.SetCharacterImage(currentPortrait); } previousCharacter = currentCharacter; if (!string.IsNullOrEmpty(item.Text)) { exitSayWait = false; sayDialog.Say(item.Text, true, true, true, false, null, () => { exitSayWait = true; }); while (!exitSayWait) { yield return null; } exitSayWait = false; } } }
/// <summary> /// Parse and execute a conversation string. /// </summary> public virtual IEnumerator DoConversation(string conv) { if (string.IsNullOrEmpty(conv)) { yield break; } var conversationItems = Parse(conv); if (conversationItems.Count == 0) { yield break; } // Track the current and previous parameter values Character currentCharacter = null; Sprite currentPortrait = null; RectTransform currentPosition = null; Character previousCharacter = null; // Play the conversation for (int i = 0; i < conversationItems.Count; ++i) { ConversationItem item = conversationItems[i]; if (item.Character != null) { currentCharacter = item.Character; } currentPortrait = item.Portrait; currentPosition = item.Position; var sayDialog = GetSayDialog(currentCharacter); if (sayDialog == null) { // Should never happen yield break; } sayDialog.SetActive(true); if (currentCharacter != null && currentCharacter != previousCharacter) { sayDialog.SetCharacter(currentCharacter); } //Handle stage changes var stage = Stage.GetActiveStage(); if (currentCharacter != null && !currentCharacter.State.onScreen && currentPortrait == null) { // No call to show portrait of hidden character // so keep hidden item.Hide = true; } if (stage != null && currentCharacter != null && (currentPortrait != currentCharacter.State.portrait || currentPosition != currentCharacter.State.position)) { var portraitOptions = new PortraitOptions(true); portraitOptions.display = item.Hide ? DisplayType.Hide : DisplayType.Show; portraitOptions.character = currentCharacter; portraitOptions.fromPosition = currentCharacter.State.position; portraitOptions.toPosition = currentPosition; portraitOptions.portrait = currentPortrait; //Flip option - Flip the opposite direction the character is currently facing if (item.Flip) { portraitOptions.facing = item.FacingDirection; } // Do a move tween if the character is already on screen and not yet at the specified position if (currentCharacter.State.onScreen && currentPosition != currentCharacter.State.position) { portraitOptions.move = true; } if (item.Hide) { stage.Hide(portraitOptions); } else { stage.Show(portraitOptions); } } if (stage == null && currentPortrait != null) { sayDialog.SetCharacterImage(currentPortrait); } previousCharacter = currentCharacter; if (!string.IsNullOrEmpty(item.Text)) { exitSayWait = false; sayDialog.Say(item.Text, true, true, true, true, false, null, () => { exitSayWait = true; }); while (!exitSayWait) { yield return(null); } exitSayWait = false; } } }
/// <summary> /// Simple show command that shows the character with an available named portrait /// </summary> /// <param name="character">Character to show</param> /// <param name="portrait">Named portrait to show for the character, i.e. "angry", "happy", etc</param> public void ShowPortrait(Character character, string portrait) { PortraitOptions options = new PortraitOptions(true); options.character = character; options.portrait = character.GetPortrait(portrait); if (character.state.position == null) { options.toPosition = options.fromPosition = stage.GetPosition("middle"); } else { options.fromPosition = options.toPosition = character.state.position; } Show(options); }
public void MoveToFront(PortraitOptions options) { options.character.state.portraitImage.transform.SetSiblingIndex(options.character.state.portraitImage.transform.parent.childCount); options.character.state.display = DisplayType.MoveToFront; FinishCommand(options); }
/// <summary> /// Makes sure all options are set correctly so it won't break whatever command it's sent to /// </summary> /// <param name="options"></param> /// <returns></returns> protected virtual PortraitOptions CleanPortraitOptions(PortraitOptions options) { // Use default stage settings if (options.useDefaultSettings) { options.fadeDuration = stage.FadeDuration; options.moveDuration = stage.MoveDuration; options.shiftOffset = stage.ShiftOffset; } // if no previous portrait, use default portrait if (options.character.State.portrait == null) { options.character.State.SetPortraitImageBySprite(options.character.ProfileSprite); } // Selected "use previous portrait" if (options.portrait == null) { options.portrait = options.character.State.portrait; } // if no previous position, use default position if (options.character.State.position == null) { options.character.State.position = stage.DefaultPosition.rectTransform; } // Selected "use previous position" if (options.toPosition == null) { options.toPosition = options.character.State.position; } if (options.replacedCharacter != null) { // if no previous position, use default position if (options.replacedCharacter.State.position == null) { options.replacedCharacter.State.position = stage.DefaultPosition.rectTransform; } } // If swapping, use replaced character's position if (options.display == DisplayType.Replace) { options.toPosition = options.replacedCharacter.State.position; } // Selected "use previous position" if (options.fromPosition == null) { options.fromPosition = options.character.State.position; } // if portrait not moving, use from position is same as to position if (!options.move) { options.fromPosition = options.toPosition; } if (options.display == DisplayType.Hide) { options.fromPosition = options.character.State.position; } // if no previous facing direction, use default facing direction if (options.character.State.facing == FacingDirection.None) { options.character.State.facing = options.character.PortraitsFace; } // Selected "use previous facing direction" if (options.facing == FacingDirection.None) { options.facing = options.character.State.facing; } if (options.character.State.portraitImage == null) { CreatePortraitObject(options.character, options.fadeDuration); } return(options); }
/// <summary> /// Simple character hide command /// </summary> /// <param name="character">Character to hide</param> public void Hide(Character character) { PortraitOptions options = new PortraitOptions(true); options.character = character; Hide(options); }