Skip to content

Commit 9dab7d9

Browse files
Adjust map tile path trail indicators for taking a map screenshot
1 parent b55d037 commit 9dab7d9

8 files changed

Lines changed: 185 additions & 14 deletions

File tree

Assets/Project/Scenes/VisualiserScene.unity

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

Assets/Project/Scripts/Game Objects/Indicators/MapTilePathTrailIndicator.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
using DG.Tweening;
22
using UnityEngine;
33

4-
public class MapTilePathTrailIndicator : MonoBehaviour
4+
public class MapTilePathTrailIndicator : MonoBehaviour, IMapScreenshotTakerElement
55
{
66
[SerializeField, Min(0f)] private float movementPeriodOfOscillation = 3f;
77
[SerializeField, Range(0f, 0.5f)] private float movementDistanceFromCenterOfTile = 0.125f;
88

99
private Tween movementTween;
10+
private Vector2 lastLocalPosition;
11+
12+
public void AdjustForTakingMapScreenshot(bool started)
13+
{
14+
if(started)
15+
{
16+
movementTween?.Pause();
17+
18+
lastLocalPosition = transform.localPosition;
19+
transform.localPosition = Vector2.zero;
20+
}
21+
else
22+
{
23+
transform.localPosition = lastLocalPosition;
24+
25+
movementTween?.Play();
26+
}
27+
}
1028

1129
public void Setup(MapTileNode currentMapTileNode, MapTileNode nextMapTileNode)
1230
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Linq;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class MapScreenshotTakerElementsManager : MonoBehaviour
6+
{
7+
private MapScreenshotTaker mapScreenshotTaker;
8+
private MapTilesPathTrailManager mapTilesPathTrailManager;
9+
10+
private readonly List<IMapScreenshotTakerElement> mapScreenshotTakerElements = new();
11+
12+
private void Awake()
13+
{
14+
mapScreenshotTaker = ObjectMethods.FindComponentOfType<MapScreenshotTaker>();
15+
mapTilesPathTrailManager = ObjectMethods.FindComponentOfType<MapTilesPathTrailManager>();
16+
17+
RegisterToListeners(true);
18+
}
19+
20+
private void OnDestroy()
21+
{
22+
RegisterToListeners(false);
23+
}
24+
25+
private void RegisterToListeners(bool register)
26+
{
27+
if(register)
28+
{
29+
if(mapScreenshotTaker != null)
30+
{
31+
mapScreenshotTaker.takingScreenshotStateWasChangedEvent.AddListener(AdjustAllElementsForTakingMapScreenshot);
32+
}
33+
34+
if(mapTilesPathTrailManager != null)
35+
{
36+
mapTilesPathTrailManager.indicatorsWereAddedEvent.AddListener(OnIndicatorsWereAdded);
37+
mapTilesPathTrailManager.indicatorsWereRemovedEvent.AddListener(OnIndicatorsWereRemoved);
38+
}
39+
}
40+
else
41+
{
42+
if(mapScreenshotTaker != null)
43+
{
44+
mapScreenshotTaker.takingScreenshotStateWasChangedEvent.RemoveListener(AdjustAllElementsForTakingMapScreenshot);
45+
}
46+
47+
if(mapTilesPathTrailManager != null)
48+
{
49+
mapTilesPathTrailManager.indicatorsWereAddedEvent.RemoveListener(OnIndicatorsWereAdded);
50+
mapTilesPathTrailManager.indicatorsWereRemovedEvent.RemoveListener(OnIndicatorsWereRemoved);
51+
}
52+
}
53+
}
54+
55+
private void AdjustAllElementsForTakingMapScreenshot(bool started)
56+
{
57+
mapScreenshotTakerElements.ForEach(mapScreenshotTakerElement => mapScreenshotTakerElement.AdjustForTakingMapScreenshot(started));
58+
}
59+
60+
private void OnIndicatorsWereAdded(List<MapTilePathTrailIndicator> mapTilePathTrailIndicators)
61+
{
62+
mapScreenshotTakerElements.AddRange(GetElementsFrom(mapTilePathTrailIndicators));
63+
}
64+
65+
private void OnIndicatorsWereRemoved(List<MapTilePathTrailIndicator> mapTilePathTrailIndicators)
66+
{
67+
var elementsFromMapTilePathTrailIndicators = GetElementsFrom(mapTilePathTrailIndicators);
68+
69+
mapScreenshotTakerElements.RemoveAll(elementsFromMapTilePathTrailIndicators.Contains);
70+
}
71+
72+
private IEnumerable<IMapScreenshotTakerElement> GetElementsFrom<T>(List<T> components) => components.OfType<IMapScreenshotTakerElement>();
73+
}

Assets/Project/Scripts/Game Objects/Managers/MapScreenshotTakerElementsManager.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/Game Objects/Managers/Pathfinding/MapTilesPathTrailManager.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
using System.Collections.Generic;
22
using UnityEngine;
3+
using UnityEngine.Events;
34

45
public class MapTilesPathTrailManager : MonoBehaviour
56
{
7+
public UnityEvent<List<MapTilePathTrailIndicator>> indicatorsWereAddedEvent;
8+
public UnityEvent<List<MapTilePathTrailIndicator>> indicatorsWereRemovedEvent;
9+
610
[SerializeField] private MapTilePathTrailIndicator mapTilePathTrailIndicatorPrefab;
711

812
private bool pathTrailIsEnabled;
913
private MapPathManager mapPathManager;
1014

11-
private readonly List<MapTilePathTrailIndicator> mapTilePathTrailIndicators = new();
15+
private readonly List<MapTilePathTrailIndicator> allMapTilePathTrailIndicators = new();
16+
private readonly List<MapTilePathTrailIndicator> modifiedMapTilePathTrailIndicators = new();
1217

1318
public void SetPathTrailEnabled(bool enabled)
1419
{
1520
pathTrailIsEnabled = enabled;
1621

17-
mapTilePathTrailIndicators.ForEach(mapTilePathTrailIndicator => mapTilePathTrailIndicator.SetActive(pathTrailIsEnabled));
22+
allMapTilePathTrailIndicators.ForEach(mapTilePathTrailIndicator => mapTilePathTrailIndicator.SetActive(pathTrailIsEnabled));
1823
}
1924

2025
private void Awake()
@@ -51,12 +56,16 @@ private void RegisterToListeners(bool register)
5156

5257
private void OnPathWasFound(List<MapTileNode> mapTileNodes)
5358
{
59+
modifiedMapTilePathTrailIndicators.Clear();
5460
mapTileNodes.ForEachReversed(CreateMapTilePathTrailIndicator);
61+
indicatorsWereAddedEvent?.Invoke(modifiedMapTilePathTrailIndicators);
5562
}
5663

5764
private void OnResultsWereCleared()
5865
{
59-
mapTilePathTrailIndicators.ForEachReversed(RemoveMapTilePathTrailIndicator);
66+
modifiedMapTilePathTrailIndicators.Clear();
67+
allMapTilePathTrailIndicators.ForEachReversed(RemoveMapTilePathTrailIndicator);
68+
indicatorsWereRemovedEvent?.Invoke(modifiedMapTilePathTrailIndicators);
6069
}
6170

6271
private void CreateMapTilePathTrailIndicator(MapTileNode currentMapTileNode, MapTileNode nextMapTileNode)
@@ -70,12 +79,24 @@ private void CreateMapTilePathTrailIndicator(MapTileNode currentMapTileNode, Map
7079

7180
mapTilePathTrailIndicator.Setup(currentMapTileNode, nextMapTileNode);
7281
mapTilePathTrailIndicator.SetActive(pathTrailIsEnabled);
73-
mapTilePathTrailIndicators.Add(mapTilePathTrailIndicator);
82+
AddIndicator(mapTilePathTrailIndicator);
7483
}
7584

7685
private void RemoveMapTilePathTrailIndicator(MapTilePathTrailIndicator mapTilePathTrailIndicator)
7786
{
78-
mapTilePathTrailIndicators.Remove(mapTilePathTrailIndicator);
87+
RemoveIndicator(mapTilePathTrailIndicator);
7988
Destroy(mapTilePathTrailIndicator.gameObject);
8089
}
90+
91+
private void AddIndicator(MapTilePathTrailIndicator mapTilePathTrailIndicator)
92+
{
93+
allMapTilePathTrailIndicators.Add(mapTilePathTrailIndicator);
94+
modifiedMapTilePathTrailIndicators.Add(mapTilePathTrailIndicator);
95+
}
96+
97+
private void RemoveIndicator(MapTilePathTrailIndicator mapTilePathTrailIndicator)
98+
{
99+
allMapTilePathTrailIndicators.Remove(mapTilePathTrailIndicator);
100+
modifiedMapTilePathTrailIndicators.Add(mapTilePathTrailIndicator);
101+
}
81102
}

Assets/Project/Scripts/Game Objects/MapScreenshotTaker.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public class MapScreenshotTaker : MonoBehaviour
77
{
8+
public UnityEvent<bool> takingScreenshotStateWasChangedEvent;
89
public UnityEvent<string> screenshotWasTakenEvent;
910

1011
private MapScreenshotSceneCamera mapScreenshotSceneCamera;
@@ -16,14 +17,16 @@ public class MapScreenshotTaker : MonoBehaviour
1617
#endif
1718
private static readonly int RENDER_TEXTURE_DEPTH = 24;
1819
private static readonly TextureFormat RENDER_TEXTURE_FORMAT = TextureFormat.RGB24;
19-
20+
2021
public void TakeMapScreenshot()
2122
{
2223
var directoryPathFolder = GetDirectoryPathFolder();
2324
var directoryPath = $"{directoryPathFolder}/{GetDirectoryName()}";
2425

26+
takingScreenshotStateWasChangedEvent?.Invoke(true);
2527
EnsureExistanceOfDirectory(directoryPath);
2628
TakeScreenshotInDirectory(directoryPath, directoryPathFolder);
29+
takingScreenshotStateWasChangedEvent?.Invoke(false);
2730
}
2831

2932
private void Awake()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IMapScreenshotTakerElement
2+
{
3+
void AdjustForTakingMapScreenshot(bool started);
4+
}

Assets/Project/Scripts/Interfaces/IMapScreenshotTakerElement.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.

0 commit comments

Comments
 (0)