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.

259 lines
8.0 KiB

package visualiser.Controllers;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Box;
import mock.app.Event;
import network.Messages.Enums.RaceStatusEnum;
import network.Messages.Enums.RequestToJoinEnum;
import visualiser.gameController.ControllerClient;
import visualiser.layout.Subject3D;
import visualiser.layout.View3D;
import visualiser.model.VisualiserBoat;
import visualiser.model.VisualiserRaceEvent;
import visualiser.model.VisualiserRaceState;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Controller for Hosting a game.
*/
public class InGameLobbyController extends Controller {
@FXML
private ImageView imageView;
@FXML
AnchorPane gameLobbyWrapper;
@FXML
GridPane playerContainer;
@FXML
private Label playerLabel;
@FXML
private Label playerLabel2;
@FXML
private Label playerLabel3;
@FXML
private Label playerLabel4;
@FXML
private Label playerLabel5;
@FXML
private Label playerLabel6;
@FXML
private Label countdownLabel;
private Event game;
private View3D playerBoat;
private VisualiserRaceEvent visualiserRaceEvent;
private boolean isHost;
private ControllerClient controllerClient;
private ArrayList<Label> allPlayerLabels;
private ObservableList<Subject3D> subjects = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
allPlayerLabels = new ArrayList(Arrays.asList(playerLabel, playerLabel2,
playerLabel3,
playerLabel4,
playerLabel5,
playerLabel6));
}
private void populatePlayers(ListChangeListener.Change change){
Platform.runLater(
() -> {
while (change.next()){
if (change.wasAdded()){
try{
int count = 0;
int row = 0;
for (VisualiserBoat boat :this.visualiserRaceEvent.getVisualiserRaceState().getBoats()) {
View3D playerBoatToSet = new View3D();
playerBoatToSet.setItems(subjects);
playerContainer.add(playerBoatToSet, (count % 3) , row);
playerContainer.setMargin(playerBoatToSet, new Insets(10, 10, 10, 10));
Subject3D subject = new Subject3D(new Box());
subjects.add(subject);
playerBoatToSet.setDistance(50);
playerBoatToSet.setYaw(45);
playerBoatToSet.setPitch(20);
AnimationTimer rotate = new AnimationTimer() {
@Override
public void handle(long now) {
subject.setHeading(subject.getHeading() + 0.1);
}
};
rotate.start();
allPlayerLabels.get(count).setText("Player: " + boat.getSourceID());
allPlayerLabels.get(count).toFront();
count += 1;
if (count > 2){
row = 1;
}
}
}
catch(ConcurrentModificationException e){
}
}
}
}
);
}
/**
* Starts the race.
*/
private void startRace() {
//Initialises the race clock.
initialiseRaceClock(this.visualiserRaceEvent.getVisualiserRaceState());
//Starts the race countdown timer.
countdownTimer();
}
public AnchorPane gameLobbyWrapper(){
return gameLobbyWrapper;
}
/**
* Initialises the race clock/timer labels for the start time, current time, and remaining time.
* @param visualiserRace The race to get data from.
*/
private void initialiseRaceClock(VisualiserRaceState visualiserRace) {
//Remaining time.
initialiseRaceClockDuration(visualiserRace);
}
/**
* Initialises the race duration label.
* @param visualiserRace The race to get data from.
*/
private void initialiseRaceClockDuration(VisualiserRaceState visualiserRace) {
visualiserRace.getRaceClock().durationProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
countdownLabel.setText(newValue);
});
});
}
/**
* Countdown timer until race starts.
*/
private void countdownTimer() {
new AnimationTimer() {
@Override
public void handle(long arg0) {
//Get the current race status.
RaceStatusEnum raceStatus = visualiserRaceEvent.getVisualiserRaceState().getRaceStatusEnum();
//If the race has reached the preparatory phase, or has started...
if (raceStatus == RaceStatusEnum.PREPARATORY || raceStatus == RaceStatusEnum.STARTED) {
//Stop this timer.
stop();
//Hide this, and display the race controller.
gameLobbyWrapper.setVisible(false);
parent.beginRace(visualiserRaceEvent, controllerClient, isHost);
}
}
}.start();
}
/**
* Hosts a game.
*/
public void enterGameLobby(Socket socket, boolean isHost){
try {
this.visualiserRaceEvent = new VisualiserRaceEvent(socket, RequestToJoinEnum.PARTICIPANT);
this.isHost = isHost;
this.controllerClient = visualiserRaceEvent.getControllerClient();
this.visualiserRaceEvent.getVisualiserRaceState().getBoats().addListener((ListChangeListener<? super VisualiserBoat>) c ->{
populatePlayers(c);
});
gameLobbyWrapper.setVisible(true);
startRace();
} catch (IOException e) {
//TODO should probably let this propagate, so that we only enter this scene if everything works
Logger.getGlobal().log(Level.WARNING, "Could not connect to server.", e);
}
}
/**
* Menu button pressed. Prompt alert then return to menu
*/
public void menuBtnPressed(){
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){
gameLobbyWrapper.setVisible(false);
parent.enterTitle();
}
}
/**
* 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");
}
public void joinSpecPressed(){
System.out.println("Spectator list pressed. Joining spectators");
}
public void joinRacePressed(){
System.out.println("Empty race user pane pressed. Joining racers");
}
}