Advertisement
Vincent26

Particule

Dec 21st, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.64 KB | None | 0 0
  1. =begin
  2. CREER UNE PARTICULE :
  3.  
  4. COLOR = [240,30,30] #<= Best color
  5. $game_map.particule.push(Game_Particule.new(X,Y,ANGLE,FORCE,COLOR,$game_map.nbr_particule))
  6. $game_map.nbr_particule += 1
  7.  
  8. =end
  9. module Math
  10.  
  11.   CONV = 3.141592654/180.0
  12.  
  13.   def self.angle_between(x1,y1,x2,y2)
  14.     x = (x1-x2).to_f
  15.     y = (y1-y2).to_f
  16.     if x < 0.0 && y < 0.0
  17.       angle = atan(x/y)*180.0/PI
  18.     elsif x < 0.0 && y > 0.0
  19.       angle = 180.0+atan(x/y)*180.0/PI
  20.     elsif x >= 0.0 && y < 0.0
  21.       angle = 360.0-atan(-x/y)*180.0/PI
  22.     else
  23.       angle = 180.0+atan(x/y)*180.0/PI
  24.     end
  25.     angle -= 180.0
  26.     angle = 90.0 if angle.nan?
  27.     return angle%360
  28.   end
  29.  
  30.   def self.get_charact(st,en,txt)
  31.     return false unless txt
  32.     id = txt.index(st)
  33.     return false unless id
  34.     id += st.length
  35.     res = ""
  36.     for i in id...txt.length
  37.       break if txt[i...i+en.length] == en
  38.       res += txt[i]
  39.     end
  40.     return res
  41.   end
  42.  
  43.   def self.intersect(x,y,angle,rect)
  44.     result = []
  45.     x2 = rect[0]
  46.     y2 = rect[1]
  47.     w = rect[2]
  48.     h = rect[3]
  49.     a = 1/tan(angle*CONV)
  50.     b = y-a*x
  51.     c1 = a*x2+b
  52.     c2 = a*(x2+w)+b
  53.     c3 = a != 0 ? (y2-b)/a : -10
  54.     c4 = a != 0 ? (y2+h-b)/a : -10
  55.     d = 0; e = 0;
  56.     d = 1 if angle > 0
  57.     e = 1 if angle.abs < 90
  58.     if (d == 0 && x2 <= x)||
  59.        (d == 1 && x2 >= x)
  60.       result.push([x2,c1]) if c1 > y2 && c1 < y2+h
  61.     end
  62.     if(d == 0 && x2+w <= x)||
  63.       (d == 1 && x2+w >= x)
  64.       result.push([x2+w,c2]) if c2 > y2 && c2 < y2+h
  65.     end
  66.     if(e == 0 && y2 <= y)||
  67.       (e == 1 && y2 >= y)
  68.       result.push([c3,y2]) if c3 > x2 && c3 < x2+w
  69.     end
  70.     if(e == 0 && y2+h <= y)||
  71.       (e == 1 && y2+h >= y)
  72.       result.push([c4,y2+h]) if c4 > x2 && c4 < x2+w
  73.     end
  74.     return result
  75.   end
  76.  
  77.   def self.pos_line(x,y,angle,dist)
  78.     x2 = x + cos(angle*CONV)*dist
  79.     y2 = y + sin(angle*CONV)*dist
  80.     return [x2,y2]
  81.   end
  82.  
  83. end
  84. class Game_Map
  85.  
  86.   attr_accessor :particule
  87.   attr_accessor :nbr_particule
  88.  
  89.   alias initialize_arpg_system_particule initialize
  90.   def initialize
  91.     @particule = []
  92.     @nbr_particule = 0
  93.     initialize_arpg_system_particule
  94.   end
  95.  
  96.   alias setup_arpg_system_particule setup
  97.   def setup(map_id)
  98.     setup_arpg_system_particule(map_id)
  99.     refresh_particule
  100.   end
  101.  
  102.   def refresh_particule
  103.     @particule = []
  104.     @nbr_particule = 0
  105.   end
  106.  
  107.   alias update_arpg_system_particule update
  108.   def update(main = false)
  109.     update_arpg_system_particule(main)
  110.     update_particule
  111.   end
  112.  
  113.   def update_particule
  114.     i = 0
  115.     while i != @particule.length
  116.       if @particule[i].deleted?
  117.         @particule.delete_at(i) rescue true
  118.       else
  119.         i += 1
  120.       end
  121.     end
  122.   end
  123. end
  124. class Spriteset_Map
  125.  
  126.   alias initialize_arpg_system_particule initialize
  127.   def initialize
  128.     initialize_particule
  129.     create_particule
  130.     @map_id = $game_map.map_id
  131.   end
  132.  
  133.   def create_particule
  134.     @particules_sprites = {}
  135.   end
  136.  
  137.   alias dispose_arpg_system_particule dispose
  138.   def dispose
  139.     dispose_arpg_system_particule
  140.     dispose_particule
  141.   end
  142.  
  143.   def dispose_particule
  144.     @particules_sprites.each {|id,sprite| sprite.dispose }
  145.     create_particule
  146.   end
  147.  
  148.   alias update_arpg_system_particule update
  149.   def update
  150.     update_particule
  151.     update_arpg_system_particule
  152.     @map_id = $game_map.map_id
  153.   end
  154.  
  155.   def update_particule
  156.     if @map_id != $game_map.map_id
  157.       @particules_sprites.each do |key,value|
  158.         value.dispose
  159.       end
  160.       @particules_sprites = {}
  161.     else
  162.       id = []
  163.       $game_map.particule.each do |part|
  164.         @particules_sprites[part.id] ||= Sprite_Particule.new(part,@parallax)
  165.         @particules_sprites[part.id].update
  166.         if @particules_sprites[part.id].disposed?
  167.           part.delete = true
  168.           id.push(part.id)
  169.         end
  170.       end
  171.       @particules_sprites.each do |key,value|
  172.         if id.include?(key)
  173.           @particules_sprites.delete(key)
  174.         end
  175.       end
  176.     end
  177.   end
  178. end
  179. class Game_Particule
  180.  
  181.   attr_accessor :x
  182.   attr_accessor :y
  183.   attr_accessor :angle
  184.   attr_accessor :force
  185.   attr_accessor :id
  186.   attr_accessor :delete
  187.   attr_accessor :color
  188.   attr_accessor :param
  189.   def initialize(x,y,angle,force,color,id,param_plus = {})
  190.     @x = x
  191.     @y = y
  192.     @angle = angle
  193.     @force = force
  194.     @color = color
  195.     @id = id
  196.     @param = param_plus
  197.     @delete = false
  198.   end
  199.  
  200.   def deleted?
  201.     return @delete
  202.   end
  203. end
  204. class Spriteset_Map
  205.   alias update_parallax_arpg update_parallax
  206.   def update_parallax
  207.     update_parallax_arpg
  208.     @parallax.ox = $game_map.display_x * 32
  209.     @parallax.oy = $game_map.display_y * 32
  210.   end
  211. end
  212. class Sprite_Particule < Sprite
  213.  
  214.   CONV = 3.141592654/180.0
  215.  
  216.   COULEUR = [20,20,20]
  217.   COULEUR_1 = 0
  218.  
  219.   def initialize(particule,sprite_parra)
  220.     super($viewport)
  221.     @x = particule.x
  222.     @y = particule.y
  223.     @angle = particule.angle
  224.     @force = particule.force
  225.     @color_base = particule.color
  226.     @vit_base = particule.param[:vitesse] ? particule.param[:vitesse] : 3
  227.     @timer = rand(2)
  228.     @particule = []
  229.     @parrallax = sprite_parra
  230.     create_particule
  231.     create_bitmap
  232.   end
  233.   def dispose
  234.     bitmap.dispose if bitmap
  235.     super
  236.   end
  237.   def create_particule
  238.     @distance = @force*20
  239.     for i in 0...@force*4
  240.       x = 0.0
  241.       y = 0.0
  242.       a = (5.0-rand(10))/10.0
  243.       var = [@color_base[0]*2/3,@color_base[1]*2/3,@color_base[2]*2/3]
  244.       b = [@color_base[0]/3+rand(var[0]),@color_base[1]/3+rand(var[1]),@color_base[2]/3+rand(var[2])]
  245.       @particule.push([x,y,a,b])
  246.     end
  247.   end
  248.   def create_bitmap
  249.     self.bitmap = Bitmap.new(@distance*2,@distance*4)
  250.     self.ox = self.width/2
  251.     self.x = @x + $game_map.display_x * 32
  252.     self.y = @y + $game_map.display_y * 32
  253.     self.z = 1
  254.     self.angle = 180.0+@angle
  255.     update_bitmap
  256.   end
  257.   def update
  258.     unless disposed?
  259.       super
  260.       return unless @parrallax.bitmap
  261.       update_position
  262.       update_bitmap if @timer % 2 == 0
  263.       @timer += 1
  264.     end
  265.   end
  266.   def update_position
  267.     begin
  268.       self.x = (@x - $game_map.display_x*32)
  269.       self.y = (@y - $game_map.display_y*32)
  270.       bit = Bitmap.new(2,2)
  271.       a = (@angle%360)*CONV
  272.       for i in 0...@particule.length
  273.         x = @particule[i][0]+[@vit_base.to_f*2.5/@timer.to_f,@vit_base.to_f].min+2-rand(4)
  274.         y = @particule[i][1]+@particule[i][2]*[@vit_base.to_f*2.0/@timer.to_f,@vit_base.to_f].min
  275.         @particule[i][0] = x
  276.         @particule[i][1] = y
  277.         x2 = -x*2/3*Math.sin(a).to_f + y*(1+rand(2))*Math.cos(a).to_f
  278.         y2 = -x*2/3*Math.cos(a).to_f - y*(1+rand(2))*Math.sin(a).to_f
  279.         unless $game_map.contact.check_pixel(@x+x2, @y+y2)
  280.           b = rand(5)
  281.         else
  282.           b = 10+rand(20)
  283.         end
  284.         if @parrallax.bitmap.get_pixel(@x+x2, @y+y2).red < 170
  285.           co = Color.new([@particule[i][3][0]-@timer,0].max,[@particule[i][3][1]-@timer,0].max,[@particule[i][3][2]-@timer,0].max,b)
  286.           bit.fill_rect(0,0,2,2,co)
  287.         end
  288.         @parrallax.bitmap.blt(@x+x2, @y+y2,bit,bit.rect)
  289.       end
  290.     rescue
  291.       return
  292.     end
  293.   end
  294.  
  295.   def update_bitmap
  296.     begin
  297.       self.bitmap.clear
  298.   #~     self.bitmap.fill_rect(0,0,width,height, Color.new(255,255,255,50))
  299.       result = []
  300.       for i in 0...@particule.length
  301.         a = 255-Math.exp(@particule[i][0]/10)
  302.         result.push(a)
  303.         co = Color.new(@particule[i][3][0],@particule[i][3][1],@particule[i][3][2],a)
  304.         self.bitmap.fill_rect(@particule[i][1]+@distance, @particule[i][0],2,2, co)
  305.       end
  306.       dispose if result.max < 0
  307.     rescue
  308.       return
  309.     end
  310.   end
  311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement