Skip to content

Commit

Permalink
Added state property to grid slot.
Browse files Browse the repository at this point in the history
  • Loading branch information
RUBIUS\chebanovdd committed Apr 21, 2022
1 parent 0cf66f9 commit 2f21e51
Show file tree
Hide file tree
Showing 32 changed files with 56 additions and 109 deletions.
13 changes: 5 additions & 8 deletions samples/Terminal.Match3/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ private static GameConfig<ITerminalGridSlot> GetGameConfig(TerminalGameBoardRend
GameBoardDataProvider = gameBoardRenderer,
LevelGoalsProvider = new LevelGoalsProvider(),
ItemSwapper = new TerminalItemSwapper(gameBoardRenderer),
GameBoardSolver = GetGameBoardSolver(gameBoardRenderer),
GameBoardSolver = GetGameBoardSolver(),
SolvedSequencesConsumers = GetSolvedSequencesConsumers()
};
}

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

private static ISequenceDetector<ITerminalGridSlot>[] GetSequenceDetectors()
Expand All @@ -58,12 +56,11 @@ private static ISequenceDetector<ITerminalGridSlot>[] GetSequenceDetectors()
};
}

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

Expand Down
5 changes: 2 additions & 3 deletions samples/Terminal.Match3/GridTiles/GridTile.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Terminal.Match3.Enums;
using Terminal.Match3.Interfaces;
using Terminal.Match3.Interfaces;

namespace Terminal.Match3.GridTiles
{
public abstract class GridTile : IGridTile
{
public abstract int GroupId { get; }
public abstract bool IsLocked { get; }
public abstract bool CanContainItem { get; }
public abstract TileGroup Group { get; }
}
}
2 changes: 1 addition & 1 deletion samples/Terminal.Match3/GridTiles/States/AvailableState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Terminal.Match3.GridTiles.States
{
public class AvailableState : GridTile
{
public override int GroupId => (int) TileGroup.Available;
public override bool IsLocked => false;
public override bool CanContainItem => true;
public override TileGroup Group => TileGroup.Available;
}
}
10 changes: 8 additions & 2 deletions samples/Terminal.Match3/GridTiles/States/LockedState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Terminal.Match3.Enums;
using Terminal.Match3.Interfaces;
using Match3.Core.Interfaces;

namespace Terminal.Match3.GridTiles.States
{
Expand All @@ -8,9 +8,9 @@ public class LockedState : GridTile, IStatefulSlot
private bool _isLocked = true;
private TileGroup _group = TileGroup.Locked;

public override int GroupId => (int) _group;
public override bool IsLocked => _isLocked;
public override bool CanContainItem => true;
public override TileGroup Group => _group;

public bool NextState()
{
Expand All @@ -19,5 +19,11 @@ public bool NextState()

return false;
}

public void ResetState()
{
_isLocked = true;
_group = TileGroup.Locked;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Terminal.Match3.GridTiles.States
{
public class NotAvailableState : GridTile
{
public override int GroupId => (int) TileGroup.Unavailable;
public override bool IsLocked => true;
public override bool CanContainItem => false;
public override TileGroup Group => TileGroup.Unavailable;
}
}
2 changes: 0 additions & 2 deletions samples/Terminal.Match3/Interfaces/IGridTile.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Match3.Core.Interfaces;
using Terminal.Match3.Enums;

namespace Terminal.Match3.Interfaces
{
public interface IGridTile : IGridSlotState
{
TileGroup Group { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Match3.Core.Structs;
using Match3.Template.Interfaces;
using Terminal.Match3.Enums;

namespace Terminal.Match3.Interfaces
{
Expand All @@ -14,7 +13,5 @@ public interface ITerminalGameBoardRenderer : IGameBoardRenderer
void RedrawGameBoard();

bool IsPositionOnBoard(GridPosition gridPosition);
TileGroup GetTileGroup(GridPosition gridPosition);
bool TrySetNextTileState(GridPosition gridPosition);
}
}
19 changes: 2 additions & 17 deletions samples/Terminal.Match3/SpecialItemDetectors/LockedItemDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,13 @@ 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)
if (gridSlot.State.GroupId == (int) TileGroup.Locked)
{
yield break;
yield return gridSlot;
}

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

yield return gridSlot;
}
}
}
10 changes: 0 additions & 10 deletions samples/Terminal.Match3/TerminalGameBoardRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ public void ActivateItem(GridPosition gridPosition)
RedrawGameBoard();
}

public TileGroup GetTileGroup(GridPosition gridPosition)
{
return _gridSlotTiles[gridPosition.RowIndex, gridPosition.ColumnIndex].Group;
}

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

public ITerminalGridSlot[,] GetGameBoardSlots(int level)
{
return _gameBoardSlots;
Expand Down
9 changes: 4 additions & 5 deletions samples/Terminal.Match3/TerminalGridSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ namespace Terminal.Match3
{
public sealed class TerminalGridSlot : ITerminalGridSlot
{
private readonly IGridSlotState _state;

public TerminalGridSlot(IGridSlotState state, GridPosition gridPosition)
{
_state = state;
State = state;
GridPosition = gridPosition;
}

public int ItemId => Item.ContentId;

public bool HasItem => Item != null;
public bool IsLocked => _state.IsLocked;
public bool CanContainItem => _state.CanContainItem;
public bool IsLocked => State.IsLocked;
public bool CanContainItem => State.CanContainItem;
public bool IsMovable => IsLocked == false && HasItem;
public bool CanSetItem => CanContainItem && HasItem == false;

public ITerminalItem Item { get; private set; }
public IGridSlotState State { get; }
public GridPosition GridPosition { get; }

public void SetItem(ITerminalItem item)
Expand Down
12 changes: 6 additions & 6 deletions samples/Unity.Match3/Assets/Scripts/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private UnityGame GetUnityGame()
GameBoardDataProvider = _gameBoardRenderer,
ItemSwapper = new AnimatedItemSwapper(),
LevelGoalsProvider = new LevelGoalsProvider(),
GameBoardSolver = GetGameBoardSolver(_gameBoardRenderer),
GameBoardSolver = GetGameBoardSolver(),
SolvedSequencesConsumers = GetSolvedSequencesConsumers()
};

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

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

private ISequenceDetector<IUnityGridSlot>[] GetSequenceDetectors()
Expand All @@ -89,12 +89,12 @@ private ISequenceDetector<IUnityGridSlot>[] GetSequenceDetectors()
};
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Common.Enums;
using Common.Interfaces;
using UnityEngine;

Expand All @@ -8,9 +7,9 @@ public abstract class GridTile : MonoBehaviour, IGridTile
{
private bool _isDestroyed;

public abstract int GroupId { get; }
public abstract bool IsLocked { get; }
public abstract bool CanContainItem { get; }
public abstract TileGroup Group { get; }

public void SetActive(bool value)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Common.Interfaces;
using Match3.Core.Interfaces;
using UnityEngine;

namespace Common.GridTiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Common.GridTiles.States
{
public class AvailableState : SpriteGridTile
{
public override int GroupId => (int) TileGroup.Available;
public override bool IsLocked => false;
public override bool CanContainItem => true;
public override TileGroup Group => TileGroup.Available;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class IceState : StatefulGridTile
private bool _isLocked = true;
private TileGroup _group = TileGroup.Ice;

public override int GroupId => (int) _group;
public override bool IsLocked => _isLocked;
public override bool CanContainItem => true;
public override TileGroup Group => _group;

protected override void OnComplete()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Common.GridTiles.States
{
public class NotAvailableState : GridTile
{
public override int GroupId => (int) TileGroup.Unavailable;
public override bool IsLocked => true;
public override bool CanContainItem => false;
public override TileGroup Group => TileGroup.Unavailable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class StoneState : StatefulGridTile
private bool _canContainItem;
private TileGroup _group = TileGroup.Stone;

public override int GroupId => (int) _group;
public override bool IsLocked => _isLocked;
public override bool CanContainItem => _canContainItem;
public override TileGroup Group => _group;

protected override void OnComplete()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using Common.Enums;
using Match3.Core.Interfaces;
using UnityEngine;

namespace Common.Interfaces
{
public interface IGridTile : IGridSlotState, IDisposable
{
TileGroup Group { get; }

void SetActive(bool value);
void SetWorldPosition(Vector3 worldPosition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public interface IUnityGameBoardRenderer : IGameBoardRenderer

Vector3 GetWorldPosition(GridPosition gridPosition);

bool TrySetNextTileState(GridPosition gridPosition);
TileGroup GetTileGroup(GridPosition gridPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public interface IUnityGridSlot : IGridSlot
bool NotAvailable { get; }

IUnityItem Item { get; }
IGridSlotState State { get; }

void SetItem(IUnityItem item);
void SetState(IGridSlotState state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,13 @@ namespace Common.SpecialItemDetectors
{
public class IceItemDetector : ISpecialItemDetector<IUnityGridSlot>
{
private readonly IUnityGameBoardRenderer _gameBoardRenderer;

public IceItemDetector(IUnityGameBoardRenderer gameBoardRenderer)
{
_gameBoardRenderer = gameBoardRenderer;
}

public IEnumerable<IUnityGridSlot> GetSpecialItemGridSlots(IGameBoard<IUnityGridSlot> gameBoard,
IUnityGridSlot gridSlot)
{
if (_gameBoardRenderer.GetTileGroup(gridSlot.GridPosition) != TileGroup.Ice)
if (gridSlot.State.GroupId == (int) TileGroup.Ice)
{
yield break;
yield return gridSlot;
}

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

yield return gridSlot;
}
}
}
Loading

0 comments on commit 2f21e51

Please sign in to comment.