-
Notifications
You must be signed in to change notification settings - Fork 36
/
LDtkComponentLayerIntGridValues.cs
140 lines (126 loc) · 5.47 KB
/
LDtkComponentLayerIntGridValues.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
namespace LDtkUnity
{
/// <summary>
/// Use for checking IntGrid values. The coordinates are usable for it's respective tilemaps. Accessible from IntGrid layer GameObjects.
/// </summary>
[HelpURL(LDtkHelpURL.COMPONENT_LAYER_INTGRID)]
[AddComponentMenu("")]
public sealed class LDtkComponentLayerIntGridValues : MonoBehaviour
{
[SerializeField] internal IntGridValuePositions[] _values;
private Dictionary<Vector3Int, LDtkDefinitionObjectIntGridValue> _valueOfPositionDict;
private Dictionary<int, Vector3Int[]> _positionsOfValueDict;
[Serializable]
internal class IntGridValuePositions
{
public LDtkDefinitionObjectIntGridValue _value;
public List<Vector3Int> _positions = new List<Vector3Int>();
public IntGridValuePositions(LDtkDefinitionObjectIntGridValue value)
{
_value = value;
}
}
internal void OnImport(LDtkDefinitionObjectsCache cache, LayerInstance instance)
{
//note: this could be optimized by not making a dictionary and adding data straight to the list instead
Dictionary<int, IntGridValuePositions> valuePositions = new Dictionary<int, IntGridValuePositions>(instance.CWid * instance.CHei);
int index = 0;
Dictionary<int, IntGridValueDefinition> defs = new Dictionary<int, IntGridValueDefinition>(instance.Definition.IntGridValues.Length);
foreach (IntGridValueDefinition def in instance.Definition.IntGridValues)
{
defs.Add(def.Value, def);
}
Vector3Int coord = new Vector3Int(0, 0, 0);
for (int y = 0; y < instance.CHei; y++)
{
coord.y = LDtkCoordConverter.ConvertCell(y, instance.CHei);
for (int x = 0; x < instance.CWid; x++)
{
coord.x = x;
int intGridValue = instance.IntGridCsv[index];
if (intGridValue != 0)
{
if (!valuePositions.ContainsKey(intGridValue))
{
LDtkDefinitionObjectIntGridValue value = new LDtkDefinitionObjectIntGridValue();
value.Populate(cache, defs[intGridValue]);
valuePositions.Add(intGridValue, new IntGridValuePositions(value));
}
valuePositions[intGridValue]._positions.Add(coord);
}
index++;
}
}
_values = valuePositions.Select(pair => pair.Value).ToArray();
}
/// <summary>
/// Get a IntGridValue tile at the coordinate for this layer. Returns 0 if a value at the coord doesn't exist. (empty tiles are an intgrid value of 0)
/// </summary>
[PublicAPI]
public int GetValue(Vector3Int coord)
{
LDtkDefinitionObjectIntGridValue def = GetValueDefinition(coord);
return def != null ? def.Value : 0;
}
/// <summary>
/// Get a IntGridValue tile at the coordinate for this layer. Returns null if a value at the coord doesn't exist.
/// </summary>
[PublicAPI]
public LDtkDefinitionObjectIntGridValue GetValueDefinition(Vector3Int coord)
{
TryCacheValueOfPositionDict();
return _valueOfPositionDict.TryGetValue(coord, out LDtkDefinitionObjectIntGridValue def) ? def : null;
}
private void TryCacheValueOfPositionDict()
{
if (_valueOfPositionDict != null)
{
return;
}
_valueOfPositionDict = new Dictionary<Vector3Int, LDtkDefinitionObjectIntGridValue>(_values.Sum(p => p._positions.Count));
foreach (IntGridValuePositions value in _values)
{
foreach (Vector3Int position in value._positions)
{
if (!_valueOfPositionDict.ContainsKey(position))
{
_valueOfPositionDict.Add(position, value._value);
continue;
}
GameObject obj = gameObject;
LDtkDebug.LogWarning($"Duplicate entry found for {obj.name}, ", obj);
}
}
}
/// <summary>
/// Get all Tilemap coordinates of a specific IntGrid value.
/// </summary>
[PublicAPI]
public Vector3Int[] GetPositionsOfValueDefinition(LDtkDefinitionObjectIntGridValue value)
{
if (value == null)
{
LDtkDebug.LogError("Argument null when trying to get IntGrid positions", gameObject);
return null;
}
return GetPositionsOfValue(value.Value);
}
/// <summary>
/// Get all Tilemap coordinates of a specific IntGrid value.
/// </summary>
[PublicAPI]
public Vector3Int[] GetPositionsOfValue(int value)
{
if (_positionsOfValueDict == null)
{
_positionsOfValueDict = _values.ToDictionary(obj => obj._value.Value, obj => obj._positions.ToArray());
}
return _positionsOfValueDict[value];
}
}
}