module DXOpal::Sprite::CollisionCheck

Methods of Sprite related to collision checking

Attributes

_collision_area[R]

(internal) Return a CollisionArea object

collision[R]

Return an array represents its collision area

collision_enable[RW]

Whether collision is detected for this object (default: true)

collision_sync[RW]

Whether collision areas synchronize with .scale and .angle (default: true) Setting this to false may improve collision detection performance

Public Instance Methods

===(sprite_or_sprites) click to toggle source

Return true when this sprite collides with other sprite(s)

# File lib/dxopal/sprite/collision_check.rb, line 101
def ===(sprite_or_sprites)
  return check(sprite_or_sprites).any?
end
_collidable?() click to toggle source

Return true when this sprite may collide

# File lib/dxopal/sprite/collision_check.rb, line 121
def _collidable?
  return !@vanished && @collision_enable
end
_collides?(sprite) click to toggle source

Return true when this sprite collides with `sprite`

# File lib/dxopal/sprite/collision_check.rb, line 112
def _collides?(sprite)
  if @_collision_area.nil? || sprite._collision_area.nil?
    raise "Sprite image not set"
  end
  return false if !self._collidable? || !sprite._collidable?
  return @_collision_area.collides?(sprite._collision_area)
end
_init_collision_info(image) click to toggle source

Called from Sprites#initialize

# File lib/dxopal/sprite/collision_check.rb, line 65
def _init_collision_info(image)
  @collision ||= nil
  @collision_enable = true if @collision_enable.nil?
  @collision_sync = true if @collision_sync.nil?
  @_collision_area ||=
    if image
      CollisionArea::Rect.new(self, 0, 0, image.width, image.height)
    else
      nil
    end
end
check(sprite_or_sprites) click to toggle source

Return list of sprites collides with this sprite

# File lib/dxopal/sprite/collision_check.rb, line 106
def check(sprite_or_sprites)
  sprites = Array(sprite_or_sprites)
  return sprites.select{|sprite| _collides?(sprite)}
end
collision=(area_spec) click to toggle source

Set collision area of this sprite

# File lib/dxopal/sprite/collision_check.rb, line 87
def collision=(area_spec)
  @_collision_area =
    case area_spec.length
    when 2 then CollisionArea::Point.new(self, *area_spec)
    when 3 then CollisionArea::Circle.new(self, *area_spec)
    when 4 then CollisionArea::Rect.new(self, *area_spec)
    when 6 then CollisionArea::Triangle.new(self, *area_spec)
    else 
      raise "Inlivad area data: #{x.inspect}"
    end
  @collision = area_spec
end
hit(other) click to toggle source
# File lib/dxopal/sprite/collision_check.rb, line 62
def hit(other); end
shot(other) click to toggle source

Default callback methods of `Sprite.check`

# File lib/dxopal/sprite/collision_check.rb, line 61
def shot(other); end