package visualiser.Controllers; import com.interactivemesh.jfx.importer.stl.StlMeshImporter; import javafx.animation.AnimationTimer; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; 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 mock.app.Event; import mock.exceptions.EventConstructionException; 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 LobbyHostingController extends Controller { private @FXML ImageView imageView; private @FXML AnchorPane imagePane; private @FXML SplitPane splitPane; private @FXML AnchorPane specPane; private @FXML GridPane playerContainer; private View3D view3D; public void initialize() { hostGame(); ObservableList subjects = FXCollections.observableArrayList(); view3D = new View3D(); view3D.setItems(subjects); playerContainer.add(view3D, 0,0); URL asset = LobbyHostingController.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 { App.game = new Event(false); connectSocket("localhost", 4942); } catch (EventConstructionException e) { Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); } } /** * 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) throws IOException { Socket socket = new Socket(address, port); RaceStartController rsc = (RaceStartController)loadScene("raceStart.fxml"); rsc.enterLobby(socket, true); } /** * 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()); } /** * Menu button pressed. Prompt alert then return to menu */ public void menuBtnPressed() throws Exception { 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 result = alert.showAndWait(); if(result.get() == ButtonType.OK){ loadTitleScreen(); } } /** * 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(); } }