summaryrefslogtreecommitdiffhomepage
path: root/samples/04_physics_and_collisions/09_arbitrary_collision/app/blocks.rb
blob: 118809f71d931a0147d146484ab4988b40792506 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
MAX_COUNT=100

def universalUpdateOne args, shape
  didHit = false
  hitters = []
  #puts shape.to_s
  toCollide = nil
  for b in args.state.balls
    if [b.x, b.y, b.width, b.height].intersect_rect?(shape.bold)
      didSquare = false
      for s in shape.squareColliders
        if (s.collision?(args, b))
          didSquare = true
          didHit = true
          #s.collide(args, b)
          toCollide = s
          #hitter = b
          hitters.append(b)
        end #end if
      end #end for
      if (didSquare == false)
        for c in shape.colliders
          #puts args.state.ball.velocity
          if c.collision?(args, b.getPoints(args),b)
            #c.collide args, b
            toCollide = c
            didHit = true
            hitters.append(b)
          end #end if
        end #end for
      end #end if
    end#end if
  end#end for
  if (didHit)
    shape.count=0
    hitters = hitters.uniq
    for hitter in hitters
      hitter.makeLeader args
      #toCollide.collide(args, hitter)
      if shape.home == "squares"
        args.state.squares.delete(shape)
      elsif shape.home == "tshapes"
        args.state.tshapes.delete(shape)
      else shape.home == "lines"
        args.state.lines.delete(shape)
      end
    end

    #puts "HIT!" + hitter.number
  end
end

def universalUpdate args, shape
  #puts shape.home
  if (shape.count <= 1)
    universalUpdateOne args, shape
    return
  end

  didHit = false
  hitter = nil
  for b in args.state.ballParents
    if [b.x, b.y, b.width, b.height].intersect_rect?(shape.bold)
      didSquare = false
      for s in shape.squareColliders
        if (s.collision?(args, b))
          didSquare = true
          didHit = true
          s.collide(args, b)
          hitter = b
        end
      end
      if (didSquare == false)
        for c in shape.colliders
          #puts args.state.ball.velocity
          if c.collision?(args, b.getPoints(args),b)
            c.collide args, b
            didHit = true
            hitter = b
          end
        end
      end
    end
  end
  if (didHit)
    shape.count=shape.count-1
    shape.damageCount.append([(hitter.leastChain+1 - hitter.number)-1, args.state.tick_count])

  end
  i=0
  while i < shape.damageCount.length
    if shape.damageCount[i][0] <= 0
      shape.damageCount.delete_at(i)
      i-=1
    elsif shape.damageCount[i][1].elapsed_time > BALL_DISTANCE and shape.damageCount[i][0] > 1
      shape.count-=1
      shape.damageCount[i][0]-=1
      shape.damageCount[i][1] = args.state.tick_count
    end
    i+=1
  end
end


class Square
   attr_accessor :count, :x, :y, :home, :bold, :squareColliders, :colliders, :damageCount
   def initialize(args, x, y, block_size, orientation, block_offset)
        @x = x * block_size
        @y = y * block_size
        @block_size = block_size
        @block_offset = block_offset
        @orientation = orientation
        @damageCount = []
        @home = 'squares'


        Kernel.srand()
        @r = rand(255)
        @g = rand(255)
        @b = rand(255)

        @count = rand(MAX_COUNT)+1

        x_offset = (args.state.board_width + args.grid.w / 8) + @block_offset / 2
        @x_adjusted = @x + x_offset
        @y_adjusted = @y
        @size_adjusted = @block_size * 2 - @block_offset

        hypotenuse=args.state.ball_hypotenuse
        @bold = [(@x_adjusted-hypotenuse/2)-1, (@y_adjusted-hypotenuse/2)-1, @size_adjusted + hypotenuse + 2, @size_adjusted + hypotenuse + 2]

        @points = [
          {x:@x_adjusted, y:@y_adjusted},
          {x:@x_adjusted+@size_adjusted, y:@y_adjusted},
          {x:@x_adjusted+@size_adjusted, y:@y_adjusted+@size_adjusted},
          {x:@x_adjusted, y:@y_adjusted+@size_adjusted}
        ]
        @squareColliders = [
          SquareCollider.new(@points[0].x,@points[0].y,{x:-1,y:-1}),
          SquareCollider.new(@points[1].x-COLLISIONWIDTH,@points[1].y,{x:1,y:-1}),
          SquareCollider.new(@points[2].x-COLLISIONWIDTH,@points[2].y-COLLISIONWIDTH,{x:1,y:1}),
          SquareCollider.new(@points[3].x,@points[3].y-COLLISIONWIDTH,{x:-1,y:1}),
        ]
        @colliders = [
          LinearCollider.new(@points[0],@points[1], :neg),
          LinearCollider.new(@points[1],@points[2], :neg),
          LinearCollider.new(@points[2],@points[3], :pos),
          LinearCollider.new(@points[0],@points[3], :pos)
        ]
   end

   def draw(args)
    #Offset the coordinates to the edge of the game area
    x_offset = (args.state.board_width + args.grid.w / 8) + @block_offset / 2
    #args.outputs.solids << [@x + x_offset, @y, @block_size * 2 - @block_offset, @block_size * 2 - @block_offset, @r, @g, @b]
    args.outputs.solids <<{x: (@x + x_offset), y: (@y), w: (@block_size * 2 - @block_offset), h: (@block_size * 2 - @block_offset), r: @r , g: @g , b: @b }
    #args.outputs.solids << @bold.append([255,0,0])
    args.outputs.labels << [@x + x_offset + (@block_size * 2 - @block_offset)/2, (@y) + (@block_size * 2 - @block_offset)/2, @count.to_s]

   end

   def update args
     universalUpdate args, self
   end
end

class TShape
    attr_accessor :count, :x, :y, :home, :bold, :squareColliders, :colliders, :damageCount
    def initialize(args, x, y, block_size, orientation, block_offset)
        @x = x * block_size
        @y = y * block_size
        @block_size = block_size
        @block_offset = block_offset
        @orientation = orientation
        @damageCount = []
        @home = "tshapes"

        Kernel.srand()
        @r = rand(255)
        @g = rand(255)
        @b = rand(255)

        @count = rand(MAX_COUNT)+1


        @shapePoints = getShapePoints(args)
        minX={x:INFINITY, y:0}
        minY={x:0, y:INFINITY}
        maxX={x:-INFINITY, y:0}
        maxY={x:0, y:-INFINITY}
        for p in @shapePoints
          if p.x < minX.x
            minX = p
          end
          if p.x > maxX.x
            maxX = p
          end
          if p.y < minY.y
            minY = p
          end
          if p.y > maxY.y
            maxY = p
          end
        end


        hypotenuse=args.state.ball_hypotenuse

        @bold = [(minX.x-hypotenuse/2)-1, (minY.y-hypotenuse/2)-1, -((minX.x-hypotenuse/2)-1)+(maxX.x + hypotenuse + 2), -((minY.y-hypotenuse/2)-1)+(maxY.y + hypotenuse + 2)]
    end
    def getShapePoints(args)
      points=[]
      x_offset = (args.state.board_width + args.grid.w / 8) + (@block_offset / 2)

      if @orientation == :right
          #args.outputs.solids << [@x + x_offset, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
          #args.outputs.solids << [@x + x_offset, @y + @block_size, @block_size * 2, @block_size, @r, @g, @b]
          points = [
            {x:@x + x_offset, y:@y},
            {x:(@x + x_offset)+(@block_size - @block_offset), y:@y},
            {x:(@x + x_offset)+(@block_size - @block_offset),y:@y + @block_size},
            {x:(@x + x_offset)+ @block_size * 2,y:@y + @block_size},
            {x:(@x + x_offset)+ @block_size * 2,y:@y + @block_size+@block_size},
            {x:(@x + x_offset)+(@block_size - @block_offset),y:@y + @block_size+@block_size},
            {x:(@x + x_offset)+(@block_size - @block_offset), y:@y+ @block_size * 3 - @block_offset},
            {x:@x + x_offset , y:@y+ @block_size * 3 - @block_offset}
          ]
          @squareColliders = [
            SquareCollider.new(points[0].x,points[0].y,{x:-1,y:-1}),
            SquareCollider.new(points[1].x-COLLISIONWIDTH,points[1].y,{x:1,y:-1}),
            SquareCollider.new(points[2].x,points[2].y-COLLISIONWIDTH,{x:1,y:-1}),
            SquareCollider.new(points[3].x-COLLISIONWIDTH,points[3].y,{x:1,y:-1}),
            SquareCollider.new(points[4].x-COLLISIONWIDTH,points[4].y-COLLISIONWIDTH,{x:1,y:1}),
            SquareCollider.new(points[5].x,points[5].y,{x:1,y:1}),
            SquareCollider.new(points[6].x-COLLISIONWIDTH,points[6].y-COLLISIONWIDTH,{x:1,y:1}),
            SquareCollider.new(points[7].x,points[7].y-COLLISIONWIDTH,{x:-1,y:1}),
          ]
          @colliders = [
            LinearCollider.new(points[0],points[1], :neg),
            LinearCollider.new(points[1],points[2], :neg),
            LinearCollider.new(points[2],points[3], :neg),
            LinearCollider.new(points[3],points[4], :neg),
            LinearCollider.new(points[4],points[5], :pos),
            LinearCollider.new(points[5],points[6], :neg),
            LinearCollider.new(points[6],points[7], :pos),
            LinearCollider.new(points[0],points[7], :pos)
          ]
      elsif @orientation == :up
          #args.outputs.solids << [@x + x_offset, @y, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
          #args.outputs.solids << [@x + x_offset + @block_size, @y, @block_size, @block_size * 2, @r, @g, @b]
          points = [
            {x:@x + x_offset, y:@y},
            {x:(@x + x_offset)+(@block_size * 3 - @block_offset), y:@y},
            {x:(@x + x_offset)+(@block_size * 3 - @block_offset), y:@y+(@block_size - @block_offset)},
            {x:@x + x_offset + @block_size + @block_size, y:@y+(@block_size - @block_offset)},
            {x:@x + x_offset + @block_size + @block_size, y:@y+@block_size*2},
            {x:@x + x_offset + @block_size, y:@y+@block_size*2},
            {x:@x + x_offset + @block_size, y:@y+(@block_size - @block_offset)},
            {x:@x + x_offset, y:@y+(@block_size - @block_offset)}
          ]
          @squareColliders = [
            SquareCollider.new(points[0].x,points[0].y,{x:-1,y:-1}),
            SquareCollider.new(points[1].x-COLLISIONWIDTH,points[1].y,{x:1,y:-1}),
            SquareCollider.new(points[2].x-COLLISIONWIDTH,points[2].y-COLLISIONWIDTH,{x:1,y:1}),
            SquareCollider.new(points[3].x,points[3].y,{x:1,y:1}),
            SquareCollider.new(points[4].x-COLLISIONWIDTH,points[4].y-COLLISIONWIDTH,{x:1,y:1}),
            SquareCollider.new(points[5].x,points[5].y-COLLISIONWIDTH,{x:-1,y:1}),
            SquareCollider.new(points[6].x-COLLISIONWIDTH,points[6].y,{x:-1,y:1}),
            SquareCollider.new(points[7].x,points[7].y-COLLISIONWIDTH,{x:-1,y:1}),
          ]
          @colliders = [
            LinearCollider.new(points[0],points[1], :neg),
            LinearCollider.new(points[1],points[2], :neg),
            LinearCollider.new(points[2],points[3], :pos),
            LinearCollider.new(points[3],points[4], :neg),
            LinearCollider.new(points[4],points[5], :pos),
            LinearCollider.new(points[5],points[6], :neg),
            LinearCollider.new(points[6],points[7], :pos),
            LinearCollider.new(points[0],points[7], :pos)
          ]
      elsif @orientation == :left
          #args.outputs.solids << [@x + x_offset + @block_size, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
          #args.outputs.solids << [@x + x_offset, @y + @block_size, @block_size * 2 - @block_offset, @block_size - @block_offset, @r, @g, @b]
          xh = @x + x_offset
          #points = [
            #{x:@x + x_offset, y:@y},
            #{x:(@x + x_offset)+(@block_size - @block_offset), y:@y},
            #{x:(@x + x_offset)+(@block_size - @block_offset),y:@y + @block_size},
            #{x:(@x + x_offset)+ @block_size * 2,y:@y + @block_size},
            #{x:(@x + x_offset)+ @block_size * 2,y:@y + @block_size+@block_size},
            #{x:(@x + x_offset)+(@block_size - @block_offset),y:@y + @block_size+@block_size},
            #{x:(@x + x_offset)+(@block_size - @block_offset), y:@y+ @block_size * 3 - @block_offset},
            #{x:@x + x_offset , y:@y+ @block_size * 3 - @block_offset}
          #]
          points = [
            {x:@x + x_offset + @block_size, y:@y},
            {x:@x + x_offset + @block_size + (@block_size - @block_offset), y:@y},
            {x:@x + x_offset + @block_size + (@block_size - @block_offset),y:@y+@block_size*3- @block_offset},
            {x:@x + x_offset + @block_size, y:@y+@block_size*3- @block_offset},
            {x:@x + x_offset+@block_size, y:@y+@block_size*2- @block_offset},
            {x:@x + x_offset, y:@y+@block_size*2- @block_offset},
            {x:@x + x_offset, y:@y+@block_size},
            {x:@x + x_offset+@block_size, y:@y+@block_size}
          ]
          @squareColliders = [
            SquareCollider.new(points[0].x,points[0].y,{x:-1,y:-1}),
            SquareCollider.new(points[1].x-COLLISIONWIDTH,points[1].y,{x:1,y:-1}),
            SquareCollider.new(points[2].x-COLLISIONWIDTH,points[2].y-COLLISIONWIDTH,{x:1,y:1}),
            SquareCollider.new(points[3].x,points[3].y-COLLISIONWIDTH,{x:-1,y:1}),
            SquareCollider.new(points[4].x-COLLISIONWIDTH,points[4].y,{x:-1,y:1}),
            SquareCollider.new(points[5].x,points[5].y-COLLISIONWIDTH,{x:-1,y:1}),
            SquareCollider.new(points[6].x,points[6].y,{x:-1,y:-1}),
            SquareCollider.new(points[7].x-COLLISIONWIDTH,points[7].y-COLLISIONWIDTH,{x:-1,y:-1}),
          ]
          @colliders = [
            LinearCollider.new(points[0],points[1], :neg),
            LinearCollider.new(points[1],points[2], :neg),
            LinearCollider.new(points[2],points[3], :pos),
            LinearCollider.new(points[3],points[4], :neg),
            LinearCollider.new(points[4],points[5], :pos),
            LinearCollider.new(points[5],points[6], :neg),
            LinearCollider.new(points[6],points[7], :neg),
            LinearCollider.new(points[0],points[7], :pos)
          ]
      elsif @orientation == :down
          #args.outputs.solids << [@x + x_offset, @y + @block_size, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
          #args.outputs.solids << [@x + x_offset + @block_size, @y, @block_size - @block_offset, @block_size * 2 - @block_offset, @r, @g, @b]

          points = [
            {x:@x + x_offset, y:@y+(@block_size*2)-@block_offset},
            {x:@x + x_offset+ @block_size*3-@block_offset, y:@y+(@block_size*2)-@block_offset},
            {x:@x + x_offset+ @block_size*3-@block_offset, y:@y+(@block_size)},
            {x:@x + x_offset+ @block_size*2-@block_offset, y:@y+(@block_size)},
            {x:@x + x_offset+ @block_size*2-@block_offset, y:@y},#
            {x:@x + x_offset+ @block_size, y:@y},#
            {x:@x + x_offset + @block_size, y:@y+(@block_size)},
            {x:@x + x_offset, y:@y+(@block_size)}
          ]
          @squareColliders = [
            SquareCollider.new(points[0].x,points[0].y-COLLISIONWIDTH,{x:-1,y:1}),
            SquareCollider.new(points[1].x-COLLISIONWIDTH,points[1].y-COLLISIONWIDTH,{x:1,y:1}),
            SquareCollider.new(points[2].x-COLLISIONWIDTH,points[2].y,{x:1,y:-1}),
            SquareCollider.new(points[3].x,points[3].y-COLLISIONWIDTH,{x:1,y:-1}),
            SquareCollider.new(points[4].x-COLLISIONWIDTH,points[4].y,{x:1,y:-1}),
            SquareCollider.new(points[5].x,points[5].y,{x:-1,y:-1}),
            SquareCollider.new(points[6].x-COLLISIONWIDTH,points[6].y-COLLISIONWIDTH,{x:-1,y:-1}),
            SquareCollider.new(points[7].x,points[7].y,{x:-1,y:-1}),
          ]
          @colliders = [
            LinearCollider.new(points[0],points[1], :pos),
            LinearCollider.new(points[1],points[2], :pos),
            LinearCollider.new(points[2],points[3], :neg),
            LinearCollider.new(points[3],points[4], :pos),
            LinearCollider.new(points[4],points[5], :neg),
            LinearCollider.new(points[5],points[6], :pos),
            LinearCollider.new(points[6],points[7], :neg),
            LinearCollider.new(points[0],points[7], :neg)
          ]
      end
      return points
    end

    def draw(args)
        #Offset the coordinates to the edge of the game area
        x_offset = (args.state.board_width + args.grid.w / 8) + (@block_offset / 2)

        if @orientation == :right
            #args.outputs.solids << [@x + x_offset, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset), y: @y, w: @block_size - @block_offset, h: (@block_size * 3 - @block_offset), r: @r , g: @g, b: @b}
            #args.outputs.solids << [@x + x_offset, @y + @block_size, @block_size * 2, @block_size, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset), y: (@y + @block_size), w: (@block_size * 2), h: (@block_size), r: @r , g: @g, b: @b }
        elsif @orientation == :up
            #args.outputs.solids << [@x + x_offset, @y, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset), y: (@y), w: (@block_size * 3 - @block_offset), h: (@block_size - @block_offset), r: @r , g: @g, b: @b}
            #args.outputs.solids << [@x + x_offset + @block_size, @y, @block_size, @block_size * 2, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset + @block_size), y: (@y), w: (@block_size), h: (@block_size * 2), r: @r , g: @g, b: @b}
        elsif @orientation == :left
            #args.outputs.solids << [@x + x_offset + @block_size, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset + @block_size), y: (@y), w: (@block_size - @block_offset), h: (@block_size * 3 - @block_offset), r: @r , g: @g, b: @b}
            #args.outputs.solids << [@x + x_offset, @y + @block_size, @block_size * 2 - @block_offset, @block_size - @block_offset, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset), y: (@y + @block_size), w: (@block_size * 2 - @block_offset), h: (@block_size - @block_offset), r: @r , g: @g, b: @b}
        elsif @orientation == :down
            #args.outputs.solids << [@x + x_offset, @y + @block_size, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset), y: (@y + @block_size), w: (@block_size * 3 - @block_offset), h: (@block_size - @block_offset), r: @r , g: @g, b: @b}
            #args.outputs.solids << [@x + x_offset + @block_size, @y, @block_size - @block_offset, @block_size * 2 - @block_offset, @r, @g, @b]
            args.outputs.solids << {x: (@x + x_offset + @block_size), y: (@y), w: (@block_size - @block_offset), h: ( @block_size * 2 - @block_offset), r: @r , g: @g, b: @b}
        end

        #psize = 5.0
        #for p in @shapePoints
          #args.outputs.solids << [p.x-psize/2, p.y-psize/2, psize, psize, 0, 0, 0]
        #end
        args.outputs.labels << [@x + x_offset + (@block_size * 2 - @block_offset)/2, (@y) + (@block_size * 2 - @block_offset)/2, @count.to_s]

    end

    def updateOne_old args
      didHit = false
      hitter = nil
      toCollide = nil
      for b in args.state.balls
        if [b.x, b.y, b.width, b.height].intersect_rect?(@bold)
          didSquare = false
          for s in @squareColliders
            if (s.collision?(args, b))
              didSquare = true
              didHit = true
              #s.collide(args, b)
              toCollide = s
              hitter = b
              break
            end
          end
          if (didSquare == false)
            for c in @colliders
              #puts args.state.ball.velocity
              if c.collision?(args, b.getPoints(args),b)
                #c.collide args, b
                toCollide = c
                didHit = true
                hitter = b
                break
              end
            end
          end
        end
        if didHit
          break
        end
      end
      if (didHit)
        @count=0
        hitter.makeLeader args
        #toCollide.collide(args, hitter)
        args.state.tshapes.delete(self)
        #puts "HIT!" + hitter.number
      end
    end

    def update_old args
      if (@count == 1)
        updateOne args
        return
      end
      didHit = false
      hitter = nil
      for b in args.state.ballParents
        if [b.x, b.y, b.width, b.height].intersect_rect?(@bold)
          didSquare = false
          for s in @squareColliders
            if (s.collision?(args, b))
              didSquare = true
              didHit=true
              s.collide(args, b)
              hitter = b
            end
          end
          if (didSquare == false)
            for c in @colliders
              #puts args.state.ball.velocity
              if c.collision?(args, b.getPoints(args), b)
                c.collide args, b
                didHit=true
                hitter = b
              end
            end
          end
        end
      end
      if (didHit)
        @count=@count-1
        @damageCount.append([(hitter.leastChain+1 - hitter.number)-1, args.state.tick_count])

        if (@count == 0)
          args.state.tshapes.delete(self)
          return
        end
      end
      i=0

      while i < @damageCount.length
        if @damageCount[i][0] <= 0
          @damageCount.delete_at(i)
          i-=1
        elsif @damageCount[i][1].elapsed_time > BALL_DISTANCE
          @count-=1
          @damageCount[i][0]-=1
        end
        if (@count == 0)
          args.state.tshapes.delete(self)
          return
        end
        i+=1
      end
    end #end update

    def update args
      universalUpdate args, self
    end

end

class Line
    attr_accessor :count, :x, :y, :home, :bold, :squareColliders, :colliders, :damageCount
    def initialize(args, x, y, block_size, orientation, block_offset)
        @x = x * block_size
        @y = y * block_size
        @block_size = block_size
        @block_offset = block_offset
        @orientation = orientation
        @damageCount = []
        @home = "lines"

        Kernel.srand()
        @r = rand(255)
        @g = rand(255)
        @b = rand(255)

        @count = rand(MAX_COUNT)+1

        @shapePoints = getShapePoints(args)
        minX={x:INFINITY, y:0}
        minY={x:0, y:INFINITY}
        maxX={x:-INFINITY, y:0}
        maxY={x:0, y:-INFINITY}
        for p in @shapePoints
          if p.x < minX.x
            minX = p
          end
          if p.x > maxX.x
            maxX = p
          end
          if p.y < minY.y
            minY = p
          end
          if p.y > maxY.y
            maxY = p
          end
        end


        hypotenuse=args.state.ball_hypotenuse

        @bold = [(minX.x-hypotenuse/2)-1, (minY.y-hypotenuse/2)-1, -((minX.x-hypotenuse/2)-1)+(maxX.x + hypotenuse + 2), -((minY.y-hypotenuse/2)-1)+(maxY.y + hypotenuse + 2)]
    end

    def getShapePoints(args)
      points=[]
      x_offset = (args.state.board_width + args.grid.w / 8) + (@block_offset / 2)

      if @orientation == :right
        #args.outputs.solids << [@x + x_offset, @y, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
        xa =@x + x_offset
        ya =@y
        wa =@block_size * 3 - @block_offset
        ha =(@block_size - @block_offset)
      elsif @orientation == :up
        #args.outputs.solids << [@x + x_offset, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
        xa =@x + x_offset
        ya =@y
        wa =@block_size - @block_offset
        ha =@block_size * 3 - @block_offset

      elsif @orientation == :left
        #args.outputs.solids << [@x + x_offset, @y, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
        xa =@x + x_offset
        ya =@y
        wa =@block_size * 3 - @block_offset
        ha =@block_size - @block_offset
      elsif @orientation == :down
        #args.outputs.solids << [@x + x_offset, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
        xa =@x + x_offset
        ya =@y
        wa =@block_size - @block_offset
        ha =@block_size * 3 - @block_offset
      end
      points = [
        {x: xa, y:ya},
        {x: xa + wa,y:ya},
        {x: xa + wa,y:ya+ha},
        {x: xa, y:ya+ha},
      ]
      @squareColliders = [
        SquareCollider.new(points[0].x,points[0].y,{x:-1,y:-1}),
        SquareCollider.new(points[1].x-COLLISIONWIDTH,points[1].y,{x:1,y:-1}),
        SquareCollider.new(points[2].x-COLLISIONWIDTH,points[2].y-COLLISIONWIDTH,{x:1,y:1}),
        SquareCollider.new(points[3].x,points[3].y-COLLISIONWIDTH,{x:-1,y:1}),
      ]
      @colliders = [
        LinearCollider.new(points[0],points[1], :neg),
        LinearCollider.new(points[1],points[2], :neg),
        LinearCollider.new(points[2],points[3], :pos),
        LinearCollider.new(points[0],points[3], :pos),
      ]
      return points
    end

    def update args
      universalUpdate args, self
    end

    def draw(args)
        x_offset = (args.state.board_width + args.grid.w / 8) + @block_offset / 2

        if @orientation == :right
            args.outputs.solids << [@x + x_offset, @y, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
        elsif @orientation == :up
            args.outputs.solids << [@x + x_offset, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
        elsif @orientation == :left
            args.outputs.solids << [@x + x_offset, @y, @block_size * 3 - @block_offset, @block_size - @block_offset, @r, @g, @b]
        elsif @orientation == :down
            args.outputs.solids << [@x + x_offset, @y, @block_size - @block_offset, @block_size * 3 - @block_offset, @r, @g, @b]
        end

        args.outputs.labels << [@x + x_offset + (@block_size * 2 - @block_offset)/2, (@y) + (@block_size * 2 - @block_offset)/2, @count.to_s]

    end
end