Module: DXOpal::Sprite::CollisionCheck

Included in:
DXOpal::Sprite
Defined in:
lib/dxopal/sprite/collision_check.rb

Overview

Methods of Sprite related to collision checking

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_collision_areaObject (readonly)

(internal) Return a CollisionArea object



84
85
86
# File 'lib/dxopal/sprite/collision_check.rb', line 84

def _collision_area
  @_collision_area
end

#collisionObject

Return an array represents its collision area



82
83
84
# File 'lib/dxopal/sprite/collision_check.rb', line 82

def collision
  @collision
end

#collision_enableObject

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



77
78
79
# File 'lib/dxopal/sprite/collision_check.rb', line 77

def collision_enable
  @collision_enable
end

#collision_syncObject

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



80
81
82
# File 'lib/dxopal/sprite/collision_check.rb', line 80

def collision_sync
  @collision_sync
end

Instance Method Details

#===(sprite_or_sprites) ⇒ Object

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



101
102
103
# File 'lib/dxopal/sprite/collision_check.rb', line 101

def ===(sprite_or_sprites)
  return check(sprite_or_sprites).any?
end

#_collidable?Boolean

Return true when this sprite may collide

Returns:

  • (Boolean)


121
122
123
# File 'lib/dxopal/sprite/collision_check.rb', line 121

def _collidable?
  return !@vanished && @collision_enable
end

#_collides?(sprite) ⇒ Boolean

Return true when this sprite collides with `sprite`

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# 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) ⇒ Object

Called from Sprites#initialize



65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object

Return list of sprites collides with this sprite



106
107
108
109
# 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

#hit(other) ⇒ Object



62
# File 'lib/dxopal/sprite/collision_check.rb', line 62

def hit(other); end

#shot(other) ⇒ Object

Default callback methods of `Sprite.check`



61
# File 'lib/dxopal/sprite/collision_check.rb', line 61

def shot(other); end