Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. -- a little flame in a functional style
  2.  
  3. function particle(step, style, t)
  4. local newStyle = step(style, t)
  5. return {
  6. style = newStyle,
  7. next = function()
  8. return particle(step, newStyle, t + 1)
  9. end
  10. }
  11. end
  12.  
  13. function flame(particles, t)
  14. local newParticles = evolve(particles, t)
  15. return {
  16. particles = newParticles,
  17. next = function()
  18. return flame(newParticles, t + 1)
  19. end
  20. }
  21. end
  22.  
  23. function evolve(particles, t)
  24. return generate(mutate(prune(particles)), t)
  25. end
  26.  
  27. function mutate(particles)
  28. return map(particles, function(p)
  29. return p.next()
  30. end)
  31. end
  32.  
  33. function prune(particles)
  34. return filter(particles, function(p)
  35. return p.style.a > 0
  36. end)
  37. end
  38.  
  39. function each(things, fn)
  40. for i, thing in ipairs(things) do
  41. fn(thing)
  42. end
  43. end
  44.  
  45. function map(things, convert)
  46. local mapped = {}
  47. each(things, function(thing)
  48. table.insert(mapped, convert(thing))
  49. end)
  50. return mapped
  51. end
  52.  
  53. function cons(things, ...)
  54. local all = {}
  55. each(things, function(t)
  56. table.insert(all, t)
  57. end)
  58. each({...}, function(t)
  59. table.insert(all, t)
  60. end)
  61. return all
  62. end
  63.  
  64. function filter(things, fn)
  65. local filtered = {}
  66. for i, thing in ipairs(things) do
  67. if fn(thing) then
  68. table.insert(filtered, thing)
  69. end
  70. end
  71. return filtered
  72. end
  73.  
  74. function spark(style, t)
  75. return {
  76. x = style.x - math.sin(t * 0.436),
  77. y = style.y + 1,
  78. r = style.r + (math.sin(t * style.o) * 40),
  79. g = style.g + (math.sin(t * style.o) * 33),
  80. b = style.b + (math.sin(t * style.o) * 15),
  81. a = (style.a - 4 * style.o) * 0.97,
  82. o = style.o * 0.977,
  83. radius = 5 - t * 0.03
  84. }
  85. end
  86.  
  87. function generate(particles, t)
  88. return cons(particles,
  89. particle(spark, {
  90. x = -3,
  91. y = 2,
  92. r = 255,
  93. g = 40,
  94. b = 10,
  95. a = 220,
  96. o = 0.5
  97. } , 9),
  98. particle(spark, {
  99. x = 2,
  100. y = 2,
  101. r = 150,
  102. g = 73,
  103. b = 10,
  104. a = 220,
  105. o = 0.5
  106. } , 4),
  107. particle(spark, {
  108. x = -1,
  109. y = 1,
  110. r = 255,
  111. g = 128,
  112. b = 51,
  113. a = 220,
  114. o = math.sin(t) * 0.9
  115. }, t % 10),
  116. particle(spark, {
  117. x = -2,
  118. y = 0,
  119. r = 111,
  120. g = 40,
  121. b = 40,
  122. a = 191,
  123. o = 0.714
  124. } , 9),
  125. particle(spark, {
  126. x = 1,
  127. y = 0,
  128. r = 170,
  129. g = 83,
  130. b = 32,
  131. a = 220,
  132. o = 0.4 + (math.sin(t) * 0.362)
  133. }, t % 3),
  134. particle(spark, {
  135. x = 0.7,
  136. y = 1,
  137. r = 168,
  138. g = 83,
  139. b = 5,
  140. a = 250,
  141. o = 0.4
  142. }, t % 4),
  143. particle(spark, {
  144. x = 1,
  145. y = 0,
  146. r = 177,
  147. g = 72,
  148. b = 17,
  149. a = 219,
  150. o = 1.154
  151. }, 0),
  152. particle(spark, {
  153. x = -0.5,
  154. y = 1,
  155. r = 189,
  156. g = 20,
  157. b = 17,
  158. a = 219,
  159. o = 0.6
  160. }, 3)
  161. )
  162. end
  163.  
  164.  
  165.  
  166. f = flame({}, 0)
  167.  
  168. function drawParticle(p)
  169. love.graphics.setColor(p.style.r, p.style.g, p.style.b, p.style.a)
  170. love.graphics.circle('fill', p.style.x, p.style.y, p.style.radius)
  171. end
  172.  
  173. local WIDTH, HEIGHT = love.graphics.getWidth(), love.graphics.getHeight()
  174. function love.draw()
  175. love.graphics.setBackgroundColor(59, 51, 82, 255)
  176. love.graphics.translate(WIDTH / 2, HEIGHT / 2)
  177. each(f.particles, function(p)
  178. drawParticle(p)
  179. end)
  180. f = f.next()
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement