Class: DXOpal::Sprite::CollisionArea::Base

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

Direct Known Subclasses

Circle, Point, Rect, Triangle

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#spriteObject (readonly)

Sprite corresponds to this hitarea



9
10
11
# File 'lib/dxopal/sprite/collision_area.rb', line 9

def sprite
  @sprite
end

Instance Method Details

#aabb(poss) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dxopal/sprite/collision_area.rb', line 73

def aabb(poss)
  x1 = y1 =  Float::INFINITY
  x2 = y2 = -Float::INFINITY
  %x{
    for(var i=0; i<poss.length; i++) {
      if (poss[i][0] < x1) x1 = poss[i][0];
      if (poss[i][1] < y1) y1 = poss[i][1];
      if (poss[i][0] > x2) x2 = poss[i][0];
      if (poss[i][1] > y2) y2 = poss[i][1];
    }
  }
  return [[x1, y1], [x2, y2]]
end

#absolute(poss) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dxopal/sprite/collision_area.rb', line 17

def absolute(poss)
  ox = @sprite.x
  oy = @sprite.y
  return poss.map{|(x, y)| [x+ox, y+oy]} if !@sprite.collision_sync
  angle = @sprite.angle
  cx = @sprite.center_x
  cy = @sprite.center_y
  sx = @sprite.scale_x
  sy = @sprite.scale_y

  ret = []
  %x{
    var rad = Math.PI / 180.0 * angle;
    var sin = Math.sin(rad);
    var cos = Math.cos(rad);
    poss.forEach(function(pos){
      var x = pos[0], y = pos[1];
      x2 = (x - cx) * sx * cos - (y - cy) * sy * sin + cx + ox;
      y2 = (x - cx) * sx * sin + (y - cy) * sy * cos + cy + oy;
      ret.push([x2, y2]);
    });
  }
  return ret
end

#absolute1(pos) ⇒ Object



42
43
44
# File 'lib/dxopal/sprite/collision_area.rb', line 42

def absolute1(pos)
  absolute([pos]).first
end

#transback(poss, sprite) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dxopal/sprite/collision_area.rb', line 46

def transback(poss, sprite)
  return poss if !sprite.collision_sync
  angle = sprite.angle
  cx = sprite.x + sprite.center_x
  cy = sprite.y + sprite.center_y
  sx = sprite.scale_x
  sy = sprite.scale_y

  ret = []
  %x{
    var rad = Math.PI / 180.0 * -angle;
    var sin = Math.sin(rad);
    var cos = Math.cos(rad);
    poss.forEach(function(pos){
      var x = pos[0], y = pos[1];
      x2 = ((x - cx) * cos - (y - cy) * sin) / sx + cx;
      y2 = ((x - cx) * sin + (y - cy) * cos) / sy + cy;
      ret.push([x2, y2]);
    });
  }
  return ret
end

#transback1(pos, sprite) ⇒ Object



69
70
71
# File 'lib/dxopal/sprite/collision_area.rb', line 69

def transback1(pos, sprite)
  transback([pos], sprite).first
end

#typeObject

Return a string like “Point”, “Rect”, etc. Used for type checking in ‘collides?` (because Opal’s Class#is_a? is not very fast)



13
14
15
# File 'lib/dxopal/sprite/collision_area.rb', line 13

def type
  raise "override me"
end