Skip to content

Commit f05265d

Browse files
committed
feat: added level index
1 parent a783b77 commit f05265d

File tree

10 files changed

+3478
-82
lines changed

10 files changed

+3478
-82
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using Code.LevelEditor;
12
using StaticData;
23

34
namespace Code.Infrastructure.Services.StaticData
45
{
56
public interface IStaticDataService
67
{
7-
void LoadData();
8-
BalanceStaticData Balance { get; }
8+
public void LoadData();
9+
10+
public BalanceStaticData Balance { get; }
11+
public LevelDataDTO GetLevelData(string levelId);
912
}
1013
}
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Code.LevelEditor;
14
using StaticData;
25
using UnityEngine;
36

@@ -6,14 +9,48 @@ namespace Code.Infrastructure.Services.StaticData
69
public class StaticDataService : IStaticDataService
710
{
811
private const string BalanceStaticDataPath = "StaticData/Balance/Balance";
12+
private const string LevelStaticDataPath = "StaticData/LevelsData/";
913

1014
private BalanceStaticData _balanceStaticData;
11-
15+
private List<LevelMatrixEditor> _levelMatrixEditors;
16+
1217
public BalanceStaticData Balance => _balanceStaticData;
1318

1419
public void LoadData()
1520
{
1621
_balanceStaticData = Resources.Load<BalanceStaticData>(BalanceStaticDataPath);
22+
23+
_levelMatrixEditors = Resources.LoadAll<LevelMatrixEditor>(LevelStaticDataPath).ToList();
24+
}
25+
26+
public LevelDataDTO GetLevelData(string levelId)
27+
{
28+
if (_levelMatrixEditors == null || _levelMatrixEditors.Count == 0)
29+
{
30+
Debug.LogWarning("⚠️ Level data not loaded or empty!");
31+
return default;
32+
}
33+
34+
int parsedIndex = ExtractIndexFromId(levelId);
35+
int loopedIndex = parsedIndex % _levelMatrixEditors.Count;
36+
37+
LevelMatrixEditor selectedEditor = _levelMatrixEditors
38+
.OrderBy(e => e.IndexLevel)
39+
.ElementAt(loopedIndex);
40+
41+
return selectedEditor.GetLevelDataDto();
42+
}
43+
44+
private int ExtractIndexFromId(string levelId)
45+
{
46+
if (string.IsNullOrEmpty(levelId))
47+
return 0;
48+
49+
var parts = levelId.Split('_');
50+
if (parts.Length == 2 && int.TryParse(parts[1], out int result))
51+
return result;
52+
53+
return 0;
1754
}
1855
}
1956
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Code.LevelEditor
2+
{
3+
public struct LevelDataDTO
4+
{
5+
public int IndexLevel;
6+
public LevelCell[,] Cells;
7+
8+
public LevelDataDTO(LevelCell[,] cells, int indexLevel)
9+
{
10+
Cells = cells;
11+
IndexLevel = indexLevel;
12+
}
13+
}
14+
}

Assets/Code/LevelEditor/LevelDataDTO.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Code/LevelEditor/LevelHegsagonEditor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace Code.LevelEditor
1010
[CreateAssetMenu(menuName = "StaticData/Levels/LevelData", fileName = "LevelData", order = 801)]
1111
public class LevelHegsagonEditor : BaseLevelDataEditor
1212
{
13+
[HorizontalGroup("Level")]
14+
[OnValueChanged(nameof(ResizeGrid))]
15+
[SerializeField, LabelText("Level Index")]
16+
public int IndexLevel;
17+
1318
[HorizontalGroup("Size")]
1419
[OnValueChanged(nameof(ResizeGrid))]
1520
[SerializeField, LabelText("Width")]

Assets/Code/LevelEditor/LevelMatrixEditor.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ namespace Code.LevelEditor
99
[CreateAssetMenu(menuName = "StaticData/Levels/LevelData", fileName = "LevelData", order = 801)]
1010
public class LevelMatrixEditor : BaseLevelDataEditor
1111
{
12+
[HorizontalGroup("Level")]
13+
[OnValueChanged(nameof(ResizeGrid))]
14+
[SerializeField, LabelText("Level Index")]
15+
public int IndexLevel;
16+
1217
[HorizontalGroup("Size")]
1318
[OnValueChanged(nameof(ResizeGrid))]
1419
[SerializeField, LabelText("Width")]
@@ -28,7 +33,7 @@ public class LevelMatrixEditor : BaseLevelDataEditor
2833
SquareCells = true,
2934
Transpose = true)]
3035
public LevelCell[,] Grid;
31-
36+
3237
#if UNITY_EDITOR
3338

3439
private static BlockLibrary cachedLibrary;
@@ -243,6 +248,12 @@ private void MarkDirty()
243248
public override LevelCell GetCell(Vector2Int pos) => Grid[pos.y, pos.x];
244249
#endif
245250

251+
public LevelDataDTO GetLevelDataDto()
252+
{
253+
var levelData = new LevelDataDTO(Grid,IndexLevel);
254+
return levelData;
255+
}
256+
246257
public IEnumerable<Vector2Int> GetAllOfType(BlockDataEditor type)
247258
{
248259
for (int y = 0; y < Grid.GetLength(1); y++)

Assets/Code/LevelEditor/LevelWindowEditor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ private static void OpenWindow()
2929
[ShowIf(nameof(SelectedLevelEditor))]
3030
public BaseLevelDataEditor SelectedLevelEditor;
3131

32+
[BoxGroup("🟩 Create Level")]
33+
[GUIColor(0.8f, 1f, 0.8f)]
34+
public int NewIndexLevel = 1;
3235
[BoxGroup("🟩 Create Level", centerLabel: true)]
3336
[GUIColor(0.8f, 1f, 0.8f)]
3437
public string NewLevelName = "NewLevel";
@@ -53,6 +56,7 @@ private void CreateNewLevelEditor()
5356
if (SelectedLevelType == LevelType.Matrix)
5457
{
5558
var matrixEditor = CreateInstance<LevelMatrixEditor>();
59+
matrixEditor.IndexLevel = NewIndexLevel;
5660
matrixEditor.Grid = new LevelCell[NewLevelWidth, NewLevelHeight];
5761
for (int y = 0; y < NewLevelHeight; y++)
5862
for (int x = 0; x < NewLevelWidth; x++)
@@ -62,6 +66,7 @@ private void CreateNewLevelEditor()
6266
else if (SelectedLevelType == LevelType.Hexagon)
6367
{
6468
var hexEditor = CreateInstance<LevelHegsagonEditor>();
69+
hexEditor.IndexLevel = NewIndexLevel;
6570
hexEditor.Grid = new LevelCell[NewLevelWidth, NewLevelHeight];
6671
for (int y = 0; y < NewLevelHeight; y++)
6772
for (int x = 0; x < NewLevelWidth; x++)

0 commit comments

Comments
 (0)