Advertisement
Guest User

vbob

a guest
Feb 10th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import Graphics.Element exposing (..)
  2. import Graphics.Collage exposing (..)
  3. import Mouse
  4. import Color exposing (..)
  5. import Time
  6. import Keyboard
  7.  
  8.  
  9. type alias Vec2 = (Float, Float)
  10.  
  11. type alias Particle = {pos: Vec2, vel: Vec2, acc: Vec2, color: Color}
  12. type alias Model = List Particle
  13. type Action = Step Float Vec2 | AddParticle Vec2 | Explode Vec2
  14.  
  15. model: Model
  16. model = []
  17.  
  18.  
  19. scene: Model -> Element
  20. scene model =
  21. collage (fst size) (snd size) (List.map renderParticle model)
  22.  
  23. newParticle: Vec2 -> Color -> Particle
  24. newParticle pos color =
  25. {pos = pos, vel = (0,0), acc = (1,0), color = (hsla (fst pos) (snd pos) 0.6 0.7)}
  26.  
  27.  
  28. clickPosition: Signal (Float, Float)
  29. clickPosition = Signal.sampleOn Mouse.clicks realMouse
  30.  
  31. renderParticle: Particle -> Form
  32. renderParticle particle =
  33. circle 5
  34. |> filled particle.color
  35. |> move particle.pos
  36.  
  37.  
  38.  
  39. size = (500, 500)
  40.  
  41. mouseToScene (w,h) (x,y) =
  42. (toFloat x - w/2, h/2 - toFloat y)
  43.  
  44. addVec: Vec2 -> Vec2 -> Vec2
  45. addVec (a,b) (c,d) = (a + c, b + d)
  46.  
  47.  
  48. mulVec: Vec2 -> Float -> Vec2
  49. mulVec (a,b) m = (a*m, b*m)
  50.  
  51.  
  52. subVec: Vec2 -> Vec2 -> Vec2
  53. subVec a b = addVec a (mulVec b -1)
  54.  
  55.  
  56. gravity: Float -> Particle -> Particle
  57. gravity dt p =
  58. -- {p | acc = (fst p.acc, -9.8)}
  59. {p | acc = (fst p.acc, -55.8)}
  60.  
  61. physics: Float -> Particle -> Particle
  62. physics dt p =
  63. {p | pos = addVec p.pos (mulVec p.vel (1/dt)), vel = addVec p.vel (mulVec p.acc (1/dt)), acc = (0,0)}
  64.  
  65.  
  66.  
  67. explode: Vec2 -> Model -> Model
  68. explode pos model =
  69. List.map (\p -> {p | vel = mulVec (subVec p.pos pos) 2}) model
  70.  
  71. slowDown: Particle -> Particle
  72. slowDown p = {p | vel = mulVec p.vel 0.99}
  73.  
  74. mousePull pos dt p =
  75. {p | acc = addVec p.acc (subVec pos p.pos)}
  76.  
  77. stepUpdate dt pos model =
  78. List.map (\m -> m |> physics dt |> gravity dt |> mousePull pos dt |> slowDown) model
  79.  
  80. addParticle: Vec2 -> Model -> Model
  81. addParticle clickPos model =
  82. (newParticle clickPos red) :: model
  83.  
  84. realMouse = Signal.map (mouseToScene size) Mouse.position
  85.  
  86.  
  87. spaceLocation: Signal Vec2
  88. spaceLocation = Signal.sampleOn Keyboard.space realMouse
  89.  
  90. --timeAndPosition: Signal Step Float Vec2
  91. timeAndPosition =
  92. Signal.map2 Step (Time.fps 50) realMouse
  93.  
  94.  
  95. input: Signal Action
  96. input =
  97. Signal.mergeMany [
  98. (Signal.sampleOn (Time.fps 50) timeAndPosition),
  99. (Signal.map AddParticle clickPosition),
  100. (Signal.map Explode spaceLocation)]
  101.  
  102. update: Action -> Model -> Model
  103. update action model =
  104. case action of
  105. AddParticle pos -> addParticle pos model
  106. Step dt pos -> stepUpdate dt pos model
  107. Explode pos -> explode pos model
  108.  
  109. modelSignal: Signal Model
  110. modelSignal =
  111. Signal.foldp update model input
  112.  
  113. main : Signal Element
  114. main =
  115. Signal.map scene modelSignal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement