Skip to content

Commit 6db69d8

Browse files
committed
Added UnityItemGenerator.
1 parent c35de9e commit 6db69d8

File tree

9 files changed

+82
-144
lines changed

9 files changed

+82
-144
lines changed

Assets/Plugins/Match3/Match3.Core.dll

512 Bytes
Binary file not shown.
Binary file not shown.

Assets/Scenes/MainScene.unity

Lines changed: 1 addition & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/AppContext.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212
using Match3.UnityApp;
1313
using Match3.UnityApp.Interfaces;
1414
using UnityEngine;
15-
using IItemGenerator = Common.Interfaces.IItemGenerator;
1615

1716
public class AppContext : MonoBehaviour, IAppContext
1817
{
1918
[SerializeField] private GameUiCanvas _gameUiCanvas;
20-
[SerializeField] private ItemGenerator _itemGenerator;
2119
[SerializeField] private CanvasInputSystem _inputSystem;
2220
[SerializeField] private UnityGameBoardRenderer _gameBoardRenderer;
2321

2422
[Space]
25-
[SerializeField] private IconsSetModel[] _iconSets;
23+
[SerializeField] private GameObject _itemPrefab;
2624

27-
private Dictionary<Type, object> _registeredTypes;
25+
[Space]
26+
[SerializeField] private IconsSetModel[] _iconSets;
2827

2928
public void Construct()
3029
{
@@ -33,14 +32,15 @@ public void Construct()
3332
RegisterInstance<IInputSystem>(_inputSystem);
3433
RegisterInstance<IconsSetModel[]>(_iconSets);
3534
RegisterInstance<IGameUiCanvas>(_gameUiCanvas);
36-
RegisterInstance<IItemGenerator>(_itemGenerator);
37-
RegisterInstance<IItemsPool<IUnityItem>>(_itemGenerator);
38-
RegisterInstance<IUnityGameBoardRenderer>(_gameBoardRenderer);
39-
RegisterInstance<IGameBoardDataProvider<IUnityGridSlot>>(_gameBoardRenderer);
35+
RegisterInstance<IUnityGameBoardRenderer, IGameBoardDataProvider<IUnityGridSlot>>(_gameBoardRenderer);
36+
4037
RegisterInstance<UnityGame>(GetUnityGame());
38+
RegisterInstance<IUnityItemGenerator, IItemsPool<IUnityItem>>(GetItemGenerator());
4139
RegisterInstance<IBoardFillStrategy<IUnityGridSlot>[]>(GetBoardFillStrategies());
4240
}
4341

42+
private Dictionary<Type, object> _registeredTypes;
43+
4444
public T Resolve<T>()
4545
{
4646
return (T) _registeredTypes[typeof(T)];
@@ -51,6 +51,12 @@ private void RegisterInstance<T>(T instance)
5151
_registeredTypes.Add(typeof(T), instance);
5252
}
5353

54+
private void RegisterInstance<T1, T2>(object instance)
55+
{
56+
_registeredTypes.Add(typeof(T1), instance);
57+
_registeredTypes.Add(typeof(T2), instance);
58+
}
59+
5460
private UnityGame GetUnityGame()
5561
{
5662
var gameConfig = new GameConfig<IUnityGridSlot>
@@ -65,6 +71,11 @@ private UnityGame GetUnityGame()
6571
return new UnityGame(_inputSystem, _gameBoardRenderer, gameConfig);
6672
}
6773

74+
private UnityItemGenerator GetItemGenerator()
75+
{
76+
return new UnityItemGenerator(_itemPrefab, new GameObject("ItemsPool").transform);
77+
}
78+
6879
private IGameBoardSolver<IUnityGridSlot> GetGameBoardSolver()
6980
{
7081
return new GameBoardSolver<IUnityGridSlot>(new ISequenceDetector<IUnityGridSlot>[]

Assets/Scripts/Common/GameModes/GameInitMode.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Match3.Core.Interfaces;
55
using Match3.Infrastructure.Interfaces;
66
using UnityEngine;
7-
using IItemGenerator = Common.Interfaces.IItemGenerator;
87

98
namespace Common.GameModes
109
{
@@ -14,7 +13,7 @@ public class GameInitMode : IGameMode, IDisposable
1413
private readonly IAppContext _appContext;
1514
private readonly IconsSetModel[] _iconSets;
1615
private readonly IGameUiCanvas _gameUiCanvas;
17-
private readonly IItemGenerator _itemGenerator;
16+
private readonly IUnityItemGenerator _itemGenerator;
1817

1918
private bool _isInitialized;
2019

@@ -24,7 +23,7 @@ public GameInitMode(IAppContext appContext)
2423
_unityGame = appContext.Resolve<UnityGame>();
2524
_iconSets = appContext.Resolve<IconsSetModel[]>();
2625
_gameUiCanvas = appContext.Resolve<IGameUiCanvas>();
27-
_itemGenerator = appContext.Resolve<IItemGenerator>();
26+
_itemGenerator = appContext.Resolve<IUnityItemGenerator>();
2827
}
2928

3029
public event EventHandler Finished;
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
1+
using Match3.Infrastructure.Interfaces;
22
using UnityEngine;
33

44
namespace Common.Interfaces
55
{
6-
public interface IItemGenerator : IDisposable
6+
public interface IUnityItemGenerator : IItemGenerator
77
{
8-
void CreateItems(int capacity);
98
void SetSprites(Sprite[] sprites);
109
}
1110
}

Assets/Scripts/Common/ItemGenerator.cs

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Common.Extensions;
2+
using Common.Interfaces;
3+
using Match3.Infrastructure;
4+
using UnityEngine;
5+
using Random = System.Random;
6+
7+
namespace Common
8+
{
9+
public class UnityItemGenerator : ItemGenerator<IUnityItem>, IUnityItemGenerator
10+
{
11+
private readonly Random _random;
12+
private readonly Transform _container;
13+
private readonly GameObject _itemPrefab;
14+
15+
private Sprite[] _sprites;
16+
17+
public UnityItemGenerator(GameObject itemPrefab, Transform container)
18+
{
19+
_random = new Random();
20+
_container = container;
21+
_itemPrefab = itemPrefab;
22+
}
23+
24+
public void SetSprites(Sprite[] sprites)
25+
{
26+
_sprites = sprites;
27+
}
28+
29+
public override void Dispose()
30+
{
31+
if (ItemsPool != null)
32+
{
33+
foreach (var item in ItemsPool)
34+
{
35+
item.Dispose();
36+
}
37+
}
38+
39+
base.Dispose();
40+
}
41+
42+
protected override IUnityItem CreateItem()
43+
{
44+
var item = _itemPrefab.CreateNew<IUnityItem>(parent: _container);
45+
item.Hide();
46+
47+
return item;
48+
}
49+
50+
protected override IUnityItem ConfigureItem(IUnityItem item)
51+
{
52+
var index = _random.Next(0, _sprites.Length);
53+
item.SetSprite(index, _sprites[index]);
54+
55+
return item;
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)