138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace CharacterSystem
|
|
{
|
|
public class VisualModule : MonoBehaviour
|
|
{
|
|
public Sprite icon;
|
|
public Sprite iconSmall;
|
|
public Sprite chipImage;
|
|
public AnimationClip chipSpawningAnim;
|
|
public AudioClip chipThrowClip;
|
|
|
|
[SerializeField] private MessageBox _messageBox;
|
|
[SerializeField, FormerlySerializedAs("replicas")] private ReplicasContainer _replicas;
|
|
[SerializeField] private AudioSource _audioSource;
|
|
[SerializeField] private Animator _animator;
|
|
[SerializeField] private Color _innerColor = Color.white;
|
|
[SerializeField, FormerlySerializedAs("_outerColor")] private Color _outlineColor = Color.white;
|
|
[SerializeField] private Color _cellsColor = Color.white;
|
|
|
|
[Header("Анимация появление/убирание")]
|
|
[SerializeField, FormerlySerializedAs("showDuration")] private float _showDuration = 1f;
|
|
[SerializeField, FormerlySerializedAs("hideDuration")] private float _hideDuration = 1f;
|
|
[SerializeField, FormerlySerializedAs("showEase")] private Ease _showEase = Ease.Linear;
|
|
[SerializeField, FormerlySerializedAs("hideEase")] private Ease _hideEase = Ease.Linear;
|
|
[SerializeField, FormerlySerializedAs("HiddenPos")] private Vector2 _hiddenPos;
|
|
[SerializeField, FormerlySerializedAs("VisiblePos")] private Vector2 _visiblePos;
|
|
|
|
public Color InnerColor => _innerColor;
|
|
public Color OutlineColor => _outlineColor;
|
|
public Color CellsColor => _cellsColor;
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SettingsManager.OnSettingsChanged -= PullSettings;
|
|
EventBus.OnGameEvent -= OnGameEvent;
|
|
}
|
|
|
|
public void Initialize(string characterName)
|
|
{
|
|
SettingsManager.OnSettingsChanged += PullSettings;
|
|
_audioSource = GetComponent<AudioSource>();
|
|
PullSettings();
|
|
|
|
if (_replicas != null)
|
|
_replicas.Initialize();
|
|
|
|
transform.position = _hiddenPos;
|
|
_animator.SetTrigger("PreAppear");
|
|
_animator.enabled = false;
|
|
|
|
if (_messageBox != null)
|
|
_messageBox.Initialize(characterName);
|
|
}
|
|
|
|
public Sequence GetShowSequence()
|
|
{
|
|
_animator.enabled = true;
|
|
_animator.SetTrigger("PreAppear");
|
|
EventBus.OnGameEvent -= OnGameEvent;
|
|
EventBus.OnGameEvent += OnGameEvent;
|
|
|
|
Sequence sequence = DOTween.Sequence();
|
|
sequence.Append(transform.DOMove(_visiblePos, _showDuration).SetEase(_showEase));
|
|
|
|
if (Board.Instance != null)
|
|
sequence.Join(Board.Instance.GetColorSequence(_innerColor, _outlineColor, _cellsColor, _showDuration));
|
|
|
|
return sequence;
|
|
}
|
|
|
|
public Sequence GetHideSequence()
|
|
{
|
|
EventBus.OnGameEvent -= OnGameEvent;
|
|
_messageBox?.Stop();
|
|
|
|
Sequence sequence = DOTween.Sequence();
|
|
sequence.Append(transform.DOMove(_hiddenPos, _hideDuration).SetEase(_hideEase));
|
|
sequence.AppendCallback(() =>
|
|
{
|
|
_animator.SetTrigger("PreAppear");
|
|
_animator.enabled = false;
|
|
});
|
|
return sequence;
|
|
}
|
|
|
|
public void OnGameEvent(GameEvent gameEvent)
|
|
{
|
|
bool forceMessage = ReplicasContainer.ShouldForceMessage[gameEvent];
|
|
bool shouldShowName = gameEvent == GameEvent.Appear;
|
|
|
|
Message(gameEvent, forceMessage, shouldShowName);
|
|
|
|
if (gameEvent == GameEvent.Appear || gameEvent == GameEvent.Return)
|
|
PlayAppearAnimation();
|
|
|
|
if (gameEvent == GameEvent.PlayerWin)
|
|
_animator.SetTrigger("Lose");
|
|
}
|
|
|
|
public void InvokeThrowChip()
|
|
=> SoundManager.instance.PlaySfx(chipThrowClip);
|
|
|
|
public void ResetModule()
|
|
{
|
|
transform.position = _hiddenPos;
|
|
_animator.SetTrigger("PreAppear");
|
|
_animator.enabled = false;
|
|
_audioSource.Stop();
|
|
_messageBox?.Stop();
|
|
}
|
|
|
|
private void PullSettings()
|
|
{
|
|
if (_audioSource != null)
|
|
_audioSource.volume = SettingsManager.GetFloat(SettingKey.SOUNDS_VOLUME);
|
|
}
|
|
|
|
private void PlayAppearAnimation()
|
|
{
|
|
_animator.ResetTrigger("Appear");
|
|
_animator.SetTrigger("Sleep");
|
|
_animator.SetTrigger("Appear");
|
|
}
|
|
|
|
private void Message(GameEvent replicaType, bool force, bool showName)
|
|
{
|
|
if (_replicas == null || _messageBox == null || _replicas.WillPlay(replicaType) == false)
|
|
return;
|
|
|
|
Replica replica = _replicas.GetNextReplica(replicaType);
|
|
_messageBox.PlayReplica(replica, force, showName);
|
|
}
|
|
}
|
|
}
|