package ge.edu.freeuni.sdp.arkanoid.model;

import ge.edu.freeuni.sdp.arkanoid.model.geometry.Point;
import ge.edu.freeuni.sdp.arkanoid.model.geometry.Rectangle;
import ge.edu.freeuni.sdp.arkanoid.model.geometry.Size;

public class Beam extends Gobj<Rectangle> {

    private boolean _isAlive;
    private Room _room;

    Beam(Point position, Room room) {
        super(position);
        _isAlive = true;
        _room = room;
    }

    @Override
    public GobjKind getKind() {
        return GobjKind.Beam;
    }

    public void interact(Gobj other) {
        if(other instanceof Brick) {
            Brick brick = (Brick) other;
            brick.interact(new Ball());
        }
    }

    @Override
    public Rectangle getShape() {
        int height = (int) getHeight();
        System.out.print(height);
        Size size = new Size(1, height);
        return new Rectangle(new Point(getPosition().X, getPosition().Y - height), size);
    }

    @Override
    public boolean isAlive() {
        return _isAlive;
    }

    private double getHeight() {
        Brick lowestBrick = null;

        for(Gobj obj : _room.getGobjs()) {
            if(obj instanceof  Brick) {
                Brick brick = (Brick) obj;
                double brickY = brick.getPosition().Y;
                int brickWidth = brick.getShape().getSize().getWidth();
                double beamY = getPosition().Y;
                if (beamY >= brickY && beamY <= brickY + brickWidth) {
                    if (lowestBrick == null)
                        lowestBrick = brick;
                    else if (lowestBrick.getPosition().X > brick.getPosition().X)
                        lowestBrick = brick;
                }
            }
        }
        return Math.abs(lowestBrick.getPosition().X - getPosition().X);
    }

}

@@ -1245,17 +1245,17 @@
 le brick
-Y
+X
  = brick
@@ -1261,33 +1261,33 @@
 k.getPosition().
-Y
+X
 ;%0A              
@@ -1371,17 +1371,17 @@
 ble beam
-Y
+X
  = getPo
@@ -1389,17 +1389,17 @@
 ition().
-Y
+X
 ;%0A      
@@ -1416,17 +1416,17 @@
 if (beam
-Y
+X
  %3E= bric
@@ -1430,17 +1430,17 @@
 rick
-Y
+X
  && beam
 Y %3C=
@@ -1435,17 +1435,17 @@
  && beam
-Y
+X
  %3C= bric
@@ -1445,17 +1445,17 @@
 %3C= brick
-Y
+X
  + brick
@@ -1608,17 +1608,17 @@
 ition().
-X
+Y
  %3E brick
@@ -1632,17 +1632,17 @@
 ition().
-X
+Y
 )%0A      
@@ -1772,17 +1772,17 @@
 ition().
-X
+Y
  - getPo
@@ -1790,17 +1790,17 @@
 ition().
-X
+Y
 );%0A    %7D

Change X and Y coordinates to calculate beam height