Skip to content

Commit 23cbcc3

Browse files
author
JasonNumberThirteen
committed
Optimise changing dimensions of the map
1 parent 1201b0f commit 23cbcc3

5 files changed

Lines changed: 145 additions & 41 deletions

File tree

Assets/Project/Scenes/AStarVisualiserScene.unity

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
public class MapTilesPooler : MonoBehaviour
7+
{
8+
[SerializeField] private MapTile mapTilePrefab;
9+
10+
private readonly List<MapTile> mapTiles = new();
11+
12+
public MapTile GetFirstAvailableMapTile(Transform parentTransform, Action<MapTile> onMapTileWasFound = null)
13+
{
14+
var mapTile = mapTiles.FirstOrDefault(mapTile => !mapTile.gameObject.activeInHierarchy);
15+
16+
if(mapTile != null)
17+
{
18+
SetupMapTile(mapTile, true, parentTransform, onMapTileWasFound);
19+
}
20+
21+
return mapTile;
22+
}
23+
24+
public void ReturnMapTileToPooler(MapTile mapTile, Action<MapTile> onMapTileWasReturned = null)
25+
{
26+
if(mapTile != null)
27+
{
28+
SetupMapTile(mapTile, false, transform, onMapTileWasReturned);
29+
}
30+
}
31+
32+
private void SetupMapTile(MapTile mapTile, bool active, Transform parentTransform, Action<MapTile> action)
33+
{
34+
if(mapTile == null)
35+
{
36+
return;
37+
}
38+
39+
mapTile.gameObject.SetActive(active);
40+
mapTile.transform.SetParent(parentTransform);
41+
action?.Invoke(mapTile);
42+
}
43+
44+
private void Awake()
45+
{
46+
if(mapTilePrefab == null)
47+
{
48+
return;
49+
}
50+
51+
var mapDimensionUpperBound = MapGenerationManager.MAP_DIMENSION_UPPER_BOUND;
52+
var numberOfTiles = mapDimensionUpperBound*mapDimensionUpperBound;
53+
54+
for (var i = 0; i < numberOfTiles; ++i)
55+
{
56+
var instance = Instantiate(mapTilePrefab);
57+
58+
ReturnMapTileToPooler(instance);
59+
mapTiles.Add(instance);
60+
}
61+
}
62+
}

Assets/Project/Scripts/Game Objects/MapTilesPooler.cs.meta

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

Assets/Project/Scripts/Managers/MapGenerationManager.cs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@ public class MapGenerationManager : MonoBehaviour
1313
public UnityEvent<List<MapTile>> mapTilesWereAddedEvent;
1414
public UnityEvent<List<MapTile>> mapTilesWereRemovedEvent;
1515

16-
[SerializeField] private MapTile mapTilePrefab;
1716
[SerializeField] private Transform goParentTransform;
1817

1918
private readonly List<MapTile> mapTiles = new();
2019

21-
private Vector2 mapDimensions;
20+
private Vector2Int mapDimensions;
21+
private MapTilesPooler mapTilesPooler;
2222

2323
public Vector2 GetCenterOfMap() => (GetMapSize() - Vector2.one)*0.5f;
2424
public Vector2 GetMapSize() => GetMapDimensions();
2525
public Vector2 GetMapDimensions() => mapDimensions;
26-
public List<MapTile> GetMapTiles() => mapTiles;
27-
public int GetMaximumMapDimension() => (int)Mathf.Max(mapDimensions.x, mapDimensions.y);
26+
public int GetMaximumMapDimension() => Mathf.Max(mapDimensions.x, mapDimensions.y);
2827

2928
public void ResetTiles()
3029
{
@@ -38,7 +37,7 @@ public void ResetTiles()
3837
mapTiles.ForEach(mapTile => mapTile.ResetTile());
3938
}
4039

41-
public void ChangeMapDimensionsIfNeeded(Vector2 newMapSize)
40+
public void ChangeMapDimensionsIfNeeded(Vector2Int newMapSize)
4241
{
4342
mapDimensions = newMapSize;
4443

@@ -48,25 +47,20 @@ public void ChangeMapDimensionsIfNeeded(Vector2 newMapSize)
4847
EnsureExistanceOfMapTileOfType(MapTileType.Destination, GetMapSize() - Vector2.one);
4948
}
5049

51-
private void RemoveTilesFromShrinkingIfNeeded(Vector2 newMapSize)
50+
private void RemoveTilesFromShrinkingIfNeeded(Vector2Int newMapSize)
5251
{
5352
var mapTilesToRemove = GetMapTilesToRemove(newMapSize);
5453

55-
if(mapTilesToRemove.Count() == 0)
54+
if(mapTilesPooler == null || mapTilesToRemove.Count() == 0)
5655
{
5756
return;
5857
}
5958

60-
mapTilesToRemove.ForEach(mapTile =>
61-
{
62-
mapTiles.Remove(mapTile);
63-
Destroy(mapTile.gameObject);
64-
});
65-
59+
mapTilesToRemove.ForEach(mapTile => mapTilesPooler.ReturnMapTileToPooler(mapTile, mapTile => mapTiles.Remove(mapTile)));
6660
mapTilesWereRemovedEvent?.Invoke(mapTilesToRemove);
6761
}
6862

69-
private void AddTilesFromExtendingIfNeeded(Vector2 newMapSize)
63+
private void AddTilesFromExtendingIfNeeded(Vector2Int newMapSize)
7064
{
7165
var mapTilesToAdd = GetMapTilesToAdd(newMapSize);
7266

@@ -84,41 +78,34 @@ private void AddTilesFromExtendingIfNeeded(Vector2 newMapSize)
8478
mapTilesWereAddedEvent?.Invoke(mapTilesToAdd);
8579
}
8680

87-
private List<MapTile> GetMapTilesToRemove(Vector2 newMapSize)
81+
private List<MapTile> GetMapTilesToRemove(Vector2Int newMapSize)
8882
{
8983
var mapTilesToRemove = new List<MapTile>();
84+
var rectangleArea = new Rect(Vector2.zero, newMapSize);
9085

91-
mapTilesToRemove.AddRange(mapTiles.Where(mapTile =>
92-
{
93-
var mapTileType = mapTile.GetTileType();
94-
var mapTileIsOutsideOfMapWidth = mapTile.transform.position.x < 0 || mapTile.transform.position.x > newMapSize.x - 1;
95-
var mapTileIsOutsideOfMapHeight = mapTile.transform.position.y < 0 || mapTile.transform.position.y > newMapSize.y - 1;
96-
97-
return mapTileIsOutsideOfMapWidth || mapTileIsOutsideOfMapHeight;
98-
}).ToList());
86+
mapTilesToRemove.AddRange(mapTiles.Where(mapTile => !rectangleArea.Contains(mapTile.transform.position)).ToList());
9987

10088
return mapTilesToRemove;
10189
}
10290

103-
private List<MapTile> GetMapTilesToAdd(Vector2 newMapSize)
91+
private List<MapTile> GetMapTilesToAdd(Vector2Int newMapSize)
10492
{
93+
if(mapTilesPooler == null)
94+
{
95+
return new List<MapTile>();
96+
}
97+
10598
var mapTilesToAdd = new List<MapTile>();
106-
var currentMapSize = GetMapSize();
107-
var mapWidthShouldBeExtended = currentMapSize.x < newMapSize.x;
108-
var mapHeightShouldBeExtended = currentMapSize.y < newMapSize.y;
99+
var alreadyTakenPositions = new List<Vector2Int>(mapTiles.Select(mapTile => Vector2Int.RoundToInt(mapTile.GetPosition())));
100+
var allTilesPositions = Enumerable.Range(0, newMapSize.x*newMapSize.y).Select(i => new Vector2Int(i % newMapSize.x, i / newMapSize.x));
101+
var missingTilesPositions = allTilesPositions.Where(position => !alreadyTakenPositions.Contains(position)).ToList();
109102

110-
for (var y = 0; y < newMapSize.y; ++y)
103+
missingTilesPositions.ForEach(position => mapTilesPooler.GetFirstAvailableMapTile(goParentTransform, mapTile =>
111104
{
112-
for (var x = 0; x < newMapSize.x; ++x)
113-
{
114-
var position = new Vector2(x, y);
115-
116-
if(!mapTiles.Any(mapTile => (Vector2)mapTile.transform.position == position))
117-
{
118-
mapTilesToAdd.Add(Instantiate(mapTilePrefab, position, Quaternion.identity, goParentTransform));
119-
}
120-
}
121-
}
105+
mapTile.transform.position = (Vector2)position;
106+
107+
mapTilesToAdd.Add(mapTile);
108+
}));
122109

123110
return mapTilesToAdd;
124111
}
@@ -140,9 +127,16 @@ private void EnsureExistanceOfMapTileOfType(MapTileType mapTileType, Vector2 pos
140127

141128
private void Awake()
142129
{
143-
var initialMapSize = Mathf.Clamp(10, MAP_DIMENSION_LOWER_BOUND, MAP_DIMENSION_UPPER_BOUND);
130+
mapTilesPooler = FindFirstObjectByType<MapTilesPooler>();
131+
132+
GenerateInitialMap(10);
133+
}
134+
135+
private void GenerateInitialMap(int size)
136+
{
137+
var initialMapSize = Mathf.Clamp(size, MAP_DIMENSION_LOWER_BOUND, MAP_DIMENSION_UPPER_BOUND);
144138

145-
ChangeMapDimensionsIfNeeded(Vector2.one*initialMapSize);
139+
ChangeMapDimensionsIfNeeded(Vector2Int.one*initialMapSize);
146140
mapGeneratedEvent?.Invoke();
147141
}
148142
}

Assets/Project/Scripts/UI/Panels/Popup/ChangeMapDimensionsPopupPanelUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void OnChangeDimensionsButtonUIClicked()
8686
var mapWidth = int.TryParse(widthMapDimensionInputFieldUI.text, out var width) ? width : 0;
8787
var mapHeight = int.TryParse(heightMapDimensionInputFieldUI.text, out var height) ? height : 0;
8888

89-
mapGenerationManager.ChangeMapDimensionsIfNeeded(new Vector2(mapWidth, mapHeight));
89+
mapGenerationManager.ChangeMapDimensionsIfNeeded(new Vector2Int(mapWidth, mapHeight));
9090
}
9191

9292
private void OnCancelButtonUIClicked()

0 commit comments

Comments
 (0)