Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using Entity;
- using Entity.Stats;
- using Game.Scripts.Battle.UI;
- using Interactors.Realization;
- using Model;
- using Models;
- using UIService;
- using UniRx;
- using User;
- namespace Battle.UI
- {
- public struct SpawnMessage
- {
- public string SpawnType { get; }
- public int Count { get; }
- public SpawnMessage(string spawnType, int count)
- {
- SpawnType = spawnType;
- Count = count;
- }
- }
- public class KillCounter
- {
- public Dictionary<string, int> Counter = new();
- }
- public struct KillEnemyMessage
- {
- public string Id;
- }
- public struct WinMessage
- {
- }
- public struct LooseMessage
- {
- }
- public class BattleScreenInteractor : Interactor
- {
- private readonly UIBattleUnitButtonCellView.Factory _unitCellFactory;
- private readonly EntityStorage _entityStorage;
- private readonly IUIService _uiService;
- private readonly ModelStorage _modelStorage;
- private readonly List<UIBattleUnitButtonCellViewProtocol> _cellViewProtocolList = new();
- private BattlePlayerEntity _battlePlayerEntity;
- public BattleScreenInteractor(
- UIBattleUnitButtonCellView.Factory unitCellFactory,
- EntityStorage entityStorage,
- ModelStorage modelStorage,
- IUIService uiService)
- {
- _unitCellFactory = unitCellFactory;
- _entityStorage = entityStorage;
- _uiService = uiService;
- _modelStorage = modelStorage;
- }
- public override void Init()
- {
- base.Init();
- _battlePlayerEntity = _entityStorage.GetSingleByType<BattlePlayerEntity>();
- _battlePlayerEntity.Stats.Set("can_win/lose", true);
- var battleWindow = _uiService.Show<UIBattleWindow>();
- battleWindow.PauseButtonClick.Subscribe(_ => AddClosableInteractor<PauseScreenInteractor>().Init()).AddTo(CompositeDisposable);
- battleWindow.ShowCancelButton(false);
- InitLoadOutCells();
- var userEntity = _entityStorage.GetSingleByType<UserEntity>();
- var levelProgress = userEntity.Stats.Get<UserLevelProgressData>("map_level_progress").Value;
- var levelModel = _modelStorage.GetItem<MapLevelModel>(levelProgress.CurrentLevel);
- battleWindow.SetMapLevelInfo(levelProgress.CurrentSubLevel, levelModel.SubLevelCount);
- battleWindow.SetActiveSubBody(userEntity.Stats.Get<bool>("is_tutorial_complete").Value || userEntity.Stats.Get<float>("tutorial_scenario").Value >= 3);
- battleWindow.SetActiveCancelSubBody(userEntity.Stats.Get<bool>("is_tutorial_complete").Value || userEntity.Stats.Get<float>("tutorial_scenario").Value >= 4);
- AddInteractor<ActiveSkillsPanelInteractor>().Init();
- AddInteractor<BattleAdsInteractor>().Init();
- AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("energy"), OnEnergyStatChange);
- AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("unit_count"), OnUnitCountStatChange);
- AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("total_unit_count"), OnUnitTotalCountStatChange);
- AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("round_time"), OnRoundTimeStatChange);
- MessageBroker.Default.Receive<KillEnemyMessage>().Subscribe(message =>
- {
- var energyStat = _battlePlayerEntity.Stats.Get<float>("energy");
- var energyMax = _battlePlayerEntity.Stats.Get<float>("energy_max").Value;
- var energyRefillAmount = _battlePlayerEntity.Stats.Get<float>("energy_refill_by_kill").Value;
- if (energyStat.Value + energyRefillAmount > energyMax)
- {
- energyStat.Set(energyMax);
- }
- else
- {
- energyStat.Add(20);
- }
- var killCounter = _battlePlayerEntity.Stats.Get<KillCounter>("kill_counter").Value ?? new KillCounter();
- if (!killCounter.Counter.ContainsKey(message.Id))
- {
- killCounter.Counter.Add(message.Id, 0);
- }
- killCounter.Counter[message.Id]++;
- _battlePlayerEntity.Stats.Set<KillCounter>("kill_counter", killCounter);
- }).AddTo(CompositeDisposable);
- var winLoseStat = _battlePlayerEntity.Stats.Get<bool>("can_win/lose");
- MessageBroker.Default.Receive<WinMessage>().Subscribe(_ =>
- {
- if (!winLoseStat.Value)
- return;
- winLoseStat.Set(false);
- AddInteractor<WinScreenInteractor>().Init();
- }).AddTo(CompositeDisposable);
- MessageBroker.Default.Receive<LooseMessage>().Subscribe(_ =>
- {
- if (!winLoseStat.Value)
- return;
- winLoseStat.Set(false);
- AddInteractor<DefeatScreenInteractor>().Init();
- }).AddTo(CompositeDisposable);
- }
- private void InitLoadOutCells()
- {
- var battleWindow = _uiService.Get<UIBattleWindow>();
- var lineUpData = _entityStorage.GetSingleByType<LineUpDataEntity>();
- var maxUnitCount = 4; // todo: get from config
- for (var i = 0; i < maxUnitCount; i++)
- {
- var key = i.ToString();
- var lineUp = lineUpData.Stats.Get<LineUpData>(key).Value;
- if (!lineUp.IsEmpty)
- {
- var cm = _modelStorage.GetItem<UnitConvertModel>(lineUp.SpawnId);
- var viewModel = _modelStorage.GetItem<UnitViewModel>(cm.ConvertedId);
- var lvlModel = _modelStorage.GetItem<UnitModel>(cm.ConvertedId);
- var unitUpgrade = lvlModel.Upgrade[0];
- var cooldownEndCommand = new ReactiveCommand();
- var protocol = new UIBattleUnitButtonCellViewProtocol
- {
- Count = (int)unitUpgrade.Count,
- Name = viewModel.NameKey,
- UnitType = viewModel.SpawnType,
- EnergyCostValue = unitUpgrade.Energy,
- Icon = viewModel.GetUnitIcon(),
- CurrentEnergy = new(),
- CurrentUnitCount = new()
- };
- cooldownEndCommand.Subscribe(_ =>
- {
- RemoveInteractor<BattleSpawnCooldownInteractor>();
- protocol.Cooldown.SetValueAndForceNotify(0);
- OnEnergyStatChange(_battlePlayerEntity.Stats.Get<float>("energy"), default);
- }).AddTo(CompositeDisposable);
- protocol.ClickCommand.Subscribe(_ =>
- {
- var currentCount = _battlePlayerEntity.Stats.Get<float>("unit_count").Value;
- var maxCount = _battlePlayerEntity.Stats.Get<float>("total_unit_count").Value;
- if (currentCount >= maxCount)
- return;
- var energyStat = _battlePlayerEntity.Stats.Get<float>("energy");
- if (energyStat.Value < protocol.EnergyCostValue)
- return;
- var count = (int)lvlModel.Upgrade[0].Count;
- if (currentCount + count > maxCount)
- return;
- energyStat.Add(-protocol.EnergyCostValue);
- MessageBroker.Default.Publish(new SpawnMessage(protocol.UnitType, count));
- // todo: get from config?
- var cooldownTime = 3f;
- protocol.Cooldown.SetValueAndForceNotify(cooldownTime);
- AddInteractor<BattleSpawnCooldownInteractor>(
- protocol.Cooldown,
- cooldownEndCommand,
- cooldownTime).Init();
- OnEnergyStatChange(_battlePlayerEntity.Stats.Get<float>("energy"), default);
- }).AddTo(CompositeDisposable);
- _cellViewProtocolList.Add(protocol);
- var cell = _unitCellFactory.Create(protocol);
- battleWindow.SetLoadOutCellView(cell);
- }
- }
- }
- private void OnRoundTimeStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
- {
- var battleWindow = _uiService.Get<UIBattleWindow>();
- battleWindow.SetTime(stat.Value);
- }
- private void OnUnitCountStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
- {
- var battleWindow = _uiService.Get<UIBattleWindow>();
- var maxValue = _battlePlayerEntity.Stats.Get<float>("total_unit_count").Value;
- battleWindow.SetUnitBarValue(stat.Value, maxValue);
- foreach (var cellViewProtocol in _cellViewProtocolList)
- {
- cellViewProtocol.CurrentUnitCount.SetValueAndForceNotify((stat.Value, maxValue));
- }
- }
- private void OnUnitTotalCountStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
- {
- var battleWindow = _uiService.Get<UIBattleWindow>();
- var value = _battlePlayerEntity.Stats.Get<float>("unit_count").Value;
- battleWindow.SetUnitBarValue(value, stat.Value);
- }
- private void OnEnergyStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
- {
- var battleWindow = _uiService.Get<UIBattleWindow>();
- var maxValue = _battlePlayerEntity.Stats.Get<float>("energy_max").Value;
- battleWindow.SetEnergyBarValue(stat.Value, maxValue);
- foreach (var cellViewProtocol in _cellViewProtocolList)
- {
- cellViewProtocol.CurrentEnergy.SetValueAndForceNotify(stat.Value);
- }
- }
- public override void Dispose()
- {
- base.Dispose();
- _cellViewProtocolList.Clear();
- var window = _uiService.Hide<UIBattleWindow>();
- window.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement