-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryArray.nut
174 lines (143 loc) · 4.81 KB
/
QueryArray.nut
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
if ( !("csQuery" in getroottable()) || typeof ::csQuery != "table" )
throw "csQuery not found. Make sure you are not directly referencing this script, instead reference csQuery.nut";
class csQuery.QueryArray {
constructor(_entArray) {
if (typeof _entArray == "array") {
entArray = _entArray;
return;
}
if (typeof _entArray == "weakref") {
entArray = [ _entArray ];
return;
}
throw "The parameter's data type is invalid (Valid Types: array, weak reference)";
}
entArray = null;
function Each(callback) {
foreach (ent in entArray) {
callback.call(this, ent);
}
return this;
}
function EachWithIndex(callback) {
for (local i = 0; i < entArray.len(); i++) {
callback.call(this, i, entArray[i]);
}
return this;
}
function SetKeyValue(key, value) {
this.Each(function (ent) : (key, value) {
switch (typeof value)
{
case "bool":
case "integer":
ent.__KeyValueFromInt( key, value.tointeger() );
break;
case "float":
ent.__KeyValueFromFloat( key, value );
break;
case "string":
ent.__KeyValueFromString( key, value );
break;
case "Vector":
ent.__KeyValueFromVector( key, value );
break;
default:
throw "Invalid input type: " + typeof value;
}
});
return this;
}
function Hide(removeCollisions = true) {
this.SetKeyValue("rendermode", 10);
if (removeCollisions == true)
this.SetKeyValue("solid", 0);
return this;
}
function Show(rendermode = 0, collisions = 6) {
this.SetKeyValue("rendermode", rendermode);
if (collisions)
this.SetKeyValue("solid", collisions);
return this;
}
function EntFire(action, value = "", delay = 0.0, activator = null, caller = null) {
this.Each(function (entity) : (action, value, delay, activator , caller) {
EntFireByHandle(entity, action, value, delay, activator, caller);
})
return this;
}
function On(output, callback, id = null) {
this.Each(function(ent) : (output, callback, id) {
ent.ValidateScriptScope();
local scope = ent.GetScriptScope();
local _id = id; // Free variables are read-only
if (_id == null)
_id = UniqueString("OutputID_");
scope[_id] <- callback;
ent.ConnectOutput( output, _id );
})
return this;
}
function Off(output, id) {
this.Each(function (ent) : (output, id) {
ent.DisconnectOutput( output, id )
local scope = ent.GetScriptScope();
delete scope[id];
});
return this;
}
function SaveData(key, value) {
local ent = this.Get(0);
ent.ValidateScriptScope();
local scope = ent.GetScriptScope();
if (!("customData" in scope))
scope.customData <- {};
scope.customData[key] <- value;
return this;
}
function HasData(key) {
local ent = this.Get(0);
ent.ValidateScriptScope();
local scope = ent.GetScriptScope();
if ("customData" in scope && key in scope.customData)
return true;
return false;
}
function GetData(key = null) {
local ent = this.Get(0);
ent.ValidateScriptScope();
local scope = ent.GetScriptScope();
if (!("customData" in scope))
throw "Data table has not been created";
if (key)
return scope.customData[key];
else
return scope.customData;
}
function Filter(fltr) {
local approvedEntities = [];
this.Each(function (ent) : (fltr, approvedEntities) {
if ( fltr.call(ent) )
approvedEntities.push(ent);
});
return csQuery.QueryArray(approvedEntities);
}
function PrecacheModels(models) {
foreach(model in models){
this.Get(0).PrecacheModel(model);
}
return this;
}
function Length() {
return entArray.len();
}
function Eq(index) {
return csQuery.QueryArray(entArray[index]);
}
function First() {
return csQuery.QueryArray(entArray[0]);
}
function Get(index) {
return entArray[index];
}
}