TheoAllen

Theo - Object Collision Check

May 1st, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.86 KB | None | 0 0
  1. # =============================================================================
  2. # THEOALLEN - OBJECT COLLISION CHECK
  3. # VERSION : 1.0
  4. # CONTACT : www.rpgmakerid.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. $imported = {} if $imported.nil?
  8. $imported[:Theo_Collision] = true
  9. # =============================================================================
  10. # CHANGE LOGS :
  11. # -----------------------------------------------------------------------------
  12. # 2013.04.30 - Started and finished script
  13. # =============================================================================
  14. =begin
  15.  
  16.   PENGENALAN:
  17.   Script ini untuk ngecek apakah suatu object bertabrakan dengan object lain
  18.   atau tidak. Object yang bisa dibandingin adalah Window dan Sprite.
  19.  
  20.   CARA PENGGUNAAN:
  21.   - object.collision?(another) >> True kalo object berkolisi ama another
  22.  
  23.   TERMS OF USE:
  24.   Karena ini emang cuman buat tool para scripter, silahkan2 aja kalo mo dipake.
  25.   Mau komersil juga boleh. Btw, kalo ada ide trus mo ngembangil ni modul
  26.   monggo terserah. Gw mah dmen banget kalo ada yg mo ngembangin script gw yg
  27.   satu ini. Tapi tetep kredit ya? \=w=/
  28.  
  29. =end
  30. # =============================================================================
  31. # Module THEO::Collision (Diimplement ke Window ama Sprite)
  32. # =============================================================================
  33. module THEO
  34.   module Collision
  35.    
  36.     def object_toCheck(obj)
  37.       @check = obj if can_collision?(obj)
  38.     end
  39.    
  40.     def border_x
  41.       @check.x + @check.width
  42.     end
  43.    
  44.     def border_y
  45.       @check.y + @check.height
  46.     end
  47.    
  48.     def can_collision?(other)
  49.       other.is_a?(Sprite) || other.is_a?(Window)
  50.     end
  51.    
  52.     def collision?(other)
  53.       return false unless can_collision?(other) && @check
  54.       return (collision_right(other) || collision_left(other)) &&
  55.       (collision_below(other) || collision_above(other))
  56.     end
  57.    
  58.     def collision_right(other)
  59.       (other.x <= border_x) && (other.x > @check.x)
  60.     end
  61.    
  62.     def collision_left(other)
  63.       (other.border_x >= @check.x) && (other.border_x < border_x)
  64.     end
  65.    
  66.     def collision_below(other)
  67.       (other.y <= border_y) && (other.y > @check.y)
  68.     end
  69.    
  70.     def collision_above(other)
  71.       (other.border_y >= @check.y) && (other.border_y < border_y)
  72.     end
  73.    
  74.   end
  75. end
  76.  
  77. class Sprite
  78.  
  79.   include THEO::Collision
  80.  
  81.   alias theo_collision_init initialize
  82.   def initialize(viewport = nil)
  83.     theo_collision_init(viewport)
  84.     object_toCheck(self)
  85.   end
  86.  
  87. end
  88.  
  89. class Window
  90.  
  91.   include THEO::Collision
  92.  
  93.   alias theo_collision_init initialize
  94.   def initialize(x,y,width,height)
  95.     theo_collision_init(x,y,width,height)
  96.     object_toCheck(self)
  97.   end
  98.  
  99. end
Advertisement
Add Comment
Please, Sign In to add comment