fireworks.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. -- title: fireworks test
  2. -- author: pixelbath
  3. -- desc: description
  4. -- script: lua
  5. t=0
  6. colors = {
  7. {3,2,1},
  8. {5,6,7},
  9. {11,10,9},
  10. {15,14,13},
  11. {12,4,2}, -- fuse colors
  12. }
  13. seeds = {}
  14. flashy_color = 1
  15. fps = {
  16. last_time = time(),
  17. frames = 0,
  18. val = 0,
  19. }
  20. -- basic todo:
  21. -- seed
  22. -- explosion shape
  23. -- types:
  24. -- willow - each star trails particles with low inertia
  25. -- grains of wheat / candles - shooting upward, possibly at angles
  26. -- rainbow chrysanthemums - red/white/blue spherical burst, ends with trail of sparks
  27. -- orange peony - similar to mums, but no trail of sparks
  28. -- peacock feather / dragonfly - lots of blue/orange with long tails
  29. -- green bees - spherical burst, curving trajectories with long green tails
  30. -- crossette - multiple large stars that burst into smaller stars with a loud crackling
  31. -- falling leaves - stars twinkle and flutter down
  32. function create_star(xpos, ypos, xvel, yvel, colorgroup)
  33. local group = colorgroup or math.random(3)+1
  34. local new_star = {
  35. vel_x = xvel,
  36. vel_y = yvel,
  37. color = colors[group][1],
  38. color_group = group,
  39. x = xpos, y = ypos,
  40. num_children = 0,
  41. t = 0, det = 200,
  42. shape = nil,
  43. is_seed = false,
  44. is_spark = false,
  45. has_trail = false,
  46. }
  47. return new_star
  48. end
  49. function fireworks_update()
  50. local delete_seeds = {}
  51. for i = 1, #seeds do
  52. local this_seed = seeds[i]
  53. local will_delete = false
  54. -- friction and gravity
  55. this_seed.vel_y = this_seed.vel_y + 0.011
  56. this_seed.x = this_seed.x + (this_seed.vel_x * 0.9)
  57. this_seed.y = this_seed.y + (this_seed.vel_y * 0.9)
  58. -- ways to remove particles
  59. if this_seed.y > 136 then
  60. will_delete = true
  61. end
  62. if this_seed.det > 0 and this_seed.t > this_seed.det then
  63. spawn_explosion(this_seed)
  64. will_delete = true
  65. end
  66. if this_seed.is_seed then
  67. if this_seed.vel_y > -0.1 and math.random() > 10 * math.abs(this_seed.vel_y) then
  68. spawn_explosion(this_seed)
  69. will_delete = true
  70. end
  71. end
  72. -- spawn a trail
  73. if this_seed.is_seed then
  74. local spark = create_star(this_seed.x, this_seed.y, math.random() * 0.5 - 0.25, math.random() * 0.5 - 0.25, 5)
  75. spark.det = 12
  76. spark.is_spark = true
  77. table.insert(seeds, spark)
  78. end
  79. if this_seed.has_trail then
  80. local spark = create_star(this_seed.x, this_seed.y, math.random() * 0.5 - 0.25, math.random() * 0.5 - 0.25, this_seed.color_group)
  81. spark.det = 6
  82. table.insert(seeds, spark)
  83. end
  84. -- color updates
  85. if this_seed.det > 0 then
  86. local perc = this_seed.t / this_seed.det
  87. if perc > 0.3 then this_seed.color = colors[this_seed.color_group][2] end
  88. if perc > 0.6 then this_seed.color = colors[this_seed.color_group][3] end
  89. end
  90. -- draw this guy
  91. if not this_seed.is_seed then pix(this_seed.x, this_seed.y, this_seed.color) end
  92. this_seed.t = this_seed.t + 1
  93. if will_delete then table.insert(delete_seeds, i) end
  94. end
  95. for i = #delete_seeds, 1, -1 do
  96. table.remove(seeds, delete_seeds[i])
  97. end
  98. end
  99. function spawn_explosion(star)
  100. if star.num_children == 0 then return end
  101. if star.shape == nil then star.shape = 'peony' end
  102. if star.shape == 'peony' then
  103. local angle = 2 * math.pi / star.num_children
  104. for i = 1, star.num_children do
  105. local xvel = math.cos(angle * i) * ((math.random() * 0.7) + 0.05)
  106. local yvel = math.sin(angle * i) * ((math.random() * 0.7) + 0.05)
  107. local child_star = create_star(star.x, star.y, xvel, yvel, star.color_group)
  108. child_star.det = math.random() * 30 + 50
  109. child_star.has_trail = true
  110. table.insert(seeds, child_star)
  111. end
  112. end
  113. end
  114. function TIC()
  115. if btnp(4) then
  116. local seed_yvel = -1 + (math.random() * -1)
  117. local seed = create_star(math.random(240), 136, 0, seed_yvel)
  118. seed.shape = 'peony'
  119. seed.num_children = 150
  120. seed.is_seed = true
  121. table.insert(seeds, seed)
  122. end
  123. cls(0)
  124. fireworks_update()
  125. if t % 3 == 0 then flashy_color = math.floor(math.random(14)) + 1 end
  126. print("WELL DONE!", 240/2-35, 10, 12, false, 1, false)
  127. print("Time: 30:00", 240/2-26, 28, 12, false, 1, true)
  128. print("Deaths: 10", 240/2-26, 36, 12, false, 1, true)
  129. print("Cat memes: 25/25", 240/2-38, 44, 12, false, 1, true)
  130. print("More motivational text!", 240/2-48, 60, flashy_color, false, 1, true)
  131. print("Press A to continue", 240/2-42, 90, 12, false, 1, true)
  132. print("Press B to go to the main menu", 240/2-58, 98, 12, false, 1, true)
  133. local fps_val = 0
  134. if (time()-fps.last_time <= 1000) then
  135. fps.frames=fps.frames+1
  136. else
  137. fps.val=fps.frames
  138. fps.frames = 0
  139. fps.last_time = time()
  140. end
  141. print("fps: "..fps.val..", particles: "..#seeds, 2, 128, 12, false, 1, true)
  142. t=t+1
  143. end
  144. -- <WAVES>
  145. -- 000:00000000ffffffff00000000ffffffff
  146. -- 001:0123456789abcdeffedcba9876543210
  147. -- 002:0123456789abcdef0123456789abcdef
  148. -- </WAVES>
  149. -- <SFX>
  150. -- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
  151. -- </SFX>
  152. -- <PALETTE>
  153. -- 000:000004610018da3038fa484ceec2612cce50108540083c2404366f042c79045dda1085fff4f4f42c0038690071a508b2
  154. -- </PALETTE>