/*----------------------------------------------------------------------------
 * Klasse:		    Bande.java
 * Beschreibung:	Randbande und Hindernisse der Bahn
 * Autor:		    Jack Meyer
 * Datum:		    November, 1997
 *---------------------------------------------------------------------------
 */


import java.awt.*;


class Bande extends Hindernis {

    /**
     * Display Farbe der Bande, Klassenkonstante!
     */
     static final Color colorBande = new Color (255,255,102);
     static final int breite = 2;


    /**
     * Konstruktor der Bande,
     * Koordinaten sind Endpunkte der Bande, nicht Grösse
     */
    Bande (int x1, int y1, int x2, int y2) {
//        super (x1-breite/2, y1-breite/2, x2-x1+breite, y2-y1+breite);
        super (x1, y1, x2, y2);
    };
    /**
     *  String Konstruktor der Bande
     */
    Bande (String aStringParameter)
    throws ParsingException,
           NumberFormatException,
           StringIndexOutOfBoundsException
    {
        this (0,0,0,0);
        double[] dArgs = Parser.ParseStringToDouble(aStringParameter);

        /* testen ob genau 4 Parameter vorhanden */
        if (dArgs.length != 4) throw new ParsingException ("Bande muss genau 4 Parameter (X1,Y1, X2, Y2) haben");

        /* Parameter neu setzten */
//        this.setPos ((int)(dArgs[0]-breite/2), (int)(dArgs[1]-breite/2));
//        this.setSize ((int)(dArgs[2]-dArgs[0]+breite), (int)(dArgs[3]-dArgs[1]+breite));
        this.setPos ((int)(dArgs[0]), (int)(dArgs[1]));
        this.setSize ((int)(dArgs[2]), (int)(dArgs[3]));
    };


    /**
     * Zeichenmethode der Bande, zeichne Rechteck
     */
    void draw (Graphics g, int posX, int posY)
    {
        g.setColor(colorBande);
//        g.fillRect(posX+position.x, posY+position.y, size.x, size.y);
        // zeichne trotzdem linie, breite ?????
        g.drawLine(posX+position.x, posY+position.y, posX+size.x, posY+size.y);
    };


    /**
     * Bewegung des Balls: Test ob Kollision
     */
    void collisionDetection (Ball aBall, int posX, int posY)
    {
        // test ob Kollision mit Bande, für Reflexion

        // Variante für Version 1, nur horizontale oder vertikale Banden
        // test auf horizontale Bande
        if (position.y == size.y) {
//            System.out.println("Kollision Check mit Horizontaler Bande");
            // teste ob Ball nach unten fährt
            if (aBall.getSpeedY() > 0) {
//                System.out.println("Bewegung nach unten");
                // teste ob Ball in nächster Zeit die Bande überfährt
                // und auch innerhalb der Bandengrenzen in X-Richtung ist
                if ((aBall.getPosY() <= (posY+this.position.y)) &
                    (aBall.getPosY() + aBall.getSpeedY()) >= (posY+this.position.y) &
                    (aBall.getPosX() >= Math.min((posX+this.position.x),(posX+this.size.x))) &
                    (aBall.getPosX() <= Math.max((posX+this.position.x),(posX+this.size.x)))
                   ) {
                    // we will have a collision
//                    System.out.println("Kollision mit Bande, neue Richtung");
                    aBall.setDirection (-(aBall.getDirection()));
                };
            };
            // teste ob Ball nach oben fährt
            if (aBall.getSpeedY() < 0) {
//                System.out.println("Bewegung nach oben");
                // teste ob Ball in nächster Zeit die Bande überfährt
                // und auch innerhalb der Bandengrenzen in X-Richtung ist
                if ((aBall.getPosY() >= (posY+this.position.y)) &
                    (aBall.getPosY() + aBall.getSpeedY()) <= (posY+this.position.y) &
                    (aBall.getPosX() >= Math.min((posX+this.position.x),(posX+this.size.x))) &
                    (aBall.getPosX() <= Math.max((posX+this.position.x),(posX+this.size.x)))
                   ) {
                    // we will have a collision
//                    System.out.println("Kollision mit Bande, neue Richtung");
                    aBall.setDirection (-(aBall.getDirection()));
                };
            };
        };
        // test auf vertikale Bande
        if (position.x == size.x) {
//            System.out.println("Kollision Check mit Vertikaler Bande");
            // teste ob Ball nach rechts fährt
            if (aBall.getSpeedX() > 0) {
//                System.out.println("Bewegung nach rechts");
                // teste ob Ball in nächster Zeit die Bande überfährt
                // und auch innerhalb der Bandengrenzen in Y-Richtung ist
                if ((aBall.getPosX() <= (posX+this.position.x)) &
                    (aBall.getPosX() + aBall.getSpeedX()) >= (posX+this.position.x) &
                    (aBall.getPosY() >= Math.min((posY+this.position.y),(posY+this.size.y))) &
                    (aBall.getPosY() <= Math.max((posY+this.position.y),(posY+this.size.y)))
                   ) {
                    // we will have a collision
//                    System.out.println("Kollision mit Bande, neue Richtung");
                    aBall.setDirection (Math.PI-aBall.getDirection());
                };
            };
            // teste ob Ball nach links fährt
            if (aBall.getSpeedX() < 0) {
//                System.out.println("Bewegung nach links");
                // teste ob Ball in nächster Zeit die Bande überfährt
                // und auch innerhalb der Bandengrenzen in Y-Richtung ist
                if ((aBall.getPosX() >= (posX+this.position.x)) &
                    (aBall.getPosX() + aBall.getSpeedX()) <= (posX+this.position.x) &
                    (aBall.getPosY() >= Math.min((posY+this.position.y),(posY+this.size.y))) &
                    (aBall.getPosY() <= Math.max((posY+this.position.y),(posY+this.size.y)))
                   ) {
                    // we will have a collision
//                    System.out.println("Kollision mit Bande, neue Richtung");
                    aBall.setDirection (Math.PI-aBall.getDirection());
                };
            };
        };
    };


};