-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathLDtkFieldsFactory.cs
More file actions
88 lines (75 loc) · 3.1 KB
/
LDtkFieldsFactory.cs
File metadata and controls
88 lines (75 loc) · 3.1 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
using System.Collections.Generic;
using UnityEngine;
namespace LDtkUnity.Editor
{
/// <summary>
/// Field conversion for the LDtkFields component specifically.
/// </summary>
internal sealed class LDtkFieldsFactory
{
private readonly GameObject _instance;
private readonly FieldInstance[] _fieldInstances;
private readonly LDtkProjectImporter _project;
private readonly LDtkJsonImporter _importer;
private readonly List<Transform> _pointTransforms = new List<Transform>();
public LDtkFields FieldsComponent { get; private set; }
public LDtkFieldsFactory(GameObject instance, FieldInstance[] fieldInstances, LDtkProjectImporter project, LDtkJsonImporter importer)
{
_instance = instance;
_fieldInstances = fieldInstances;
_project = project;
_importer = importer;
}
public void SetEntityFieldsComponent()
{
if (_fieldInstances.IsNullOrEmpty())
{
return;
}
if (!_instance.TryGetComponent(out LDtkFields fields))
{
fields = _instance.AddComponent<LDtkFields>();
}
LDtkProfiler.BeginSample("GetFields");
LDtkField[] fieldData = GetFields();
LDtkProfiler.EndSample();
fields.SetFieldData(fieldData);
FieldsComponent = fields;
}
private LDtkField[] GetFields()
{
LDtkFieldFactory fieldFactory = new LDtkFieldFactory(_project, _importer);
LDtkField[] fields = new LDtkField[_fieldInstances.Length];
for (int i = 0; i < _fieldInstances.Length; i++)
{
fields[i] = fieldFactory.GetFieldFromInstance(_fieldInstances[i].Definition, _fieldInstances[i].Value);
//setup transforms for the points so that they are easy to follow along.
LDtkProfiler.BeginSample($"Setup point transforms");
LDtkField field = fields[i];
if (field.Type == LDtkFieldType.Point)
{
for (int ii = 0; ii < field._data.Length; ii++)
{
LDtkFieldElement element = field._data[ii];
Transform newPoint = new GameObject($"{_fieldInstances[i].Identifier}_{ii}").transform;
_pointTransforms.Add(newPoint);
element.SetPointLocalTransform(newPoint);
newPoint.SetParent(_instance.transform, true);
}
}
LDtkProfiler.EndSample();
}
return fields;
}
public void ApplyPointScale(Vector2 scale)
{
foreach (Transform point in _pointTransforms)
{
Vector3 local = point.localPosition;
local.x /= scale.x;
local.y /= scale.y;
point.localPosition = local;
}
}
}
}