forked from brunoborges/fx2048
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame2048.java
110 lines (92 loc) · 3.34 KB
/
Game2048.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package io.fxgame.game2048;
import dev.webfx.kit.util.scene.DeviceSceneUtil;
import dev.webfx.platform.resource.Resource;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
/**
* @author Bruno Borges
*/
public class Game2048 extends Application {
public static final String VERSION = "1.1.0";
private static Game2048 applicationInstance;
private GamePane gamePane;
@Override
public void stop() {
gamePane.getGameManager().saveRecord();
}
@Override
public void start(Stage primaryStage) {
var scene = DeviceSceneUtil.newScene(new Pane() /* temporary dummy root */, 600, 600);
//scene.getStylesheets().add(Resource.toUrl("css/game.css", getClass()));
// Ensuring the font is loaded before instantiating GamePane (otherwise some bounds may be wrong on first layout - ex: "FX" after 2048)
//Font.loadFont(Resource.toUrl("css/ClearSans-Bold.ttf", getClass()), 14);
gamePane = new GamePane();
scene.setRoot(gamePane);
gamePane.requestFocus();
setGameBounds(primaryStage, scene);
setEnhancedDeviceSettings(primaryStage, scene);
setQuitListener(primaryStage);
primaryStage.show();
}
private void setQuitListener(Stage primaryStage) {
primaryStage.setOnCloseRequest(t -> {
t.consume();
gamePane.getGameManager().quitGame();
});
}
private void setEnhancedDeviceSettings(Stage primaryStage, Scene scene) {
/*
var isARM = System.getProperty("os.arch").toUpperCase().contains("ARM");
if (isARM) {
primaryStage.setFullScreen(true);
primaryStage.setFullScreenExitHint("");
}
*/
/*if (Platform.isSupported(ConditionalFeature.INPUT_TOUCH)) {
scene.setCursor(Cursor.NONE);
}*/
}
private void setGameBounds(Stage primaryStage, Scene scene) {
/*
var margin = UserSettings.MARGIN;
var gameBounds = gamePane.getGameManager().getLayoutBounds();
var visualBounds = Screen.getPrimary().getVisualBounds();
double factor = Math.min(visualBounds.getWidth() / (gameBounds.getWidth() + margin),
visualBounds.getHeight() / (gameBounds.getHeight() + margin));
*/
primaryStage.setTitle("2048FX");
primaryStage.setScene(scene);
/*
primaryStage.setMinWidth(gameBounds.getWidth() / 2d);
primaryStage.setMinHeight(gameBounds.getHeight() / 2d);
primaryStage.setWidth(((gameBounds.getWidth() + margin) * factor) / 1.5d);
primaryStage.setHeight(((gameBounds.getHeight() + margin) * factor) / 1.5d);
*/
}
public static interface URLOpener {
public void open(String url);
}
public static URLOpener urlOpener() {
return (url) -> getInstance().getHostServices().showDocument(url);
}
private synchronized static Game2048 getInstance() {
/*
if (applicationInstance == null) {
while (applicationInstance == null) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
*/
return applicationInstance;
}
public Game2048() {
applicationInstance = this;
}
}