Untitled

 avatar
unknown
plain_text
2 years ago
12 kB
5
Indexable
package de.tum.in.ase;

import de.tum.in.ase.logic.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.scene.control.Label;

import java.util.ArrayList;
import java.util.List;

import static javafx.geometry.Pos.CENTER;

public class Game extends Application {
    private GameBoard gameBoard;
    private Hero hero;
    private GridPane gridPane = new GridPane();
    private Button[][] buttons = new Button[SIZE_X][SIZE_Y];
    private List<Button> directionButtons;
    private List<Button> specialDirectionButtons;
    private HBox firstControlButtonsRow;
    private HBox secondControlButtonsRow;

    // Constants used for configuration/setup.
    private static final int SIZE_X = 5;
    private static final int SIZE_Y = 5;
    private Scene scene;
    private Stage stage;
    private static final double BUTTON_SIZE = 70;
    private static final double SCENE_WIDTH = 400;
    private static final double SCENE_HEIGHT = 550;
    private Alert alert = new Alert(Alert.AlertType.INFORMATION);

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        this.stage = primaryStage;
        // Do not change the hero type.
        this.hero = new Mage(this);
        this.gameBoard = new GameBoard(SIZE_X, SIZE_Y);
        this.directionButtons = new ArrayList<Button>();

        // TODO: Task 1.1, Initialization of the gameBoard visualization.
        //GridPane gPane = new GridPane(); //should it be called gridPane or is it alright gPane + this one or the variable above?
        //gPane.setAlignment(CENTER);
        //gridPane.setHgap(25);
        //gridPane.setVgap(15);

//        for (int i = 0; i < SIZE_Y; i++) {
//            ColumnConstraints colConst = new ColumnConstraints();
//            colConst.setPercentWidth(100.0 / SIZE_Y);
//            gPane.getColumnConstraints().add(colConst);
//        }
//        for (int i = 0; i < SIZE_X; i++) {
//            RowConstraints rowConst = new RowConstraints();
//            rowConst.setPercentHeight(100.0 / SIZE_X);
//            gPane.getRowConstraints().add(rowConst);
//        }

        for (int x = 0; x < SIZE_X; x++) {
            for (int y = 0; y < SIZE_Y; y++) {
                //Button variable
                Button button = new Button();
                button.setTextAlignment(TextAlignment.CENTER);
                button.setMinSize(BUTTON_SIZE, BUTTON_SIZE);
                buttons[x][y] = button;
                gridPane.add(button, x, y);
            }
        }

       // gridPane.setAlignment(Pos.CENTER);

//        stage.setScene(scene);
//        stage.show();

//        for (int x = 0; x < SIZE_X; x++) {
//            for (int y = 0; y < SIZE_Y; y++) {
//                //Button variable
//                Button button = new Button();
//                button.setTextAlignment(TextAlignment.CENTER);
//                button.setMinSize(BUTTON_SIZE, BUTTON_SIZE);
//                gPane.add(button, x, y);
//
//            }
//        }
        // TODO: Task 1.2, Movement buttons configuration.

        directionButtons = new ArrayList<>(); //new

//
        Button button1 = new Button("Down");
       // button1.setOnAction(action -> addDirectionButtonsFunctionality());

        Button button2 = new Button("Up");
       // button2.setOnAction(action -> addDirectionButtonsFunctionality());

        Button button3 = new Button("Right");
        //button3.setOnAction(action -> addSpecialDirectionButtonsFunctionality());

        Button button4 = new Button("Left");
       // button4.setOnAction(action -> addDirectionButtonsFunctionality());


        directionButtons.add(button1);
        directionButtons.add(button2);
        directionButtons.add(button3);
        directionButtons.add(button4);

        addDirectionButtonsFunctionality();

        applyStyleForButtons(directionButtons);

        firstControlButtonsRow = new HBox();//new
        firstControlButtonsRow.getChildren().addAll(directionButtons); //new

//

//
//        hBox.getChildren().addAll(directionButtons);



        // TODO: Task 1.3, Ability buttons configuration.


        specialDirectionButtons  = new ArrayList<>(); //new


        Button specialButton1 = new Button("Special Up");
        Button specialButton2 = new Button("Special Down");
        Button specialButton3 = new Button("Special Left");
        Button specialButton4 = new Button("Special Right");

        specialDirectionButtons.add(specialButton1);
        specialDirectionButtons.add(specialButton2);
        specialDirectionButtons.add(specialButton3);
        specialDirectionButtons.add(specialButton4);

        addSpecialDirectionButtonsFunctionality();
        applyStyleForButtons(specialDirectionButtons);


        secondControlButtonsRow = new HBox();
        secondControlButtonsRow.getChildren().addAll(specialDirectionButtons); //new





        // TODO: Task 2.1, Add an HBox for the movement buttons, an HBox for the ability buttons and add them to a VBox. Add the VBox to the gridPane.
        VBox vBox = new VBox(firstControlButtonsRow, secondControlButtonsRow);
        vBox.setAlignment(CENTER);
        vBox.setSpacing(10);
        vBox.setPadding(new Insets(10));

        //TODO 3.2: Center the content
        //vBox.getChildren().addAll(firstControlButtonsRow,secondControlButtonsRow);

        //gridPane.setAlignment(Pos.CENTER);
        //gridPane.setMinSize(400, 200);
        BorderPane bpane = new BorderPane();
        bpane.setTop(gridPane);
        bpane.setBottom(vBox);
        gridPane.addRow(7, vBox);

        scene = new Scene(bpane, SCENE_WIDTH, SCENE_HEIGHT);


        // Stage setup.
        stage.setScene(scene);
        stage.setTitle("Game");
        stage.setResizable(false);
        stage.show();

        // Displaying actual game status.
        updateUI();

    }

    /**
     * Applies custom (inline) css to the buttons.
     *
     * @param buttonsList List of the buttons without styles.
     */
    private void applyStyleForButtons(List<Button> buttonsList) {
        // TODO: Task 1.2, Movement buttons configuration.
        for (Button button : directionButtons) {
            // Add what I want as style
            button.setTextAlignment(TextAlignment.CENTER);
            button.setMinSize(BUTTON_SIZE, BUTTON_SIZE);
        }
    }

    /**
     * Adds functionality to specified direction buttons.
     * When a direction button is clicked, it will call the move method with the corresponding direction and then update the UI.
     */
    private void addDirectionButtonsFunctionality() {
        // TODO: Task 1.2, Movement buttons configuration.


        for (Button button : directionButtons) {
            button.setOnAction(e -> {

                try {
                    String buttonText = button.getText();
                    if (buttonText.equals("Up")) {
                        move('U');
                    } else if (buttonText.equals("Down")) {
                        move('D');
                    } else if (buttonText.equals("Left")) {
                        move('L');
                    } else if (buttonText.equals("Right")) {
                        move('R');
                    }
                }
                catch (Exception exception) {

                }

            });
        }
    }

    /**
     * Adds functionality to specified hero's special ability buttons.
     * When a hero's special ability button is clicked, it will call the hero's special ability method with the corresponding direction and then update the UI.
     */
    private void addSpecialDirectionButtonsFunctionality() {
        // TODO: Task 1.3, Ability buttons configuration.
        for (Button button : specialDirectionButtons) {
            button.setOnAction(e -> {

                try {
                    String buttonText = button.getText();
                    if (buttonText.equals("Special Up")) {
                        move('U');
                    } else if (buttonText.equals("Special Down")) {
                        move('D');
                    } else if (buttonText.equals("Special Left")) {
                        move('L');
                    } else if (buttonText.equals("Special Right")) {
                        move('R');
                    }
                }
                catch (Exception exception) {

                }

            });
        }

    }

    /**
     * Updates the current game status by updating text of the buttons in the
     * gridPane.
     */
    private void updateUI() {
        for (int i = 0; i < gameBoard.getSizeX(); i++) {
            for (int j = 0; j < gameBoard.getSizeY(); j++) {
                // TODO: Task 1.1
//                 Uncomment this line, once you have implemented Task 1
                 buttons[i][j].setText(String.valueOf(gameBoard.get(i, j)));
            }
        }
    }

    /**
     * Moves the character on the gameBoard in the indicated direction.
     *
     * @param direction Direction to move.
     * @throws IllegalMoveException Thrown if unsupported move has been detected.
     */
    public void move(char direction) throws IllegalMoveException {
        int deltaX = 0;
        int deltaY = 0;
        if (direction == 'L') {
            deltaX = -1;
            if (this.hero.getPosX() == 0) {
                throw new IllegalMoveException("IllegalMoveException: Cannot move left, out of bounds!");
            }
        } else if (direction == 'R') {
            deltaX = 1;
            if (this.hero.getPosX() == this.gameBoard.getSizeX() - 1) {
                throw new IllegalMoveException("IllegalMoveException: Cannot move right, out of bounds!");
            }
        } else if (direction == 'U') {
            deltaY = -1;
            if (this.hero.getPosY() == 0) {
                throw new IllegalMoveException("IllegalMoveException: Cannot move up, out of bounds!");
            }
        } else if (direction == 'D') {
            deltaY = 1;
            if (this.hero.getPosY() == this.gameBoard.getSizeY() - 1) {
                throw new IllegalMoveException("IllegalMoveException: Cannot move down, out of bounds!");
            }
        }
        if (this.gameBoard.get(this.hero.getPosX() + deltaX, this.hero.getPosY() + deltaY) == 'M') {
            informationAlert("You died!", "A monster has killed you!");
            restart();
        }
        this.gameBoard.set(this.hero.getPosX(), this.hero.getPosY(), '_');
        this.hero.setPosX(this.hero.getPosX() + deltaX);
        this.hero.setPosY(this.hero.getPosY() + deltaY);
        this.gameBoard.set(this.hero.getPosX(), this.hero.getPosY(), 'H');
        if (isWon()) {
            informationAlert("You won!", "You won the game!");
            restart();
        }
    }

    private void restart() {
        stage.close();
        Game game = new Game();
        game.start(stage);
    }

    /**
     * Shows user information alert.
     *
     * @param title   Title of the alert window.
     * @param content Content text of the alert.
     */
    private void informationAlert(String title, String content) {
        // TODO: Task 2.2, Displaying information alerts.
    }

    /**
     * Verify if the hero reached the goal.
     *
     * @return Result of the verification.
     */
    public boolean isWon() {
        return gameBoard.get(gameBoard.getSizeX() - 1, gameBoard.getSizeY() - 1) == 'H' && this.hero.getPosX() == gameBoard.getSizeX() - 1 && this.hero.getPosY() == gameBoard.getSizeY() - 1;
    }

    // Required for logic.
    public GameBoard getGameBoard() {
        return this.gameBoard;
    }

    // Required for logic.
    public Hero getHero() {
        return this.hero;
    }

}
Editor is loading...