14 years
edited 8 years
This manages trackable objects in such a way that it actually improves functionality and has an interface to make even the toughest projects very readable.
It allows you to:
- Create trackables for specific players. (without desyncs)
- Easily register a trackable click and trackable hover.
- Retrieve which player clicked the trackable.
- Retrieve trackable information - the model, x, y, z, and facing.
Trackable2 allows for similar functionality, but this one is improved in both speed and handle efficiency. There are some cases where trackable2 has unnecessary lag/freezes, but the Track library fixes it.
This is one of the only systems I haven't completely rewritten time and time again.
This is the same thing as the hive version but I improved the documentation a bit for readability.
API List:
If you need a sample, then check out the demo map below.
It allows you to:
- Create trackables for specific players. (without desyncs)
- Easily register a trackable click and trackable hover.
- Retrieve which player clicked the trackable.
- Retrieve trackable information - the model, x, y, z, and facing.
Trackable2 allows for similar functionality, but this one is improved in both speed and handle efficiency. There are some cases where trackable2 has unnecessary lag/freezes, but the Track library fixes it.
This is one of the only systems I haven't completely rewritten time and time again.
This is the same thing as the hive version but I improved the documentation a bit for readability.
1 library Track /* v3.0.1.0
2 *************************************************************************************3 *4 * Manages trackable objects, allowing for easy event registrations, data retrieval,5 * and adds the capability of retrieving which player interacted with the trackable. 6 *7 *************************************************************************************8 *9 * */uses/*
10 * 11 * */ Table /* hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
12 *13 ************************************************************************************14 *15 * SETTINGS16 */17 globals18 private constant integer PLATFORM = 'Otip'
19 endglobals20 /*21 ************************************************************************************22 *23 * Functions24 *25 * function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track26 * - Creates a trackable of "modelPath" at the coordinates ( x, y, z ) with 27 * - "facing" in degrees.28 * function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing returns Track29 * - Same as the function above, except it will create it for only one player.30 *31 * function RegisterAnyClickEvent takes code c returns nothing32 * - The code will be executed every time a trackable is clicked.33 * function RegisterAnyHoverEvent takes code c returns nothing34 * - The code will be executed every time a trackable is hovered.35 * function RegisterClickEvent takes Track obj, code c returns nothing36 * - The code will be executed every time a trackable of the instance37 * - "obj" is clicked.38 * function RegisterHoverEvent takes Track obj, code returns nothing39 * - The code will be executed every time a trackable of the instance40 * - "obj" is hovered.41 *42 * function EnableTrackInstance takes Track obj, boolean flag returns nothing43 * - If an instance is enabled, it will enable its events to fire.44 * - All instances are enabled by default.45 * function IsTrackInstanceEnabled takes Track obj returns boolean46 * - Returns whether or not an instance has its events enabled.47 *48 * Event Responses49 *50 * function GetTriggerTrackInstance takes nothing returns Track51 * - Returns the Track instance that had a player interaction.52 * function GetTriggerTrackable takes nothing returns trackable53 * - Returns the trackable object that had a player interaction.54 * function GetTriggerTrackablePlayer takes nothing returns player55 * - Returns the player that had interacted with the trackable object.56 *57 *******************************************************************58 *59 * struct Track60 *61 * static Track instance62 * - The triggering instance of the event.63 * static trackable object64 * - The triggering trackable object of the event.65 * static player tracker66 * - The player who interacted with the trackable object of the event.67 *68 * readonly real x69 * readonly real y70 * readonly real z71 * - The coordinates of the trackable object.72 * readonly real facing73 * - The facing angle of the trackable object.74 * readonly string model75 * - The string path of the trackable object.76 *77 * method operator enabled takes nothing returns boolean78 * 79 * static method create takes string modelPath, real x, real y, real z, real facing returns Track80 * static method createForPlayer takes string modelPath, real x, real y, real z, real facing, player p returns Track81 *82 * static method registerAnyClick takes code c returns nothing83 * static method registerAnyHover takes code c returns nothing84 *85 * method registerOnClick takes code c returns nothing86 * method registerOnHover takes code c returns nothing87 *88 * method enable takes nothing returns nothing89 * method disable takes nothing returns nothing90 *91 * - All of the above are the struct interface equivalents of the functions.92 *93 ********************************************************************94 * 95 * Credits96 * - Azlier for Trackable297 * - Arhowk for finding a bug for me98 *99 ********************************************************************/100
101 private module Init
102 private static method onInit takes nothing returns nothing
103 set thistype.TrackTable = Table.create()
104 endmethod105 endmodule106 107 struct Track extends array
108 private static trigger anyClick = CreateTrigger()
109 private static trigger anyHover = CreateTrigger()
110 private static Table TrackTable = 0
111
112 static thistype instance = 0
113 static trackable object = null
114 static player tracker = null
115
116 private static integer ic = 0
117 private static integer ir = 0
118 private thistype rn
119
120 readonly real x
121 readonly real y
122 readonly real z
123 readonly real facing
124 readonly string model
125
126 private boolean flag
127 private trigger reg
128 private trigger onClick
129 private trigger onHover
130
131 static method registerAnyClick takes code c returns nothing
132 call TriggerAddCondition(.anyClick, Filter(c))
133 endmethod134 static method registerAnyHover takes code c returns nothing
135 call TriggerAddCondition(.anyHover, Filter(c))
136 endmethod137
138 method registerClick takes code c returns nothing
139 if .onClick == null then
140 set .onClick = CreateTrigger()
141 endif142 call TriggerAddCondition(.onClick, Filter(c))
143 endmethod144 method registerHover takes code c returns nothing
145 if .onHover == null then
146 set .onHover = CreateTrigger()
147 endif148 call TriggerAddCondition(.onHover, Filter(c))
149 endmethod150
151 method destroy takes nothing returns nothing
152 call TrackTable.remove(GetHandleId(.reg))
153 call TrackTable.remove(GetHandleId(.object))
154 call DestroyTrigger(.reg)
155 call DestroyTrigger(.onClick)
156 call DestroyTrigger(.onHover)
157 set .rn = ir
158 set ir = this
159 endmethod160
161 method enable takes nothing returns nothing
162 set this.flag = true
163 endmethod164 method disable takes nothing returns nothing
165 set this.flag = false
166 endmethod167 method operator enabled takes nothing returns boolean
168 return this.flag
169 endmethod170
171 private static method onInteract takes nothing returns boolean
172 local thistype temp = instance
173 local trackable tr = object
174 local player p = tracker
175
176 set instance = TrackTable[GetHandleId(GetTriggeringTrigger())]
177 set object = GetTriggeringTrackable()
178 set tracker = Player(TrackTable[GetHandleId(object)])
179
180 if instance.flag then
181 if GetTriggerEventId() == EVENT_GAME_TRACKABLE_TRACK then
182 call TriggerEvaluate(instance.onHover)
183 call TriggerEvaluate(anyHover)
184 else185 call TriggerEvaluate(instance.onClick)
186 call TriggerEvaluate(anyClick)
187 endif188 endif189
190 set instance = temp
191 set tracker = p
192 set object = tr
193 set tr = null
194 set p = null
195 return false
196 endmethod197
198 private static method createTrack takes string modelPath, real x, real y, real z, real facing, player j returns thistype
199 local destructable dest = null
200 local thistype this = ir
201 local integer i = 11
202 local trackable tr
203 local player p
204 local string s
205 if this == 0 then
206 set ic = ic + 1
207 set this = ic
208 else209 set ir = .rn
210 endif211 if z != 0 then
212 set dest = CreateDestructableZ(PLATFORM, x, y, z, 0, 1, 0)
213 endif214 if j != null then
215 set i = GetPlayerId(j)
216 endif217 set .x = x
218 set .y = y
219 set .z = z
220 set .flag = true
221 set .facing = facing
222 set .model = modelPath
223 set .reg = CreateTrigger()
224 set .onClick = null
225 set .onHover = null
226 set TrackTable[GetHandleId(.reg)] = this
227 call TriggerAddCondition(.reg, Condition(function thistype.onInteract))
228 loop229 set p = Player(i)
230 if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER then
231 if GetLocalPlayer() == p then
232 set s = modelPath
233 else234 set s = ""
235 endif 236 set tr = CreateTrackable(s, .x, .y, .facing)
237 call TriggerRegisterTrackableHitEvent(.reg, tr)
238 call TriggerRegisterTrackableTrackEvent(.reg, tr)
239 set TrackTable[GetHandleId(tr)] = i
240 if j != null then
241 exitwhen true
242 endif243 endif244 exitwhen i == 0
245 set i = i - 1
246 endloop247 if dest != null then
248 call RemoveDestructable(dest)
249 set dest = null
250 endif251 set p = null
252 set tr = null
253 return this
254 endmethod255
256 static method create takes string modelPath, real x, real y, real z, real facing returns thistype
257 return thistype.createTrack(modelPath, x, y, z, facing, null)
258 endmethod 259
260 static method createForPlayer takes string modelPath, real x, real y, real z, real facing, player p returns thistype
261 if not (GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER) then
262 return 0
263 endif264 return thistype.createTrack(modelPath, x, y, z, facing, p)
265 endmethod266
267 implement Init268 endstruct269
270 function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
271 return Track.create(modelPath, x, y, z, facing)
272 endfunction273
274 function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing, player who returns Track
275 return Track.createForPlayer(modelPath, x, y, z, facing, who)
276 endfunction277
278 function EnableTrackInstance takes Track instance, boolean flag returns nothing
279 if flag then
280 call instance.enable()
281 else282 call instance.disable()
283 endif284 endfunction285
286 function IsTrackInstanceEnabled takes Track instance returns boolean
287 return instance.enabled
288 endfunction289
290 function RegisterAnyClickEvent takes code c returns nothing
291 call Track.registerAnyClick(c)
292 endfunction293
294 function RegisterAnyHoverEvent takes code c returns nothing
295 call Track.registerAnyHover(c)
296 endfunction297
298 function RegisterClickEvent takes Track obj, code c returns nothing
299 call obj.registerClick(c)
300 endfunction301
302 function RegisterHoverEvent takes Track obj, code c returns nothing
303 call obj.registerHover(c)
304 endfunction305
306 function GetTriggerTrackInstance takes nothing returns Track
307 return Track.instance
308 endfunction309
310 function GetTriggerTrackable takes nothing returns trackable
311 return Track.object
312 endfunction313
314 function GetTriggerTrackablePlayer takes nothing returns player
315 return Track.tracker
316 endfunction317 endlibrary
API List:
Spoiler
1 function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
2 3 function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing returns Track
4 5 function RegisterAnyClickEvent takes code c returns nothing
6 7 function RegisterAnyHoverEvent takes code c returns nothing
8 9 function RegisterClickEvent takes Track obj, code c returns nothing
10 11 function RegisterHoverEvent takes Track obj, code c returns nothing
12 13 function EnableTrackInstance takes Track obj, boolean flag returns nothing
14 15 function IsTrackInstanceEnabled takes Track obj returns boolean
16 17 function GetTriggerTrackInstance takes nothing returns Track
18 19 function GetTriggerTrackable takes nothing returns trackable
20 21 function GetTriggerTrackablePlayer takes nothing returns player
If you need a sample, then check out the demo map below.