Issue 19 changes.
This commit is contained in:
@@ -101,6 +101,13 @@ public class ConnectionAcceptor implements Runnable {
|
||||
return serverPort;
|
||||
}
|
||||
|
||||
public void closeConnection() throws IOException {
|
||||
this.raceLogic.boolFalse();
|
||||
if(!this.serverSocket.isClosed()){
|
||||
this.serverSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -113,6 +120,9 @@ public class ConnectionAcceptor implements Runnable {
|
||||
|
||||
try {
|
||||
|
||||
if(Thread.currentThread().isInterrupted()){
|
||||
break;
|
||||
}
|
||||
|
||||
Socket mockSocket = serverSocket.accept();
|
||||
|
||||
|
||||
@@ -60,6 +60,10 @@ public class Event {
|
||||
*/
|
||||
private SourceIdAllocator sourceIdAllocator;
|
||||
|
||||
private Thread raceThread;
|
||||
|
||||
private Thread connectionThread;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -130,7 +134,8 @@ public class Event {
|
||||
this.latestMessages,
|
||||
this.compositeCommand);
|
||||
|
||||
new Thread(newRace, "Event.Start()->RaceLogic thread").start();
|
||||
this.raceThread = new Thread(newRace, "Event.Start()->RaceLogic thread");
|
||||
raceThread.start();
|
||||
|
||||
|
||||
//Create connection acceptor.
|
||||
@@ -143,13 +148,20 @@ public class Event {
|
||||
}
|
||||
|
||||
|
||||
new Thread(connectionAcceptor, "Event.Start()->ConnectionAcceptor thread").start();
|
||||
this.connectionThread = new Thread(connectionAcceptor, "Event.Start()->ConnectionAcceptor thread");
|
||||
connectionThread.start();
|
||||
|
||||
sendXMLs();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void endEvent() throws IOException {
|
||||
this.connectionThread.interrupt();
|
||||
this.connectionAcceptor.closeConnection();
|
||||
this.raceThread.interrupt();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,8 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
||||
|
||||
private CompositeCommand commands;
|
||||
|
||||
private boolean loopBool = true;
|
||||
|
||||
/**
|
||||
* Initialises race loop with state and server message queue
|
||||
* @param race state of race to modify
|
||||
@@ -46,6 +48,10 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
||||
raceLoop();
|
||||
}
|
||||
|
||||
public void boolFalse(){
|
||||
loopBool = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Countdown timer until race starts.
|
||||
@@ -54,7 +60,7 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
||||
|
||||
long previousFrameTime = System.currentTimeMillis();
|
||||
|
||||
while (race.getRaceStatusEnum() != RaceStatusEnum.STARTED) {
|
||||
while (race.getRaceStatusEnum() != RaceStatusEnum.STARTED && loopBool) {
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
@@ -93,7 +99,7 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
||||
|
||||
long previousFrameTime = System.currentTimeMillis();
|
||||
|
||||
while (race.getRaceStatusEnum() != RaceStatusEnum.FINISHED) {
|
||||
while (race.getRaceStatusEnum() != RaceStatusEnum.FINISHED && loopBool) {
|
||||
|
||||
//Get the current time.
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
@@ -36,6 +36,8 @@ public class HostController extends Controller {
|
||||
@FXML
|
||||
AnchorPane hostWrapper;
|
||||
|
||||
private Event game;
|
||||
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
@@ -47,7 +49,7 @@ public class HostController extends Controller {
|
||||
*/
|
||||
public void hostGamePressed() throws IOException{
|
||||
try {
|
||||
Event game = new Event(false);
|
||||
this.game = new Event(false);
|
||||
game.start();
|
||||
connectSocket("localhost", 4942);
|
||||
} catch (EventConstructionException e) {
|
||||
@@ -56,6 +58,10 @@ public class HostController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
public void endEvent() throws IOException {
|
||||
game.endEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a socket
|
||||
* @param address address of the server
|
||||
|
||||
@@ -3,10 +3,12 @@ package visualiser.Controllers;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import visualiser.app.App;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.model.VisualiserBoat;
|
||||
import visualiser.model.VisualiserRaceEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -43,6 +45,8 @@ public class MainController extends Controller {
|
||||
raceController.startRace(visualiserRace, controllerClient);
|
||||
}
|
||||
|
||||
public void endEvent() throws IOException { hostController.endEvent(); }
|
||||
|
||||
/**
|
||||
* Transitions from the server selection screen to the pre-race lobby for a given server.
|
||||
* @param socket The server to read data from.
|
||||
@@ -62,7 +66,13 @@ public class MainController extends Controller {
|
||||
/**
|
||||
* Transitions into the title screen
|
||||
*/
|
||||
public void enterTitle(){ titleController.enterTitle(); }
|
||||
public void enterTitle() {
|
||||
try {
|
||||
App.loadStart(App.getStage());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions into lobby screen
|
||||
|
||||
@@ -11,6 +11,7 @@ import javafx.fxml.FXML;
|
||||
import javafx.scene.chart.LineChart;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
@@ -25,6 +26,7 @@ import visualiser.gameController.Keys.KeyFactory;
|
||||
import visualiser.model.*;
|
||||
import visualiser.network.ServerConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.logging.Level;
|
||||
@@ -121,6 +123,15 @@ public class RaceController extends Controller {
|
||||
Logger.getGlobal().log(Level.WARNING, "RaceController was interrupted on thread: " + Thread.currentThread() + "while sending: " + controlKey, e);
|
||||
}
|
||||
}
|
||||
if(event.getCode() == KeyCode.ESCAPE){
|
||||
race.setVisible(false);
|
||||
try {
|
||||
parent.endEvent();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
parent.enterTitle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
import visualiser.Controllers.MainController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class App extends Application {
|
||||
private static Stage stage;
|
||||
|
||||
@@ -36,16 +38,22 @@ public class App extends Application {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("/visualiser/scenes/main.fxml"));
|
||||
App.stage = stage;
|
||||
loadStart(App.stage);
|
||||
}
|
||||
|
||||
public static void loadStart(Stage stage) throws IOException {
|
||||
FXMLLoader loader = new FXMLLoader(App.class.getResource("/visualiser/scenes/main.fxml"));
|
||||
Parent root = loader.load();
|
||||
stage.setResizable(false);
|
||||
MainController mc = (MainController) loader.getController();
|
||||
mc.enterTitle();
|
||||
Scene scene = new Scene(root);
|
||||
|
||||
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("RaceVision - Team 7");
|
||||
stage.getIcons().add(new Image(getClass().getClassLoader().getResourceAsStream("images/SailIcon.png")));
|
||||
stage.getIcons().add(new Image(App.class.getClassLoader().getResourceAsStream("images/SailIcon.png")));
|
||||
mc.startCss();
|
||||
setStage(stage);
|
||||
stage.show();
|
||||
|
||||
Reference in New Issue
Block a user