TheoAllen

ASR map pic collision

Jul 11th, 2016
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.10 KB | None | 0 0
  1. #==============================================================================#
  2. # ASR Map Picture Collision v0.8.1
  3. # Require : Vicky pixel movement
  4. #-------------------------------------------------------------------------------
  5. # How to use :
  6. #-------------------------------------------------------------------------------
  7. # > Siapin gambar buat collisionnya
  8. # > Warna buat collision harus sama kek di config bawah ↓
  9. # > Masukin gambar ke parallax
  10. # > Masukin tag <cm=nama_gambarnya> di note map
  11. #-------------------------------------------------------------------------------
  12. # Extra note :
  13. # Potentially buggy ~ =w=a
  14. #==============================================================================#
  15. module ASR
  16.   #-----------------------------------------------------------------------------
  17.   # Jangan diutak-atik
  18.   #-----------------------------------------------------------------------------
  19.   def self.color(hex, alpha=255)
  20.     r = hex[0, 2].to_i(16)
  21.     g = hex[2, 2].to_i(16)
  22.     b = hex[4, 2].to_i(16)
  23.     return Color.new(r, g, b, alpha)
  24.   end
  25.  
  26.   COLLISION = ASR.color("ff0000") # Hexacode warnanya
  27.   SHOW_PIC_COLLISION = true       # Tampilin gambar?
  28.  
  29. end
  30. #==============================================================================#
  31. # End of safe zone
  32. #==============================================================================#
  33. class Game_CharacterBase
  34.   def passable?(x, y, d)
  35.     x = fix_position(x)
  36.     y = fix_position(y)
  37.     passable1 = passable_tile?(x, y, d)
  38.     passable2 = character_collision?(x, y, d)
  39.     fix_movement(x, y, d)  if player? && !passable1 && passable2
  40.     fix_collision(x, y, d) if player? && !passable2 #&& passable1
  41.     passable1 && passable2
  42.   end
  43.  
  44.   def out_of_screen?
  45.     rx = (@x * 32).floor
  46.     rx2 = rx + bw * 32
  47.     ry = (@y * 32).floor
  48.     ry2 = ry + bh * 32
  49.   end
  50.  
  51.   alias asr_pixel_collide_passable_tile? passable_tile?
  52.   def passable_tile?(x, y, d)
  53.     return asr_pixel_collide_passable_tile?(x, y, d) unless
  54.       $game_map.use_pic_collision?
  55.    
  56.     case d
  57.     when 2
  58.       y += distance_per_frame + 0.1
  59.     when 4
  60.       x -= distance_per_frame + 0.1
  61.     when 6
  62.       x += distance_per_frame + 0.1
  63.     when 8
  64.       y -= distance_per_frame + 0.1
  65.     end
  66.    
  67.     rx = (x * 32).floor
  68.     rx2 = rx + bw * 32
  69.     ry = (y * 32).floor
  70.     ry2 = ry + bh * 32
  71.     if debug_through?
  72.       return !(rx < 0 || rx2 > Graphics.width || ry < 0 ||
  73.         ry2 > Graphics.height)
  74.     end
  75.     top_left = Coordinate.new(rx,ry)
  76.     top_right = Coordinate.new(rx2,ry)
  77.     bottom_left = Coordinate.new(rx,ry2)
  78.     bottom_right = Coordinate.new(rx2,ry2)
  79.    
  80.     dircheck = {}
  81.     dircheck[2] = [bottom_left, bottom_right]
  82.     dircheck[4] = [top_left, bottom_left]
  83.     dircheck[6] = [top_right, bottom_right]
  84.     dircheck[8] = [top_left, top_right]
  85.    
  86.     to_check = dircheck[d]
  87.     result = to_check.any? do |point|
  88.       $game_temp.pic_collision.get_pixel(point.x, point.y) == ASR::COLLISION
  89.     end
  90.     return !result
  91.   end
  92.  
  93. end
  94.  
  95. class Game_Map
  96.  
  97.   alias asr_setup_map setup
  98.   def setup(map_id)
  99.     asr_setup_map(map_id)
  100.     setup_pic_collision
  101.   end
  102.  
  103.   def use_pic_collision?
  104.     @pic_collision
  105.   end
  106.  
  107.   def setup_pic_collision
  108.     @pic_collision = nil
  109.     if @map.note[/<cm:\s*(.+)>/i]
  110.       @pic_collision = $1.to_s
  111.     end
  112.     $game_temp.pic_collision = Cache.parallax(@pic_collision) if @pic_collision
  113.   end
  114.  
  115. end
  116.  
  117. class Game_Temp
  118.   attr_accessor :pic_collision
  119. end
  120.  
  121. class Scene_Map
  122.  
  123.   alias addon_start start
  124.   def start
  125.     addon_start
  126.     setup_pic_collision
  127.   end
  128.  
  129.   def setup_pic_collision
  130.     @collision.dispose if @collision
  131.     return unless ASR::SHOW_PIC_COLLISION
  132.     @collision = Sprite.new
  133.     @collision.bitmap = $game_temp.pic_collision
  134.     @collision.opacity = 128
  135.   end
  136.  
  137.   alias asr_pic_collision_update update
  138.   def update
  139.     asr_pic_collision_update
  140.     setup_pic_collision if $game_map.need_refresh
  141.   end
  142.  
  143.   alias asr_pic_collision_terminate terminate
  144.   def terminate
  145.     asr_pic_collision_terminate
  146.     @collision.dispose if @collision
  147.   end
  148.  
  149. end
Advertisement
Add Comment
Please, Sign In to add comment