summaryrefslogtreecommitdiffhomepage
path: root/samples/04_physics_and_collisions/09_arbitrary_collision/app/paddle.rb
blob: a4fe710f51133ee39ef2c69e796bef29aa22d7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Paddle
  attr_accessor :enabled

  def initialize ()
    @x=WIDTH/2
    @y=100
    @width=100
    @height=20
    @speed=10

    @xyCollision  = LinearCollider.new({x: @x,y: @y+@height+5}, {x: @x+@width, y: @y+@height+5})
    @xyCollision2 = LinearCollider.new({x: @x,y: @y}, {x: @x+@width, y: @y}, :pos)
    @xyCollision3 = LinearCollider.new({x: @x,y: @y}, {x: @x, y: @y+@height+5})
    @xyCollision4 = LinearCollider.new({x: @x+@width,y: @y}, {x: @x+@width, y: @y+@height+5}, :pos)

    @enabled = true
  end

  def update args
    @xyCollision.resetPoints({x: @x,y: @y+@height+5}, {x: @x+@width, y: @y+@height+5})
    @xyCollision2.resetPoints({x: @x,y: @y}, {x: @x+@width, y: @y})
    @xyCollision3.resetPoints({x: @x,y: @y}, {x: @x, y: @y+@height+5})
    @xyCollision4.resetPoints({x: @x+@width,y: @y}, {x: @x+@width, y: @y+@height+5})

    @xyCollision.update  args
    @xyCollision2.update args
    @xyCollision3.update args
    @xyCollision4.update args

    args.inputs.keyboard.key_held.left  ||= false
    args.inputs.keyboard.key_held.right  ||= false

    if not (args.inputs.keyboard.key_held.left == args.inputs.keyboard.key_held.right)
      if args.inputs.keyboard.key_held.left && @enabled
        @x-=@speed
      elsif args.inputs.keyboard.key_held.right && @enabled
        @x+=@speed
      end
    end

    xmin =WIDTH/4
    xmax = 3*(WIDTH/4)
    @x = (@x+@width > xmax) ? xmax-@width : (@x<xmin) ? xmin : @x;
  end

  def render args
    args.outputs.solids << [@x,@y,@width,@height,255,0,0];
  end

  def rect
    [@x, @y, @width, @height]
  end
end