2021-06-25 18:42:16 +02:00

360 lines
9.8 KiB
Java

package launcher;
import gameplay.match.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.CheckBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.fxml.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* <p>Game configuration window and engine launcher</p>
* @author François Autin
*/
@SuppressWarnings("unchecked")
public class Launcher extends Application {
//public static Launcher pointer; // Self pointer, required to regain context if ever exited
private static Settings setter; // Settings class allows manipulation of settings
protected static HashMap<String, Object> arraysettings; // Array that allows passing values of UI elements to setter
private static Map<String, Object> namespace; // Namespace containing all elements from UI
/**
* Starts the Launcher thread
*/
public Launcher() {
try {
setter = new Settings();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
arraysettings = new HashMap<String, Object>();
}
/**
* Start method is used by Launcher as an implementation of the Application class to create a JavaFX thread to display the GUI window
* @param primaryStage the base Stage on which to place UI elements
* @throws IOException if the FXMLLoader fails to find or parse the launcher.fxml file
* @author François Autin
*/
public void start(Stage primaryStage) throws IOException {
// Loading UI from FXML file
FXMLLoader loader = new FXMLLoader(getClass().getResource("launcher.fxml"));
Parent root = loader.load();
Scene main = new Scene(root);
// Assigning pointer to namespace
namespace = loader.getNamespace();
/******************/
/* Resolution box */
/******************/
// Getting resolution ChoiceBox object from namespace
ChoiceBox<String> cb = (ChoiceBox<String>) namespace.get("resolution");
// Assigning list of possible choices to ChoiceBox
ObservableList<String> availableres = FXCollections.observableArrayList("320x240", "640x480", "800x600", "1024x768", "1280x720", "1366x768", "1600x900", "1920x1080");
cb.setItems(availableres);
// Setting default ChoiceBox value to the one already in the config file
if (!availableres.contains(setter.getResolution())) {
cb.setValue("640x480");
} else {
cb.setValue(setter.getResolution());
}
/******************/
/* Fullscreen box */
/******************/
// Getting fullscreen CheckBox object from namespace
CheckBox fs = (CheckBox) namespace.get("fullscreen");
// Setting default CheckBox value to the one already in the config file
if(setter.getFullscreen()) {
fs.setSelected(true);
} else {
fs.setSelected(false);
}
/**************/
/* Rounds box */
/**************/
// Getting rounds ChoiceBox object from namespace
ChoiceBox<String> cbr = (ChoiceBox<String>) namespace.get("rounds");
// Assigning list of possible choices to ChoiceBox
ObservableList<String> nbrounds = FXCollections.observableArrayList("1", "3", "5", "7", "9");
cbr.setItems(nbrounds);
// Setting default ChoiceBox value to the one already in the config file
if (!nbrounds.contains(setter.getRounds())) {
cbr.setValue("3");
} else {
cbr.setValue(setter.getRounds());
}
/****************/
/* Hitboxes box */
/****************/
// Getting hitboxes CheckBox from namespace
CheckBox hb = (CheckBox) namespace.get("hitboxes");
// Setting default CheckBox value to the one already in the config file
if (setter.getHitboxes()) {
hb.setSelected(true);
} else {
hb.setSelected(false);
}
/********************/
/* Character select */
/********************/
// Getting character 1 ChoiceBox from namespace
VBox v1 = (VBox) namespace.get("p1");
ChoiceBox<String> b1 = (ChoiceBox<String>) v1.getChildren().get(1);
// Getting character 2 ChoiceBox from namespace
VBox v2 = (VBox) namespace.get("p2");
ChoiceBox<String> b2 = (ChoiceBox<String>) v2.getChildren().get(1);
// Assigning list of possible choices to ChoiceBoxes
ObservableList<String> availablechar = FXCollections.observableArrayList("Blue");
b1.setItems(availablechar);
b2.setItems(availablechar);
// Setting default ChoiceBoxes values to the ones already in the config file
if(setter.getChar1().equals("blue") || setter.getChar1().equals("default")) {
b1.setValue("Blue");
}
if(setter.getChar2().equals("blue") || setter.getChar2().equals("default")) {
b2.setValue("Blue");
}
// Hiding character select (only a single character anyways
v1.getParent().setManaged(false);
v1.getParent().setVisible(false);
/*******************/
/* Window settings */
/*******************/
// Removing window decorations
primaryStage.initStyle(StageStyle.UNDECORATED);
// Setting the window as unresizeable
primaryStage.setResizable(false);
// Setting window title
primaryStage.setTitle("Boulevard Combattant");
// Assinging main scene to primaryStage
primaryStage.setScene(main);
// Adding icon to taskbar
primaryStage.getIcons().add(new Image(Launcher.class.getResourceAsStream("logo.png")));
// Showing the stage to the user
primaryStage.show();
}
/**
* saves all settings to game.set and then launches the Engine thread
* @author François Autin
*/
@FXML
private void runGame() {
try {
fillArraySettings();
setter.setSettings();
hideWindow();
Thread t = new Thread() {
public void run() {
try {
match.main(null);
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
t.join();
showWindow();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/**
* fills the array bridging between the launcher and the setter class
* @throws NullPointerException if namespace does not contain required classes
* @author François Autin
*/
private void fillArraySettings() throws NullPointerException {
// Converting the choice of resolution to two workable variables
int width, height;
ChoiceBox<String> cb = (ChoiceBox<String>) namespace.get("resolution");
switch (cb.getValue()) {
case "320x240":
width = 320;
height = 240;
break;
case "800x600":
width = 800;
height = 600;
break;
case "1024x768":
width = 1024;
height = 768;
break;
case "1280x720":
width = 1280;
height = 720;
break;
case "1366x768":
width = 1366;
height = 768;
break;
case "1600x900":
width = 1600;
height = 900;
break;
case "1920x1080":
width = 1920;
height = 1080;
break;
default:
width = 640;
height = 480;
break;
}
arraysettings.put("width", width);
arraysettings.put("height", height);
// Fullscreen
CheckBox fs = (CheckBox) namespace.get("fullscreen");
arraysettings.put("fullscreen", fs.isSelected());
// Hitboxes
CheckBox hb = (CheckBox) namespace.get("hitboxes");
arraysettings.put("hitboxes", hb.isSelected());
// Number of rounds
ChoiceBox<String> rnd = (ChoiceBox<String>) namespace.get("rounds");
arraysettings.put("rounds", rnd.getValue());
// Character 1
VBox vp1 = (VBox) namespace.get("p1");
ChoiceBox<String> p1 = (ChoiceBox<String>) vp1.getChildren().get(1);
arraysettings.put("character1", p1.getValue().toLowerCase());
// Character 2
VBox vp2 = (VBox) namespace.get("p2");
ChoiceBox<String> p2 = (ChoiceBox<String>) vp2.getChildren().get(1);
arraysettings.put("character2", p2.getValue().toLowerCase());
// Stage
arraysettings.put("stage", "default");
}
/**
* hides the launcher
*/
@FXML
private void hideWindow() {
HBox hb = (HBox) namespace.get("window");
Scene sc = hb.getScene();
Window win = sc.getWindow();
win.hide();
}
/**
* shows the launcher
* @throws Exception
*/
@FXML
private void showWindow() throws Exception {
setter.parse();
Stage st = new Stage();
start(st);
}
/**
* quits the launcher and the game
*/
@FXML
private void quit() {
System.exit(0);
}
/**
* links to the gitlab project page
*/
@FXML
private void website() {
getHostServices().showDocument("https://gitlab.istic.univ-rennes1.fr/fautin/jeu-de-combat");
}
/**
* changes the character image of the player 1 depending on its character selection
*/
@FXML
private void chp1() {
chp(1);
}
/**
* changes the character image of the player 1 depending on its character selection
*/
@FXML
private void chp2() {
chp(2);
}
/**
* changes the character image of the player n depending on its character selection
*/
@FXML
private void chp(int n) {
// if we try to change the character image of a player who does not exist, we simply return without doing anything
if(n > 2 || n < 1) {
return;
}
// getting the corresponding VBox
VBox v = (VBox) namespace.get("p" + Integer.toString(n));
// getting the first child of the VBox (the imageview containing the character image
ImageView iv = (ImageView) v.getChildren().get(0);
// Evaluating the new character choice in order to change the image accordingly
ChoiceBox<String> b = (ChoiceBox<String>) v.getChildren().get(1);
switch (b.getValue()) {
case "Blue":
iv.setImage(new Image("/launcher/charfaces/blue.png"));
break;
default:
iv.setImage(new Image("/launcher/default.png"));
break;
}
}
/**
* Returns the hashmap containing all settings inputed in the launcher by the user
* @return the hashmap containing all settings inputed in the launcher by the user
*/
protected static HashMap<String, Object> getArraysettings() {
return arraysettings;
}
}