Advanced POC - portals, dialogue system + floating words, room loading
This commit is contained in:
+501
@@ -0,0 +1,501 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System.Collections.Generic;
|
||||
using Unheimlich.Data.Dialogues;
|
||||
using Unheimlich.Managers;
|
||||
using Unheimlich.Gameplay.Player;
|
||||
|
||||
namespace Unheimlich.Interactions.FloatingWords
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages individual dialogue choice button behavior including visual feedback during focus,
|
||||
/// dynamic collider sizing, and smooth animations. Responds to player gaze through the focus system.
|
||||
/// </summary>
|
||||
public class ChoiceFocusHandler : MonoBehaviour
|
||||
{
|
||||
[Header("Components")]
|
||||
[SerializeField] private TextMeshProUGUI _choiceTextComponent;
|
||||
[SerializeField] private TextMeshProUGUI _ghostTextComponent;
|
||||
|
||||
public DialogueChoiceData DialogueData { get; private set; }
|
||||
|
||||
public string GetChoiceText()
|
||||
{
|
||||
return DialogueData?.ChoiceText ?? string.Empty;
|
||||
}
|
||||
|
||||
public DialogueNode GetNextNode()
|
||||
{
|
||||
return DialogueData?.NextNode;
|
||||
}
|
||||
|
||||
private FloatingWordsSpawner.SpawnConfig _config;
|
||||
private bool _hasConfig;
|
||||
|
||||
[Header("Local Feedback Settings")]
|
||||
[SerializeField] private float _focusLerpSpeed = 10f;
|
||||
[SerializeField] private float _colliderSizeMultiplier = 1.4f;
|
||||
[SerializeField] private float _choiceDistanceFromPlayer = 1f;
|
||||
|
||||
[Header("Text Sizing Settings")]
|
||||
[Tooltip("Maximum width for short texts (1-3 words)")]
|
||||
[SerializeField] private float _shortTextMaxWidth = 80f;
|
||||
[Tooltip("Maximum width for medium texts (4-5 words)")]
|
||||
[SerializeField] private float _mediumTextMaxWidth = 150f;
|
||||
[Tooltip("Maximum width for long texts (6-10 words)")]
|
||||
[SerializeField] private float _longTextMaxWidth = 200f;
|
||||
[Tooltip("Maximum width for very long texts (11+ words)")]
|
||||
[SerializeField] private float _veryLongTextMaxWidth = 250f;
|
||||
[Tooltip("Minimum height for text container")]
|
||||
[SerializeField] private float _minTextHeight = 40f;
|
||||
|
||||
private const int FOCUSED_SORTING_ORDER = 10;
|
||||
private const float MAX_FOCUS_SCALE = 1.2f;
|
||||
|
||||
private BoxCollider _choiceCollider;
|
||||
private Canvas _canvas;
|
||||
private int _originalSortingOrder;
|
||||
private Vector3 _startLocalPos;
|
||||
private float _originalScale;
|
||||
private bool _positionCaptured = false;
|
||||
|
||||
private Vector3 _shakeOffset = Vector3.zero;
|
||||
private Vector3 _shakeTarget = Vector3.zero;
|
||||
private float _shakeTimer = 0f;
|
||||
private bool _isShaking = false;
|
||||
|
||||
private bool _disableFloatingEffects = false;
|
||||
|
||||
private Queue<Vector3> _positionHistory = new Queue<Vector3>();
|
||||
private float _historyTimer = 0f;
|
||||
private const float HISTORY_SAMPLE_RATE = 0.016f;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_canvas = GetComponentInParent<Canvas>();
|
||||
if (_canvas != null)
|
||||
_originalSortingOrder = _canvas.sortingOrder;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_isShaking)
|
||||
UpdateOrganicShake();
|
||||
|
||||
UpdateGhostTrail();
|
||||
}
|
||||
|
||||
private void SetupGhostTrail()
|
||||
{
|
||||
if (!GetEnableGhostTrail())
|
||||
return;
|
||||
|
||||
if (_ghostTextComponent == null && _choiceTextComponent != null)
|
||||
{
|
||||
GameObject ghostObj = new GameObject($"GhostText_{gameObject.name}");
|
||||
ghostObj.transform.SetParent(transform.parent, false);
|
||||
|
||||
ghostObj.transform.localPosition = transform.localPosition;
|
||||
ghostObj.transform.localRotation = transform.localRotation;
|
||||
ghostObj.transform.localScale = transform.localScale;
|
||||
|
||||
ghostObj.layer =gameObject.layer;
|
||||
|
||||
_ghostTextComponent = ghostObj.AddComponent<TextMeshProUGUI>();
|
||||
_ghostTextComponent.font = _choiceTextComponent.font;
|
||||
_ghostTextComponent.fontSize = _choiceTextComponent.fontSize;
|
||||
_ghostTextComponent.alignment = _choiceTextComponent.alignment;
|
||||
_ghostTextComponent.textWrappingMode = _choiceTextComponent.textWrappingMode;
|
||||
_ghostTextComponent.text = _choiceTextComponent.text;
|
||||
|
||||
_ghostTextComponent.rectTransform.sizeDelta = _choiceTextComponent.rectTransform.sizeDelta;
|
||||
_ghostTextComponent.rectTransform.anchorMin = _choiceTextComponent.rectTransform.anchorMin;
|
||||
_ghostTextComponent.rectTransform.anchorMax = _choiceTextComponent.rectTransform.anchorMax;
|
||||
_ghostTextComponent.rectTransform.pivot = _choiceTextComponent.rectTransform.pivot;
|
||||
|
||||
Color ghostColor = _choiceTextComponent.color;
|
||||
ghostColor.a = GetGhostTrailAlpha();
|
||||
_ghostTextComponent.color = ghostColor;
|
||||
|
||||
Canvas ghostCanvas = ghostObj.AddComponent<Canvas>();
|
||||
ghostCanvas.overrideSorting = true;
|
||||
ghostCanvas.sortingOrder = -1;
|
||||
|
||||
_positionHistory.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGhostTrail()
|
||||
{
|
||||
if (!GetEnableGhostTrail() || _ghostTextComponent == null || _choiceTextComponent == null)
|
||||
return;
|
||||
|
||||
_historyTimer += Time.deltaTime;
|
||||
|
||||
if (_historyTimer >= HISTORY_SAMPLE_RATE)
|
||||
{
|
||||
_positionHistory.Enqueue(transform.localPosition);
|
||||
_historyTimer = 0f;
|
||||
|
||||
int maxHistorySize = Mathf.CeilToInt(GetGhostTrailDelay() / HISTORY_SAMPLE_RATE);
|
||||
|
||||
while (_positionHistory.Count > maxHistorySize && _positionHistory.Count > 0)
|
||||
_positionHistory.Dequeue();
|
||||
}
|
||||
|
||||
if (_positionHistory.Count > 0)
|
||||
{
|
||||
Vector3 delayedPosition = _positionHistory.Peek();
|
||||
_ghostTextComponent.transform.localPosition = delayedPosition;
|
||||
_ghostTextComponent.transform.localRotation = transform.localRotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
_ghostTextComponent.transform.localPosition = transform.localPosition;
|
||||
_ghostTextComponent.transform.localRotation = transform.localRotation;
|
||||
}
|
||||
|
||||
_ghostTextComponent.text = _choiceTextComponent.text;
|
||||
_ghostTextComponent.fontSize = _choiceTextComponent.fontSize;
|
||||
_ghostTextComponent.rectTransform.sizeDelta = _choiceTextComponent.rectTransform.sizeDelta;
|
||||
|
||||
Color ghostColor = _choiceTextComponent.color;
|
||||
ghostColor.a = GetGhostTrailAlpha();
|
||||
_ghostTextComponent.color = ghostColor;
|
||||
}
|
||||
|
||||
private void UpdateOrganicShake()
|
||||
{
|
||||
if (!GetEnableOrganicShake())
|
||||
return;
|
||||
|
||||
_shakeTimer += Time.deltaTime * GetShakeSpeed();
|
||||
|
||||
if (_shakeTimer >= 1f)
|
||||
{
|
||||
_shakeTimer = 0f;
|
||||
_shakeTarget = Random.insideUnitSphere * GetShakeRadius();
|
||||
}
|
||||
|
||||
_shakeOffset = Vector3.Lerp(_shakeOffset, _shakeTarget, Time.deltaTime * GetShakeSmoothness());
|
||||
|
||||
if (!_disableFloatingEffects)
|
||||
transform.localPosition = _startLocalPos + _shakeOffset;
|
||||
}
|
||||
|
||||
public Vector3 GetShakeOffset(float convergenceFactor)
|
||||
{
|
||||
return _shakeOffset * convergenceFactor;
|
||||
}
|
||||
|
||||
public void StartOrganicShake()
|
||||
{
|
||||
if (GetEnableOrganicShake())
|
||||
{
|
||||
_isShaking = true;
|
||||
_shakeTimer = 0f;
|
||||
_shakeTarget = Random.insideUnitSphere * GetShakeRadius();
|
||||
}
|
||||
}
|
||||
|
||||
public void StopOrganicShake()
|
||||
{
|
||||
_isShaking = false;
|
||||
_shakeOffset = Vector3.zero;
|
||||
}
|
||||
|
||||
public void HideGhostTrail()
|
||||
{
|
||||
if (_ghostTextComponent != null)
|
||||
_ghostTextComponent.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void ShowGhostTrail()
|
||||
{
|
||||
if (_ghostTextComponent != null)
|
||||
_ghostTextComponent.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies progressive visual feedback as player focuses on this choice.
|
||||
/// Scales the choice, changes color, optionally moves toward player, and adjusts sorting order.
|
||||
/// Progress ranges from 0 (unfocused) to 1 (fully focused).
|
||||
/// </summary>
|
||||
public void ApplyFocusFeedback(float progress)
|
||||
{
|
||||
if (!_positionCaptured)
|
||||
{
|
||||
_startLocalPos = transform.localPosition;
|
||||
_positionCaptured = true;
|
||||
|
||||
LeanTween.cancel(gameObject);
|
||||
StopOrganicShake();
|
||||
}
|
||||
|
||||
transform.localScale = Vector3.one * Mathf.Lerp(_originalScale, MAX_FOCUS_SCALE, progress);
|
||||
|
||||
bool shouldMoveToPlayer = GetMoveTowardPlayerOnFocus();
|
||||
|
||||
if (shouldMoveToPlayer && Player.Instance != null)
|
||||
{
|
||||
float distance = GetFocusDistanceFromPlayer();
|
||||
Vector3 targetPos = Player.Instance.transform.position
|
||||
+ Player.Instance.transform.forward * distance;
|
||||
transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * _focusLerpSpeed);
|
||||
}
|
||||
|
||||
if (_choiceTextComponent != null)
|
||||
{
|
||||
_choiceTextComponent.color = Color.Lerp(Color.white, Color.red, progress);
|
||||
|
||||
if (_ghostTextComponent != null)
|
||||
{
|
||||
float ghostAlpha = GetGhostTrailAlpha();
|
||||
Color ghostColor = Color.Lerp(Color.white, Color.red, progress);
|
||||
ghostColor.a = ghostAlpha;
|
||||
_ghostTextComponent.color = ghostColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (_canvas != null && _canvas.sortingOrder != FOCUSED_SORTING_ORDER)
|
||||
{
|
||||
_canvas.sortingOrder = FOCUSED_SORTING_ORDER;
|
||||
_canvas.overrideSorting = true;
|
||||
}
|
||||
}
|
||||
|
||||
private DialogueAnimationSettings GetAnimationSettings()
|
||||
{
|
||||
if (DialogueManager.Instance != null)
|
||||
return DialogueManager.Instance.CurrentAnimationSettings;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool GetEnableGhostTrail()
|
||||
{
|
||||
if (_hasConfig) return _config.EnableGhostTrail;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null && settings.EnableGhostTrail;
|
||||
}
|
||||
|
||||
private float GetGhostTrailAlpha()
|
||||
{
|
||||
if (_hasConfig) return _config.GhostTrailAlpha;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null ? settings.GhostTrailAlpha : 0.3f;
|
||||
}
|
||||
|
||||
private float GetGhostTrailDelay()
|
||||
{
|
||||
if (_hasConfig) return _config.GhostTrailDelay;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null ? settings.GhostTrailDelay : 0.1f;
|
||||
}
|
||||
|
||||
private bool GetEnableOrganicShake()
|
||||
{
|
||||
if (_hasConfig) return _config.EnableOrganicShake;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null && settings.EnableOrganicShake;
|
||||
}
|
||||
|
||||
private float GetShakeRadius()
|
||||
{
|
||||
if (_hasConfig) return _config.ShakeRadius;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null ? settings.ShakeRadius : 0.01f;
|
||||
}
|
||||
|
||||
private float GetShakeSpeed()
|
||||
{
|
||||
if (_hasConfig) return _config.ShakeSpeed;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null ? settings.ShakeSpeed : 1f;
|
||||
}
|
||||
|
||||
private float GetShakeSmoothness()
|
||||
{
|
||||
if (_hasConfig) return _config.ShakeSmoothness;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null ? settings.ShakeSmoothness : 5f;
|
||||
}
|
||||
|
||||
private bool GetMoveTowardPlayerOnFocus()
|
||||
{
|
||||
if (_hasConfig) return _config.MoveTowardPlayerOnFocus;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings == null || settings.MoveTowardPlayerOnFocus;
|
||||
}
|
||||
|
||||
private float GetFocusDistanceFromPlayer()
|
||||
{
|
||||
if (_hasConfig) return _config.FocusDistanceFromPlayer;
|
||||
DialogueAnimationSettings settings = GetAnimationSettings();
|
||||
return settings != null ? settings.FocusDistanceFromPlayer : _choiceDistanceFromPlayer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the choice button with dialogue data.
|
||||
/// Sets up text, creates/configures collider based on text bounds, and caches initial state.
|
||||
/// </summary>
|
||||
public void Setup(DialogueChoiceData dialogueData, FloatingWordsSpawner.SpawnConfig config, bool disableFloatingEffects = false)
|
||||
{
|
||||
DialogueData = dialogueData;
|
||||
_config = config;
|
||||
_hasConfig = true;
|
||||
_disableFloatingEffects = disableFloatingEffects;
|
||||
|
||||
PerformSetup();
|
||||
}
|
||||
|
||||
private void PerformSetup()
|
||||
{
|
||||
if (_choiceTextComponent != null)
|
||||
{
|
||||
_choiceTextComponent.text = GetChoiceText();
|
||||
_choiceTextComponent.ForceMeshUpdate();
|
||||
|
||||
AdjustTextSizeForContent();
|
||||
}
|
||||
|
||||
_choiceCollider = GetComponent<BoxCollider>();
|
||||
if (_choiceCollider == null)
|
||||
{
|
||||
_choiceCollider = gameObject.AddComponent<BoxCollider>();
|
||||
_choiceCollider.isTrigger = true;
|
||||
}
|
||||
|
||||
if (_choiceTextComponent != null)
|
||||
{
|
||||
Vector3 textSize = _choiceTextComponent.bounds.size;
|
||||
_choiceCollider.size = textSize * _colliderSizeMultiplier;
|
||||
_choiceCollider.center = _choiceTextComponent.rectTransform.localPosition;
|
||||
}
|
||||
|
||||
_startLocalPos = transform.localPosition;
|
||||
_originalScale = transform.localScale.x;
|
||||
|
||||
LeanTween.cancel(gameObject);
|
||||
|
||||
SetupGhostTrail();
|
||||
|
||||
if (_ghostTextComponent != null)
|
||||
_ghostTextComponent.text = GetChoiceText();
|
||||
|
||||
StartOrganicShake();
|
||||
}
|
||||
|
||||
private void AdjustTextSizeForContent()
|
||||
{
|
||||
if (_choiceTextComponent == null) return;
|
||||
|
||||
string text = GetChoiceText();
|
||||
if (string.IsNullOrEmpty(text)) return;
|
||||
|
||||
int wordCount = text.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries).Length;
|
||||
|
||||
float maxWidth = GetMaxWidthForWordCount(wordCount);
|
||||
|
||||
string longestWord = GetLongestWord(text);
|
||||
Vector2 longestWordSize = _choiceTextComponent.GetPreferredValues(longestWord, float.MaxValue, 0f);
|
||||
|
||||
if (longestWordSize.x > maxWidth)
|
||||
maxWidth = longestWordSize.x + 10f;
|
||||
|
||||
_choiceTextComponent.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxWidth);
|
||||
_choiceTextComponent.ForceMeshUpdate();
|
||||
|
||||
Vector2 textSize = _choiceTextComponent.GetPreferredValues(text, maxWidth, 0f);
|
||||
float finalHeight = Mathf.Max(textSize.y, _minTextHeight);
|
||||
|
||||
Vector2 newSize = new Vector2(maxWidth, finalHeight);
|
||||
|
||||
RectTransform parentRect = transform as RectTransform;
|
||||
if (parentRect != null)
|
||||
parentRect.sizeDelta = newSize;
|
||||
|
||||
ConfigureTextComponent(_choiceTextComponent, newSize);
|
||||
|
||||
if (_ghostTextComponent != null)
|
||||
ConfigureTextComponent(_ghostTextComponent, newSize);
|
||||
}
|
||||
|
||||
private void ConfigureTextComponent(TextMeshProUGUI textComponent, Vector2 size)
|
||||
{
|
||||
textComponent.textWrappingMode = TextWrappingModes.Normal;
|
||||
textComponent.overflowMode = TextOverflowModes.Overflow;
|
||||
textComponent.rectTransform.anchorMin = Vector2.one * 0.5f;
|
||||
textComponent.rectTransform.anchorMax = Vector2.one * 0.5f;
|
||||
textComponent.rectTransform.pivot = Vector2.one * 0.5f;
|
||||
textComponent.rectTransform.anchoredPosition = Vector2.zero;
|
||||
textComponent.rectTransform.sizeDelta = size;
|
||||
textComponent.ForceMeshUpdate();
|
||||
}
|
||||
|
||||
private string GetLongestWord(string text)
|
||||
{
|
||||
string[] words = text.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
|
||||
string longest = "";
|
||||
|
||||
foreach (string word in words)
|
||||
{
|
||||
if (word.Length > longest.Length)
|
||||
longest = word;
|
||||
}
|
||||
|
||||
return longest;
|
||||
}
|
||||
|
||||
private float GetMaxWidthForWordCount(int wordCount)
|
||||
{
|
||||
if (wordCount <= 2)
|
||||
return _shortTextMaxWidth;
|
||||
else if (wordCount <= 5)
|
||||
return _mediumTextMaxWidth;
|
||||
else if (wordCount <= 10)
|
||||
return _longTextMaxWidth;
|
||||
else
|
||||
return _veryLongTextMaxWidth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Smoothly returns the choice to its original state after losing focus.
|
||||
/// Animates position back to start, resets scale/color, and unlocks the focus system when complete.
|
||||
/// </summary>
|
||||
public void ResetFeedback(float duration)
|
||||
{
|
||||
LeanTween.cancel(gameObject);
|
||||
StopOrganicShake();
|
||||
|
||||
LeanTween.moveLocal(gameObject, _startLocalPos, duration)
|
||||
.setEase(LeanTweenType.easeOutQuad)
|
||||
.setOnComplete(() => {
|
||||
_positionCaptured = false;
|
||||
|
||||
if (DialogueManager.Instance != null)
|
||||
DialogueManager.Instance.UnlockFocus();
|
||||
|
||||
StartOrganicShake();
|
||||
});
|
||||
|
||||
transform.localScale = Vector3.one;
|
||||
|
||||
if (_choiceTextComponent != null)
|
||||
{
|
||||
_choiceTextComponent.color = Color.white;
|
||||
|
||||
if (_ghostTextComponent != null)
|
||||
{
|
||||
float ghostAlpha = GetGhostTrailAlpha();
|
||||
Color ghostColor = Color.white;
|
||||
ghostColor.a = ghostAlpha;
|
||||
_ghostTextComponent.color = ghostColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (_canvas != null)
|
||||
_canvas.sortingOrder = _originalSortingOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: abd0f63a44b2272448eb41767c2ce44e
|
||||
+367
@@ -0,0 +1,367 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using Unheimlich.Data.Dialogues;
|
||||
using Unheimlich.Gameplay.Player;
|
||||
|
||||
namespace Unheimlich.Interactions.FloatingWords
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility class for spawning floating 3D text choices in world space.
|
||||
/// Used for both dialogue choices and object descriptions. Handles positioning,
|
||||
/// rotation, animations (spawn, merge, floating) using LeanTween for smooth effects.
|
||||
/// </summary>
|
||||
public static class FloatingWordsSpawner
|
||||
{
|
||||
private const float ROTATOR_SCALE = 0.01f;
|
||||
|
||||
/// <summary>
|
||||
/// Internal configuration struct to unify settings from different sources.
|
||||
/// </summary>
|
||||
public struct SpawnConfig
|
||||
{
|
||||
public float SpawnRadius;
|
||||
public float SpawnHeightOffset;
|
||||
public RotationAxis RotationAxis;
|
||||
public bool EnableOrganicShake;
|
||||
public float ForwardOffset;
|
||||
public float TiltAngle;
|
||||
public float RotationSpeed;
|
||||
public bool EnableRotation;
|
||||
public ChoiceSpawnMode SpawnMode;
|
||||
public bool EnableFloating;
|
||||
public float MergeDuration;
|
||||
public float MergeScalePulse;
|
||||
|
||||
public bool EnableGhostTrail;
|
||||
public float GhostTrailAlpha;
|
||||
public float GhostTrailDelay;
|
||||
public float ShakeRadius;
|
||||
public float ShakeSpeed;
|
||||
public float ShakeSmoothness;
|
||||
public bool MoveTowardPlayerOnFocus;
|
||||
public float FocusDistanceFromPlayer;
|
||||
public bool DisableFloatingEffects;
|
||||
}
|
||||
|
||||
private static SpawnConfig GetConfig(ObjectDescriptionSettings settings)
|
||||
{
|
||||
return new SpawnConfig
|
||||
{
|
||||
SpawnRadius = settings.SpawnRadius,
|
||||
SpawnHeightOffset = settings.SpawnHeightOffset,
|
||||
RotationAxis = settings.RotationAxis,
|
||||
EnableOrganicShake = settings.EnableOrganicShake,
|
||||
ForwardOffset = settings.ForwardOffset,
|
||||
TiltAngle = settings.TiltAngle,
|
||||
RotationSpeed = settings.RotationSpeed,
|
||||
EnableRotation = settings.EnableRotation,
|
||||
SpawnMode = settings.SpawnMode,
|
||||
EnableFloating = settings.EnableFloating,
|
||||
MergeDuration = settings.MergeDuration,
|
||||
MergeScalePulse = settings.MergeScalePulse,
|
||||
|
||||
EnableGhostTrail = settings.EnableGhostTrail,
|
||||
GhostTrailAlpha = settings.GhostTrailAlpha,
|
||||
GhostTrailDelay = settings.GhostTrailDelay,
|
||||
ShakeRadius = settings.ShakeRadius,
|
||||
ShakeSpeed = settings.ShakeSpeed,
|
||||
ShakeSmoothness = settings.ShakeSmoothness,
|
||||
MoveTowardPlayerOnFocus = false,
|
||||
FocusDistanceFromPlayer = 1f,
|
||||
DisableFloatingEffects = true
|
||||
};
|
||||
}
|
||||
|
||||
private static SpawnConfig GetConfig(DialogueAnimationSettings settings)
|
||||
{
|
||||
return new SpawnConfig
|
||||
{
|
||||
SpawnRadius = settings.SpawnRadius,
|
||||
SpawnHeightOffset = settings.SpawnHeightOffset,
|
||||
RotationAxis = settings.RotationAxis,
|
||||
EnableOrganicShake = settings.EnableOrganicShake,
|
||||
ForwardOffset = 0f,
|
||||
TiltAngle = 0f,
|
||||
RotationSpeed = settings.NormalRotationSpeed,
|
||||
EnableRotation = settings.EnableRotation,
|
||||
SpawnMode = settings.SpawnMode,
|
||||
EnableFloating = settings.EnableFloating,
|
||||
MergeDuration = 0.3f,
|
||||
MergeScalePulse = 1.2f,
|
||||
|
||||
EnableGhostTrail = settings.EnableGhostTrail,
|
||||
GhostTrailAlpha = settings.GhostTrailAlpha,
|
||||
GhostTrailDelay = settings.GhostTrailDelay,
|
||||
ShakeRadius = settings.ShakeRadius,
|
||||
ShakeSpeed = settings.ShakeSpeed,
|
||||
ShakeSmoothness = settings.ShakeSmoothness,
|
||||
MoveTowardPlayerOnFocus = settings.MoveTowardPlayerOnFocus,
|
||||
FocusDistanceFromPlayer = settings.FocusDistanceFromPlayer,
|
||||
DisableFloatingEffects = false
|
||||
};
|
||||
}
|
||||
|
||||
public static List<ChoiceFocusHandler> SpawnByMode(List<DialogueChoiceData> nodeChoices, ChoiceFocusHandler prefab, Transform container, DialogueAnimationSettings settings, out Vector3[] originalPositions)
|
||||
{
|
||||
return SpawnByMode(nodeChoices, prefab, container, GetConfig(settings), out originalPositions);
|
||||
}
|
||||
|
||||
public static List<ChoiceFocusHandler> SpawnByMode(List<DialogueChoiceData> choices, ChoiceFocusHandler prefab, Transform container, ObjectDescriptionSettings settings, out Vector3[] originalPositions)
|
||||
{
|
||||
return SpawnByMode(choices, prefab, container, GetConfig(settings), out originalPositions);
|
||||
}
|
||||
|
||||
private static List<ChoiceFocusHandler> SpawnByMode(List<DialogueChoiceData> nodeChoices, ChoiceFocusHandler prefab, Transform container, SpawnConfig config, out Vector3[] originalPositions)
|
||||
{
|
||||
switch (config.SpawnMode)
|
||||
{
|
||||
case ChoiceSpawnMode.AroundAnchor:
|
||||
return SpawnWordsInCircle(nodeChoices, prefab, container, config, out originalPositions);
|
||||
case ChoiceSpawnMode.AtAnchorPosition:
|
||||
originalPositions = new Vector3[0];
|
||||
return SpawnAtAnchor(nodeChoices, prefab, container, config);
|
||||
case ChoiceSpawnMode.InFrontOfPlayer:
|
||||
originalPositions = new Vector3[0];
|
||||
return SpawnInFrontOfPlayer(nodeChoices, prefab, container, config);
|
||||
case ChoiceSpawnMode.CircleAroundPlayer:
|
||||
return SpawnAroundPlayer(nodeChoices, prefab, container, config, out originalPositions);
|
||||
default:
|
||||
return SpawnWordsInCircle(nodeChoices, prefab, container, config, out originalPositions);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<ChoiceFocusHandler> SpawnWordsInCircle(List<DialogueChoiceData> choices, ChoiceFocusHandler wordPrefab, Transform parent, SpawnConfig config, out Vector3[] originalPositions)
|
||||
{
|
||||
List<ChoiceFocusHandler> words = new List<ChoiceFocusHandler>();
|
||||
originalPositions = new Vector3[choices.Count];
|
||||
float angleStep = 360f / choices.Count;
|
||||
|
||||
for (int i = 0; i < choices.Count; i++)
|
||||
{
|
||||
float angle = i * angleStep * Mathf.Deg2Rad;
|
||||
Vector3 localOffset = CalculateCircularPosition(angle, config.SpawnRadius, config.RotationAxis, config.SpawnHeightOffset);
|
||||
originalPositions[i] = localOffset;
|
||||
|
||||
ChoiceFocusHandler word = Object.Instantiate(wordPrefab, parent);
|
||||
word.transform.localPosition = localOffset;
|
||||
word.transform.localRotation = Quaternion.identity;
|
||||
|
||||
CanvasGroup canvasGroup = word.GetComponent<CanvasGroup>();
|
||||
if (canvasGroup == null)
|
||||
canvasGroup = word.gameObject.AddComponent<CanvasGroup>();
|
||||
|
||||
canvasGroup.alpha = 1f;
|
||||
|
||||
word.Setup(choices[i], config, config.DisableFloatingEffects);
|
||||
|
||||
if (config.EnableOrganicShake) word.StartOrganicShake();
|
||||
|
||||
words.Add(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
private static List<ChoiceFocusHandler> SpawnAtAnchor(List<DialogueChoiceData> choices, ChoiceFocusHandler prefab, Transform parent, SpawnConfig config)
|
||||
{
|
||||
List<ChoiceFocusHandler> words = new List<ChoiceFocusHandler>();
|
||||
for (int i = 0; i < choices.Count; i++)
|
||||
{
|
||||
ChoiceFocusHandler word = Object.Instantiate(prefab, parent);
|
||||
word.transform.localPosition = Vector3.up * i * 50f;
|
||||
word.transform.localRotation = Quaternion.identity;
|
||||
word.Setup(choices[i], config);
|
||||
words.Add(word);
|
||||
}
|
||||
return words;
|
||||
}
|
||||
|
||||
private static List<ChoiceFocusHandler> SpawnInFrontOfPlayer(List<DialogueChoiceData> choices, ChoiceFocusHandler prefab, Transform parent, SpawnConfig config)
|
||||
{
|
||||
List<ChoiceFocusHandler> words = new List<ChoiceFocusHandler>();
|
||||
if (Player.Instance == null) return words;
|
||||
|
||||
Vector3 playerForward = Player.Instance.transform.forward;
|
||||
for (int i = 0; i < choices.Count; i++)
|
||||
{
|
||||
ChoiceFocusHandler word = Object.Instantiate(prefab, parent);
|
||||
Vector3 offset = playerForward * 2f + Vector3.up * (i - choices.Count / 2f) * (config.SpawnRadius / choices.Count / 100f);
|
||||
word.transform.position = Player.Instance.transform.position + offset;
|
||||
word.transform.LookAt(Player.Instance.transform);
|
||||
word.Setup(choices[i], config);
|
||||
words.Add(word);
|
||||
}
|
||||
return words;
|
||||
}
|
||||
|
||||
private static List<ChoiceFocusHandler> SpawnAroundPlayer(List<DialogueChoiceData> choices, ChoiceFocusHandler prefab, Transform parent, SpawnConfig config, out Vector3[] originalPositions)
|
||||
{
|
||||
List<ChoiceFocusHandler> words = new List<ChoiceFocusHandler>();
|
||||
originalPositions = new Vector3[choices.Count];
|
||||
if (Player.Instance == null) return words;
|
||||
|
||||
parent.position = Player.Instance.transform.position;
|
||||
parent.rotation = Quaternion.identity;
|
||||
|
||||
float angleStep = 360f / choices.Count;
|
||||
for (int i = 0; i < choices.Count; i++)
|
||||
{
|
||||
float angle = i * angleStep * Mathf.Deg2Rad;
|
||||
Vector3 localOffset = new Vector3(Mathf.Cos(angle) * config.SpawnRadius, config.SpawnHeightOffset, Mathf.Sin(angle) * config.SpawnRadius);
|
||||
originalPositions[i] = localOffset;
|
||||
|
||||
ChoiceFocusHandler word = Object.Instantiate(prefab, parent);
|
||||
word.transform.localPosition = localOffset;
|
||||
word.transform.LookAt(Player.Instance.transform);
|
||||
word.Setup(choices[i], config);
|
||||
words.Add(word);
|
||||
}
|
||||
return words;
|
||||
}
|
||||
|
||||
public static void MergeWords(List<ChoiceFocusHandler> words, ObjectDescriptionSettings settings, System.Action onMergeComplete = null)
|
||||
{
|
||||
MergeWords(words, GetConfig(settings), onMergeComplete);
|
||||
}
|
||||
|
||||
private static void MergeWords(List<ChoiceFocusHandler> words, SpawnConfig config, System.Action onMergeComplete)
|
||||
{
|
||||
for (int i = 0; i < words.Count; i++)
|
||||
{
|
||||
if (words[i] != null)
|
||||
{
|
||||
words[i].StopOrganicShake();
|
||||
words[i].HideGhostTrail();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < words.Count; i++)
|
||||
{
|
||||
if (words[i] != null)
|
||||
{
|
||||
CanvasGroup canvasGroup = words[i].GetComponent<CanvasGroup>();
|
||||
if (canvasGroup != null)
|
||||
{
|
||||
LeanTween.value(words[i].gameObject, 1f, 0f, config.MergeDuration)
|
||||
.setOnUpdate((float val) => { canvasGroup.alpha = val; })
|
||||
.setEaseInQuad();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (words.Count > 0 && words[0] != null)
|
||||
{
|
||||
LeanTween.scale(words[0].gameObject, Vector3.one * config.MergeScalePulse, config.MergeDuration / 2f)
|
||||
.setEaseOutBack()
|
||||
.setOnComplete(() =>
|
||||
{
|
||||
if (words.Count > 0 && words[0] != null)
|
||||
{
|
||||
LeanTween.scale(words[0].gameObject, Vector3.one, config.MergeDuration / 2f)
|
||||
.setEaseInBack()
|
||||
.setOnComplete(() => onMergeComplete?.Invoke());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnmergeWords(List<ChoiceFocusHandler> words, ObjectDescriptionSettings settings)
|
||||
{
|
||||
UnmergeWords(words, GetConfig(settings));
|
||||
}
|
||||
|
||||
private static void UnmergeWords(List<ChoiceFocusHandler> words, SpawnConfig config)
|
||||
{
|
||||
for (int i = 0; i < words.Count; i++)
|
||||
{
|
||||
if (words[i] != null)
|
||||
{
|
||||
words[i].StartOrganicShake();
|
||||
words[i].ShowGhostTrail();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < words.Count; i++)
|
||||
{
|
||||
if (words[i] != null)
|
||||
{
|
||||
LeanTween.cancel(words[i].gameObject);
|
||||
CanvasGroup canvasGroup = words[i].GetComponent<CanvasGroup>();
|
||||
if (canvasGroup != null)
|
||||
{
|
||||
LeanTween.value(words[i].gameObject, canvasGroup.alpha, 1f, config.MergeDuration)
|
||||
.setOnUpdate((float val) => { canvasGroup.alpha = val; })
|
||||
.setEaseOutQuad();
|
||||
}
|
||||
words[i].transform.localScale = Vector3.one;
|
||||
}
|
||||
}
|
||||
|
||||
if (words.Count > 0 && words[0] != null)
|
||||
{
|
||||
LeanTween.cancel(words[0].gameObject);
|
||||
words[0].transform.localScale = Vector3.one;
|
||||
}
|
||||
}
|
||||
|
||||
#region For obj description
|
||||
public static GameObject CreateWordsContainer(string containerName, Transform anchor, ObjectDescriptionSettings settings)
|
||||
{
|
||||
SpawnConfig config = GetConfig(settings);
|
||||
GameObject rotator = new GameObject($"{containerName}_Rotator");
|
||||
rotator.transform.localPosition = Vector3.forward * config.ForwardOffset;
|
||||
rotator.transform.localRotation = Quaternion.Euler(config.TiltAngle, 0f, 0f);
|
||||
rotator.transform.localScale = Vector3.one * ROTATOR_SCALE;
|
||||
|
||||
Canvas containerCanvas = rotator.AddComponent<Canvas>();
|
||||
containerCanvas.renderMode = RenderMode.WorldSpace;
|
||||
|
||||
return rotator;
|
||||
}
|
||||
|
||||
|
||||
public static void UpdateContainerRotation(GameObject container, GameObject rotator, Transform playerTransform, ObjectDescriptionSettings settings)
|
||||
{
|
||||
if (container == null || playerTransform == null) return;
|
||||
|
||||
Vector3 lookAtPosition = playerTransform.position;
|
||||
lookAtPosition.y = container.transform.position.y;
|
||||
container.transform.LookAt(lookAtPosition);
|
||||
|
||||
if (!settings.EnableRotation || rotator == null) return;
|
||||
|
||||
Vector3 rotationAxis = GetRotationAxisVector(settings.RotationAxis);
|
||||
float rotationAmount = (360f / settings.RotationSpeed) * Time.deltaTime;
|
||||
rotator.transform.Rotate(rotationAxis, rotationAmount, Space.Self);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Tools
|
||||
private static Vector3 CalculateCircularPosition(float angleRad, float radius, RotationAxis axis, float heightOffset)
|
||||
{
|
||||
switch (axis)
|
||||
{
|
||||
case RotationAxis.Forward:
|
||||
return new Vector3(Mathf.Cos(angleRad) * radius, Mathf.Sin(angleRad) * radius + heightOffset, 0f);
|
||||
case RotationAxis.Up:
|
||||
return new Vector3(Mathf.Cos(angleRad) * radius, heightOffset, Mathf.Sin(angleRad) * radius);
|
||||
case RotationAxis.Right:
|
||||
return new Vector3(0f, Mathf.Cos(angleRad) * radius + heightOffset, Mathf.Sin(angleRad) * radius);
|
||||
default:
|
||||
return new Vector3(Mathf.Cos(angleRad) * radius, heightOffset, Mathf.Sin(angleRad) * radius);
|
||||
}
|
||||
}
|
||||
|
||||
private static Vector3 GetRotationAxisVector(RotationAxis axis)
|
||||
{
|
||||
switch (axis)
|
||||
{
|
||||
case RotationAxis.Forward: return Vector3.forward;
|
||||
case RotationAxis.Up: return Vector3.up;
|
||||
case RotationAxis.Right: return Vector3.right;
|
||||
default: return Vector3.up;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b52b115a50463d46b645f5579cf22fd
|
||||
Reference in New Issue
Block a user