1
|
using System;
|
2
|
using System.Collections;
|
3
|
using System.Collections.Generic;
|
4
|
using UnityEngine;
|
5
|
|
6
|
public class MapRenderer : MonoBehaviour
|
7
|
{
|
8
|
[SerializeField]
|
9
|
public SceneManager sceneManager;
|
10
|
|
11
|
[SerializeField]
|
12
|
private SpriteRenderer underlayTemplate;
|
13
|
|
14
|
[SerializeField]
|
15
|
private SpriteRenderer overlayTemplate;
|
16
|
|
17
|
[SerializeField]
|
18
|
public bool interactable = true;
|
19
|
|
20
|
public Color minColor;
|
21
|
|
22
|
public Color maxColor;
|
23
|
|
24
|
|
25
|
private SpriteRenderer underlay;
|
26
|
|
27
|
private Dictionary<string, SpriteRenderer> overlay = new Dictionary<string, SpriteRenderer>();
|
28
|
|
29
|
|
30
|
// Start is called before the first frame update
|
31
|
void Start()
|
32
|
{
|
33
|
Load();
|
34
|
interactable = true;
|
35
|
}
|
36
|
|
37
|
/// <summary>
|
38
|
/// Loads map resources from an annotated directory
|
39
|
/// A "layout.txt" file with directory annotation is required
|
40
|
/// Creates the underlay map and building overlays addressable by their name as per the layout file
|
41
|
/// </summary>
|
42
|
/// <param name="sourceDirectory">Path to the source directory</param>
|
43
|
public void Load()
|
44
|
{
|
45
|
// Reset map to clear state
|
46
|
Reset();
|
47
|
|
48
|
// Get source directory
|
49
|
string sourceDirectory = sceneManager.mapSourceDirectory;
|
50
|
|
51
|
// Load directory layout
|
52
|
// Read filenames
|
53
|
// First filename is the underlay
|
54
|
// All the others are the overlays, names as their respective building codes
|
55
|
TextAsset layout = Resources.Load<TextAsset>($"{sourceDirectory}/layout");
|
56
|
|
57
|
if (layout == null)
|
58
|
{
|
59
|
Debug.Log($"Layout file missing at {sourceDirectory}/layout.txt. Returning.");
|
60
|
return;
|
61
|
}
|
62
|
|
63
|
string[] filenames = layout.ToString().Split('\n');
|
64
|
|
65
|
// A map must contain at least an underlay
|
66
|
if (filenames.Length < 1)
|
67
|
{
|
68
|
Debug.Log("Attempted to load an empty map. Returning.");
|
69
|
return;
|
70
|
}
|
71
|
|
72
|
// Trim filenames (line breaks), remove empty line breaks and comments
|
73
|
List<string> filenamesTMP = new List<string>();
|
74
|
|
75
|
for (int i = 0; i < filenames.Length; i++)
|
76
|
{
|
77
|
filenames[i] = filenames[i].Trim();
|
78
|
if (filenames[i].Length != 0 && !filenames[i].StartsWith("//"))
|
79
|
filenamesTMP.Add(filenames[i]);
|
80
|
}
|
81
|
|
82
|
filenames = filenamesTMP.ToArray();
|
83
|
|
84
|
// Load underlay sprite
|
85
|
Sprite underlaySprite = Resources.Load<Sprite>($"{sourceDirectory}/{filenames[0]}");
|
86
|
|
87
|
if (underlaySprite == null)
|
88
|
{
|
89
|
Debug.LogError($"Underlay sprite does not exist at path \"{sourceDirectory}/{filenames[0]}.png\". Returning.");
|
90
|
return;
|
91
|
}
|
92
|
|
93
|
// Load overlay sprites
|
94
|
Sprite[] overlaySprites = new Sprite[filenames.Length - 1];
|
95
|
|
96
|
for (int i = 1; i < filenames.Length; i++)
|
97
|
{
|
98
|
overlaySprites[i-1] = Resources.Load<Sprite>($"{sourceDirectory}/{filenames[i]}");
|
99
|
|
100
|
if (overlaySprites[i - 1] == null)
|
101
|
{
|
102
|
Debug.LogError($"Overlay sprite does not exist at path \"{sourceDirectory}/{filenames[i]}.png\". Returning.");
|
103
|
return;
|
104
|
}
|
105
|
}
|
106
|
|
107
|
// Create game objects
|
108
|
underlay = Instantiate(underlayTemplate, transform);
|
109
|
underlay.name = "Underlay";
|
110
|
underlay.sprite = underlaySprite;
|
111
|
|
112
|
for (int i = 1; i < filenames.Length; i++)
|
113
|
{
|
114
|
overlay[filenames[i]] = Instantiate(overlayTemplate, transform);
|
115
|
overlay[filenames[i]].name = filenames[i];
|
116
|
overlay[filenames[i]].sprite = overlaySprites[i - 1];
|
117
|
overlay[filenames[i]].sortingOrder = i;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
public void Animate(float value)
|
122
|
{
|
123
|
foreach (var sprite in overlay)
|
124
|
{
|
125
|
sprite.Value.color = Color.Lerp(minColor, maxColor, value);
|
126
|
}
|
127
|
}
|
128
|
|
129
|
/// <summary>
|
130
|
/// Resets the map to original pre-loaded state by clearing the transform and destroying all child objects
|
131
|
/// Clears the child object dictonary
|
132
|
/// </summary>
|
133
|
private void Reset()
|
134
|
{
|
135
|
// Reset scale and position
|
136
|
transform.position = new Vector3(0, 0, 0);
|
137
|
transform.localScale = new Vector3(1, 1, 1);
|
138
|
|
139
|
// Destroy children
|
140
|
if (underlay != null && underlay.gameObject != null)
|
141
|
Destroy(underlay.gameObject);
|
142
|
|
143
|
foreach (var go in overlay)
|
144
|
if (go.Value != null && go.Value.gameObject != null)
|
145
|
Destroy(go.Value.gameObject);
|
146
|
|
147
|
// Clear overlays
|
148
|
overlay.Clear();
|
149
|
}
|
150
|
|
151
|
}
|