neuttower/timer.jor

39 lines
1.2 KiB
Plaintext
Executable file

( timer + lerping )
: clamp0 ( range val -- i )
2dup <= if drop else
dup 0 <= if drop drop 0 else
swap drop then then ;
: >ratio ( range value -- f )
over swap clamp0 swap />ratio ;
: <ratio ( range ratio -- v ) *<ratio ;
: >range ( start end -- start range ) over - ;
: <range ( start range -- start end ) over + ;
: lerpr ( start end ratio ) >r >range <r <ratio + ;
: lerpn ( start1 end1 start2 end2 val )
>r >range <r <rot - >ratio lerpr ;
: lerp ( start end duration start -- i )
ticks udelta ( start end duration delta )
>ratio lerpr ;
: triggered ( duration timer -- b )
dup >r @ ticks udelta ( duration delta )
2dup <= if drop <r +! 1 else drop drop rdrop 0 then ;
: now! ( timer -- ) ticks swap ! ;
: advance! ( timer -- delta )
dup @ ticks udelta ( timer delta )
dup <rot +! ;
: move-to ( target speed p -- )
dup >r @ >rot ticks ( from to duration start )
begin
4dup lerp r@ !
<rot dup r@ @ != ( from duration start to !done )
while
>rot suspend
repeat rdrop drop drop drop drop ;
: still-waiting? ( count ticks-start -- ) ticks udelta u> ;
: sleep ( count -- )
ticks begin 2dup still-waiting? while suspend repeat drop drop ;