Skip to content

Commit

Permalink
Fixed special item issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
RUBIUS\chebanovdd committed Apr 20, 2022
1 parent ee857d8 commit e9f5063
Show file tree
Hide file tree
Showing 44 changed files with 404 additions and 274 deletions.
30 changes: 21 additions & 9 deletions samples/Terminal.Match3/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Extensions.DependencyInjection;
using Terminal.Match3.FillStrategies;
using Terminal.Match3.Interfaces;
using Terminal.Match3.TileGroupDetectors;
using Terminal.Match3.SpecialItemDetectors;

namespace Terminal.Match3
{
Expand Down Expand Up @@ -37,27 +37,39 @@ private static GameConfig<ITerminalGridSlot> GetGameConfig(TerminalGameBoardRend
GameBoardDataProvider = gameBoardRenderer,
LevelGoalsProvider = new LevelGoalsProvider(),
ItemSwapper = new TerminalItemSwapper(gameBoardRenderer),
GameBoardSolver = GetGameBoardSolver(),
SolvedSequencesConsumers = GetSolvedSequencesConsumers(gameBoardRenderer)
GameBoardSolver = GetGameBoardSolver(gameBoardRenderer),
SolvedSequencesConsumers = GetSolvedSequencesConsumers()
};
}

private static IGameBoardSolver<ITerminalGridSlot> GetGameBoardSolver()
private static IGameBoardSolver<ITerminalGridSlot> GetGameBoardSolver(
ITerminalGameBoardRenderer gameBoardRenderer)
{
return new GameBoardSolver<ITerminalGridSlot>(GetSequenceDetectors(),
GetSpecialItemDetectors(gameBoardRenderer));
}

private static ISequenceDetector<ITerminalGridSlot>[] GetSequenceDetectors()
{
return new GameBoardSolver<ITerminalGridSlot>(new ISequenceDetector<ITerminalGridSlot>[]
return new ISequenceDetector<ITerminalGridSlot>[]
{
new VerticalLineDetector<ITerminalGridSlot>(),
new HorizontalLineDetector<ITerminalGridSlot>()
});
};
}

private static ISolvedSequencesConsumer<ITerminalGridSlot>[] GetSolvedSequencesConsumers(
private static ISpecialItemDetector<ITerminalGridSlot>[] GetSpecialItemDetectors(
ITerminalGameBoardRenderer gameBoardRenderer)
{
return new ISolvedSequencesConsumer<ITerminalGridSlot>[]
return new ISpecialItemDetector<ITerminalGridSlot>[]
{
new TileGroupDetector(gameBoardRenderer)
new LockedItemDetector(gameBoardRenderer)
};
}

private static ISolvedSequencesConsumer<ITerminalGridSlot>[] GetSolvedSequencesConsumers()
{
return System.Array.Empty<ISolvedSequencesConsumer<ITerminalGridSlot>>();
}
}
}
25 changes: 11 additions & 14 deletions samples/Terminal.Match3/Extensions/ItemsSequenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ namespace Terminal.Match3.Extensions
{
public static class ItemsSequenceExtensions
{
public static IEnumerable<TGridSlot> GetUniqueGridSlots<TGridSlot>(
this IEnumerable<ItemSequence<TGridSlot>> sequences, bool onlyMovable = false) where TGridSlot : IGridSlot
public static IEnumerable<TGridSlot> GetUniqueSolvedGridSlots<TGridSlot>(this SolvedData<TGridSlot> solvedData,
bool onlyMovable = false) where TGridSlot : IGridSlot
{
var solvedGridSlots = new HashSet<TGridSlot>();

foreach (var sequence in sequences)
foreach (var solvedGridSlot in solvedData.GetSolvedGridSlots())
{
foreach (var solvedGridSlot in sequence.SolvedGridSlots)
if (onlyMovable && solvedGridSlot.IsMovable == false)
{
if (onlyMovable && solvedGridSlot.IsMovable == false)
{
continue;
}

if (solvedGridSlots.Add(solvedGridSlot) == false)
{
continue;
}
continue;
}

yield return solvedGridSlot;
if (solvedGridSlots.Add(solvedGridSlot) == false)
{
continue;
}

yield return solvedGridSlot;
}

solvedGridSlots.Clear();
Expand Down
4 changes: 2 additions & 2 deletions samples/Terminal.Match3/FillStrategies/SimpleFillStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public IEnumerable<IJob> GetFillJobs(IGameBoard<ITerminalGridSlot> gameBoard)
}

public IEnumerable<IJob> GetSolveJobs(IGameBoard<ITerminalGridSlot> gameBoard,
IEnumerable<ItemSequence<ITerminalGridSlot>> sequences)
SolvedData<ITerminalGridSlot> solvedData)
{
var itemsToShow = new List<ITerminalItem>();

foreach (var solvedGridSlot in sequences.GetUniqueGridSlots(true))
foreach (var solvedGridSlot in solvedData.GetUniqueSolvedGridSlots(true))
{
var newItem = _itemsPool.GetItem();
var currentItem = solvedGridSlot.Item;
Expand Down
8 changes: 6 additions & 2 deletions samples/Terminal.Match3/GridTiles/States/LockedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ namespace Terminal.Match3.GridTiles.States
public class LockedState : GridTile, IStatefulSlot
{
private bool _isLocked = true;
private TileGroup _group = TileGroup.Locked;

public override bool IsLocked => _isLocked;
public override bool CanContainItem => true;
public override TileGroup Group => TileGroup.Locked;
public override TileGroup Group => _group;

public void NextState()
public bool NextState()
{
_isLocked = false;
_group = TileGroup.Available;

return false;
}
}
}
2 changes: 1 addition & 1 deletion samples/Terminal.Match3/Interfaces/IStatefulSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IStatefulSlot
{
void NextState();
bool NextState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface ITerminalGameBoardRenderer : IGameBoardRenderer

bool IsPositionOnBoard(GridPosition gridPosition);
TileGroup GetTileGroup(GridPosition gridPosition);
void TrySetNextTileState(GridPosition gridPosition);
bool TrySetNextTileState(GridPosition gridPosition);
}
}
9 changes: 0 additions & 9 deletions samples/Terminal.Match3/Interfaces/ITileDetector.cs

This file was deleted.

34 changes: 34 additions & 0 deletions samples/Terminal.Match3/SpecialItemDetectors/LockedItemDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Match3.App.Interfaces;
using Terminal.Match3.Enums;
using Terminal.Match3.Interfaces;

namespace Terminal.Match3.SpecialItemDetectors
{
public class LockedItemDetector : ISpecialItemDetector<ITerminalGridSlot>
{
private readonly ITerminalGameBoardRenderer _gameBoardRenderer;

public LockedItemDetector(ITerminalGameBoardRenderer gameBoardRenderer)
{
_gameBoardRenderer = gameBoardRenderer;
}

public IEnumerable<ITerminalGridSlot> GetSpecialItemGridSlots(IGameBoard<ITerminalGridSlot> gameBoard,
ITerminalGridSlot gridSlot)
{
if (_gameBoardRenderer.GetTileGroup(gridSlot.GridPosition) != TileGroup.Locked)
{
yield break;
}

var hasNextState = _gameBoardRenderer.TrySetNextTileState(gridSlot.GridPosition);
if (hasNextState)
{
yield break;
}

yield return gridSlot;
}
}
}
4 changes: 2 additions & 2 deletions samples/Terminal.Match3/TerminalGameBoardRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public TileGroup GetTileGroup(GridPosition gridPosition)
return _gridSlotTiles[gridPosition.RowIndex, gridPosition.ColumnIndex].Group;
}

public void TrySetNextTileState(GridPosition gridPosition)
public bool TrySetNextTileState(GridPosition gridPosition)
{
((IStatefulSlot) _gridSlotTiles[gridPosition.RowIndex, gridPosition.ColumnIndex]).NextState();
return ((IStatefulSlot) _gridSlotTiles[gridPosition.RowIndex, gridPosition.ColumnIndex]).NextState();
}

public ITerminalGridSlot[,] GetGameBoardSlots(int level)
Expand Down
23 changes: 0 additions & 23 deletions samples/Terminal.Match3/TileGroupDetectors/LockedTileDetector.cs

This file was deleted.

32 changes: 0 additions & 32 deletions samples/Terminal.Match3/TileGroupDetectors/TileGroupDetector.cs

This file was deleted.

28 changes: 21 additions & 7 deletions samples/Unity.Match3/Assets/Scripts/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Common;
using Common.Interfaces;
using Common.Models;
using Common.TileGroupDetectors;
using Common.SpecialItemDetectors;
using FillStrategies;
using Match3.App;
using Match3.App.Interfaces;
Expand Down Expand Up @@ -63,7 +63,7 @@ private UnityGame GetUnityGame()
GameBoardDataProvider = _gameBoardRenderer,
ItemSwapper = new AnimatedItemSwapper(),
LevelGoalsProvider = new LevelGoalsProvider(),
GameBoardSolver = GetGameBoardSolver(),
GameBoardSolver = GetGameBoardSolver(_gameBoardRenderer),
SolvedSequencesConsumers = GetSolvedSequencesConsumers()
};

Expand All @@ -75,21 +75,35 @@ private UnityItemGenerator GetItemGenerator()
return new UnityItemGenerator(_itemPrefab, new GameObject("ItemsPool").transform);
}

private IGameBoardSolver<IUnityGridSlot> GetGameBoardSolver()
private IGameBoardSolver<IUnityGridSlot> GetGameBoardSolver(IUnityGameBoardRenderer gameBoardRenderer)
{
return new GameBoardSolver<IUnityGridSlot>(new ISequenceDetector<IUnityGridSlot>[]
return new GameBoardSolver<IUnityGridSlot>(GetSequenceDetectors(), GetSpecialItemDetectors(gameBoardRenderer));
}

private static ISequenceDetector<IUnityGridSlot>[] GetSequenceDetectors()
{
return new ISequenceDetector<IUnityGridSlot>[]
{
new VerticalLineDetector<IUnityGridSlot>(),
new HorizontalLineDetector<IUnityGridSlot>()
});
};
}

private static ISpecialItemDetector<IUnityGridSlot>[] GetSpecialItemDetectors(
IUnityGameBoardRenderer gameBoardRenderer)
{
return new ISpecialItemDetector<IUnityGridSlot>[]
{
new StoneItemDetector(gameBoardRenderer),
new IceItemDetector(gameBoardRenderer)
};
}

private ISolvedSequencesConsumer<IUnityGridSlot>[] GetSolvedSequencesConsumers()
{
return new ISolvedSequencesConsumer<IUnityGridSlot>[]
{
new GameScoreBoard(),
new TileGroupDetector(_gameBoardRenderer)
new GameScoreBoard()
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ namespace Common.Extensions
{
public static class ItemsSequenceExtensions
{
public static IEnumerable<TGridSlot> GetUniqueGridSlots<TGridSlot>(
this IEnumerable<ItemSequence<TGridSlot>> sequences, bool onlyMovable = false) where TGridSlot : IGridSlot
public static IEnumerable<TGridSlot> GetUniqueSolvedGridSlots<TGridSlot>(this SolvedData<TGridSlot> solvedData,
bool onlyMovable = false) where TGridSlot : IGridSlot
{
var solvedGridSlots = new HashSet<TGridSlot>();

foreach (var sequence in sequences)
foreach (var solvedGridSlot in solvedData.GetSolvedGridSlots())
{
foreach (var solvedGridSlot in sequence.SolvedGridSlots)
if (onlyMovable && solvedGridSlot.IsMovable == false)
{
if (onlyMovable && solvedGridSlot.IsMovable == false)
{
continue;
}

if (solvedGridSlots.Add(solvedGridSlot) == false)
{
continue;
}
continue;
}

yield return solvedGridSlot;
if (solvedGridSlots.Add(solvedGridSlot) == false)
{
continue;
}

yield return solvedGridSlot;
}

solvedGridSlots.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ protected override void Start()
_stateSpriteRenderer.sprite = GetStateSprite(_currentStateIndex);
}

public void NextState()
public bool NextState()
{
_currentStateIndex++;

if (_currentStateIndex < _stateSpriteNames.Length)
{
_stateSpriteRenderer.sprite = GetStateSprite(_currentStateIndex);
return true;
}
else
{
_stateSpriteRenderer.enabled = false;
OnComplete();
}

_stateSpriteRenderer.enabled = false;
OnComplete();

return false;
}

public void ResetState()
Expand Down
Loading

0 comments on commit e9f5063

Please sign in to comment.