Skip to content
Snippets Groups Projects
GameController.java 2.89 KiB
Newer Older
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
package controller;

Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import javafx.scene.Node;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;

import java.io.IOException;
import java.net.URL;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import java.util.Iterator;
import java.util.Random;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import java.util.ResourceBundle;

Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
import model.Board;

public class GameController implements Initializable {

    /*  ----- FXML ATTRIBUTES -----  */

        @FXML
        private GridPane board;

        @FXML
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
        private Label labelLevelNumber;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed


    /*  ----- ATTRIBUTES -----  */

        /**
         *
         */
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
        private static final String LEVEL_FILENAME = "data/level";
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
         *
         */
        private static final int HOW_MANY_LEVELS = 1;

        /**
         *
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
         */
        private Board model;

Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
        /**
         *
         */
        private Color[] colors;

        /**
         *
         */
        private IntegerProperty levelNumber;

        public GameController() {
            levelNumber = new SimpleIntegerProperty();
        }

        public void nextLevel() {
            levelNumber.set(levelNumber.get() + 1);
            try {
                model = new Board(LEVEL_FILENAME + levelNumber.get() + ".txt");
            } catch (IOException e) {
                System.out.println(LEVEL_FILENAME + levelNumber.get() + ".txt not found");
            }
        }

        private void initColors() {
            colors = new Color[model.getCarsNumber()];
            colors[0] = Color.RED;

            for (int i = 1; i < colors.length; i++) {
                Random random = new Random();
                colors[i] = Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            }
        }

        private void initBoard() {
            for (int i = 0; i < model.getSize(); i++)
                for (int j = 0; j < model.getSize(); j++)
                    setCaseColor(i, j, model.getValue(i, j));
        }

        private void setCaseColor(int i, int j, int value) {
            Iterator<Node> itr = board.getChildren().iterator();
            for (int k = 0; k < i * model.getSize() + j; k++)
                itr.next();

            ((Rectangle) itr.next()).setFill(colors[value]);
        }
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed

        @Override
        public void initialize(URL location, ResourceBundle resources) {
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
            nextLevel();
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
            Rectangle cell;
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
            double size = 300.0 / model.getSize();
            for (int i = 0; i < model.getSize(); i++)
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
                for (int j = 0; j < model.getSize(); j++)
                    board.add(new Rectangle(size, size), i, j);

            labelLevelNumber.textProperty().bind(levelNumber.asString());
            initColors();
            initBoard();
Alexis BUSSENEAU's avatar
Alexis BUSSENEAU committed
}