Class: DXOpal::Sprite::CollisionArea::Rect

Inherits:
Base
  • Object
show all
Defined in:
lib/dxopal/sprite/collision_area.rb

Instance Attribute Summary

Attributes inherited from Base

#sprite

Instance Method Summary collapse

Methods inherited from Base

#aabb, #absolute, #absolute1, #transback, #transback1

Constructor Details

#initialize(sprite, x1, y1, x2, y2) ⇒ Rect

Returns a new instance of Rect.



215
216
217
218
219
# File 'lib/dxopal/sprite/collision_area.rb', line 215

def initialize(sprite, x1, y1, x2, y2)
  @sprite, @x1, @y1, @x2, @y2 = sprite, x1, y1, x2, y2
  @poss = [[x1, y1], [x2, y1], [x2, y2], [x1, y2]]
  super()
end

Instance Method Details

#absolute_norot_possObject



254
255
256
257
# File 'lib/dxopal/sprite/collision_area.rb', line 254

def absolute_norot_poss
  [[@x1 + @sprite.x, @y1 + @sprite.y],
   [@x2 + @sprite.x, @y2 + @sprite.y]]
end

#absolute_possObject



250
251
252
# File 'lib/dxopal/sprite/collision_area.rb', line 250

def absolute_poss
  absolute(@poss)
end

#collides?(other) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/dxopal/sprite/collision_area.rb', line 227

def collides?(other)
  case other.type
  when :Point, :Circle
    other.collides?(self)
  when :Rect
    ((ox1, oy1), (ox2, oy2)) = self.absolute_norot_poss
    ((dx1, dy1), (dx2, dy2)) = aabb(transback(other.absolute_poss, @sprite))
    return false unless `Opal.DXOpal.CCk.check_rect_rect(ox1, oy1, ox2, oy2, dx1, dy1, dx2, dy2)`

    ((ox1, oy1), (ox2, oy2)) = other.absolute_norot_poss
    ((dx1, dy1), (dx2, dy2)) = aabb(transback(self.absolute_poss, other.sprite))
    return false unless `Opal.DXOpal.CCk.check_rect_rect(ox1, oy1, ox2, oy2, dx1, dy1, dx2, dy2)`
    true
  when :Triangle
    (ox1, oy1), (ox2, oy2), (ox3, oy3), (ox4, oy4) = *self.absolute_poss
    (dx1, dy1), (dx2, dy2), (dx3, dy3) = *other.absolute_poss
    `Opal.DXOpal.CCk.check_tilted_rect_triangle(ox1, oy1, ox2, oy2, ox3, oy3, ox4, oy4,
                                                dx1, dy1, dx2, dy2, dx3, dy3)`

  else raise
  end
end

#inspectObject



223
224
225
# File 'lib/dxopal/sprite/collision_area.rb', line 223

def inspect
  "#<CollisionArea::Rect(#{@x1}, #{@y1}, #{@x2}, #{@y2})>"
end

#typeObject



221
# File 'lib/dxopal/sprite/collision_area.rb', line 221

def type; :Rect; end