Skip to content

Commit cbad856

Browse files
committed
feat: LoadNextLevel
1 parent a57f6fb commit cbad856

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

Assets/Code/Infrastructure/Generator/ButtonGenerateLevel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void OnDestroy()
3636

3737
private void OnGenerateLevelButtonClick()
3838
{
39-
_levelGeneratorService.GenerateLevel();
39+
_levelGeneratorService.LoadNextLevel();
4040
}
4141
}
4242
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using System;
12
using UnityEngine;
23

34
namespace Code.Infrastructure.Generator.Services
45
{
56
public interface ILevelGeneratorService
67
{
78
void SetUpRootMapHolder(GameObject rootMapHolder);
8-
void CleanUp();
9+
void CleanUp(Action onComplete = null);
910
void GenerateLevel();
11+
void LoadNextLevel();
1012
}
1113
}

Assets/Code/Infrastructure/Generator/Services/LevelGeneratorService.cs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Code.Infrastructure.StaticData;
44
using Code.LevelEditor;
55
using DG.Tweening;
6+
using System;
67
using UnityEngine;
78

89
namespace Code.Infrastructure.Generator.Services
@@ -12,39 +13,67 @@ public class LevelGeneratorService : ILevelGeneratorService
1213
private Tile[,] _tileMatrix;
1314
private GameObject _rootMapHolder;
1415
private int _currentLevelIndex = 1;
15-
16+
1617
private readonly IStaticDataService _staticDataService;
1718
private readonly ITileFactory _tileFactory;
1819
private readonly IBlockFactory _blockFactory;
1920

2021
public LevelGeneratorService(
21-
IStaticDataService staticDataService,
22+
IStaticDataService staticDataService,
2223
ITileFactory tileFactory,
2324
IBlockFactory blockFactory)
2425
{
2526
_staticDataService = staticDataService;
2627
_tileFactory = tileFactory;
2728
_blockFactory = blockFactory;
2829
}
29-
30+
3031
public void SetUpRootMapHolder(GameObject rootMapHolder)
3132
{
3233
_rootMapHolder = rootMapHolder;
3334
}
3435

35-
public void CleanUp()
36+
public void CleanUp(Action onComplete = null)
3637
{
37-
if (_tileMatrix == null) return;
38+
if (_tileMatrix == null)
39+
{
40+
onComplete?.Invoke();
41+
return;
42+
}
43+
44+
int width = _tileMatrix.GetLength(0);
45+
int height = _tileMatrix.GetLength(1);
46+
float baseDelay = 0.01f;
47+
int counter = 0;
48+
int total = width * height;
3849

39-
foreach (var tile in _tileMatrix)
50+
for (int y = height - 1; y >= 0; y--)
4051
{
41-
if (tile != null)
52+
for (int x = width - 1; x >= 0; x--)
4253
{
43-
UnityEngine.Object.Destroy(tile.gameObject);
54+
Tile tile = _tileMatrix[x, y];
55+
if (tile == null)
56+
{
57+
counter++;
58+
continue;
59+
}
60+
61+
float delay = ((width - x) + (height - y)) * baseDelay;
62+
DOVirtual.DelayedCall(delay, () =>
63+
{
64+
tile.PlayAnimationHideTile(() =>
65+
{
66+
UnityEngine.Object.Destroy(tile.gameObject);
67+
counter++;
68+
if (counter >= total)
69+
{
70+
_tileMatrix = null;
71+
onComplete?.Invoke();
72+
}
73+
});
74+
});
4475
}
4576
}
46-
47-
_tileMatrix = null;
4877
}
4978

5079
public void GenerateLevel()
@@ -96,5 +125,10 @@ public void GenerateLevel()
96125
}
97126
}
98127

128+
public void LoadNextLevel()
129+
{
130+
CleanUp(() => GenerateLevel());
131+
_currentLevelIndex++;
132+
}
99133
}
100-
}
134+
}

Assets/Code/Infrastructure/Services/StaticData/StaticDataService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public LevelDataDTO GetLevelData(string levelId)
4343

4444
private int ExtractIndexFromId(string levelId)
4545
{
46-
if (string.IsNullOrEmpty(levelId))
47-
return 0;
48-
46+
if (int.TryParse(levelId, out int result))
47+
return result;
48+
4949
var parts = levelId.Split('_');
50-
if (parts.Length == 2 && int.TryParse(parts[1], out int result))
50+
if (parts.Length == 2 && int.TryParse(parts[1], out result))
5151
return result;
5252

5353
return 0;

0 commit comments

Comments
 (0)