-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweenVS.lua
936 lines (714 loc) · 23.5 KB
/
TweenVS.lua
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
--Inspired by the GDC talk "Math for Game Programmers: Fast and Funky 1D Nonlinear Transformations" -- https://www.youtube.com/watch?v=mr5xkf6zSzk&ab_channel=GDC
local TweenVS = {
_VERSION = "1.0.3",
_NAME = "TweenVS",
_DESCRIPTION = "VScript Tweening library for the Source2 game engine",
_LINK = "github.com/Angel-foxxo/TweenVS",
_COPYRIGHT = [[
* Copyright (C) 2023 Angel - All Rights Reserved
* You may use, distribute and modify this code under the
* terms of the MIT license.
*
* You should have received a copy of the LICENSE file with
* this file. If not, please write to: [email protected] or visit : github.com/Angel-foxxo/TweenVS
]]
}
TweenVS.Tweens = {}
if TweenVS.ROUND_START_EVENT ~= nil then
StopListeningToGameEvent(TweenVS.ROUND_START_EVENT)
TweenVS.ROUND_START_EVENT = nil
end
--------------------
-- Main Tween Class
--------------------
TweenVS.Tween = {}
local TweenMetatable = {
__call = function(self, ...)
return self:new(...)
end,
}
setmetatable(TweenVS.Tween, TweenMetatable)
function TweenVS.Tween:new()
local NewTween = {
_target = nil,
_type = nil,
_initVal = nil,
_endVal = nil,
_initTime = nil,
_duration = nil,
_running = false,
_timeElapsed = 0,
_resultVal = nil,
_callbackUpdateList = {},
_callbackFinishList = {},
_callbackStartList = {},
_callbackEveryStartList = {},
_callbackStopList = {},
_nextTween = nil,
_looping = nil,
_loopCount = nil,
_runCount = 0,
_paused = false,
_pausedTime = nil,
_delay = nil,
_delayTime = nil,
_easingFunction = nil,
_property = nil,
_localVal = nil,
_localLoop = nil,
_inverted = false,
_bounce = false,
_justStarted = true
}
setmetatable(NewTween, self)
self.__index = self
return NewTween
end
TweenVS.Tween.__type = "Tween"
function TweenVS.Tween:__tostring()
return string.format("Tween [StartVal: %s | EndVal: %s | ResultVal: %s]", self._initVal, self._endVal, self._resultVal)
end
--get the target to interpolate, the target can be either an entity or a basic type
function TweenVS.Tween:from(target, property)
property = property or nil
--is it an entity?
if TweenVS.instanceof(target, "CBaseEntity") then
if property == nil then
error("TweenVS: from() target is an entity but no entity propery is specified!", 2)
end
--set what property we are modifying
if TweenVS.EntProps[property] ~= nil then
self._type = property
else
error("TweenVS: from() target is an entity but the entity property is invalid!", 2)
end
--set the property
TweenVS.Switch(self._type, {
[TweenVS.EntProps.pos] = function()
self._initVal = target:GetOrigin()
end,
[TweenVS.EntProps.ang] = function()
self._initVal = target:EyeAngles()
end,
[TweenVS.EntProps.scale] = function()
self._initVal = target:GetAbsScale()
end,
[TweenVS.EntProps.color] = function()
self._initVal = target:GetRenderColor()
end,
[TweenVS.EntProps.alpha] = function()
self._initVal = target:GetRenderAlpha()
end,
[TweenVS.EntProps.mass] = function()
self._initVal = target:GetMass()
end,
[TweenVS.EntProps.health] = function()
self._initVal = target:GetHealth()
end,
[TweenVS.EntProps.velocity] = function()
self._initVal = GetPhysVelocity(target)
end,
[TweenVS.EntProps.angVelocity] = function()
self._initVal = GetPhysAngularVelocity(target)
end,
})
self._target = target
self._property = property
--not an entity, perhaps a type?
elseif TweenVS.ValTypes[TweenVS.type(target)] ~= nil then
self._initVal = target
self._target = target
--NEITHER!! WHAT ARE YOU TRYING TO DO!?
else
error("TweenVS: from() target is invalid!", 2)
end
return self
end
--target value of the tween, has to match with the initial value type
function TweenVS.Tween:to(value, duration)
duration = duration or 1
if self._initVal == nil then
error("TweenVS: start value doesn't exist! did you run from() first?", 2)
end
if TweenVS.type(value) ~= TweenVS.type(self._initVal) then
error("TweenVS: start value type doesn't match end value type!", 2)
end
if TweenVS.type(duration) ~= TweenVS.ValTypes.number then
error("TweenVS: tween duration is invalid!", 2)
end
self._endVal = value
self._duration = duration
return self
end
--only for entities, makes the tween local to the entity
function TweenVS.Tween:toLocal(value, duration, localLoop)
duration = duration or 1
localLoop = localLoop or false
if self._initVal == nil then
error("TweenVS: start value doesn't exist! did you run from() first?", 2)
end
if not TweenVS.instanceof(self._target, "CBaseEntity") then
error("TweenVS: toLocal is only meant for entities!", 2)
end
self._localVal = value
self._localLoop = localLoop
self:to(self._initVal+value, duration)
return self
end
--start tweening
function TweenVS.Tween:start()
if self._localLoop then
self:from(self._target, self._property)
self:toLocal(self._localVal, self._duration, self._localLoop)
end
self._initTime = Time()
self._timeElapsed = 0
self._running = true
if TweenVS.FindInArray(TweenVS.Tweens, self) == nil then
self._runCount = 0
self._justStarted = true
table.insert(TweenVS.Tweens, self)
end
return self
end
--stops tweening, will actually remove it from the Tweens array
function TweenVS.Tween:stop()
table.remove(TweenVS.Tweens, TweenVS.FindInArray(TweenVS.Tweens, self))
self:handleCallback(self._callbackStopList, "TweenVS: on(stop) is attempting to call something that isn't a function!")
return self
end
--add a callback function
function TweenVS.Tween:on(type, func)
if not TweenVS.FindInArray(TweenVS.Callbacks, type) then
error("TweenVS: on() was passed an invalid callback type!", 2)
end
TweenVS.Switch(type, {
["update"] = function()
table.insert(self._callbackUpdateList, func)
end,
["finish"] = function()
table.insert(self._callbackFinishList, func)
end,
["start"] = function()
table.insert(self._callbackStartList, func)
end,
["everyStart"] = function()
table.insert(self._callbackEveryStartList, func)
end,
["stop"] = function()
table.insert(self._callbackStopList, func)
end,
})
return self
end
--pauses the tweening, passing a number in seconds makes it act as a delay
--otherwise it pauses forever
function TweenVS.Tween:pause(time)
time = time or nil
if self._paused == true then
return self
end
self._pausedTime = Time()
self._running = false
self._paused = true
if time ~= nil then
if TweenVS.type(time) ~= "number" then
error("TweenVS: delay() has an invalid parameter!", 2)
end
self._delay = time
self._delayTime = Time()
end
return self
end
--unpauses a tween, does nothing if its not paused
function TweenVS.Tween:unpause()
if not self._initTime then
return self
end
if self._paused ~= true then
return self
end
if self._delay ~= nil then
return self
end
local resumeTime = Time()
self._initTime = resumeTime - (self._pausedTime -self._initTime)
self._running = true
self._paused = false
return self
end
--loops the tween, loopCount of -1 is infinite looping
function TweenVS.Tween:loop(loopCount)
loopCount = loopCount or -1
if TweenVS.type(loopCount) ~= "number" and loopCount ~= nil then
error("TweenVS: loop() has an invalid parameter!", 2)
end
self._looping = true
self._loopCount = loopCount
if loopCount == 0 then
self._looping = false
self._loopCount = 0
end
if loopCount < 0 then
self._looping = true
self._loopCount = nil
end
return self
end
--makes the tween reverse direction at the end of each loop
--each start of the tween in a single direction counts as a single bounce, for example
--to play a tween once forward, and once back, set the loop() function to 1
function TweenVS.Tween:bounce(val)
val = val or true
if TweenVS.type(val) ~= "boolean" then
error("TweenVS: bounce() has an invalid parameter!", 2)
end
self._bounce = val
return self
end
--runs the provided tween when the current tween finishes
function TweenVS.Tween:chain(tween)
tween = tween or nil
if tween == nil then
error("TweenVS: chain() is missing a value!", 2)
end
if TweenVS.type(tween) ~= "Tween" then
error("TweenVS: chain() was pass an invalid value!", 2)
end
self._nextTween = tween
return self
end
--sets the easing function, leave black for linear interpolation
--you can either use a function from TweenVS.EasingFunctions or
--pass a custom function, the function needs to take one parameter (t)
--and return the modified t parameter
function TweenVS.Tween:easing(easingFunction)
easingFunction = easingFunction or nil
if easingFunction ~= null and TweenVS.type(easingFunction) ~= "function" then
error("TweenVS: easing() has an invalid parameter!", 2)
end
self._easingFunction = easingFunction
return self
end
--inverts the tween, this does not modify the self._initVal and self._endVal variables like bounce()
--but is instead done by inverting the t value used to interpolate
function TweenVS.Tween:invert(val)
val = val or true
if TweenVS.type(val) ~= "boolean" then
error("TweenVS: invert() has an invalid parameter!", 2)
end
self._inverted = val
return self
end
--read more in the function, this shit does too much stuff to write it all here
function TweenVS.Tween:update()
if self._delay ~= nil then
if Time() - self._delayTime < self._delay then
return
else
self._delay = nil
self:unpause()
end
end
if not self._running then
return
end
self._timeElapsed = Time() - self._initTime
--horrible hack to fix the tween not starting from the initial value on loops due to execution order
if self._justLooped then
self._timeElapsed = 0
self._justLooped = false
end
if self._timeElapsed < self._duration then
local t = self._timeElapsed/self._duration
if self._easingFunction == nil then
t = t
else
t = self._easingFunction(t)
end
if self._inverted then
t = (t * -1) + 1
end
if TweenVS.type(self._initVal) == "QAngle" then
--builting slerp, thank god
self._resultVal = QSlerp(self._initVal, self._endVal, t)
else
self._resultVal = TweenVS.Lerp(self._initVal, self._endVal, t)
end
if self._justStarted then
self:handleCallback(self._callbackEveryStartList, "TweenVS: on(everyStart) is attempting to call something that isn't a function!")
end
else
self._resultVal = self._endVal
self._runCount = self._runCount + 1
self._running = false
self:handleCallback(self._callbackFinishList, "TweenVS: on(update) is attempting to call something that isn't a function!")
if self._looping then
if self._loopCount == nil or self._runCount <= self._loopCount then
if self._bounce then
local tempInitVal = self._initVal
local tempEndVal = self._endVal
self._initVal = tempEndVal
self._endVal = tempInitVal
end
self._justLooped = true
self:start()
end
end
end
--its an entity!
if TweenVS.instanceof(self._target, "CBaseEntity") then
--set entity property based on the type
TweenVS.Switch(self._type, {
[TweenVS.EntProps.pos] = function()
self._target:SetOrigin(self._resultVal)
end,
[TweenVS.EntProps.ang] = function()
self._target:SetAngles(self._resultVal.x, self._resultVal.y, self._resultVal.z)
end,
[TweenVS.EntProps.scale] = function()
self._target:SetAbsScale(self._resultVal)
end,
[TweenVS.EntProps.color] = function()
self._target:SetRenderColor(self._resultVal.x, self._resultVal.y, self._resultVal.z)
end,
[TweenVS.EntProps.alpha] = function()
self._target:SetRenderAlpha(self._resultVal)
end,
[TweenVS.EntProps.mass] = function()
self._target:SetMass(self._resultVal)
end,
[TweenVS.EntProps.health] = function()
self._target:SetHealth(self._resultVal)
end,
[TweenVS.EntProps.velocity] = function()
self._target:SetVelocity(self._resultVal)
end,
[TweenVS.EntProps.angVelocity] = function()
SetPhysAngularVelocity(self._target, self._resultVal)
end,
})
--not an entity, perhaps a type?
elseif TweenVS.FindInArray(TweenVS.ValTypes, TweenVS.type(self._target)) then
self._target = self._resultVal
end
self:handleCallback(self._callbackUpdateList, "TweenVS: on(start) is attempting to call something that isn't a function!")
if self._runCount == 0 and self._justStarted then
self:handleCallback(self._callbackStartList, "TweenVS: on(start) is attempting to call something that isn't a function!")
end
--twin chaining is executed here because otherwise we wouldnt get the final start position
if self._nextTween ~= nil and self._timeElapsed >= self._duration then
if self._nextTween._property ~= nil then
self._nextTween:from(self._nextTween._target, self._nextTween._property)
if self._nextTween._localVal ~= nil then
self._nextTween:toLocal(self._nextTween._localVal, self._nextTween._duration, self._nextTween._localLoop)
end
end
self._nextTween:start()
if self._nextTween._paused then
self._nextTween._paused = false
self._nextTween:pause(self._nextTween._delay)
end
end
self._justStarted = false
end
--handles callbacks
function TweenVS.Tween:handleCallback(callbackList, errorString)
for _, callback in ipairs(callbackList) do
if TweenVS.type(callback) ~= "function" then
error(errorString, 2)
end
callback(self._resultVal)
end
end
--------------------
-- Easing Functions
-- taken from https://easings.net/
--------------------
TweenVS.EaseInSine = function(t)
return 1.0 - math.cos((t * math.pi) / 2.0)
end
TweenVS.EaseOutSine = function(t)
return math.sin((t * math.pi) / 2)
end
TweenVS.EaseInOutSine = function(t)
return -(math.cos(math.pi * t) - 1.0) / 2.0
end
TweenVS.EaseInCubic = function(t)
return t * t * t
end
TweenVS.EaseOutCubic = function(t)
return 1.0 - math.pow(1.0 - t, 3.0)
end
TweenVS.EaseInOutCubic = function(t)
return t < 0.5 and (4.0 * t * t * t) or (1 - math.pow(-2.0 * t + 2.0, 3.0) / 2.0)
end
TweenVS.EaseInQuint = function(t)
return t * t * t * t * t
end
TweenVS.EaseOutQuint = function(t)
return 1.0 - math.pow(1.0 - t, 5.0)
end
TweenVS.EaseInOutQuint = function(t)
return t < 0.5 and (16.0 * t * t * t * t * t) or (1.0 - math.pow(-2.0 * t + 2.0, 5.0) / 2.0)
end
TweenVS.EaseInCircle = function(t)
return 1.0 - math.sqrt(1.0 - math.pow(t, 2.0))
end
TweenVS.EaseOutCircle = function(t)
return math.sqrt(1.0 - math.pow(t - 1.0, 2.0))
end
TweenVS.EaseInOutCircle = function(t)
return t < 0.5 and ((1.0 - math.sqrt(1.0 - math.pow(2.0 * t, 2.0))) / 2.0) or ((math.sqrt(1.0 - math.pow(-2.0 * t + 2.0, 2.0)) + 1.0) / 2.0)
end
TweenVS.EaseInElastic = function(t)
local c4 = (2.0 * math.pi) / 3.0
if t == 0 then
return 0
elseif t == 1.0 then
return 1.0
else
return -math.pow(2.0, 10.0 * t - 10.0) * math.sin((t * 10.0 - 10.75) * c4)
end
end
TweenVS.EaseOutElastic = function(t)
local c4 = (2.0 * math.pi) / 3.0
if t == 0 then
return 0
elseif t == 1.0 then
return 1.0
else
return math.pow(2.0, -10.0 * t) * math.sin((t * 10.0 - 0.75) * c4) + 1.0
end
end
TweenVS.EaseInOutElastic = function(t)
local c5 = (2.0 * math.pi) / 4.5
if t == 0 then
return 0
elseif t == 1 then
return 1
elseif t < 0.5 then
return -(math.pow(2.0, 20.0 * t - 10.0) * math.sin((20.0 * t - 11.125) * c5)) / 2.0
else
return (math.pow(2.0, -20.0 * t + 10.0) * math.sin((20.0 * t - 11.125) * c5)) / 2.0 + 1.0
end
end
TweenVS.EaseInQuad = function(t)
return t * t
end
TweenVS.EaseOutQuad = function(t)
return 1.0 - (1.0 - t) * (1.0 - t)
end
TweenVS.EaseInOutQuad = function(t)
return t < 0.5 and (2.0 * t * t) or (1.0 - math.pow(-2.0 * t + 2.0, 2.0) / 2.0)
end
TweenVS.EaseInQuart = function(t)
return t * t * t * t
end
TweenVS.EaseOutQuart = function(t)
return 1.0 - math.pow(1.0 - t, 4.0)
end
TweenVS.EaseInOutQuart = function(t)
return t < 0.5 and (8.0 * t * t * t * t) or (1.0 - math.pow(-2.0 * t + 2.0, 4.0) / 2.0)
end
TweenVS.EaseInExpo = function(t)
return t == 0 and 0 or math.pow(2.0, 10.0 * t - 10.0)
end
TweenVS.EaseOutExpo = function(t)
return t == 1.0 and 1.0 or 1.0 - math.pow(2.0, -10.0 * t)
end
TweenVS.EaseInOutExpo = function(t)
if t == 0 then
return 0
elseif t == 1.0 then
return 1.0
elseif t < 0.5 then
return math.pow(2.0, 20.0 * t - 10.0) / 2.0
else
return (2.0 - math.pow(2.0, -20.0 * t + 10.0)) / 2.0
end
end
TweenVS.EaseInBack = function(t)
local c1 = 1.70158
local c3 = c1 + 1
return c3 * t * t * t - c1 * t * t
end
TweenVS.EaseOutBack = function(t)
local c1 = 1.70158
local c3 = c1 + 1
return 1.0 + c3 * math.pow(t - 1.0, 3.0) + c1 * math.pow(t - 1.0, 2.0)
end
TweenVS.EaseInOutBack = function(t)
local c1 = 1.70158
local c2 = c1 * 1.525
return t < 0.5 and (math.pow(2.0 * t, 2.0) * ((c2 + 1.0) * 2.0 * t - c2)) / 2.0 or (math.pow(2.0 * t - 2.0, 2.0) * ((c2 + 1.0) * (t * 2.0 - 2.0) + c2) + 2.0) / 2.0
end
TweenVS.EaseInBounce = function(t)
return 1 - TweenVS.EaseOutBounce(1 - t)
end
TweenVS.EaseOutBounce = function(t)
local n1 = 7.5625
local d1 = 2.75
if t < 1.0 / d1 then
return n1 * t * t
elseif t < 2.0 / d1 then
t = t - 1.5 / d1
return n1 * t * t + 0.75
elseif t < 2.5 / d1 then
t = t - 2.25 / d1
return n1 * t * t + 0.9375
else
t = t - 2.625 / d1
return n1 * t * t + 0.984375
end
end
TweenVS.EaseInOutBounce = function(t)
return t < 0.5 and (1.0 - TweenVS.EaseOutBounce(1.0 - 2.0 * t)) / 2.0 or (1.0 + TweenVS.EaseOutBounce(2.0 * t - 1.0)) / 2.0
end
TweenVS.EasingFunctions =
{
TweenVS.EaseInSine,
TweenVS.EaseOutSine,
TweenVS.EaseInOutSine,
TweenVS.EaseInCubic,
TweenVS.EaseOutCubic,
TweenVS.EaseInOutCubic,
TweenVS.EaseInQuint,
TweenVS.EaseOutQuint,
TweenVS.EaseInOutQuint,
TweenVS.EaseInCircle,
TweenVS.EaseOutCircle,
TweenVS.EaseInOutCircle,
TweenVS.EaseInElastic,
TweenVS.EaseOutElastic,
TweenVS.EaseInOutElastic,
TweenVS.EaseInQuad,
TweenVS.EaseOutQuad,
TweenVS.EaseInQuart,
TweenVS.EaseOutQuart,
TweenVS.EaseInOutQuart,
TweenVS.EaseInExpo,
TweenVS.EaseOutExpo,
TweenVS.EaseInOutExpo,
TweenVS.EaseInBack,
TweenVS.EaseOutBack,
TweenVS.EaseInOutBack,
TweenVS.EaseInBounce,
TweenVS.EaseOutBounce,
TweenVS.EaseInOutBounce
}
--------------------
-- Util functions
--------------------
--Overrides the Tweens array with an empty one
function TweenVS.PurgeTweens()
TweenVS.Tweens = {}
end
--Squirrel style switch statement
function TweenVS.Switch(param, case_table)
local case = case_table[param]
if case then return case() end
local def = case_table['default']
return def and def() or nil
end
function TweenVS.Lerp(a, b, t)
return (a * (1.0 - t)) + (b * t);
end
function TweenVS.FindInArray(array, target)
for i, value in ipairs(array) do
if value == target then
return i
end
end
return nil
end
--Horrible hack to get type() to acknowledge my class and other classes like Vector and Qangle, I miss squirrel
function TweenVS.type(obj)
local otype = type(obj)
if otype == "table" and getmetatable(obj) == TweenVS.Tween then
return TweenVS.Tween.__type
end
if otype == "userdata" and getmetatable(obj) == debug.getregistry()["Vector"] then
return "Vector"
end
if otype == "userdata" and getmetatable(obj) == debug.getregistry()["QAngle"] then
return "QAngle"
end
return otype
end
--Less Horrible hack to get instanceof functionality from squirrel back, how can people still say lua is better
function TweenVS.instanceof(obj, classname)
local classname = debug.getregistry()[tostring(classname)]
if classname == nil then
error("instanceof() invalid classname", 2)
end
local mt2 = getmetatable(obj)
while mt2 ~= nil do
if mt2 == classname then
return true
end
mt2 = getmetatable(mt2.__index)
end
return false
end
--------------------
-- Tween Value Types
--------------------
TweenVS.ValTypes =
{
number = "number",
Vector = "Vector",
QAngle = "QAngle"
}
--------------------
-- Entity Properties
--------------------
TweenVS.EntProps =
{
pos = "pos",
ang = "ang",
mass = "mass",
scale = "scale",
color = "color",
alpha = "alpha",
health = "health",
velocity = "velocity",
angVelocity = "angVelocity"
}
--------------------
-- Tween Callback Function Types
--------------------
TweenVS.Callbacks =
{
"update",
"finish",
"start",
"everyStart",
"stop"
}
--------------------
-- Tween Update Function
--------------------
function TweenVS.UpdateTweens()
for _, Tween in ipairs(TweenVS.Tweens) do
if Tween._initVal == nil then
error("TweenVS: Updating Tween without a start value!", 2);
end
if Tween._endVal == nil then
error("TweenVS: Updating Tween without an end value!", 2);
end
Tween:update()
end
return FrameTime()
end
Entities:FindByClassname(nil, "worldent"):SetContextThink("TWEENVS_THINK", TweenVS.UpdateTweens, FrameTime())
--------------------
-- Round Start Function
--------------------
function TweenVS.OnRoundStart()
--this is needed to reset the code if the round restarts, so tweens from the last round dont exist into the next round
TweenVS.PurgeTweens()
end
TweenVS.ROUND_START_EVENT = ListenToGameEvent("round_start", TweenVS.OnRoundStart, nil)
print("TweenVS: Succesfully loaded, version " .. TweenVS._VERSION)
return TweenVS