-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueryDistance.em
76 lines (62 loc) · 2.14 KB
/
queryDistance.em
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
// QueryDistance
// by Elliot Conte
Distance = 0;
var AddFunction;
var RemoveFunction;
FoundObjects = new Array();
var TimerName;
TimerLength = 2;
function QueryDistance(distance, CallbackFunctionAdd, CallbackFunctionRemove)
{
system.import('std/core/repeatingTimer.em');
Distance = distance;
AddFunction = CallbackFunctionAdd;
RemoveFunction = CallbackFunctionRemove;
system.presences[0].onProxAdded( userAddedCallback);
system.presences[0].setQueryAngle(.01);
var repTimer = new std.core.RepeatingTimer(TimerLength, DistancePoll);
TimerName = repTimer;
}
function userAddedCallback (nowImportantPresence)
{
var visibleobject = new Object();
visibleobject.presence = nowImportantPresence;
visibleobject.distance = nowImportantPresence.dist(system.presences[0].getPosition());
for(var i=0; i<FoundObjects.length; i++) {
if (FoundObjects[i].presence.toString() == nowImportantPresence.toString()) return;
}
if (visibleobject.distance <= Distance) {
AddFunction(nowImportantPresence);
}
FoundObjects.push(visibleobject);
}
//Is called every TimerLength seconds and polls and updates distance of all objects.
function DistancePoll()
{
for (visibleobject in FoundObjects)
{
//If the object used to be > Distance but is now within it.
if (FoundObjects[visibleobject].distance > Distance &&
FoundObjects[visibleobject].presence.dist(system.presences[0].getPosition()) <= Distance)
{
AddFunction(FoundObjects[visibleobject].presence);
//If the object used to be <= Distance but is now outside it.
} else if (FoundObjects[visibleobject].distance <= Distance &&
FoundObjects[visibleobject].presence.dist(system.presences[0].getPosition()) > Distance)
{
RemoveFunction(FoundObjects[visibleobject].presence);
}
FoundObjects[visibleobject].distance = FoundObjects[visibleobject].presence.dist(system.presences[0].getPosition());
}
}
//Call to stop the query.
function StopQueryDistance()
{
TimerName.suspend();
}
function SetQueryDRate(time)
{
TimerName.suspend();
var repeTimer = new std.core.RepeatingTimer(time, DistancePoll);
TimerName = repeTimer;
}