11using Cysharp . Threading . Tasks ;
2- using UnityEngine ;
32using UnityEngine . Events ;
43
5-
6-
7- public class RegularBoardFiller
8- {
9- private ConfigFactory < int , IItem > _itemFactory ;
10- private ItemInCellInjector _itemInCellInjector ;
11-
12- public RegularBoardFiller ( ConfigFactory < int , IItem > itemFactory )
13- {
14- _itemFactory = itemFactory ;
15- _itemInCellInjector = new ( ) ;
16- }
17-
18- public async UniTask TryFillCellRegularItem ( ICell cell , params Vector3 [ ] path )
19- {
20- bool isNeedToFill = cell . HasItem == false && cell . CanContainsItem ;
21- if ( isNeedToFill )
22- {
23- await FillCellRegularItem ( cell , path ) ;
24- }
25- }
26- private async UniTask FillCellRegularItem ( ICell cell , params Vector3 [ ] path )
27- {
28- var item = _itemFactory . Get ( cell . GridPosition . ColumnIndex ) ;
29- await _itemInCellInjector . InjectItemInCell ( item , cell , path ) ;
30- }
31- }
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
454public class BoardSolver
465{
47- private SolveSlotsDetecor _solveSlotsDetecor ;
48- private ItemSwaper _itemSwaper ;
49- private IBoardFaller _boardFaller ;
50- private IBoardFiller _boardFiller ;
516 private BoostExicuter _boostExicuter ;
52- private PositionMemory _positionMemory ;
53- private ItemNextStateMover _itemStateMover ;
54- private ObstilcleSolver _obstilcleSolver ;
55- private BoardClearer _boardClearer ;
7+ private SequenceSolver _sequenceSolver ;
8+ private ISwapSolwer _regularSwapSolver ;
9+ private ISwapSolwer _boostSwapSolver ;
5610
5711 public event UnityAction CountedSwapMaked ;
5812
59- public BoardSolver ( SolveSlotsDetecor solveSlotsDetecor , ItemSwaper itemSwaper , IBoardFiller boardFiller , Board board , BoostExicuter boostExicuter )
13+ public BoardSolver ( Workers workers , ItemSwaper itemSwaper , BoostExicuter boostExicuter )
6014 {
61- _solveSlotsDetecor = solveSlotsDetecor ;
62- _itemSwaper = itemSwaper ;
63- _boardFiller = boardFiller ;
64-
65- _boardFaller = new DownBoardFaller ( board ) ;
66- _boardClearer = new ( board ) ;
67- _positionMemory = new ( board ) ;
68- _itemStateMover = new ( ) ;
6915 _boostExicuter = boostExicuter ;
70- _obstilcleSolver = new ( board ) ;
16+ _sequenceSolver = new ( workers ) ;
17+
18+ _regularSwapSolver = new RegularSwapSolwer ( workers . SolveSlotsDetecor , itemSwaper , _sequenceSolver , ( ) => CountedSwapMaked ? . Invoke ( ) ) ;
19+ _boostSwapSolver = new BoostSwapSolver ( _boostExicuter , ( ) => CountedSwapMaked ? . Invoke ( ) ) ;
20+
7121 }
7222
7323 public async UniTask SolveSwap ( ICell selectedCell , ICell cell )
@@ -76,131 +26,23 @@ public async UniTask SolveSwap(ICell selectedCell, ICell cell)
7626
7727 if ( isBoostSwap )
7828 {
79- await SolveBoostSwap ( selectedCell , cell ) ;
29+ await _boostSwapSolver . SolveSwap ( selectedCell , cell ) ;
30+ await SolveBoard ( ) ;
8031 }
8132 else
8233 {
83- await SolveRegularSwap ( selectedCell , cell ) ;
34+ await _regularSwapSolver . SolveSwap ( selectedCell , cell ) ;
8435 }
8536 }
8637
87- private async UniTask SolveBoostSwap ( ICell selectedCell , ICell cell )
88- {
89- CountedSwapMaked ? . Invoke ( ) ;
90-
91- if ( selectedCell . Item is IBoostItem selectedBoost )
92- {
93- selectedBoost . SwapWith = cell . Item ;
94- await _boostExicuter . Execute ( selectedBoost ) ; //ToDo: await нужно ожидать паралельно у обоих бустов?
95- }
96-
97- if ( cell . Item is IBoostItem boost )
98- {
99- boost . SwapWith = selectedCell . Item ;
100- await _boostExicuter . Execute ( boost ) ; //ToDo: await нужно ожидать паралельно у обоих бустов?
101- }
102-
103- _boardClearer . ClearBordFromDeadItems ( ) ;
104-
105- await _boardFaller . FallBoard ( ) ;
106- await _boardFiller . FillBoard ( ) ;
107- await SolveAllSequence ( ) ;
108- }
10938
11039 public async UniTask SolveBoard ( )
11140 {
112- _boardClearer . ClearBordFromDeadItems ( ) ;
113- await _boardFaller . FallBoard ( ) ;
114- await _boardFiller . FillBoard ( ) ;
115- await SolveAllSequence ( ) ;
41+ await _sequenceSolver . FallAndSolve ( ) ;
11642 }
11743
118- private async UniTask SolveRegularSwap ( ICell selectedCell , ICell cell )
119- {
120- bool isMatchExist = _solveSlotsDetecor . TryGetSequence ( selectedCell . GridPosition , out var sequence ) ||
121- _solveSlotsDetecor . TryGetSequence ( cell . GridPosition , out sequence ) ;
122-
123- if ( isMatchExist )
124- {
125- await CreateRegularSwapWithExistingMath ( sequence ) ;
126- }
127- else
128- {
129- await _itemSwaper . SwapItems ( cell , selectedCell ) ;
130- }
131- }
132-
133- private async UniTask CreateRegularSwapWithExistingMath ( MatchSequence sequence )
134- {
135- CountedSwapMaked ? . Invoke ( ) ;
136-
137- await SolveSequence ( sequence ) ;
138-
139- await SolveAllSequence ( ) ;
140- }
141-
142- private async UniTask SolveAllSequence ( )
143- {
144- var positions = _positionMemory . GetUppdatedGridPositions ( ) ;
145-
146- while ( _solveSlotsDetecor . TryGetBestSequence ( positions , out var sequence ) )
147- {
148- await SolveSequence ( sequence ) ;
149-
150- positions = _positionMemory . GetUppdatedGridPositions ( ) ;
151- }
15244
153- _positionMemory . RememorizeAllPositions ( ) ;
154- }
155-
156- private async UniTask SolveSequence ( MatchSequence sequence )
157- {
158- await _obstilcleSolver . SolveAll ( sequence ) ;
159- await SolveItemInSequence ( sequence ) ;
160-
161- _boardClearer . ClearBordFromDeadItems ( ) ;
162- await _boardFaller . FallBoard ( ) ;
163- await _boardFiller . FillBoard ( ) ;
164- }
16545
166- private async UniTask SolveItemInSequence ( MatchSequence sequence )
167- {
168- await _itemStateMover . SetNextStateSequence ( sequence ) ;
169-
170- bool isNeedToSpawnBoost = sequence . Type != SequenceTypes . Three ;
171- if ( isNeedToSpawnBoost )
172- {
173- SpawnBoostIn ( sequence ) ;
174- }
175- }
176-
177- private void SpawnBoostIn ( MatchSequence sequence )
178- {
179- var origin = sequence . Origin ;
180- var type = sequence . Type ;
181-
182- _boardFiller . FillBoostInCell ( ConvertCequenceToBoost ( type ) , origin ) ;
183- }
184-
185- private BoostTypes ConvertCequenceToBoost ( SequenceTypes boostTypes )
186- {
187- switch ( boostTypes )
188- {
189- case SequenceTypes . FourHorisontal :
190- return BoostTypes . Vertical ;
191- case SequenceTypes . FourVertical :
192- return BoostTypes . Horizontal ;
193- case SequenceTypes . FiveLine :
194- return BoostTypes . Rainbow ;
195- case SequenceTypes . TShape :
196- case SequenceTypes . LShape :
197- return BoostTypes . Bomb ;
198- case SequenceTypes . Square :
199- return BoostTypes . Rocket ;
200- default :
201- return BoostTypes . None ;
202- }
203- }
20446
20547
20648}
0 commit comments