Advertisement
BorisKotlyar

Untitled

May 15th, 2024
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.55 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using Entity;
  3. using Entity.Stats;
  4. using Game.Scripts.Battle.UI;
  5. using Interactors.Realization;
  6. using Model;
  7. using Models;
  8. using UIService;
  9. using UniRx;
  10. using User;
  11.  
  12. namespace Battle.UI
  13. {
  14.     public struct SpawnMessage
  15.     {
  16.         public string SpawnType { get; }
  17.         public int Count { get; }
  18.  
  19.         public SpawnMessage(string spawnType, int count)
  20.         {
  21.             SpawnType = spawnType;
  22.             Count = count;
  23.         }
  24.     }
  25.  
  26.     public class KillCounter
  27.     {
  28.         public Dictionary<string, int> Counter = new();
  29.     }
  30.  
  31.     public struct KillEnemyMessage
  32.     {
  33.         public string Id;
  34.     }
  35.     public struct WinMessage
  36.     {
  37.     }
  38.     public struct LooseMessage
  39.     {
  40.     }
  41.  
  42.  
  43.     public class BattleScreenInteractor : Interactor
  44.     {
  45.         private readonly UIBattleUnitButtonCellView.Factory _unitCellFactory;
  46.         private readonly EntityStorage _entityStorage;
  47.         private readonly IUIService _uiService;
  48.         private readonly ModelStorage _modelStorage;
  49.  
  50.         private readonly List<UIBattleUnitButtonCellViewProtocol> _cellViewProtocolList = new();
  51.         private BattlePlayerEntity _battlePlayerEntity;
  52.  
  53.         public BattleScreenInteractor(
  54.             UIBattleUnitButtonCellView.Factory unitCellFactory,
  55.             EntityStorage entityStorage,
  56.             ModelStorage modelStorage,
  57.             IUIService uiService)
  58.         {
  59.             _unitCellFactory = unitCellFactory;
  60.             _entityStorage = entityStorage;
  61.             _uiService = uiService;
  62.             _modelStorage = modelStorage;
  63.         }
  64.  
  65.         public override void Init()
  66.         {
  67.             base.Init();
  68.  
  69.             _battlePlayerEntity = _entityStorage.GetSingleByType<BattlePlayerEntity>();
  70.             _battlePlayerEntity.Stats.Set("can_win/lose", true);
  71.  
  72.             var battleWindow = _uiService.Show<UIBattleWindow>();
  73.             battleWindow.PauseButtonClick.Subscribe(_ => AddClosableInteractor<PauseScreenInteractor>().Init()).AddTo(CompositeDisposable);
  74.             battleWindow.ShowCancelButton(false);
  75.                
  76.             InitLoadOutCells();
  77.  
  78.             var userEntity = _entityStorage.GetSingleByType<UserEntity>();
  79.             var levelProgress = userEntity.Stats.Get<UserLevelProgressData>("map_level_progress").Value;
  80.             var levelModel = _modelStorage.GetItem<MapLevelModel>(levelProgress.CurrentLevel);
  81.             battleWindow.SetMapLevelInfo(levelProgress.CurrentSubLevel, levelModel.SubLevelCount);
  82.             battleWindow.SetActiveSubBody(userEntity.Stats.Get<bool>("is_tutorial_complete").Value || userEntity.Stats.Get<float>("tutorial_scenario").Value >= 3);
  83.             battleWindow.SetActiveCancelSubBody(userEntity.Stats.Get<bool>("is_tutorial_complete").Value || userEntity.Stats.Get<float>("tutorial_scenario").Value >= 4);
  84.  
  85.             AddInteractor<ActiveSkillsPanelInteractor>().Init();
  86.             AddInteractor<BattleAdsInteractor>().Init();
  87.             AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("energy"), OnEnergyStatChange);
  88.             AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("unit_count"), OnUnitCountStatChange);
  89.             AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("total_unit_count"), OnUnitTotalCountStatChange);
  90.             AddInteractor<StatInteractor>().Init(_battlePlayerEntity.Stats.Get<float>("round_time"), OnRoundTimeStatChange);
  91.  
  92.             MessageBroker.Default.Receive<KillEnemyMessage>().Subscribe(message =>
  93.             {
  94.                 var energyStat = _battlePlayerEntity.Stats.Get<float>("energy");
  95.                 var energyMax = _battlePlayerEntity.Stats.Get<float>("energy_max").Value;
  96.                 var energyRefillAmount = _battlePlayerEntity.Stats.Get<float>("energy_refill_by_kill").Value;
  97.                
  98.                 if (energyStat.Value + energyRefillAmount > energyMax)
  99.                 {
  100.                     energyStat.Set(energyMax);
  101.                 }
  102.                 else
  103.                 {
  104.                     energyStat.Add(20);
  105.                 }
  106.  
  107.                 var killCounter = _battlePlayerEntity.Stats.Get<KillCounter>("kill_counter").Value ?? new KillCounter();
  108.                 if (!killCounter.Counter.ContainsKey(message.Id))
  109.                 {
  110.                     killCounter.Counter.Add(message.Id, 0);
  111.                 }
  112.  
  113.                 killCounter.Counter[message.Id]++;
  114.                 _battlePlayerEntity.Stats.Set<KillCounter>("kill_counter", killCounter);
  115.             }).AddTo(CompositeDisposable);
  116.  
  117.             var winLoseStat = _battlePlayerEntity.Stats.Get<bool>("can_win/lose");
  118.             MessageBroker.Default.Receive<WinMessage>().Subscribe(_ =>
  119.             {
  120.                 if (!winLoseStat.Value)
  121.                     return;
  122.                 winLoseStat.Set(false);
  123.                 AddInteractor<WinScreenInteractor>().Init();
  124.             }).AddTo(CompositeDisposable);
  125.  
  126.             MessageBroker.Default.Receive<LooseMessage>().Subscribe(_ =>
  127.             {
  128.                 if (!winLoseStat.Value)
  129.                     return;
  130.                 winLoseStat.Set(false);
  131.                 AddInteractor<DefeatScreenInteractor>().Init();
  132.             }).AddTo(CompositeDisposable);
  133.         }
  134.  
  135.  
  136.         private void InitLoadOutCells()
  137.         {
  138.             var battleWindow = _uiService.Get<UIBattleWindow>();
  139.  
  140.             var lineUpData = _entityStorage.GetSingleByType<LineUpDataEntity>();
  141.             var maxUnitCount = 4; // todo: get from config
  142.             for (var i = 0; i < maxUnitCount; i++)
  143.             {
  144.                 var key = i.ToString();
  145.                 var lineUp = lineUpData.Stats.Get<LineUpData>(key).Value;
  146.                 if (!lineUp.IsEmpty)
  147.                 {
  148.                     var cm = _modelStorage.GetItem<UnitConvertModel>(lineUp.SpawnId);
  149.                     var viewModel = _modelStorage.GetItem<UnitViewModel>(cm.ConvertedId);
  150.                     var lvlModel = _modelStorage.GetItem<UnitModel>(cm.ConvertedId);
  151.                     var unitUpgrade = lvlModel.Upgrade[0];
  152.                     var cooldownEndCommand = new ReactiveCommand();
  153.  
  154.                     var protocol = new UIBattleUnitButtonCellViewProtocol
  155.                     {
  156.                         Count = (int)unitUpgrade.Count,
  157.                         Name = viewModel.NameKey,
  158.                         UnitType = viewModel.SpawnType,
  159.                         EnergyCostValue = unitUpgrade.Energy,
  160.                         Icon = viewModel.GetUnitIcon(),
  161.                         CurrentEnergy = new(),
  162.                         CurrentUnitCount = new()
  163.                     };
  164.  
  165.                     cooldownEndCommand.Subscribe(_ =>
  166.                     {
  167.                         RemoveInteractor<BattleSpawnCooldownInteractor>();
  168.                         protocol.Cooldown.SetValueAndForceNotify(0);
  169.                         OnEnergyStatChange(_battlePlayerEntity.Stats.Get<float>("energy"), default);
  170.                     }).AddTo(CompositeDisposable);
  171.  
  172.                     protocol.ClickCommand.Subscribe(_ =>
  173.                     {
  174.                         var currentCount = _battlePlayerEntity.Stats.Get<float>("unit_count").Value;
  175.                         var maxCount = _battlePlayerEntity.Stats.Get<float>("total_unit_count").Value;
  176.                         if (currentCount >= maxCount)
  177.                             return;
  178.  
  179.                         var energyStat = _battlePlayerEntity.Stats.Get<float>("energy");
  180.                         if (energyStat.Value < protocol.EnergyCostValue)
  181.                             return;
  182.  
  183.                         var count = (int)lvlModel.Upgrade[0].Count;
  184.                         if (currentCount + count > maxCount)
  185.                             return;
  186.  
  187.                         energyStat.Add(-protocol.EnergyCostValue);
  188.                         MessageBroker.Default.Publish(new SpawnMessage(protocol.UnitType, count));
  189.  
  190.                         // todo: get from config?
  191.                         var cooldownTime = 3f;
  192.                         protocol.Cooldown.SetValueAndForceNotify(cooldownTime);
  193.                         AddInteractor<BattleSpawnCooldownInteractor>(
  194.                             protocol.Cooldown,
  195.                             cooldownEndCommand,
  196.                             cooldownTime).Init();
  197.  
  198.                         OnEnergyStatChange(_battlePlayerEntity.Stats.Get<float>("energy"), default);
  199.                     }).AddTo(CompositeDisposable);
  200.  
  201.                     _cellViewProtocolList.Add(protocol);
  202.  
  203.                     var cell = _unitCellFactory.Create(protocol);
  204.  
  205.                     battleWindow.SetLoadOutCellView(cell);
  206.                 }
  207.             }
  208.         }
  209.  
  210.         private void OnRoundTimeStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
  211.         {
  212.             var battleWindow = _uiService.Get<UIBattleWindow>();
  213.             battleWindow.SetTime(stat.Value);
  214.         }
  215.  
  216.         private void OnUnitCountStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
  217.         {
  218.             var battleWindow = _uiService.Get<UIBattleWindow>();
  219.             var maxValue = _battlePlayerEntity.Stats.Get<float>("total_unit_count").Value;
  220.             battleWindow.SetUnitBarValue(stat.Value, maxValue);
  221.  
  222.             foreach (var cellViewProtocol in _cellViewProtocolList)
  223.             {
  224.                 cellViewProtocol.CurrentUnitCount.SetValueAndForceNotify((stat.Value, maxValue));
  225.             }
  226.         }
  227.  
  228.         private void OnUnitTotalCountStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
  229.         {
  230.             var battleWindow = _uiService.Get<UIBattleWindow>();
  231.             var value = _battlePlayerEntity.Stats.Get<float>("unit_count").Value;
  232.             battleWindow.SetUnitBarValue(value, stat.Value);
  233.         }
  234.        
  235.         private void OnEnergyStatChange(IStat<float> stat, StatChangeData<float> statChangeData)
  236.         {
  237.             var battleWindow = _uiService.Get<UIBattleWindow>();
  238.             var maxValue = _battlePlayerEntity.Stats.Get<float>("energy_max").Value;
  239.             battleWindow.SetEnergyBarValue(stat.Value, maxValue);
  240.  
  241.             foreach (var cellViewProtocol in _cellViewProtocolList)
  242.             {
  243.                 cellViewProtocol.CurrentEnergy.SetValueAndForceNotify(stat.Value);
  244.             }
  245.         }
  246.  
  247.         public override void Dispose()
  248.         {
  249.             base.Dispose();
  250.  
  251.             _cellViewProtocolList.Clear();
  252.  
  253.             var window = _uiService.Hide<UIBattleWindow>();
  254.             window.Clear();
  255.         }
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement