-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpico-tween-machine-demo.p8
More file actions
803 lines (716 loc) · 17.8 KB
/
pico-tween-machine-demo.p8
File metadata and controls
803 lines (716 loc) · 17.8 KB
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
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
------------
-- PICO-Tween -
-- A small library of tweening/easing
-- functions for use in the PICO-8
-- fantasy console, inspired by Robert
-- Penner's easing functions.
-- Code ported from EmmanuelOga's
-- Lua port of the easing functions.
-- Adapted to work with the PICO-8's
-- math functionality.
--
-- For all easing functions:
--
-- t = elapsed time
--
-- b = begin
--
-- c = change == ending - beginning
--
-- d = move_duration (total time)
--
-- For visual represenations of
-- easing functions, visit this
-- website: http://easings.net/
-- @script PICO-Tween
-- @author Joeb Rogers
-- @license MIT
-- @copyright Joeb Rogers 2018
--- Definition of Pi.
pi = 3.14
--- Cos now uses radians
cos1 = cos function cos(angle) return cos1(angle/(3.1415*2)) end
--- Sin now uses radians
sin1 = sin function sin(angle) return sin1(-angle/(3.1415*2)) end
--- Implementation of asin.
-- Source converted from
-- http://developer.download.nvidia.com/cg/asin.html
function asin(x)
local negate = (x < 0 and 1.0 or 0.0)
x = abs(x)
local ret = -0.0187293
ret *= x
ret += 0.0742610
ret *= x
ret -= 0.2121144
ret *= x
ret += 1.5707288
ret = 3.14159265358979*0.5 - sqrt(1.0 - x)*ret
return ret - 2 * negate * ret
end
--- Implementation of acos.
-- Source converted from
-- http://developer.download.nvidia.com/cg/acos.html
function acos(x)
local negate = (x < 0 and 1.0 or 0.0)
x = abs(x);
local ret = -0.0187293;
ret *= x;
ret += 0.0742610;
ret *= x;
ret -= 0.2121144;
ret *= x;
ret += 1.5707288;
ret *= sqrt(1.0-x);
ret -= 2 * negate * ret;
return negate * 3.14159265358979 + ret;
end
--- Function for calculating
-- exponents to a higher degree
-- of accuracy than using the
-- ^ operator.
-- Function created by samhocevar.
-- Source: https://www.lexaloffle.com/bbs/?tid=27864
-- @param x Number to apply exponent to.
-- @param a Exponent to apply.
-- @return The result of the
-- calculation.
function pow(x,a)
if (a==0) return 1
if (a<0) x,a=1/x,-a
local ret,a0,xn=1,flr(a),x
a-=a0
while a0>=1 do
if (a0%2>=1) ret*=xn
xn,a0=xn*xn,shr(a0,1)
end
while a>0 do
while a<1 do x,a=sqrt(x),a+a end
ret,a=ret*x,a-1
end
return ret
end
function linear(t, b, c, d)
return c * t / d + b
end
function inQuad(t, b, c, d)
return c * pow(t / d, 2) + b
end
function outQuad(t, b, c, d)
t = t / d
return -c * t * (t - 2) + b
end
function inOutQuad(t, b, c, d)
t = t / d * 2
if (t < 1) return c / 2 * pow(t, 2) + b
return -c / 2 * ((t - 1) * (t - 3) - 1) + b
end
function outInQuad(t, b, c, d)
if (t < d / 2) return outQuad(t * 2, b, c / 2, d)
return inQuad((t * 2) - d, b + c / 2, c / 2, d)
end
function inCubic(t, b, c, d)
return c * pow(t / d, 3) + b
end
function outCubic(t, b, c, d)
return c * (pow(t / d - 1, 3) + 1) + b
end
function inOutCubic(t, b, c, d)
t = t / d * 2
if (t < 1) return c / 2 * t * t * t + b
t = t - 2
return c / 2 * (t * t * t + 2) + b
end
function outInCubic(t, b, c, d)
if (t < d / 2) return outCubic(t * 2, b, c / 2, d)
return inCubic((t * 2) - d, b + c / 2, c / 2, d)
end
function inQuart(t, b, c, d)
return c * pow(t/d, 4) + b
end
function outQuart(t, b, c, d)
return -c * (pow(t / d - 1, 4) - 1) + b
end
function inOutQuart(t, b, c, d)
t = t / d * 2
if (t < 1) return c / 2 * pow(t, 4) + b
t = t - 2
return -c / 2 * (pow(t, 4) - 2) + b
end
function outInQuart(t, b, c, d)
if (t < d / 2) return outQuart(t * 2, b, c / 2, d)
return inQuart((t * 2) - d, b + c / 2, c / 2, d)
end
function inQuint(t, b, c, d)
return c * pow(t / d, 5) + b
end
function outQuint(t, b, c, d)
return c * (pow(t / d - 1, 5) + 1) + b
end
function inOutQuint(t, b, c, d)
t = t / d * 2
if (t < 1) return c / 2 * pow(t, 5) + b
return c / 2 * (pow(t - 2, 5) + 2) + b
end
function outInQuint(t, b, c, d)
if (t < d / 2) return outQuint(t * 2, b, c / 2, d)
return inQuint((t * 2) - d, b + c / 2, c / 2, d)
end
function inSine(t, b, c, d)
return -c * cos(t / d * (pi / 2)) + c + b
end
function outSine(t, b, c, d)
return c * sin(t / d * (pi / 2)) + b
end
function inOutSine(t, b, c, d)
return -c / 2 * (cos(pi * t / d) - 1) + b
end
function outInSine(t, b, c, d)
if (t < d / 2) return outSine(t * 2, b, c / 2, d)
return inSine((t * 2) - d, b + c / 2, c / 2, d)
end
function inExpo(t, b, c, d)
if (t == 0) return b
return c * pow(2, 10 * (t / d - 1)) + b - c * 0.001
end
function outExpo(t, b, c, d)
if (t == d) return b + c
return c * 1.001 * (-pow(2, -10 * t / d) + 1) + b
end
function inOutExpo(t, b, c, d)
if (t == 0) return b
if (t == d) return b + c
t = t / d * 2
if (t < 1) return c / 2 * pow(2, 10 * (t - 1)) + b - c * 0.0005
return c / 2 * 1.0005 * (-pow(2, -10 * (t - 1)) + 2) + b
end
function outInExpo(t, b, c, d)
if (t < d / 2) return outExpo(t * 2, b, c / 2, d)
return inExpo((t * 2) - d, b + c / 2, c / 2, d)
end
function inCirc(t, b, c, d)
return (-c * (sqrt(1 - pow(t / d, 2)) - 1) + b)
end
function outCirc(t, b, c, d)
return (c * sqrt(1 - pow(t / d - 1, 2)) + b)
end
function inOutCirc(t, b, c, d)
t = t / d * 2
if (t < 1) return -c / 2 * (sqrt(1 - t * t) - 1) + b
t = t - 2
return c / 2 * (sqrt(1 - t * t) + 1) + b
end
function outInCirc(t, b, c, d)
if (t < d / 2) return outCirc(t * 2, b, c / 2, d)
return inCirc((t * 2) - d, b + c / 2, c / 2, d)
end
function inElastic(t, b, c, d, a, p)
if (t == 0) return b
t /= d
if (t == 1) return b + c
p = p or d * 0.3
local s
if not a or a < abs(c) then
a = c
s = p / 4
else
s = p / (2 * pi) * asin(c/a)
end
t -= 1
return -(a * pow(2, 10 * t) * sin((t * d - s) * (2 * pi) / p)) + b
end
function outElastic(t, b, c, d, a, p)
if (t == 0) return b
t /= d
if (t == 1) return b + c
p = p or d * 0.3
local s
if not a or a < abs(c) then
a = c
s = p / 4
else
s = p / (2 * pi) * asin(c/a)
end
return a * pow(2, -10 * t) * sin((t * d - s) * (2 * pi) / p) + c + b
end
function inOutElastic(t, b, c, d, a, p)
if (t == 0) return b
t = t / d * 2
if (t == 2) return b + c
p = p or d * (0.3 * 1.5)
a = a or 0
local s
if not a or a < abs(c) then
a = c
s = p / 4
else
s = p / (2 * pi) * asin(c / a)
end
if t < 1 then
t -= 1
return -0.5 * (a * pow(2, 10 * t) * sin((t * d - s) * (2 * pi) / p)) + b
end
t -= 1
return a * pow(2, -10 * t) * sin((t * d - s) * (2 * pi) / p ) * 0.5 + c + b
end
function outInElastic(t, b, c, d, a, p)
if (t < d / 2) return outElastic(t * 2, b, c / 2, d, a, p)
return inElastic((t * 2) - d, b + c / 2, c / 2, d, a, p)
end
function inBack(t, b, c, d, s)
s = s or 1.70158
t = t / d
return c * t * t * ((s + 1) * t - s) + b
end
function outBack(t, b, c, d, s)
s = s or 1.70158
t = t / d - 1
return c * (t * t * ((s + 1) * t + s) + 1) + b
end
function inOutBack(t, b, c, d, s)
s = s or 1.70158
s = s * 1.525
t = t / d * 2
if (t < 1) return c / 2 * (t * t * ((s + 1) * t - s)) + b
t = t - 2
return c / 2 * (t * t * ((s + 1) * t + s) + 2) + b
end
function outInBack(t, b, c, d, s)
if (t < d / 2) return outBack(t * 2, b, c / 2, d, s)
return inBack((t * 2) - d, b + c / 2, c / 2, d, s)
end
function outBounce(t, b, c, d)
t = t / d
if t < 1 / 2.75 then
return c * (7.5625 * t * t) + b
elseif t < 2 / 2.75 then
t = t - (1.5 / 2.75)
return c * (7.5625 * t * t + 0.75) + b
elseif t < 2.5 / 2.75 then
t = t - (2.25 / 2.75)
return c * (7.5625 * t * t + 0.9375) + b
end
t = t - (2.625 / 2.75)
return c * (7.5625 * t * t + 0.984375) + b
end
function inBounce(t, b, c, d)
return c - outBounce(d - t, 0, c, d) + b
end
function inOutBounce(t, b, c, d)
if (t < d / 2) return inBounce(t * 2, 0, c, d) * 0.5 + b
return outBounce(t * 2 - d, 0, c, d) * 0.5 + c * .5 + b
end
function outInBounce(t, b, c, d)
if (t < d / 2) return outBounce(t * 2, b, c / 2, d)
return inBounce((t * 2) - d, b + c / 2, c / 2, d)
end
--------
-- tween list and names
--------
tweenlist = {
linear,
inquad,
outquad,
inoutquad,
outinquad,
incubic,
outcubic,
inoutcubic,
outincubic,
inquart,
outquart,
inoutquart,
outinquart,
inquint,
outquint,
inoutquint,
outinquint,
insine,
outsine,
inoutsine,
outinsine,
inexpo,
outexpo,
inoutexpo,
outinexpo,
incirc,
outcirc,
inoutcirc,
outincirc,
inelastic,
outelastic,
inoutelastic,
outinelastic,
inback,
outback,
inoutback,
outinback,
inbounce,
outbounce,
inoutbounce,
outinbounce,
}
tweennames = {
"linear",
"inquad",
"outquad",
"inoutquad",
"outinquad",
"incubic",
"outcubic",
"inoutcubic",
"outincubic",
"inquart",
"outquart",
"inoutquart",
"outinquart",
"inquint",
"outquint",
"inoutquint",
"outinquint",
"insine",
"outsine",
"inoutsine",
"outinsine",
"inexpo",
"outexpo",
"inoutexpo",
"outinexpo",
"incirc",
"outcirc",
"inoutcirc",
"outincirc",
"inelastic",
"outelastic",
"inoutelastic",
"outinelastic",
"inback",
"outback",
"inoutback",
"outinback",
"inbounce",
"outbounce",
"inoutbounce",
"outinbounce",
}
--------------------------------
------------
-- PICO-TweenMachine -
-- An additional small extension
-- library for PICO-Tween that
-- acts as a wrapper, powering
-- all tween related functionality
-- internally, rather than having
-- large chunks of tweening
-- cluttering the codebase.
--
-- @script PICO-TweenMachine
-- @author Joeb Rogers
-- @license MIT
-- @copyright Joeb Rogers 2018
--- A table storing various utility
-- functions used by the ECS.
utilities = {}
--- Assigns the contents of a table to another.
-- Copy over the keys and values from source
-- tables to a target. Assign only shallow copies
-- to the target table. For a deep copy, use
-- deep_assign instead.
-- @param target The table to be copied to.
-- @param source Either a table to copy from,
-- or an array storing multiple source tables.
-- @param multiple Specifies whether source contains
-- more than one table.
-- @return The target table with overwritten and
-- appended values.
function utilities.assign(target, source, multiple)
multiple = multiple or false
if multiple == true then
for count = 1, #source do
target = utilities.assign(target, source[count])
end
return target
else
for k, v in pairs(source) do
target[k] = v;
end
end
return target;
end
--- Deep assigns the contents of a table to another.
-- Copy over the keys and values from source
-- tables to a target. Will recurse through child
-- tables to copy over their keys/values as well.
-- @param target The table to be copied to.
-- @param source Either a table to copy from,
-- or an array storing multiple source tables.
-- @param multipleSource Specifies whether source
-- contains more than one table.
-- @param exclude Either a string or an array of
-- string containing keys to exclude from copying.
-- @param multipleExclude Specifies whether exclude
-- contains more than one string.
-- @return The target table with overwritten and
-- appended values.
function utilities.deep_assign(target, source, multipleSource, exclude, multipleExclude)
multipleSource = multipleSource or false
exclude = exclude or nil
multipleExclude = multipleExclude or false
if multipleSource then
for count = 1, #source do
target = utilities.deep_assign(target, source[count], false, exclude, multipleExclude)
end
return target
else
for k, v in pairs(source) do
local match = false
if multipleExclude then
for count = 1, #exclude do
if (k == exclude[count]) match = true
end
elseif exclude then
if (k == exclude) match = true
end
if not match then
if type(v) == "table" then
target[k] = utilities.deep_assign({}, v, false, exclude, multipleExclude)
else
target[k] = v;
end
end
end
end
return target;
end
--- The main wrapper object
-- of the library.
-- Stores all curent instances
-- of tween objects and drives
-- them.
tween_machine = {
instances = {}
}
--- Calls update() on all current
-- tween instances.
function tween_machine:update()
for t in all(self.instances) do
t:update()
end
end
--- Adds a created tween instance to
-- the table. The passed in object only
-- needs to define the fields it needs
-- to change, the rest will be defaulted
-- to the base tween object.
-- For example:
-- tween_machine:add_tween({
-- func = linear,
-- v_start = 10,
-- v_end = 5
-- })
-- @param instance The tween object to add
-- to the machine.
-- @return Returns the tween object.
function tween_machine:add_tween(instance)
local this =
{
func = nil,
v_start = 0,
v_end = 1,
value = 0,
start_time = 0,
duration = 0,
elapsed = 0,
finished = false,
--- Callbacks
-- Will pass through value
-- as argument.
step_callbacks = {},
-- Will pass through tween
-- object as argument.
finished_callbacks = {}
}
utilities.deep_assign(this, instance)
setmetatable(this, __tween)
add(self.instances, this)
this:init()
return this
end
--- The base table for all tween
-- objects.
-- @field func The easing function
-- to use for this tween.
-- @field v_start The starting value
-- for the tween.
-- @field v_end The end value of the
-- tween.
-- @field value The value between
-- v_start and v_end representing
-- the current tween progress.
-- @field start_time The time at which
-- the tween was started, set in init()
-- via the time() function.
-- @field duration The duration of time
-- the tween should last for.
-- @field elapsed The amount of time
-- elapsed since the tween was started.
-- @field finished A bool for whether
-- or not the tween has finished
-- running.
-- @field step_callbacks A table of
-- registered callback functions.
-- Called in update() after a new
-- value has been set.
-- Will call all registered functions
-- with value as the argument.
-- @field finished_callbacks A table
-- of registered callback functions.
-- Called at the end of update()
-- after the tween has been marked
-- as finished.
-- Will call all registered functions
-- with self as the argument.
__tween = {}
__tween.__index = __tween
--- Registers the passed in function
-- as a step callback, to be called
-- in update() after a new value has
-- been set.
-- @param func The function to be
-- called every step.
function __tween:register_step_callback(func)
add(self.step_callbacks, func)
end
--- Registers the passed in function
-- as a finished callback, to be
-- called at the end of update()
-- after the tween has been marked
-- as finished.
-- @param func The function to be
-- called when finished.
function __tween:register_finished_callback(func)
add(self.finished_callbacks, func)
end
--- Sets the tween's necessary
-- fields prior to being
-- ran.
-- Called automatically when
-- added to the wrapper object
-- or when restarted.
function __tween:init()
self.start_time = time()
self.value = self.v_start
end
--- Restarts the tween's
-- necessary fields in order to be
-- ran again.
function __tween:restart()
self:init()
self.elapsed = 0
self.finished = false
end
--- Updates the tween object.
-- Gets the current value for the
-- tween from the set function and
-- will pass it through all the
-- registered step callbacks.
-- Will set the tween as finished
-- when the elapsed time passes
-- the duration and will pass
-- the tween object to all
-- registered finished callback
-- functions.
-- @return Will return early if
-- the tween is finished or no
-- easing function has been set.
function __tween:update()
if (self.finished or self.func == nil) return
self.elapsed = time() - self.start_time
if (self.elapsed > self.duration) self.elapsed = self.duration
self.value = self.func(
self.elapsed,
self.v_start,
self.v_end - self.v_start,
self.duration
)
if #self.step_callbacks > 0 then
for v in all(self.step_callbacks) do
v(self.value)
end
end
local progress = self.elapsed / self.duration
if (progress >= 1) then
self.finished = true
if #self.finished_callbacks > 0 then
for v in all(self.finished_callbacks) do
v(self)
end
end
end
end
--- Removes the tween from the
-- wrapper object.
function __tween:remove()
del(tween_machine.instances, self)
end
--------
-- main
--------
local move_distance = 30
local move_duration = 1
local tween_index = 1
local dir = 1
local easeprop = 0
function set_ball_position(position)
easeprop = position
end
function reverse_ball_direction(tween)
tween.v_start = tween.v_end
tween.v_end = -tween.v_end
tween:restart()
end
function set_ball_tween()
ball_tween.func = tweenlist[tween_index]
ball_tween:restart()
end
function increment_tween()
tween_index += 1
if tween_index > #tweenlist then
tween_index = 1
end
end
function decrement_tween()
tween_index -= 1
if tween_index <= 0 then
tween_index = #tweenlist
end
end
function _init()
ball_tween = tween_machine:add_tween({
func = tweenlist[tween_index],
v_start = -move_distance,
v_end = move_distance,
duration = move_duration
})
ball_tween:register_step_callback(set_ball_position)
ball_tween:register_finished_callback(reverse_ball_direction)
end
function _update()
if btnp(0) then
decrement_tween()
set_ball_tween()
elseif btnp(1) then
increment_tween()
set_ball_tween()
end
tween_machine:update()
end
function _draw()
rectfill(0, 0, 128, 128, 3)
circfill(64, 60 + easeprop, 20, 15)
print("function: "..tweennames[tween_index], 5, 120)
print("⬅️ ➡️", 100, 120)
end