-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapRenderer.cs
92 lines (77 loc) · 3.13 KB
/
MapRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using UnityEngine;
using System.Collections;
namespace Grout
{
public class MapRenderer
{
private Map map;
private GameObject Container;
private GameObject[][][] rendered;
private bool hasBeenRendered = false;
public MapRenderer(Map map) {
this.map = map;
rendered = new GameObject[map.Layers.Count][][];
for (int i = 0; i < map.Layers.Count; i++) {
rendered[i] = new GameObject[map.SizeX][];
for(int j = 0; j < map.SizeX; j++) {
rendered[i][j] = new GameObject[map.SizeY];
}
}
map.OnTileUpdate += Update;
map.OnMapResize += OnMapResize;
}
public void OnMapResize() {
rendered = new GameObject[map.Layers.Count][][];
for (int i = 0; i < map.Layers.Count; i++) {
rendered[i] = new GameObject[map.SizeX][];
for(int j = 0; j < map.SizeX; j++) {
rendered[i][j] = new GameObject[map.SizeY];
}
}
if (hasBeenRendered) {
Cleanup();
Render();
}
}
public void Render() {
Container = CreateContainer();
for(int l = 0; l < map.Layers.Count; l++) {
for(int i = 0; i < map.SizeX; i++) {
for(int j = 0; j < map.SizeY; j++) {
MapTile tile = map.Layers[l].Tiles[i][j];
if (tile == null || tile.Tile == null) continue;
GameObject go = GameObject.Instantiate(tile.Tile.Renderable);
go.transform.SetParent(Container.transform);
go.transform.localRotation = Quaternion.Euler(0, 90 * tile.Rotation, 0);
go.transform.localPosition = new Vector3(i * map.TileScale, 0, j * map.TileScale);
rendered[l][i][j] = go;
}
}
}
hasBeenRendered = true;
}
public void Update(MapTile tile, int layer, int x, int y) {
if (Container == null) {
Debug.LogWarning("Tried to update but container was not present, aborting...");
return;
}
if (rendered[layer][x][y] != null) {
GameObject.DestroyImmediate(rendered[layer][x][y]);
}
if (tile == null || tile.Tile == null) return;
GameObject go = GameObject.Instantiate(tile.Tile.Renderable);
go.transform.SetParent(Container.transform);
go.transform.localRotation = Quaternion.Euler(0, 90 * tile.Rotation, 0);
go.transform.localPosition = new Vector3(x * map.TileScale, 0, y * map.TileScale);
rendered[layer][x][y] = go;
}
public void Cleanup() {
GameObject.DestroyImmediate(Container);
map.OnTileUpdate -= Update;
}
public virtual GameObject CreateContainer()
{
return new GameObject(map.name);
}
}
}