Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
UnityLV committed May 20, 2023
1 parent 4fc61bc commit c8e69bf
Show file tree
Hide file tree
Showing 16 changed files with 1,730 additions and 204 deletions.

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Assets/ScriptableObjects/Levels/Levels.asset
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ MonoBehaviour:
m_Name: Levels
m_EditorClassIdentifier:
_levels:
- {fileID: 11400000, guid: 969abf9905e44c34a83e9618e313130d, type: 2}
- {fileID: 11400000, guid: a38ad66e62dc02740aa4725e1ffce7a5, type: 2}
- {fileID: 11400000, guid: 5e3ee75d318f63344ba7c75e0f39c67a, type: 2}
- {fileID: 11400000, guid: ab2e9c59beb93e44a876e6e79d14934e, type: 2}
- {fileID: 11400000, guid: a555c1d7d7ddacc4887a07dabd9976d2, type: 2}
- {fileID: 11400000, guid: f9db246a5941ff645a083ac5150ce1e7, type: 2}
Expand All @@ -25,11 +28,8 @@ MonoBehaviour:
- {fileID: 11400000, guid: dae7e9792f8d5774292a944bd2ad1250, type: 2}
- {fileID: 11400000, guid: d6aeb77fa82a2a0488dea096946304c0, type: 2}
- {fileID: 11400000, guid: b36d6b4ce759c264f8931759d122b2ef, type: 2}
- {fileID: 11400000, guid: 5e3ee75d318f63344ba7c75e0f39c67a, type: 2}
- {fileID: 11400000, guid: 713ba49b5dd872345a9c0a43e045ef9f, type: 2}
- {fileID: 11400000, guid: 969abf9905e44c34a83e9618e313130d, type: 2}
- {fileID: 11400000, guid: 15a99b2ddf32a1a478a2ae41a0d15119, type: 2}
- {fileID: 11400000, guid: b15f35c4fb114e9479c5e1fe3cd7f096, type: 2}
- {fileID: 11400000, guid: 920b6c1885d43fb439798895a4ec91a2, type: 2}
- {fileID: 11400000, guid: a38ad66e62dc02740aa4725e1ffce7a5, type: 2}
- {fileID: 11400000, guid: 8d8ac44a2d8598042926097d963d74a7, type: 2}
184 changes: 13 additions & 171 deletions Assets/Scripts/Board/BoardSolver.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,23 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;



public class RegularBoardFiller
{
private ConfigFactory<int, IItem> _itemFactory;
private ItemInCellInjector _itemInCellInjector;

public RegularBoardFiller(ConfigFactory<int, IItem> itemFactory)
{
_itemFactory = itemFactory;
_itemInCellInjector = new();
}

public async UniTask TryFillCellRegularItem(ICell cell, params Vector3[] path)
{
bool isNeedToFill = cell.HasItem == false && cell.CanContainsItem;
if (isNeedToFill)
{
await FillCellRegularItem(cell, path);
}
}
private async UniTask FillCellRegularItem(ICell cell, params Vector3[] path)
{
var item = _itemFactory.Get(cell.GridPosition.ColumnIndex);
await _itemInCellInjector.InjectItemInCell(item, cell, path);
}
}













public class BoardSolver
{
private SolveSlotsDetecor _solveSlotsDetecor;
private ItemSwaper _itemSwaper;
private IBoardFaller _boardFaller;
private IBoardFiller _boardFiller;
private BoostExicuter _boostExicuter;
private PositionMemory _positionMemory;
private ItemNextStateMover _itemStateMover;
private ObstilcleSolver _obstilcleSolver;
private BoardClearer _boardClearer;
private SequenceSolver _sequenceSolver;
private ISwapSolwer _regularSwapSolver;
private ISwapSolwer _boostSwapSolver;

public event UnityAction CountedSwapMaked;

public BoardSolver(SolveSlotsDetecor solveSlotsDetecor, ItemSwaper itemSwaper, IBoardFiller boardFiller, Board board, BoostExicuter boostExicuter)
public BoardSolver(Workers workers, ItemSwaper itemSwaper, BoostExicuter boostExicuter)
{
_solveSlotsDetecor = solveSlotsDetecor;
_itemSwaper = itemSwaper;
_boardFiller = boardFiller;

_boardFaller = new DownBoardFaller(board);
_boardClearer = new(board);
_positionMemory = new(board);
_itemStateMover = new();
_boostExicuter = boostExicuter;
_obstilcleSolver = new(board);
_sequenceSolver = new(workers);

_regularSwapSolver = new RegularSwapSolwer(workers.SolveSlotsDetecor, itemSwaper, _sequenceSolver, () => CountedSwapMaked?.Invoke());
_boostSwapSolver = new BoostSwapSolver(_boostExicuter, () => CountedSwapMaked?.Invoke());

}

public async UniTask SolveSwap(ICell selectedCell, ICell cell)
Expand All @@ -76,131 +26,23 @@ public async UniTask SolveSwap(ICell selectedCell, ICell cell)

if (isBoostSwap)
{
await SolveBoostSwap(selectedCell, cell);
await _boostSwapSolver.SolveSwap(selectedCell, cell);
await SolveBoard();
}
else
{
await SolveRegularSwap(selectedCell, cell);
await _regularSwapSolver.SolveSwap(selectedCell, cell);
}
}

private async UniTask SolveBoostSwap(ICell selectedCell, ICell cell)
{
CountedSwapMaked?.Invoke();

if (selectedCell.Item is IBoostItem selectedBoost)
{
selectedBoost.SwapWith = cell.Item;
await _boostExicuter.Execute(selectedBoost);//ToDo: await нужно ожидать паралельно у обоих бустов?
}

if (cell.Item is IBoostItem boost)
{
boost.SwapWith = selectedCell.Item;
await _boostExicuter.Execute(boost);//ToDo: await нужно ожидать паралельно у обоих бустов?
}

_boardClearer.ClearBordFromDeadItems();

await _boardFaller.FallBoard();
await _boardFiller.FillBoard();
await SolveAllSequence();
}

public async UniTask SolveBoard()
{
_boardClearer.ClearBordFromDeadItems();
await _boardFaller.FallBoard();
await _boardFiller.FillBoard();
await SolveAllSequence();
await _sequenceSolver.FallAndSolve();
}

private async UniTask SolveRegularSwap(ICell selectedCell, ICell cell)
{
bool isMatchExist = _solveSlotsDetecor.TryGetSequence(selectedCell.GridPosition, out var sequence) ||
_solveSlotsDetecor.TryGetSequence(cell.GridPosition, out sequence);

if (isMatchExist)
{
await CreateRegularSwapWithExistingMath(sequence);
}
else
{
await _itemSwaper.SwapItems(cell, selectedCell);
}
}

private async UniTask CreateRegularSwapWithExistingMath(MatchSequence sequence)
{
CountedSwapMaked?.Invoke();

await SolveSequence(sequence);

await SolveAllSequence();
}

private async UniTask SolveAllSequence()
{
var positions = _positionMemory.GetUppdatedGridPositions();

while (_solveSlotsDetecor.TryGetBestSequence(positions, out var sequence))
{
await SolveSequence(sequence);

positions = _positionMemory.GetUppdatedGridPositions();
}

_positionMemory.RememorizeAllPositions();
}

private async UniTask SolveSequence(MatchSequence sequence)
{
await _obstilcleSolver.SolveAll(sequence);
await SolveItemInSequence(sequence);

_boardClearer.ClearBordFromDeadItems();
await _boardFaller.FallBoard();
await _boardFiller.FillBoard();
}

private async UniTask SolveItemInSequence(MatchSequence sequence)
{
await _itemStateMover.SetNextStateSequence(sequence);

bool isNeedToSpawnBoost = sequence.Type != SequenceTypes.Three;
if (isNeedToSpawnBoost)
{
SpawnBoostIn(sequence);
}
}

private void SpawnBoostIn(MatchSequence sequence)
{
var origin = sequence.Origin;
var type = sequence.Type;

_boardFiller.FillBoostInCell(ConvertCequenceToBoost(type), origin);
}

private BoostTypes ConvertCequenceToBoost(SequenceTypes boostTypes)
{
switch (boostTypes)
{
case SequenceTypes.FourHorisontal:
return BoostTypes.Vertical;
case SequenceTypes.FourVertical:
return BoostTypes.Horizontal;
case SequenceTypes.FiveLine:
return BoostTypes.Rainbow;
case SequenceTypes.TShape:
case SequenceTypes.LShape:
return BoostTypes.Bomb;
case SequenceTypes.Square:
return BoostTypes.Rocket;
default:
return BoostTypes.None;
}
}


}
Expand Down
37 changes: 37 additions & 0 deletions Assets/Scripts/Board/BoostSwapSolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Cysharp.Threading.Tasks;
using UnityEngine.Events;

public class BoostSwapSolver : ISwapSolwer
{
private BoostExicuter _boostExicuter;
private UnityAction _swapCallback;

public BoostSwapSolver(BoostExicuter boostExicuter, UnityAction swapCallback)
{
_boostExicuter = boostExicuter;
_swapCallback = swapCallback;
}

public async UniTask SolveSwap(ICell selectedCell, ICell cell)
{
_swapCallback?.Invoke();

if (selectedCell.Item is IBoostItem selectedBoost)
{
selectedBoost.SwapWith = cell.Item;
await _boostExicuter.Execute(selectedBoost);//ToDo: await нужно ожидать паралельно у обоих бустов?
}

if (cell.Item is IBoostItem boost)
{
boost.SwapWith = selectedCell.Item;
await _boostExicuter.Execute(boost);//ToDo: await нужно ожидать паралельно у обоих бустов?
}
}


}




11 changes: 11 additions & 0 deletions Assets/Scripts/Board/BoostSwapSolver.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Assets/Scripts/Board/ISwapSolwer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Cysharp.Threading.Tasks;
using UnityEngine.Events;

public interface ISwapSolwer
{
UniTask SolveSwap(ICell selectedCell, ICell cell);
}




11 changes: 11 additions & 0 deletions Assets/Scripts/Board/ISwapSolwer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Assets/Scripts/Board/RegularBoardFiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Cysharp.Threading.Tasks;
using UnityEngine;

public class RegularBoardFiller
{
private ConfigFactory<int, IItem> _itemFactory;
private ItemInCellInjector _itemInCellInjector;

public RegularBoardFiller(ConfigFactory<int, IItem> itemFactory)
{
_itemFactory = itemFactory;
_itemInCellInjector = new();
}

public async UniTask TryFillCellRegularItem(ICell cell, params Vector3[] path)
{
bool isNeedToFill = cell.HasItem == false && cell.CanContainsItem;
if (isNeedToFill)
{
await FillCellRegularItem(cell, path);
}
}
private async UniTask FillCellRegularItem(ICell cell, params Vector3[] path)
{
var item = _itemFactory.Get(cell.GridPosition.ColumnIndex);
await _itemInCellInjector.InjectItemInCell(item, cell, path);
}
}




11 changes: 11 additions & 0 deletions Assets/Scripts/Board/RegularBoardFiller.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c8e69bf

Please sign in to comment.