@@ -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}
0 commit comments