This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
InterestQuery.cs
125 lines (115 loc) · 4.2 KB
/
InterestQuery.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Improbable.Gdk.QueryBasedInterest
{
/// <summary>
/// Utility class to help construct ComponentInterest.Query objects.
/// </summary>
public class InterestQuery
{
private ComponentInterest.Query query;
private InterestQuery()
{
}
/// <summary>
/// Creates an <see cref="InterestQuery"/>.
/// </summary>
/// <param name="constraint">
/// A <see cref="Constraint"/> object defining the constraints of the query.
/// </param>
/// <remarks>
/// Returns the full snapshot result by default.
/// </remarks>
/// <returns>
/// An <see cref="InterestQuery"/> object.
/// </returns>
public static InterestQuery Query(Constraint constraint)
{
var interest = new InterestQuery
{
query =
{
Constraint = constraint.AsQueryConstraint(),
FullSnapshotResult = true,
ResultComponentId = new List<uint>()
}
};
return interest;
}
/// <summary>
/// Sets the maximum frequency (Hz) of the query.
/// </summary>
/// <param name="frequencyHz">
/// The maximum frequency (Hz) to return query results.
/// </param>
/// <remarks>
/// A frequency of 0 means there will be no rate limiting.
/// </remarks>
/// <returns>
/// An updated <see cref="InterestQuery"/> object.
/// </returns>
public InterestQuery WithMaxFrequencyHz(float frequencyHz)
{
if (frequencyHz < 0)
{
throw new ArgumentOutOfRangeException(nameof(frequencyHz), frequencyHz, "The max frequency must be greater than or equal to zero.");
}
query.Frequency = frequencyHz;
return this;
}
/// <summary>
/// Defines what components to return in the query results.
/// </summary>
/// <param name="resultComponentId">
/// First ID of a component to return from the query results.
/// </param>
/// <param name="resultComponentIds">
/// Further IDs of components to return from the query results.
/// </param>
/// <remarks>
/// At least one component ID must be provided.
/// </remarks>
/// <returns>
/// An updated <see cref="InterestQuery"/> object.
/// </returns>
public InterestQuery FilterResults(uint resultComponentId, params uint[] resultComponentIds)
{
var resultIds = new List<uint>(resultComponentIds.Length + 1) { resultComponentId };
resultIds.AddRange(resultComponentIds);
return FilterResults(resultIds);
}
/// <summary>
/// Defines what components to return in the query results.
/// </summary>
/// <param name="resultComponentIds">
/// Set of IDs of components to return from the query results.
/// </param>
/// <remarks>
/// At least one component ID must be provided. Query results are not filtered
/// if resultComponentIds is empty.
/// </remarks>
/// <returns>
/// An updated <see cref="InterestQuery"/> object.
/// </returns>
public InterestQuery FilterResults(IEnumerable<uint> resultComponentIds)
{
if (!resultComponentIds.Any())
{
UnityEngine.Debug.LogWarning("At least one component ID must be provided to filter a query's results.");
return this;
}
query.FullSnapshotResult = null;
query.ResultComponentId.AddRange(resultComponentIds);
return this;
}
/// <summary>
/// Returns the underlying ComponentInterest.Query object from the <see cref="InterestQuery"/> class.
/// </summary>
public ComponentInterest.Query AsComponentInterestQuery()
{
return query;
}
}
}