Инкапсулированы методы появления и ухода персонажа Инкапсулирована работа с аниматором Работа с репликами вынесена в ScriptableObject Логика MessageBox вынесена в отдельный класс Переработан event bus персонажей Сложность и призовой модуль вынесены в отдельные классы Character.cs приведён к Single Responsibility Principle.
129 lines
3.7 KiB
C#
129 lines
3.7 KiB
C#
using DG.Tweening;
|
||
using PrizeSystem;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace CharacterSystem
|
||
{
|
||
public class CharactersController : MonoBehaviour
|
||
{
|
||
public static CharactersController I;
|
||
|
||
[SerializeField] private List<Character> characterPrefabs;
|
||
[SerializeField] float shownX = 0, hiddenX = 2000;
|
||
|
||
[SerializeField] private float speed;
|
||
|
||
private Character[] characters;
|
||
private int currentIndex = 0;
|
||
|
||
public static int CharactersAmount => I.characters.Length;
|
||
public static Character CurrentCharacter => I.characters[I.currentIndex];
|
||
|
||
public void Initialize()
|
||
{
|
||
I = this;
|
||
SetupCharacters();
|
||
GameManager.OnGameStarted += Enable;
|
||
GameManager.OnGameFinished += Disable;
|
||
}
|
||
|
||
public void Enable(GameMode gameMode)
|
||
{
|
||
if (gameMode == GameMode.pve)
|
||
print("ВКЛЮЧЕНИЕ СИСТЕМЫ ПЕРСОНАЖЕЙ");
|
||
}
|
||
public void Disable(GameMode gameMode)
|
||
{
|
||
if (gameMode == GameMode.pve)
|
||
print("ВЫКЛЮЧЕНИЕ СИСТЕМЫ ПЕРСОНАЖЕЙ");
|
||
}
|
||
|
||
public void SetAllCharactersPrizeAvailable()
|
||
{
|
||
foreach (Character character in I.characters)
|
||
character.prizeModule.ResetModule();
|
||
}
|
||
|
||
public static void ResetAllCharactersDifficulty()
|
||
{
|
||
foreach (Character character in I.characters)
|
||
character.difficultyModule.ResetPresetIndex();
|
||
}
|
||
|
||
public void HideCharacter()
|
||
{
|
||
//CurrentCharacter?.UnbindEvents();
|
||
}
|
||
|
||
public static void SwitchToCharacter(int index)
|
||
{
|
||
I.ShowCharacter(index);
|
||
}
|
||
|
||
public static void ResetState()
|
||
{
|
||
I.StopAllCoroutines();
|
||
I.currentIndex = -1;
|
||
}
|
||
|
||
public static Character[] GetCharactersArray() => new List<Character>(I.characters).ToArray();
|
||
|
||
private void SetupCharacters()
|
||
{
|
||
if (characterPrefabs.Count <= 0)
|
||
throw new System.Exception("Не заданы боты в контроллере");
|
||
|
||
int amount = characterPrefabs.Count;
|
||
characters = new Character[amount];
|
||
|
||
for (int i = 0; i < amount; i++)
|
||
{
|
||
var character = Instantiate(characterPrefabs[i]/*, charactersPool*/);
|
||
characters[i] = character;
|
||
character.Initialize(i);
|
||
}
|
||
|
||
RefreshPrizes();
|
||
}
|
||
|
||
public void RefreshPrizes()
|
||
{
|
||
List<Prize> prizes = PrizeSerializer.LoadPrizes();
|
||
|
||
for (int i = 0; i < characters.Length; i++)
|
||
characters[i].prizeModule.SetPrize(prizes[i]);
|
||
}
|
||
|
||
private void ShowCharacter(int newIndex)
|
||
{
|
||
int oldIndex = currentIndex;
|
||
currentIndex = newIndex;
|
||
|
||
if (newIndex == oldIndex)
|
||
return;
|
||
|
||
var oldCharacter = characters[oldIndex];
|
||
var newCharacter = characters[newIndex];
|
||
|
||
Sequence seq = DOTween.Sequence();
|
||
|
||
|
||
|
||
seq.Append(oldCharacter.visualModule.GetHideSequence());
|
||
|
||
seq.AppendInterval(0.1f);
|
||
|
||
//вывод нового персонажа
|
||
seq.Append(newCharacter.visualModule.GetShowSequence());
|
||
|
||
seq.AppendCallback(() =>
|
||
{
|
||
if (newIndex < oldIndex)
|
||
CharacterEventBus.TriggerEvent(GameEvent.Return);
|
||
else
|
||
CharacterEventBus.TriggerEvent(GameEvent.Appear);
|
||
});
|
||
}
|
||
}
|
||
} |