Advertisement
Guest User

Codea - Fireworks

a guest
Dec 26th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.57 KB | None | 0 0
  1. -- Particle Sim Fireworks
  2.  
  3. -- Use this function to perform your initial setup
  4. function setup()
  5.     p = ParticleSim()
  6.     p:addSpawner("shooter", "touch_began", {})
  7.     p:addDrawLayer("shooter", color(255, 127, 0, 75), 30, 1, 10, 2)
  8.     p:addDrawLayer("shooter", color(255, 192, 0, 50), 20, 2, 10, 2)
  9.     p:addDrawLayer("shooter", color(255, 255, 0, 25), 10, 3, 5, 2)
  10.     p:addBehavior("shooter", "init_velocity", {["x"] = {-1, 1}, ["y"] = {5, 6}})
  11.     p:addBehavior("shooter", "gravity", {["x"] = 0, ["y"] = -0.007, ["yv"] = {0.5, 0.9}, ["xv"] = {-0.1, 0.1}})
  12.     p:addBehavior("shooter", "timeout", {["time"] = 100})
  13.     p:addBehavior("shooter", "loop_spawn_particle", {["time"] = 10, ["type"] = "smoke"})
  14.     p:addDrawLayer("smoke", color(50, 100), 15, -10, 15, 2)
  15.     p:addDrawLayer("smoke", color(50, 100), 5, -9, 15, 2)
  16.     p:addBehavior("smoke", "init_velocity", {["x"] = 0, ["y"] = 0})
  17.     p:addBehavior("smoke", "wander", {["amount"] = 0.2})
  18.     p:addBehavior("smoke", "chance_timeout", {["chance"] = 60})
  19.     p:addBehavior("shooter", "loop_spawn_particle", {["type"] = "trail", ["time"] = 10})
  20.     p:addDrawLayer("trail", color(255, 0, 0, 75), 20, -8, 10, 2)
  21.     p:addDrawLayer("trail", color(255, 64, 0, 75), 10, -7, 10, 2)
  22.     p:addBehavior("trail", "wander", {["amount"] = 0.1})
  23.     p:addBehavior("trail", "gravity", {["x"] = 0, ["y"] = -0.001})
  24.     p:addBehavior("trail", "timeout", {["time"] = 80})
  25.     p:addBehavior("shooter", "spawn_particle", {["amount"] = {8, 12}, ["time"] = 0, ["type"] = "ember"}, "despawn")
  26.     p:addBehavior("shooter", "spawn_particle", {["amount"] = {15, 20}, ["time"] = 0, ["type"] = "smoke"}, "despawn")
  27.     p:addDrawLayer("ember", color(255, 192, 0, 75), 20, -6, 10, 2)
  28.     p:addDrawLayer("ember", color(255, 255, 0, 75), 10, -5, 10, 2)
  29.     p:addBehavior("ember", "wander", {["amount"] = {4, 5}}, "spawn")
  30.     p:addBehavior("ember", "gravity", {["x"] = 0, ["y"] = -0.007})
  31.     p:setSlowdown("ember", 0.96)
  32.     p:addBehavior("ember", "timeout", {["time"] = 200})
  33.     p:addBehavior("ember", "loop_spawn_particle", {["type"] = "smoke", ["amount"] = 1, ["time"] = 15})
  34.     p:addBehavior("ember", "chance_spawn_particle", {["type"] = "spark", ["chance"] = 20})
  35.     p:addDrawLayer("spark", color(128, 0, 255, 50), 15, -12, 10, 3)
  36.     p:addDrawLayer("spark", color(255, 0, 255, 50), 5, -11, 10, 3)
  37.     p:addBehavior("spark", "wander", {["amount"] = {2, 3}}, "spawn")
  38.     p:addBehavior("spark", "gravity", {["x"] = 0, ["y"] = -0.003})
  39.     p:addBehavior("spark", "chance_timeout", {["chance"] = 50})
  40. end
  41.  
  42. -- This function gets called once every frame
  43. function draw()
  44.     background(0, 0, 0, 255)
  45.     p:draw()
  46. end
  47.  
  48. ParticleSim = class()
  49.  
  50. function ParticleSim:init()
  51.     self.particles = {}
  52.     self.types = {}
  53.     self.pEllipses = {}
  54.     self.spawnerTypes = {
  55.     "touch_began",
  56.     "touch_move",
  57.     "touch_ended",
  58.     "loop_random_pos",
  59.     "loop_set_pos",
  60.     "chance_random_pos",
  61.     "chance_set_pos"
  62.     }
  63.     self.behaviorTypes = {
  64.     "timeout",
  65.     "chance_timeout",
  66.     "spawn_particle",
  67.     "chance_spawn_particle",
  68.     "loop_spawn_particle",
  69.     "wander",
  70.     "move_straight",
  71.     "gravity",
  72.     "timeout_radius",
  73.     "loop_change_direction",
  74.     "chance_change_direction",
  75.     "init_velocity"
  76.     }
  77.     self.timer = 0
  78.     self.remove = {}
  79. end
  80.  
  81. function ParticleSim:newType(name)
  82.     if self.types[name] == nil then
  83.         self.types[name] = {
  84.         ["drawLayers"] = {},
  85.         ["spawners"] = {},
  86.         ["slowdown"] = 0.9,
  87.         ["behaviors"] = {
  88.         ["default"] = {},
  89.         ["spawn"] = {},
  90.         ["duplicate"] = {},
  91.         ["despawn"] = {}
  92.         }
  93.         }
  94.     end
  95. end
  96.  
  97. function ParticleSim:setSlowdown(t, s)
  98.     self:newType(t)
  99.     self.types[t]["slowdown"] = s
  100. end
  101.  
  102. function ParticleSim:addDrawLayer(name, col, size, layer, cVariation, sVariation)
  103.     self:newType(name)
  104.     local cv = 0
  105.     if cVariation ~= nil then
  106.         cv = cVariation
  107.     end
  108.     local sv = 0
  109.     if sVariation ~= nil then
  110.         sv = sVariation
  111.     end
  112.     table.insert(self.types[name]["drawLayers"], {col, size, layer, cv, sv})
  113. end
  114.  
  115. function ParticleSim:addSpawner(name, spawner, params)
  116.     self:newType(name)
  117.     local isValid = false
  118.     for k, v in pairs(self.spawnerTypes) do
  119.         if v == spawner then
  120.             isValid = true
  121.         end
  122.     end
  123.     if isValid then
  124.         table.insert(self.types[name]["spawners"], {[spawner] = params})
  125.     end
  126. end
  127.  
  128. function ParticleSim:addBehavior(name, behavior, params, trigger)
  129.     self:newType(name)
  130.     local isValid = false
  131.     for k, v in pairs(self.behaviorTypes) do
  132.         if v == behavior then
  133.             isValid = true
  134.         end
  135.     end
  136.     local t
  137.     if trigger ~= "spawn" and trigger ~= "duplicate" and trigger ~= "despawn" then
  138.         t = "default"
  139.     else
  140.         t = trigger
  141.     end
  142.     if isValid then
  143.         table.insert(self.types[name]["behaviors"][t], {[behavior] = params})
  144.     end
  145. end
  146.  
  147. function ParticleSim:doSpawners()
  148.     for t, d in pairs(self.types) do
  149.         for bn, tb in pairs(d["spawners"]) do
  150.             -- Touch began
  151.             if tb["touch_began"] ~= nil then
  152.                 if CurrentTouch.state == BEGAN then
  153.                     self:addParticle(t, CurrentTouch.x, CurrentTouch.y, 0, 0)
  154.                 end
  155.             end
  156.             -- Touch moved
  157.             if tb["touch_move"] ~= nil then
  158.                 if CurrentTouch.state == MOVING then
  159.                     self:addParticle(t, CurrentTouch.x, CurrentTouch.y, 0, 0)
  160.                 end
  161.             end
  162.             -- Touch ended
  163.             if tb["touch_ended"] ~= nil then
  164.                 if CurrentTouch.state == ENDED then
  165.                     self:addParticle(t, CurrentTouch.x, CurrentTouch.y, 0, 0)
  166.                 end
  167.             end
  168.             -- Loop at random postion
  169.             if tb["loop_random_pos"] ~= nil then
  170.                 if tb["loop_random_pos"]["time"] == nil then
  171.                     tb["loop_random_pos"]["time"] = 20
  172.                 end
  173.                 if self.timer % tb["loop_random_pos"]["time"] == 0 then
  174.                     self:addParticle(t, math.random(0, WIDTH), math.random(0, HEIGHT), 0, 0)
  175.                 end
  176.             end
  177.             -- Loop at set postion
  178.             if tb["loop_set_pos"] ~= nil then
  179.                 if tb["loop_set_pos"]["time"] == nil then
  180.                     tb["loop_set_pos"]["time"] = 20
  181.                 end
  182.                 if tb["loop_set_pos"]["x"] == nil then
  183.                     tb["loop_set_pos"]["x"] = WIDTH / 2
  184.                 end
  185.                 if tb["loop_set_pos"]["y"] == nil then
  186.                     tb["loop_set_pos"]["y"] = HEIGHT / 2
  187.                 end
  188.                 if self.timer % tb["loop_set_pos"]["time"] == 0 then
  189.                     self:addParticle(t, tb["loop_set_pos"]["x"], tb["loop_set_pos"]["y"], 0, 0)
  190.                 end
  191.             end
  192.             -- Random chance at random postion
  193.             if tb["chance_random_pos"] ~= nil then
  194.                 if tb["chance_random_pos"]["chance"] == nil then
  195.                     tb["chance_random_pos"]["chance"] = 15
  196.                 end
  197.                 if math.random(1, tb["chance_random_pos"]["chance"]) == 1 then
  198.                     self:addParticle(t, math.random(0, WIDTH), math.random(0, HEIGHT), 0, 0)
  199.                 end
  200.             end
  201.             -- Random chance at set postion
  202.             if tb["chance_set_pos"] ~= nil then
  203.                 if tb["chance_set_pos"]["chance"] == nil then
  204.                     tb["chance_set_pos"]["chance"] = 15
  205.                 end
  206.                 if tb["chance_set_pos"]["x"] == nil then
  207.                     tb["chance_set_pos"]["x"] = WIDTH / 2
  208.                 end
  209.                 if tb["chance_set_pos"]["y"] == nil then
  210.                     tb["chance_set_pos"]["y"] = HEIGHT / 2
  211.                 end
  212.                 if math.random(1, tb["chance_set_pos"]["chance"]) == 1 then
  213.                     self:addParticle(t, tb["chance_set_pos"]["x"], tb["chance_set_pos"]["y"], 0, 0)
  214.                 end
  215.             end
  216.         end
  217.     end
  218. end
  219.  
  220. function ParticleSim:addParticle(t, x, y, xv, yv)
  221.     local isValid = false
  222.     for k, v in pairs(self.types) do
  223.         if k == t then
  224.             isValid = true
  225.         end
  226.     end
  227.     if isValid then
  228.         local va = {}
  229.         for k, v in pairs(self.types[t]["drawLayers"]) do
  230.             local c = v[4]
  231.             local s = v[5]
  232.             table.insert(va, {math.random(-s, s), math.random(-c, c), math.random(-c, c), math.random(-c, c), math.random(-c, c)})
  233.         end
  234.         table.insert(self.particles, {
  235.         ["type"] = t,
  236.         ["pos"] = vec2(x, y),
  237.         ["vel"] = vec2(xv, yv),
  238.         ["data"] = self:copyTWithRandomizers(self.types[t]),
  239.         ["var"] = self:copyT(va)
  240.         })
  241.     end
  242. end
  243.  
  244. function ParticleSim:getPEllipses()
  245.     for _, p in pairs(self.particles) do
  246.         local pes = p["data"]["drawLayers"]
  247.         for k, pe in pairs(pes) do
  248.             local c = color(pe[1].r, pe[1].g, pe[1].b, pe[1].a)
  249.             c.r = c.r + p["var"][k][2]
  250.             c.g = c.g + p["var"][k][3]
  251.             c.b = c.b + p["var"][k][4]
  252.             c.a = c.a + p["var"][k][5]
  253.             self:addPEllipse(p["pos"], c, pe[2] + p["var"][k][1], pe[3])
  254.         end
  255.     end
  256. end
  257.  
  258. function ParticleSim:addPEllipse(pos, col, size, layer)
  259.     if self.pEllipses[layer] == nil then
  260.         self.pEllipses[layer] = {}
  261.     end
  262.     table.insert(self.pEllipses[layer], {pos, col, size})
  263. end
  264.  
  265. function ParticleSim:drawPEllipses()
  266.     self.pEllipses = {}
  267.     self:getPEllipses()
  268.     if self.pEllipses ~= {} then
  269.         local min = 9999
  270.         while min < 1000000000000 do
  271.             min = 1000000000000
  272.             for k, v in pairs(self.pEllipses) do
  273.                 if k < min then
  274.                     min = k
  275.                 end
  276.             end
  277.             if self.pEllipses[min] ~= nil then
  278.                 for _, v in pairs(self.pEllipses[min]) do
  279.                     pushStyle()
  280.                     fill(v[2])
  281.                     noStroke()
  282.                     ellipse(v[1].x, v[1].y, v[3], v[3])
  283.                     popStyle()
  284.                 end
  285.             end
  286.             self.pEllipses[min] = nil
  287.         end
  288.     end
  289. end
  290.  
  291. function ParticleSim:pBehavior(i, t, tri)
  292.     local d = self.particles[i]
  293.     for bn, tb in pairs(self.particles[i]["data"]["behaviors"][t]) do
  294.         if tri then
  295.             if d["spawnTimer"] == nil then
  296.                 d["spawnTimer"] = 0
  297.             else
  298.                 d["spawnTimer"] = d["spawnTimer"] + 1
  299.             end
  300.             if d["spawnTimer"] <= 1 then
  301.                 if tri then
  302.                     self:pBehavior(i, "spawn", false)
  303.                 end
  304.             end
  305.         end
  306.         -- Delete particles after a set amount of time
  307.         if tb["timeout"] ~= nil then
  308.             if tb["timeout"]["currentTime"] == nil then
  309.                 tb["timeout"]["currentTime"] = 0
  310.             else
  311.                 tb["timeout"]["currentTime"] = tb["timeout"]["currentTime"] + 1
  312.             end
  313.             if tb["timeout"]["time"] == nil then
  314.                 tb["timeout"]["time"] = 120
  315.             end
  316.             if tb["timeout"]["currentTime"] == tb["timeout"]["time"] then
  317.                 table.insert(self.remove, i)
  318.                 if tri then
  319.                     self:pBehavior(i, "despawn", false)
  320.                 end
  321.             end
  322.         end
  323.         -- Delete particles at random each frame
  324.         if tb["chance_timeout"] ~= nil then
  325.             if tb["chance_timeout"]["chance"] == nil then
  326.                 tb["chance_timeout"]["chance"] = 100
  327.             end
  328.             if math.random(1, tb["chance_timeout"]["chance"]) == 1 then
  329.                 table.insert(self.remove, i)
  330.                 if tri then
  331.                     self:pBehavior(i, "despawn", false)
  332.                 end
  333.             end
  334.         end
  335.         -- Spawn particles from other particles
  336.         if tb["spawn_particle"] ~= nil then
  337.             if tb["spawn_particle"]["type"] == nil then
  338.                 tb["spawn_particle"]["type"] = d["type"]
  339.             end
  340.             if tb["spawn_particle"]["time"] == nil then
  341.                 tb["spawn_particle"]["time"] = 100
  342.             end
  343.             if tb["spawn_particle"]["currentTime"] == nil then
  344.                 tb["spawn_particle"]["currentTime"] = 0
  345.             else
  346.                 tb["spawn_particle"]["currentTime"] = tb["spawn_particle"]["currentTime"] + 1
  347.             end
  348.             if tb["spawn_particle"]["amount"] == nil then
  349.                 tb["spawn_particle"]["amount"] = 1
  350.             end
  351.             if tb["spawn_particle"]["currentTime"] == tb["spawn_particle"]["time"] then
  352.                 for i = 1, tb["spawn_particle"]["amount"] do
  353.                     self:addParticle(tb["spawn_particle"]["type"], d["pos"].x, d["pos"].y, d["vel"].x, d["vel"].y)
  354.                 end
  355.                 if tri then
  356.                     self:pBehavior(i, "duplicate", false)
  357.                 end
  358.             end
  359.         end
  360.         -- Spawn particles from other particles with a chance
  361.         if tb["chance_spawn_particle"] ~= nil then
  362.             if tb["chance_spawn_particle"]["type"] == nil then
  363.                 tb["chance_spawn_particle"]["type"] = d["type"]
  364.             end
  365.             if tb["chance_spawn_particle"]["chance"] == nil then
  366.                 tb["chance_spawn_particle"]["chance"] = 50
  367.             end
  368.             if tb["chance_spawn_particle"]["amount"] == nil then
  369.                 tb["chance_spawn_particle"]["amount"] = 1
  370.             end
  371.             if math.random(1, tb["chance_spawn_particle"]["chance"]) == 1 then
  372.                 for i = 1, tb["chance_spawn_particle"]["amount"] do
  373.                     self:addParticle(tb["chance_spawn_particle"]["type"], d["pos"].x, d["pos"].y, d["vel"].x, d["vel"].y)
  374.                 end
  375.                 if tri then
  376.                     self:pBehavior(i, "duplicate", false)
  377.                 end
  378.             end
  379.         end
  380.         -- Spawn particles from other particles on an interval
  381.         if tb["loop_spawn_particle"] ~= nil then
  382.             if tb["loop_spawn_particle"]["type"] == nil then
  383.                 tb["loop_spawn_particle"]["type"] = d["type"]
  384.             end
  385.             if tb["loop_spawn_particle"]["time"] == nil then
  386.                 tb["loop_spawn_particle"]["time"] = 50
  387.             end
  388.             if tb["loop_spawn_particle"]["currentTime"] == nil then
  389.                 tb["loop_spawn_particle"]["currentTime"] = 1
  390.             else
  391.                 tb["loop_spawn_particle"]["currentTime"] = tb["loop_spawn_particle"]["currentTime"] + 1
  392.             end
  393.             if tb["loop_spawn_particle"]["amount"] == nil then
  394.                 tb["loop_spawn_particle"]["amount"] = 1
  395.             end
  396.             if tb["loop_spawn_particle"]["currentTime"] % tb["loop_spawn_particle"]["time"] == 0 then
  397.                 for i = 1, tb["loop_spawn_particle"]["amount"] do
  398.                     self:addParticle(tb["loop_spawn_particle"]["type"], d["pos"].x, d["pos"].y, d["vel"].x, d["vel"].y)
  399.                 end
  400.                 if tri then
  401.                     self:pBehavior(i, "duplicate", false)
  402.                 end
  403.             end
  404.         end
  405.         -- Move around randomly
  406.         if tb["wander"] ~= nil then
  407.             if tb["wander"]["amount"] == nil then
  408.                 tb["wander"]["amount"] = 2
  409.             end
  410.             local move = vec2(tb["wander"]["amount"], 0)
  411.             move = move:rotate(math.rad(math.random(0, 359)))
  412.             d["vel"] = d["vel"] + move
  413.         end
  414.         -- Move in a straight line
  415.         if tb["move_straight"] ~= nil then
  416.             if tb["move_straight"]["dir"] == nil then
  417.                 tb["move_straight"]["dir"] = 90
  418.             end
  419.             if tb["move_straight"]["speed"] == nil then
  420.                 tb["move_straight"]["speed"] = 2
  421.             end
  422.             local move = vec2(tb["move_straight"]["speed"], 0)
  423.             move = move:rotate(math.rad(tb["move_straight"]["dir"]))
  424.             d["vel"] = d["vel"] + move
  425.         end
  426.         -- Apply gravity
  427.         if tb["gravity"] ~= nil then
  428.             if tb["gravity"]["x"] == nil then
  429.                 tb["gravity"]["x"] = 0
  430.             end
  431.             if tb["gravity"]["y"] == nil then
  432.                 tb["gravity"]["y"] = -0.07
  433.             end
  434.             if tb["gravity"]["xv"] == nil then
  435.                 tb["gravity"]["xv"] = 0
  436.             else
  437.                 tb["gravity"]["xv"] = tb["gravity"]["xv"] + tb["gravity"]["x"]
  438.             end
  439.             if tb["gravity"]["yv"] == nil then
  440.                 tb["gravity"]["yv"] = 0
  441.             else
  442.                 tb["gravity"]["yv"] = tb["gravity"]["yv"] + tb["gravity"]["y"]
  443.             end
  444.             d["vel"] = d["vel"] + vec2(tb["gravity"]["xv"], tb["gravity"]["yv"])
  445.         end
  446.         -- Delete a particle if it goes too far from the center of the screen
  447.         if tb["timeout_radius"] ~= nil then
  448.             if tb["timeout_radius"]["radius"] == nil then
  449.                 tb["timeout_radius"]["radius"] = 750
  450.             end
  451.             if vec2(WIDTH / 2, HEIGHT / 2):dist(d["pos"]) > tb["timeout_radius"]["radius"] then
  452.                 table.insert(self.remove, i)
  453.                 if tri then
  454.                     self:pBehavior(i, "despawn", false)
  455.                 end
  456.             end
  457.         end
  458.         -- Have a particle change direction periodically
  459.         if tb["loop_change_direction"] ~= nil then
  460.             if tb["loop_change_direction"]["time"] == nil then
  461.                 tb["loop_change_direction"]["time"] = 30
  462.             end
  463.             if tb["loop_change_direction"]["currentTime"] == nil then
  464.                 tb["loop_change_direction"]["currentTime"] = 1
  465.             else
  466.                 tb["loop_change_direction"]["currentTime"] = tb["loop_change_direction"]["currentTime"] + 1
  467.             end
  468.             if tb["loop_change_direction"]["direction"] == nil then
  469.                 tb["loop_change_direction"]["direction"] = math.random(0, 359)
  470.             end
  471.             if tb["loop_change_direction"]["speed"] == nil then
  472.                 tb["loop_change_direction"]["speed"] = 0.5
  473.             end
  474.             if tb["loop_change_direction"]["currentTime"] % tb["loop_change_direction"]["time"] == 0 then
  475.                 tb["loop_change_direction"]["direction"] = math.random(0, 359)
  476.             end
  477.             local move = vec2(tb["loop_change_direction"]["speed"], 0)
  478.             move = move:rotate(math.rad(tb["loop_change_direction"]["direction"]))
  479.             d["vel"] = d["vel"] + move
  480.         end
  481.         -- Have a particle chance direction with a chance
  482.         if tb["chance_change_direction"] ~= nil then
  483.             if tb["chance_change_direction"]["chance"] == nil then
  484.                 tb["chance_change_direction"]["chance"] = 30
  485.             end
  486.             if tb["chance_change_direction"]["speed"] == nil then
  487.                 tb["chance_change_direction"]["speed"] = 0.5
  488.             end
  489.             if tb["chance_change_direction"]["direction"] == nil then
  490.                 tb["chance_change_direction"]["direction"] = math.random(0, 359)
  491.             end
  492.             if math.random(1, tb["chance_change_direction"]["chance"]) == 1 then
  493.                 tb["chance_change_direction"]["direction"] = math.random(0, 359)
  494.             end
  495.             local move = vec2(tb["chance_change_direction"]["speed"], 0)
  496.             move = move:rotate(math.rad(tb["chance_change_direction"]["direction"]))
  497.             d["vel"] = d["vel"] + move
  498.         end
  499.            
  500.         if tb["init_velocity"] ~= nil then
  501.             if tb["init_velocity"]["x"] == nil then
  502.                 tb["init_velocity"]["x"] = 0
  503.             end
  504.             if tb["init_velocity"]["y"] == nil then
  505.                 tb["init_velocity"]["y"] = 10
  506.             end
  507.             if tb["init_velocity"]["time"] == nil then
  508.                 tb["init_velocity"]["time"] = 0
  509.             else
  510.                 tb["init_velocity"]["time"] = tb["init_velocity"]["time"] + 1
  511.             end
  512.             if tb["init_velocity"]["time"] == 0 then
  513.                 d["vel"] = vec2(tb["init_velocity"]["x"], tb["init_velocity"]["y"])
  514.             end
  515.         end
  516.     end
  517.        
  518.     -- Do movement
  519.     d["vel"] = d["vel"] * self.types[d["type"]]["slowdown"]
  520.     d["pos"] = d["pos"] + d["vel"]
  521. end
  522.  
  523. function ParticleSim:doBehaviors()
  524.     self.remove = {}
  525.     for t, d in pairs(self.particles) do
  526.         self:pBehavior(t, "default", true)
  527.     end
  528.    
  529.     for k, v in pairs(self.remove) do
  530.         self.particles[v] = nil
  531.     end
  532. end
  533.  
  534. function ParticleSim:draw()
  535.     self.timer = self.timer + 1
  536.     self:doBehaviors()
  537.     self:doSpawners()
  538.     self:drawPEllipses()
  539. end
  540.  
  541. function ParticleSim:copyT(t)
  542.     local ret = {}
  543.    
  544.     for k, v in pairs(t) do
  545.         if type(v) == "table" then
  546.             ret[k] = self:copyT(v)
  547.         else
  548.             ret[k] = v
  549.         end
  550.     end
  551.    
  552.     return ret
  553. end
  554.  
  555. function ParticleSim:copyTWithRandomizers(t)
  556.     local ret = {}
  557.    
  558.     for k, v in pairs(t) do
  559.         if type(v) == "table" then
  560.             if #v == 2 and type(v[1]) == "number" and type(v[2]) == "number" then
  561.                 local mi, ma
  562.                 if v[1] > v[2] then
  563.                     mi = v[2]
  564.                     ma = v[1]
  565.                 else
  566.                     mi = v[1]
  567.                     ma = v[2]
  568.                 end
  569.                 ret[k] = math.random(mi * 1000, ma * 1000) / 1000
  570.             else
  571.                 ret[k] = self:copyTWithRandomizers(v)
  572.             end
  573.         else
  574.             ret[k] = v
  575.         end
  576.     end
  577.    
  578.     return ret
  579. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement