Repository navigation
Expand file tree
/
Copy pathLoadLevelState.cs
More file actions
187 lines (162 loc) · 6.99 KB
/
Copy pathLoadLevelState.cs
File metadata and controls
187 lines (162 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System.Collections.Generic;
using System.Threading;
using Code.Logic.Zuma.Level;
using Code.Services.BallController;
using Code.Services.Factories.Game;
using Code.Services.Factories.UIFactory;
using Code.Services.Input;
using Code.Services.Input.Device;
using Code.Services.Levels;
using Code.Services.Providers.Balls;
using Code.Services.Providers.Widgets;
using Code.Services.Random;
using Code.Services.SaveLoad;
using Code.Services.StaticData;
using Code.StaticData;
using Code.StaticData.Levels;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace Code.Infrastructure.StateMachine.Game.States
{
public class LoadLevelState : IPayloadedState<string>, IGameState
{
private readonly ISceneLoader _sceneLoader;
private readonly ILoadingCurtain _loadingCurtain;
private readonly IUIFactory _uiFactory;
private readonly IStateMachine<IGameState> _gameStateMachine;
private readonly IGameFactory _gameFactory;
private readonly IInputService _inputService;
private readonly ILevelService _levelService;
private readonly IBallProvider _ballProvider;
private readonly IWidgetProvider _widgetProvider;
private readonly IBallChainController _ballChainController;
private readonly IStaticDataService _staticDataService;
private readonly IRandomService _randomService;
private readonly ISaveLoadFacade _saveLoad;
private int _loadVersion;
public LoadLevelState(
IStateMachine<IGameState> gameStateMachine,
ISceneLoader sceneLoader,
ILoadingCurtain loadingCurtain,
IUIFactory uiFactory,
IGameFactory gameFactory,
IInputService inputService,
ILevelService levelService,
IBallProvider ballProvider,
IWidgetProvider widgetProvider,
IBallChainController ballChainController,
IStaticDataService staticDataService,
IRandomService randomService,
ISaveLoadFacade saveLoad)
{
_gameFactory = gameFactory;
_gameStateMachine = gameStateMachine;
_sceneLoader = sceneLoader;
_loadingCurtain = loadingCurtain;
_uiFactory = uiFactory;
_inputService = inputService;
_levelService = levelService;
_ballProvider = ballProvider;
_widgetProvider = widgetProvider;
_ballChainController = ballChainController;
_staticDataService = staticDataService;
_randomService = randomService;
_saveLoad = saveLoad;
}
public void Enter(string payload)
{
_loadingCurtain.Show();
int version = ++_loadVersion;
_sceneLoader.Load(payload, () =>
{
if (version == _loadVersion)
OnLevelLoad();
});
}
public void Exit()
{
++_loadVersion;
_loadingCurtain.Hide();
}
protected virtual void OnLevelLoad()
{
if (_levelService.Mode == ZumaMode.Adventure)
_levelService.PrepareAdventureSelection();
InitGameWorld();
if (_levelService.VisitLevel())
_saveLoad.SaveProgress(SaveMethodType.PlayerPrefs);
_gameStateMachine.Enter<GameLoopState>();
}
private void InitGameWorld()
{
_uiFactory.CreateUiRoot();
InitHud();
InitLevelHolder();
InitProviders();
InitPlayer();
InitBallChainController();
PlayStartLevelAsync(_ballChainController.AttemptToken).Forget();
}
private void InitLevelHolder()
{
LevelStaticData levelStaticData = _levelService.GetCurrentLevelStaticData();
ChapterStaticData chapter = _staticDataService.ForChapter(_levelService.GetCurrentChapter());
LevelHolder levelHolder = _gameFactory.CreateLevelHolder(levelStaticData, chapter.Theme);
_levelService.SetLevelHolder(levelHolder);
}
private void InitProviders()
{
_ballProvider.CreatePoolBall();
_widgetProvider.CreatePoolWidgets();
}
private void InitPlayer()
{
var levelHolder = _levelService.GetLevelHolder();
var player = _gameFactory.CreatePlayer(levelHolder.SpawnPositionPlayer.position, Quaternion.identity);
player.Initialize();
}
private void InitHud()
{
var gameHud = _uiFactory.CreateGameHud();
gameHud.Initialize();
}
private void InitBallChainController()
{
var levelHolder = _levelService.GetLevelHolder();
BallChainDTO ballChainDTO = _staticDataService.GetBallChainDTO();
LevelConfig config = _levelService.GetCurrentLevelStaticData().LevelConfig;
ballChainDTO.Survival = _levelService.Mode == ZumaMode.Survival;
ballChainDTO.ZumaTargetScore = config.GetZumaTarget();
ballChainDTO.AceTime = config.AceTime;
ballChainDTO.CollisionThreshold = Mathf.Min(ballChainDTO.CollisionThreshold, ballChainDTO.SpacingBalls * 1.15f);
_ballChainController.Initialize(levelHolder.PathCreator, ballChainDTO);
}
private async UniTask PlayStartLevelAsync(CancellationToken token)
{
await UniTask.WaitUntil(() => !_loadingCurtain.IsActive, cancellationToken: token);
await UniTask.Delay(150, cancellationToken: token);
var startLevelInfoDisplayer = _uiFactory.CreateStartLevelInfoDisplayer();
var nameLevel = _levelService.GetCurrentLevelStaticData().LevelName;
var numberLevel = _levelService.GetCurrentChapter() + "-" + _levelService.GetCurrentLevel();
var level = nameLevel + " " + numberLevel;
startLevelInfoDisplayer.Initialize(level);
startLevelInfoDisplayer.Play();
await UniTask.WaitUntil(() => !startLevelInfoDisplayer.IsActive, cancellationToken: token);
await _ballChainController.MoveParticleAlongPathAsync(_levelService.GetLevelHolder().DefaultParticleSystemHolder);
token.ThrowIfCancellationRequested();
List<Color> colors = _randomService.GetColorsByLevelRandomConfig();
_gameFactory.Player.PlayerShooting.Activate(colors);
_ballChainController.StartBallSpawning(colors);
TypeInput inputType = Application.isMobilePlatform ? TypeInput.Mobile : TypeInput.PC;
switch (inputType)
{
case TypeInput.PC:
_inputService.SetInputDevice(new MouseInputDevice());
break;
case TypeInput.Mobile:
_inputService.SetInputDevice(new TouchInputDevice());
break;
}
}
}
}