Game UI #38
@ -4,26 +4,43 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
import javafx.application.Application;
 | 
			
		||||
import javafx.scene.Group;
 | 
			
		||||
import javafx.scene.Parent;
 | 
			
		||||
import javafx.scene.Scene;
 | 
			
		||||
import javafx.scene.control.Button;
 | 
			
		||||
 | 
			
		||||
import javafx.scene.input.KeyCombination;
 | 
			
		||||
import javafx.stage.Screen;
 | 
			
		||||
import javafx.stage.Stage;
 | 
			
		||||
import school_project.Parsers.FileParserFactory;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class Controller extends Application {
 | 
			
		||||
    private Stage stage;
 | 
			
		||||
    Parent root;
 | 
			
		||||
    public static Vec2 screen_size;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void start(Stage primaryStage) throws Exception {
 | 
			
		||||
        primaryStage.setTitle("test");
 | 
			
		||||
        Button btn = new Button("test");
 | 
			
		||||
        btn.setOnAction(event -> System.out.println("hey"));
 | 
			
		||||
    public void start(Stage primaryStage) throws IOException {
 | 
			
		||||
        stage = primaryStage;
 | 
			
		||||
        screen_size = new Vec2(
 | 
			
		||||
            (int) Screen.getPrimary().getBounds().getWidth(),
 | 
			
		||||
            (int) Screen.getPrimary().getBounds().getHeight()
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        Group root = new Group();
 | 
			
		||||
        root.getChildren().add(btn);
 | 
			
		||||
        stage.setTitle("ROAD TO MASTER YOU");
 | 
			
		||||
 | 
			
		||||
        Scene scene = new Scene(root, 300,300);
 | 
			
		||||
        primaryStage.setScene(scene);
 | 
			
		||||
        // Full Screen mode
 | 
			
		||||
        stage.setFullScreen(true);
 | 
			
		||||
        stage.setFullScreenExitHint("");
 | 
			
		||||
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
 | 
			
		||||
 | 
			
		||||
        primaryStage.show();
 | 
			
		||||
        root = new GameUI(FileParserFactory.loadMapFromFile(new File(getClass().getResource("level11.level").getFile())));
 | 
			
		||||
 | 
			
		||||
        Scene scene = new Scene(root, screen_size.x, screen_size.y);
 | 
			
		||||
        stage.setScene(scene);
 | 
			
		||||
        stage.show();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										61
									
								
								app/src/main/java/school_project/GameUI.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								app/src/main/java/school_project/GameUI.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
import javafx.scene.Group;
 | 
			
		||||
import javafx.scene.input.MouseButton;
 | 
			
		||||
import school_project.Utils.MatrixShape;
 | 
			
		||||
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
 | 
			
		||||
public class GameUI extends Group{
 | 
			
		||||
    public final static int SEGMENT_SIZE = 50;
 | 
			
		||||
    public final static int SPACE_SIZE = 5;
 | 
			
		||||
    private final Vec2 piece_pos_click = new Vec2();
 | 
			
		||||
 | 
			
		||||
    public GameUI(Map level) throws FileNotFoundException {
 | 
			
		||||
        super();
 | 
			
		||||
 | 
			
		||||
        MatrixShape grid = new MatrixShape(level);
 | 
			
		||||
 | 
			
		||||
        //center the grid
 | 
			
		||||
        grid.setLayoutX((Controller.screen_size.x - grid.boundary_size.x) >> 1);
 | 
			
		||||
        grid.setLayoutY((Controller.screen_size.y - grid.boundary_size.y) >> 1);
 | 
			
		||||
 | 
			
		||||
        getChildren().add(grid);
 | 
			
		||||
 | 
			
		||||
        Vec2 piece_space = new Vec2(SPACE_SIZE, SPACE_SIZE);
 | 
			
		||||
        int column = 0;
 | 
			
		||||
        for (Piece p : level.getPieces()) {
 | 
			
		||||
            MatrixShape _piece = new MatrixShape(p);
 | 
			
		||||
 | 
			
		||||
            _piece.setLayoutX(piece_space.x);
 | 
			
		||||
            _piece.setLayoutY(piece_space.y);
 | 
			
		||||
 | 
			
		||||
            piece_space.y += _piece.boundary_size.y;
 | 
			
		||||
 | 
			
		||||
            if(piece_space.y >= Controller.screen_size.y){
 | 
			
		||||
                column++;
 | 
			
		||||
                piece_space.y = SPACE_SIZE;
 | 
			
		||||
                piece_space.x = (SEGMENT_SIZE*3 + SPACE_SIZE*4 )* column;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Pieces Events
 | 
			
		||||
            _piece.setOnMouseClicked(event -> {
 | 
			
		||||
                if(event.getButton() == MouseButton.SECONDARY){
 | 
			
		||||
                    ((Piece) _piece.shape).RotateRight(1);
 | 
			
		||||
                    _piece.update();
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            _piece.setOnMousePressed(event -> {
 | 
			
		||||
                piece_pos_click.x = (int) event.getX();
 | 
			
		||||
                piece_pos_click.y = (int) event.getY();
 | 
			
		||||
            });
 | 
			
		||||
            _piece.setOnMouseDragged(event -> {
 | 
			
		||||
                _piece.setLayoutX(event.getSceneX() - piece_pos_click.x);
 | 
			
		||||
                _piece.setLayoutY(event.getSceneY() - piece_pos_click.y);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            getChildren().add(_piece);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -20,13 +20,19 @@ public class MapGenerator {
 | 
			
		||||
 | 
			
		||||
        // define map size depending on the difficulty
 | 
			
		||||
        switch (difficulty){
 | 
			
		||||
            case Easy -> map_size = new Vec2(rand.nextInt(3, 5), rand.nextInt(3, 5));
 | 
			
		||||
            case Medium -> map_size = new Vec2(rand.nextInt(5, 8), rand.nextInt(5, 8));
 | 
			
		||||
            case Difficult -> {
 | 
			
		||||
                map_size = new Vec2(rand.nextInt(8, 10), rand.nextInt(8, 10));
 | 
			
		||||
            case Easy:
 | 
			
		||||
                map_size = new Vec2(rand.nextInt(2) + 3, rand.nextInt(2) + 3);
 | 
			
		||||
                break;
 | 
			
		||||
            case Medium:
 | 
			
		||||
                map_size = new Vec2(rand.nextInt(3)+5, rand.nextInt(3)+5);
 | 
			
		||||
                break;
 | 
			
		||||
            case Difficult:
 | 
			
		||||
                map_size = new Vec2(rand.nextInt(2)+8, rand.nextInt(2)+8);
 | 
			
		||||
                depth = 2;
 | 
			
		||||
            }
 | 
			
		||||
            default -> map_size = new Vec2();
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                map_size = new Vec2();
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Cut edges
 | 
			
		||||
@ -57,7 +63,7 @@ public class MapGenerator {
 | 
			
		||||
        while (EmptySlots.size() > 0){
 | 
			
		||||
            Collections.shuffle(EmptySlots);
 | 
			
		||||
            Vec2 selected = EmptySlots.get(0);
 | 
			
		||||
            int size = rand.nextInt(1, 4);
 | 
			
		||||
            int size = rand.nextInt(3)+1;
 | 
			
		||||
            boolean[][] shape = new boolean[size][size];
 | 
			
		||||
            for(int i = 0; i < size; i++){
 | 
			
		||||
                for (int j = 0; j < size; j++) {
 | 
			
		||||
 | 
			
		||||
@ -68,7 +68,8 @@ public class Piece extends Shape{
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean equals(Object obj) {
 | 
			
		||||
        if(obj instanceof Piece pieceObj){
 | 
			
		||||
        if(obj instanceof Piece){
 | 
			
		||||
            Piece pieceObj = (Piece) obj;
 | 
			
		||||
            if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										64
									
								
								app/src/main/java/school_project/Utils/MatrixShape.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								app/src/main/java/school_project/Utils/MatrixShape.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,64 @@
 | 
			
		||||
package school_project.Utils;
 | 
			
		||||
 | 
			
		||||
import javafx.scene.Node;
 | 
			
		||||
import javafx.scene.image.Image;
 | 
			
		||||
import javafx.scene.image.ImageView;
 | 
			
		||||
import javafx.scene.layout.GridPane;
 | 
			
		||||
import javafx.scene.layout.Pane;
 | 
			
		||||
import javafx.scene.paint.Paint;
 | 
			
		||||
import javafx.scene.shape.Rectangle;
 | 
			
		||||
import school_project.*;
 | 
			
		||||
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
 | 
			
		||||
public class MatrixShape extends GridPane {
 | 
			
		||||
    public Shape shape;
 | 
			
		||||
    public Vec2 boundary_size = new Vec2();
 | 
			
		||||
    private Paint color;
 | 
			
		||||
    public MatrixShape(Shape shape) {
 | 
			
		||||
        super();
 | 
			
		||||
        this.shape = shape;
 | 
			
		||||
        if(shape instanceof Piece){
 | 
			
		||||
            Piece p = (Piece) shape;
 | 
			
		||||
            color = p.getColor();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        update();
 | 
			
		||||
 | 
			
		||||
        setHgap(GameUI.SPACE_SIZE);
 | 
			
		||||
        setVgap(GameUI.SPACE_SIZE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void update(){
 | 
			
		||||
        getChildren().clear();
 | 
			
		||||
        boolean[][] shape_matrix = shape.getShape();
 | 
			
		||||
        for (int i = 0; i < shape_matrix.length; i++) {
 | 
			
		||||
            for (int j = 0; j < shape_matrix[i].length; j++) {
 | 
			
		||||
                Node _cell;
 | 
			
		||||
                if(shape_matrix[i][j]){
 | 
			
		||||
                    if(shape instanceof Piece){
 | 
			
		||||
                        Piece p = (Piece) shape;
 | 
			
		||||
                        _cell = new Rectangle(GameUI.SEGMENT_SIZE, GameUI.SEGMENT_SIZE);
 | 
			
		||||
                        ((Rectangle) _cell).setFill(color);
 | 
			
		||||
                    }else{
 | 
			
		||||
                        try {
 | 
			
		||||
                          _cell = new ImageView(new Image(new FileInputStream(Controller.class.getResource("tile.png").getFile())));
 | 
			
		||||
                        } catch (FileNotFoundException e) {
 | 
			
		||||
                            throw new RuntimeException(e);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else{
 | 
			
		||||
                    _cell = new Pane();
 | 
			
		||||
                    ((Pane) _cell).setPrefSize(GameUI.SEGMENT_SIZE, GameUI.SEGMENT_SIZE);
 | 
			
		||||
                }
 | 
			
		||||
                add(_cell, j, i);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        boundary_size = new Vec2((GameUI.SEGMENT_SIZE + GameUI.SPACE_SIZE) * shape.getWidth(), (GameUI.SEGMENT_SIZE + GameUI.SPACE_SIZE) * shape.getHeight());
 | 
			
		||||
    }
 | 
			
		||||
    public void setColor(Paint p) {
 | 
			
		||||
        color = p;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -21,7 +21,8 @@ public class Vec2 implements Serializable {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean equals(Object obj) {
 | 
			
		||||
        if (obj instanceof Vec2 vec) {
 | 
			
		||||
        if (obj instanceof Vec2) {
 | 
			
		||||
            Vec2 vec = (Vec2) obj;
 | 
			
		||||
            return this.x == vec.x && this.y == vec.y;
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								app/src/main/resources/school_project/tile.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/resources/school_project/tile.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 3.8 KiB  | 
		Reference in New Issue
	
	Block a user