You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.0 KiB
158 lines
5.0 KiB
package visualiser.Controllers;
|
|
|
|
import com.interactivemesh.jfx.importer.stl.StlMeshImporter;
|
|
import javafx.animation.AnimationTimer;
|
|
import javafx.application.Platform;
|
|
import javafx.collections.FXCollections;
|
|
import javafx.collections.ObservableList;
|
|
import javafx.event.EventHandler;
|
|
import javafx.fxml.FXML;
|
|
import javafx.fxml.FXMLLoader;
|
|
import javafx.scene.Parent;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.ButtonType;
|
|
import javafx.scene.control.SplitPane;
|
|
import javafx.scene.image.ImageView;
|
|
import javafx.scene.layout.AnchorPane;
|
|
import javafx.scene.layout.GridPane;
|
|
import javafx.scene.shape.MeshView;
|
|
import javafx.stage.Stage;
|
|
import javafx.stage.WindowEvent;
|
|
import mock.app.Event;
|
|
import mock.exceptions.EventConstructionException;
|
|
import visualiser.Controllers2.Controller2;
|
|
import visualiser.Controllers2.StartController;
|
|
import visualiser.app.App;
|
|
import visualiser.layout.Subject3D;
|
|
import visualiser.layout.View3D;
|
|
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import java.net.URL;
|
|
import java.util.Optional;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* Controller for Hosting a game.
|
|
*/
|
|
public class HostController extends Controller2 {
|
|
private @FXML ImageView imageView;
|
|
private @FXML AnchorPane imagePane;
|
|
private @FXML SplitPane splitPane;
|
|
private @FXML AnchorPane specPane;
|
|
private @FXML GridPane playerContainer;
|
|
private Event game;
|
|
private View3D view3D;
|
|
|
|
public void initialize() {
|
|
hostGame();
|
|
ObservableList<Subject3D> subjects = FXCollections.observableArrayList();
|
|
|
|
view3D = new View3D();
|
|
view3D.setItems(subjects);
|
|
playerContainer.add(view3D, 0,0);
|
|
|
|
URL asset = HostController.class.getClassLoader().getResource("assets/V1.2 Complete Boat.stl");
|
|
|
|
StlMeshImporter importer = new StlMeshImporter();
|
|
importer.read(asset);
|
|
Subject3D subject = new Subject3D(new MeshView(importer.getImport()));
|
|
|
|
subjects.add(subject);
|
|
|
|
view3D.setPivot(subject);
|
|
view3D.setDistance(50);
|
|
view3D.setYaw(45);
|
|
view3D.setPitch(20);
|
|
|
|
AnimationTimer rotate = new AnimationTimer() {
|
|
@Override
|
|
public void handle(long now) {
|
|
subject.setHeading(subject.getHeading() + 0.1);
|
|
}
|
|
};
|
|
rotate.start();
|
|
}
|
|
|
|
/**
|
|
* Hosts a game
|
|
*/
|
|
public void hostGamePressed() {
|
|
try {
|
|
this.game = new Event(false);
|
|
connectSocket("localhost", 4942);
|
|
} catch (EventConstructionException e) {
|
|
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public void endEvent() throws IOException {
|
|
game.endEvent();
|
|
}
|
|
|
|
/**
|
|
* Connect to a socket
|
|
* @param address address of the server
|
|
* @param port port that the server is run off
|
|
*/
|
|
public void connectSocket(String address, int port) {
|
|
try{
|
|
Socket socket = new Socket(address, port);
|
|
StartController sc = (StartController)loadScene
|
|
("start.fxml");
|
|
sc.enterLobby(socket, true);
|
|
} catch (IOException e) { /* Never reached */ }
|
|
}
|
|
|
|
/**
|
|
* Hosts a game.
|
|
*/
|
|
public void hostGame(){
|
|
splitPane.setResizableWithParent(specPane, false);
|
|
splitPane.lookupAll(".split-pane-divider").stream().forEach(div -> div.setMouseTransparent(true));
|
|
imageView.fitWidthProperty().bind(imagePane.widthProperty());
|
|
imageView.fitHeightProperty().bind(imagePane.heightProperty());
|
|
// hostWrapper.setVisible(true); tick
|
|
}
|
|
|
|
/**
|
|
* Menu button pressed. Prompt alert then return to menu
|
|
*/
|
|
public void menuBtnPressed() throws IOException {
|
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
|
alert.setTitle("Quitting race");
|
|
alert.setContentText("Do you wish to quit the race?");
|
|
alert.setHeaderText("You are about to quit the race");
|
|
Optional<ButtonType> result = alert.showAndWait();
|
|
if(result.get() == ButtonType.OK){
|
|
// hostWrapper.setVisible(false);
|
|
Stage stage = App.getStage();
|
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("/visualiser/scenes/main.fxml"));
|
|
Parent root = loader.load();
|
|
stage.setResizable(false);
|
|
Scene scene = new Scene(root);
|
|
stage.setScene(scene);
|
|
stage.show();
|
|
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
|
|
@Override public void handle(WindowEvent event) {
|
|
Platform.exit();
|
|
System.exit(0);
|
|
}
|
|
});
|
|
// parent.enterTitle(); tick
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start button pressed. Currently only prints out start
|
|
*/
|
|
public void startBtnPressed(){
|
|
//System.out.println("Should start the race. This button is only visible for the host");
|
|
hostGamePressed();
|
|
}
|
|
|
|
}
|