Skip to content

Instantly share code, notes, and snippets.

@sogaiu

sogaiu/trail-fun.janet

Last active Apr 11, 2021
Embed
What would you like to do?
trail fun
(use ./build/jaylib)
(set-config-flags
:msaa-4x-hint)
(init-window 800 600 "Chart")
(init-audio-device)
(set-target-fps 60)
(def current-animations @{})
(def current-colors @{})
(def big 50)
(def small 30)
(def frames 30)
(def hex-colors
[[0 0 0]
[200 200 200]
[130 130 130]
[80 80 80]
[253 249 0]
[255 203 0]
[255 161 0]
[255 109 194]
[230 41 55]
[190 33 55]
[0 228 48]
[0 158 47]
[0 117 44]
[102 191 255]
[0 121 241]
[0 82 172]
[200 122 255]
[135 60 190]
[112 31 126]
[211 176 131]
[127 106 79]
[76 63 47]])
(def end-color-idx 10)
(def colors
(map (fn [[r g b]]
[(/ r 255.0) (/ g 255.0) (/ b 255.0)])
hex-colors))
(def n-colors
(length colors))
(def rects
(seq [x :range [20 (+ 40 (get-screen-width)) 40]
y :range [20 (+ 40 (get-screen-height)) 40]
:let [r @[x y small small]]]
r))
(defn bigger-anim
[r]
(def w 2)
(def h 3)
(loop [i :range [0 frames]
:let [p (/ i frames)
d (* (- big small) (* 1.05 p p))
v (+ small d)]] # here's the tweening
(put r w v)
(put r h v)
(put current-colors r (math/round (* n-colors p)))
(yield))
(put current-colors r end-color-idx)
(put current-animations r nil))
(defn smaller-anim
[r]
(def w 2)
(def h 3)
(loop [i :range [0 frames]
:let [p (/ i frames)
d (* (- big small) (* 1.05 p p))
v (- big d)]] # here's the tweening
(put r w v)
(put r h v)
(put current-colors r (math/round (* n-colors p)))
(yield))
(put current-colors r end-color-idx)
(put current-animations r nil))
(defn make-bigger
[r]
(when (and (not (current-animations r))
(< (r 2) (inc small)))
(def anim (fiber/new |(bigger-anim r)))
(put current-animations r anim)))
(defn make-smaller
[r]
(when (and (not (current-animations r))
(> (r 2) (dec big)))
(def anim (fiber/new |(smaller-anim r)))
(put current-animations r anim)))
(while (not (window-should-close))
(begin-drawing)
(clear-background [0 0 0])
(def [mx my] (get-mouse-position))
(def mr [(- mx 15) (- my 15) small small])
(draw-rectangle-rec mr :blue)
(loop [a :in current-animations]
(resume a))
(loop [r :in rects
:let [coll (check-collision-recs
r
mr)]]
(if coll
(make-bigger r)
(make-smaller r))
(draw-rectangle-rec r (if-let [color-idx (current-colors r)]
(colors color-idx)
:green)))
(end-drawing))
(close-window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment