Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.54 KB | None | 0 0
  1.     def particle_spawner
  2.         pic_index = 21
  3.         spawn_interval = 20
  4.        
  5.         on_screen_particle = []
  6.         rand_pos_x = []; target_pos_x = []; current_pos_x = []
  7.         rand_pos_y = []; target_pos_y = []; current_pos_y = []
  8.         rand_scale = []; target_scale = []; current_scale = []
  9.         rand_angle = []; target_angle = []; current_angle = []
  10.         rand_speed = []
  11.         lifetime = []
  12.         index = pic_index
  13.        
  14.         spawn_loop = 40
  15.        
  16.         #update function
  17.         loop do
  18.             #--------------------------------------------------------------
  19.             # Check if you should spawn particle
  20.             #--------------------------------------------------------------
  21.             p (on_screen_particle)
  22.             if (spawn_loop >= spawn_interval)
  23.                 if (on_screen_particle.empty?)
  24.                     index = pic_index
  25.                 else
  26.                     for i in pic_index..pic_index+10
  27.                         if (!on_screen_particle.include?(i))
  28.                             #msgbox(on_screen_particle)
  29.                             #msgbox("particale #" + i.to_s + " is not in the array!")
  30.                             #msgbox("so, spawn a particle with #" + i.to_s)
  31.                             index = i  
  32.                             break
  33.                         end
  34.                     end
  35.                 end
  36.                 #msgbox(index)
  37.                 on_screen_particle.insert(0, index)
  38.                 #msgbox(on_screen_particle)
  39.                 #--------------------------------------------------------------
  40.                 # Spawning a new particle
  41.                 #--------------------------------------------------------------
  42.                 rand_pos_x.insert(0, rand(484) + 30); target_pos_x.insert(0, rand_pos_x[0] + 10 + rand(20))
  43.                 rand_pos_y.insert(0, rand(356) + 30); target_pos_y.insert(0, rand_pos_y[0] - 100 - rand(20))
  44.                 rand_scale.insert(0, 100 + rand(100)); target_scale.insert(0, rand_scale[0] + rand(30))
  45.                 rand_angle.insert(0, -30 + rand(60)); target_angle.insert(0, rand_angle[0] - 30 + rand(60))
  46.                 rand_speed.insert(0, 40 + rand(80))
  47.                 lifetime.insert(0, 0)
  48.                
  49.                 screen.pictures[on_screen_particle[0]].show(
  50.                     "Screenfx/Heart_Particle" , 1,
  51.                     rand_pos_x[0],
  52.                     rand_pos_y[0],
  53.                     rand_scale[0],
  54.                     rand_scale[0],
  55.                     100, 1)
  56.                 current_pos_x.insert(0, rand_pos_x)
  57.                 current_pos_y.insert(0, rand_pos_y)
  58.                 current_scale.insert(0, rand_scale)
  59.                 current_angle.insert(0, rand_angle)
  60.                
  61.                 screen.pictures[on_screen_particle[0]].start_tone_change(
  62.                     Tone.new(0,255,0,255), 1)
  63.                
  64.                 rotate_pic(on_screen_particle[0],rand_angle[0],1,1)
  65.                 spawn_loop = 0
  66.             else
  67.                 spawn_loop += 1
  68.             end
  69.             #--------------------------------------------------------------
  70.             # If no particle exist in canvas, next
  71.             #--------------------------------------------------------------
  72.             if (on_screen_particle.count < 1)
  73.                 wait(1)
  74.                 next
  75.             end
  76.             #--------------------------------------------------------------
  77.             # updating the position of particles
  78.             #--------------------------------------------------------------
  79.             for i in 0..on_screen_particle.count-1
  80.                 #msgbox(rand_pos_y[i].to_s + target_pos_y[i].to_s + )
  81.                 current_pos_x[i] = lerp( rand_pos_x[i], target_pos_x[i], lifetime[i]*100/rand_speed[i])
  82.                 current_pos_y[i] = lerp( rand_pos_y[i], target_pos_y[i], lifetime[i]*100/rand_speed[i])
  83.                 current_scale[i] = lerp( rand_scale[i], target_scale[i], lifetime[i]*100/rand_speed[i])
  84.                 current_angle[i] = lerp( rand_angle[i], target_angle[i], lifetime[i]*100/rand_speed[i])
  85.                
  86.                 screen.pictures[on_screen_particle[i]].move(
  87.                     1,
  88.                     current_pos_x[i],
  89.                     current_pos_y[i],
  90.                     current_scale[i],
  91.                     current_scale[i],
  92.                     100,0,1
  93.                     )
  94.                
  95.                 rotate_pic(on_screen_particle[i],current_angle[i],1,1)
  96.                 lifetime[i] += 1  
  97.                
  98.                 if(lifetime[i] >= rand_speed[i])
  99.                     erase_pic(on_screen_particle[i])
  100.                         #msgbox("deleting particle #"+ on_screen_particle[i].to_s )
  101.                     on_screen_particle.delete_at(i)
  102.  
  103.                     rand_pos_x.delete_at(i); target_pos_x.delete_at(i); current_pos_x.delete_at(i)
  104.                     rand_pos_y.delete_at(i); target_pos_y.delete_at(i); current_pos_y.delete_at(i)
  105.                     rand_scale.delete_at(i); target_scale.delete_at(i); current_scale.delete_at(i)
  106.                     rand_angle.delete_at(i); target_angle.delete_at(i); current_angle.delete_at(i)
  107.                     rand_speed.delete_at(i)
  108.                     lifetime.delete_at(i)
  109.                        # msgbox(on_screen_particle.count)
  110.                 end
  111.             end
  112.            
  113.         wait(1)
  114.         end
  115.     end
  116.        
  117.        
  118.     def lerp(origin, destination, time)
  119.         return (origin * (100-time) + destination * time)/100
  120.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement