Last active
August 29, 2015 13:56
-
-
Save chribben/9239033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import List | |
import Mouse | |
import Window | |
--MODEL | |
defaultBall = {x = 0, y = 0, v = toFloat 0, angle = toFloat 0} | |
--UPDATE | |
location t b = { b | x <- b.x - t * b.v * cos b.angle, | |
y <- b.y - t * b.v * sin b.angle } | |
velocity (x,y) b = { b | v <- sqrt((b.y-y)^2 + (b.x-x)^2)/10, | |
angle <- atan2 (b.y-y) (b.x-x)} | |
updateBall (t, xy) = location t . velocity xy | |
--INPUT | |
input = let delta = lift (\t -> t/20) (fps 40) | |
in sampleOn delta (lift2 (,) delta ((keepWhen Mouse.isDown (0,0) (translate <~ Mouse.position ~ Window.dimensions)))) | |
--DISPLAY | |
display balls (w, h) (x,y)= | |
flow down | |
[ | |
[markdown|_Press and drag using mouse left button_|], | |
collage w h | |
<| List.map (\b -> move (b.x, b.y) <| filled red (circle 10.0) ) balls | |
] | |
ball = foldp updateBall defaultBall input | |
balls = combine <| List.map (\d -> delay (d*100) ball) [0..4] | |
main = lift3 display balls Window.dimensions (floatify <~ Mouse.position) | |
--HELPERS | |
floatify (x,y) = (toFloat x, toFloat y) | |
translate (x,y) (w,h)= (toFloat x - toFloat w/2, toFloat h/2 - toFloat y + 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment