-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructureSupportPoints.cs
113 lines (98 loc) · 3.66 KB
/
StructureSupportPoints.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
using UnityEngine;
using System.Collections.Generic;
namespace Structures
{
/// <summary>
/// A shared collection of support points for a structure object.
///
/// The points are defined in local space relative to the structure they will represent.
///
/// The same StructureSupportPoints object can be used across multiple
/// varieties of the same structure.
///
/// For example a two wall structure may have identical support points but
/// provided different functionality.
/// </summary>
[CreateAssetMenu(menuName = "Structures/SupportPoints")]
public class StructureSupportPoints : ScriptableObject
{
[Tooltip("A collection of support points on a structure.")]
[SerializeField] private List<SupportPoint> supportPoints = new List<SupportPoint>();
[Tooltip("A structure without any support points can still snap to a surface.")]
[SerializeField] private bool m_snapsToSurface = false;
#region Public Properties
/// <summary>
/// Returns the number of support points.
/// </summary>
public int count => supportPoints.Count;
/// <summary>
/// Returns true if the object can snap to a surface.
/// </summary>
public bool snapsToSurface => m_snapsToSurface;
/// <summary>
/// Returns an array of all the support points.
/// </summary>
public ISupportPoint[] array
{
get
{
var results = new ISupportPoint[supportPoints.Count];
for (int i = 0; i < supportPoints.Count; i++)
{
results[i] = supportPoints[i];
}
return results;
}
}
/// <summary>
/// Returns an array of all the required support points.
/// </summary>
public ISupportPoint[] required
{
get
{
var results = new List<ISupportPoint>();
for (int i = 0; i < supportPoints.Count; i++)
{
if (supportPoints[i].required)
results.Add(supportPoints[i]);
}
return results.ToArray();
}
}
#endregion
#region Public Methods
/// <summary>
/// Get the support point at the given index.
/// The index must be valid.
/// </summary>
/// <param name="index">The index to retrieve.</param>
/// <returns>The support point at the index given.</returns>
public ISupportPoint Get(int index)
{
return supportPoints[index];
}
/// <summary>
/// Finds the support point closest to the given position.
/// </summary>
/// <param name="position">The position to use as the origin for the search.</param>
/// <returns>The closest support point.</returns>
public ISupportPoint GetClosestSupportPointTo(Vector3 position)
{
if (count == 0) return null;
var result = supportPoints[0];
var closestDistance = float.PositiveInfinity;
for (int i = 0; i < supportPoints.Count; i++)
{
var distance = Vector3.Distance(supportPoints[i].position, position);
if (distance < closestDistance)
{
closestDistance = distance;
result = supportPoints[i];
}
}
return result;
}
#endregion
}
}