class DXOpal::Sprite::CollisionArea::Circle

Attributes

r[R]

Public Class Methods

new(sprite, x, y, r) click to toggle source
Calls superclass method
# File lib/dxopal/sprite/collision_area.rb, line 123
def initialize(sprite, x, y, r)
  @sprite, @x, @y, @r = sprite, x, y, r
  super()
end

Public Instance Methods

absolute_norot_pos() click to toggle source
# File lib/dxopal/sprite/collision_area.rb, line 159
def absolute_norot_pos
  [@x + @sprite.x, @y + @sprite.y]
end
absolute_pos() click to toggle source

Return [x, y]

# File lib/dxopal/sprite/collision_area.rb, line 155
def absolute_pos
  absolute1([@x, @y])
end
circle?() click to toggle source

Return true if this is not an ellipsis

# File lib/dxopal/sprite/collision_area.rb, line 132
def circle?
  @sprite.scale_x == @sprite.scale_y
end
collides?(other) click to toggle source
# File lib/dxopal/sprite/collision_area.rb, line 136
def collides?(other)
  case other.type
  when :Point
    other.collides?(self)
  when :Circle
    collides_circle?(other)
  when :Rect
    cx, cy = *self.absolute_norot_pos
    (x1, y1), (x2, y2), (x3, y3), (x4, y4) = *transback(other.absolute_poss, @sprite)
    `Opal.DXOpal.CCk.check_circle_tilted_rect(cx, cy, #{@r}, x1, y1, x2, y2, x3, y3, x4, y4)`
  when :Triangle
    cx, cy = *self.absolute_norot_pos
    (x1, y1), (x2, y2), (x3, y3) = *transback(other.absolute_poss, @sprite)
    `Opal.DXOpal.CCk.check_circle_triangle(cx, cy, #{@r}, x1, y1, x2, y2, x3, y3)`
  else raise
  end
end
type() click to toggle source
# File lib/dxopal/sprite/collision_area.rb, line 129
def type; :Circle; end

Private Instance Methods

collides_circle?(other) click to toggle source
# File lib/dxopal/sprite/collision_area.rb, line 165
def collides_circle?(other)
  x1, y1 = *self.absolute_pos
  r1 = @r
  x2, y2 = *other.absolute_pos
  r2 = other.r
  if self.circle? && other.circle?
    `Opal.DXOpal.CCk.check_circle_circle(x1, y1, #{@r}, x2, y2, #{other.r})`
  else
    if @sprite.collision_sync
      scale_x1 = @sprite.scale_x
      scale_y1 = @sprite.scale_y
      angle1 = @sprite.angle * Math::PI / 180
    else
      scale_x1 = 1
      scale_y1 = 1
      angle1 = 0
    end
    if other.sprite.collision_sync
      scale_x2 = other.sprite.scale_x
      scale_y2 = other.sprite.scale_y
      angle2 = other.sprite.angle * Math::PI / 180
    else
      scale_x2 = 1
      scale_y2 = 1
      angle2 = 0
    end
    ret = nil
    %x{
      var e1 = {
        fRad_X: scale_x1 * r1,
        fRad_Y: scale_y1 * r1,
        fAngle: angle1,
        fCx: x1,
        fCy: y1,
      }
      var e2 = {
        fRad_X: scale_x2 * r2,
        fRad_Y: scale_y2 * r2,
        fAngle: angle2,
        fCx: x2,
        fCy: y2,
      }
      ret = Opal.DXOpal.CCk.check_ellipse_ellipse(e1, e2);
    }
    ret
  end
end