-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathLDtkBuilderEntity.cs
More file actions
177 lines (144 loc) · 6.7 KB
/
LDtkBuilderEntity.cs
File metadata and controls
177 lines (144 loc) · 6.7 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using UnityEngine;
namespace LDtkUnity.Editor
{
internal sealed class LDtkBuilderEntity : LDtkBuilderLayer
{
private readonly WorldLayout _layout;
private readonly LDtkAssetProcessorActionCache _assetProcess;
private readonly LDtkLinearLevelVector _linearVector;
private EntityInstance _entity;
private GameObject _entityObj;
private LDtkComponentEntity _entityComponent;
private LDtkFields _fieldsComponent;
private LDtkIid _iidComponent;
private LDtkFieldsFactory _fieldsFactory;
public LDtkBuilderEntity(LDtkProjectImporter project, Level level, LDtkComponentLayer layerComponent, LDtkSortingOrder sortingOrder, LDtkLinearLevelVector linearVector, WorldLayout layout, LDtkAssetProcessorActionCache assetProcess, LDtkJsonImporter importer)
: base(project, level, layerComponent, sortingOrder, importer)
{
_linearVector = linearVector;
_layout = layout;
_assetProcess = assetProcess;
}
public LDtkComponentEntity[] BuildEntityLayerInstances()
{
SortingOrder.Next();
LDtkFieldParser.CacheRecentBuilder(this);
var entities = new LDtkComponentEntity[Layer.EntityInstances.Length];
for (int i = 0; i < Layer.EntityInstances.Length; i++)
{
_entity = Layer.EntityInstances[i];
LDtkProfiler.BeginSample($"BuildEntityInstance {_entity.Identifier}");
BuildEntityInstance();
LDtkProfiler.EndSample();
entities[i] = _entityComponent;
}
return entities;
}
private void BuildEntityInstance()
{
CreateEntityInstance();
// Reason to give them unique names is to add them to the importer correctly. The importer requires unique identifiers
_entityObj.name = $"{_entity.Identifier}_{_entity.Iid}";
AddEntityComponent();
AddIidComponent();
PositionEntity();
LDtkProfiler.BeginSample("AddFieldData");
AddFieldData();
LDtkProfiler.EndSample();
PopulateEntityComponent();
ScaleEntity();
}
private void PopulateEntityComponent()
{
Vector2 size = (Vector2)_entity.UnityPxSize / Project.PixelsPerUnit;
_entityComponent.OnImport(Importer.DefinitionObjects, _entity, LayerComponent, _fieldsComponent, _iidComponent, size);
}
private void CreateEntityInstance()
{
GameObject entityPrefab = Project.GetEntity(_entity.Identifier);
_entityObj = entityPrefab ? LDtkPrefabFactory.Instantiate(entityPrefab) : new GameObject();
}
private void AddIidComponent()
{
_iidComponent = _entityObj.AddComponent<LDtkIid>();
_iidComponent.SetIid(_entity);
}
private void AddEntityComponent()
{
_entityComponent = _entityObj.AddComponent<LDtkComponentEntity>();
}
private void AddFieldData()
{
LDtkProfiler.BeginSample("SetEntityFieldsComponent");
_fieldsFactory = new LDtkFieldsFactory(_entityObj, _entity.FieldInstances, Project, Importer);
_fieldsFactory.SetEntityFieldsComponent();
_fieldsComponent = _fieldsFactory.FieldsComponent;
LDtkProfiler.EndSample();
LDtkProfiler.BeginSample("InterfaceEvents");
InterfaceEvents();
LDtkProfiler.EndSample();
}
private void InterfaceEvents()
{
//leaving it like this instead of getting children because a level could get all the children of entities.
MonoBehaviour[] behaviors = _entityObj.GetComponents<MonoBehaviour>();
//caching here to make the import context work properly.
int sortingOrder = SortingOrder.SortingOrderValue;
LayerInstance layer = Layer;
EntityInstance entity = _entity;
LDtkFields fieldsComponent = _fieldsComponent;
_assetProcess.TryAddInterfaceEvent<ILDtkImportedLayer>(behaviors, e => e.OnLDtkImportLayer(layer));
if (fieldsComponent != null)
{
_assetProcess.TryAddInterfaceEvent<ILDtkImportedFields>(behaviors, e => e.OnLDtkImportFields(fieldsComponent));
}
_assetProcess.TryAddInterfaceEvent<ILDtkImportedEntity>(behaviors, e => e.OnLDtkImportEntity(entity));
_assetProcess.TryAddInterfaceEvent<ILDtkImportedSortingOrder>(behaviors, e => e.OnLDtkImportSortingOrder(sortingOrder));
}
private void ScaleEntity()
{
if (!Project.ScaleEntities) return;
//modify by the resized entity scaling from LDtk
Vector3 newScale = _entityObj.transform.localScale;
newScale.x *= _entityComponent.ScaleFactor.x;
newScale.y *= _entityComponent.ScaleFactor.y;
_entityObj.transform.localScale = newScale;
if (_fieldsFactory != null)
{
_fieldsFactory.ApplyPointScale(_entityComponent.ScaleFactor);
}
}
private void PositionEntity()
{
_entityObj.transform.parent = LayerGameObject.transform;
Vector2 localPos = LDtkCoordConverter.EntityLocalPosition(_entity.UnityPx, Layer.LevelReference.PxHei, Project.PixelsPerUnit);
localPos += Layer.UnityWorldTotalOffset;
_entityObj.transform.localPosition = localPos;
}
public PointParseData GetParsedPointData()
{
if (Project == null)
{
LDtkDebug.LogError("Failed to parse point data; the Importer was null");
return new PointParseData();
}
if (Layer == null)
{
LDtkDebug.LogError("Failed to parse point data; the layer was null");
return new PointParseData();
}
int levelOffset = 0;
if (_linearVector != null)
{
levelOffset = _linearVector.Scaler;
}
return new PointParseData()
{
LvlCellHeight = Layer.CHei,
PixelsPerUnit = Project.PixelsPerUnit,
GridSize = Layer.GridSize,
LevelPosition = Layer.LevelReference.UnityWorldSpaceCoord(_layout, Project.PixelsPerUnit, levelOffset) //todo could be a better way to work with this. could even be the LayerObject position to save on calculating
};
}
}
}