Implemented turning mode toggle

- when the mode is toggled, a boat action package will be sent to notify server
to change the boat's turning mode
- turning mode toggle is now fully functional

#story[1245]
This commit is contained in:
Haoming Yin
2017-09-23 19:37:13 +12:00
parent 4011295b8b
commit 066557584f
8 changed files with 152 additions and 62 deletions
@@ -1,12 +1,35 @@
package seng302.gameServer; package seng302.gameServer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import seng302.gameServer.messages.*; import seng302.gameServer.messages.BoatAction;
import seng302.model.*; import seng302.gameServer.messages.BoatStatus;
import seng302.gameServer.messages.ChatterMessage;
import seng302.gameServer.messages.CustomizeRequestType;
import seng302.gameServer.messages.MarkRoundingMessage;
import seng302.gameServer.messages.MarkType;
import seng302.gameServer.messages.Message;
import seng302.gameServer.messages.RoundingBoatStatus;
import seng302.gameServer.messages.YachtEventCodeMessage;
import seng302.gameServer.messages.YachtEventType;
import seng302.model.GeoPoint;
import seng302.model.Limit;
import seng302.model.Player;
import seng302.model.PolarTable;
import seng302.model.ServerYacht;
import seng302.model.mark.CompoundMark; import seng302.model.mark.CompoundMark;
import seng302.model.mark.Mark; import seng302.model.mark.Mark;
import seng302.model.mark.MarkOrder; import seng302.model.mark.MarkOrder;
@@ -14,10 +37,6 @@ import seng302.model.token.Token;
import seng302.model.token.TokenType; import seng302.model.token.TokenType;
import seng302.utilities.GeoUtility; import seng302.utilities.GeoUtility;
import seng302.utilities.XMLParser; import seng302.utilities.XMLParser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.*;
import seng302.visualiser.fxObjects.assets_3D.BoatMeshType; import seng302.visualiser.fxObjects.assets_3D.BoatMeshType;
/** /**
@@ -272,6 +291,12 @@ public class GameState implements Runnable {
case DOWNWIND: case DOWNWIND:
playerYacht.turnDownwind(); playerYacht.turnDownwind();
break; break;
case CONTINUOUSLY_TURNING:
playerYacht.setContinuouslyTurning(true);
break;
case DEFAULT_TURNING:
playerYacht.setContinuouslyTurning(false);
break;
} }
} }
@@ -14,7 +14,9 @@ public enum BoatAction {
TACK_GYBE(4), TACK_GYBE(4),
UPWIND(5), UPWIND(5),
DOWNWIND(6), DOWNWIND(6),
MAINTAIN_HEADING(7); MAINTAIN_HEADING(7),
CONTINUOUSLY_TURNING(8),
DEFAULT_TURNING(9);
private final int type; private final int type;
private static final Map<Integer, BoatAction> intToTypeMap = new HashMap<>(); private static final Map<Integer, BoatAction> intToTypeMap = new HashMap<>();
+2 -2
View File
@@ -10,7 +10,7 @@ public class GameKeyBind {
private static GameKeyBind instance; private static GameKeyBind instance;
private Map<KeyCode, KeyAction> keyToActionMap; private Map<KeyCode, KeyAction> keyToActionMap;
private Map<KeyAction, KeyCode> actionToKeyMap; private Map<KeyAction, KeyCode> actionToKeyMap;
private boolean continuouslyTurning; private Boolean continuouslyTurning;
private GameKeyBind() { private GameKeyBind() {
@@ -71,7 +71,7 @@ public class GameKeyBind {
continuouslyTurning = !continuouslyTurning; continuouslyTurning = !continuouslyTurning;
} }
public boolean isContinuouslyTurning() { public Boolean isContinuouslyTurning() {
return continuouslyTurning; return continuouslyTurning;
} }
+47 -34
View File
@@ -1,5 +1,6 @@
package seng302.model; package seng302.model;
import java.util.HashMap;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -8,10 +9,6 @@ import seng302.gameServer.messages.BoatStatus;
import seng302.model.mark.Mark; import seng302.model.mark.Mark;
import seng302.model.token.TokenType; import seng302.model.token.TokenType;
import seng302.utilities.GeoUtility; import seng302.utilities.GeoUtility;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import seng302.visualiser.fxObjects.assets_3D.BoatMeshType; import seng302.visualiser.fxObjects.assets_3D.BoatMeshType;
/** /**
@@ -57,6 +54,8 @@ public class ServerYacht {
private TokenType powerUp; private TokenType powerUp;
private Long powerUpStartTime; private Long powerUpStartTime;
//turning mode
private Boolean continuouslyTurning;
public ServerYacht(BoatMeshType boatType, Integer sourceId, String hullID, String shortName, public ServerYacht(BoatMeshType boatType, Integer sourceId, String hullID, String shortName,
String boatName, String country) { String boatName, String country) {
@@ -81,6 +80,8 @@ public class ServerYacht {
this.hasEnteredRoundingZone = false; this.hasEnteredRoundingZone = false;
this.hasPassedLine = false; this.hasPassedLine = false;
this.hasPassedThroughGate = false; this.hasPassedThroughGate = false;
this.continuouslyTurning = false;
} }
@@ -188,44 +189,52 @@ public class ServerYacht {
public void turnUpwind() { public void turnUpwind() {
disableAutoPilot(); disableAutoPilot();
Double normalizedHeading = normalizeHeading(); Double normalizedHeading = normalizeHeading();
if (normalizedHeading == 0) { if (continuouslyTurning) {
if (lastHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
} else if (normalizedHeading == 180) {
if (lastHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
} else if (normalizedHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP); adjustHeading(TURN_STEP);
} else {
if (normalizedHeading == 0) {
if (lastHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
} else if (normalizedHeading == 180) {
if (lastHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
} else if (normalizedHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
} }
} }
public void turnDownwind() { public void turnDownwind() {
disableAutoPilot(); disableAutoPilot();
Double normalizedHeading = normalizeHeading(); Double normalizedHeading = normalizeHeading();
if (normalizedHeading == 0) { if (continuouslyTurning) {
if (lastHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
} else if (normalizedHeading == 180) {
if (lastHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
} else if (normalizedHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP); adjustHeading(-TURN_STEP);
} else {
if (normalizedHeading == 0) {
if (lastHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
} else if (normalizedHeading == 180) {
if (lastHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
} else if (normalizedHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
} }
} }
@@ -429,4 +438,8 @@ public class ServerYacht {
public BoatMeshType getBoatType() { public BoatMeshType getBoatType() {
return boatType; return boatType;
} }
public void setContinuouslyTurning(Boolean continuouslyTurning) {
this.continuouslyTurning = continuouslyTurning;
}
} }
@@ -460,4 +460,11 @@ public class GameClient {
return allBoatsMap; return allBoatsMap;
} }
public void sendToggleTurningModePacket() {
if (gameKeyBind.isContinuouslyTurning()) {
socketThread.sendBoatAction(BoatAction.CONTINUOUSLY_TURNING);
} else {
socketThread.sendBoatAction(BoatAction.DEFAULT_TURNING);
}
}
} }
@@ -27,6 +27,7 @@ import seng302.gameServer.ServerAdvertiser;
import seng302.utilities.BonjourInstallChecker; import seng302.utilities.BonjourInstallChecker;
import seng302.utilities.Sounds; import seng302.utilities.Sounds;
import seng302.visualiser.GameClient; import seng302.visualiser.GameClient;
import seng302.visualiser.controllers.dialogs.KeyBindingDialogController;
public class ViewManager { public class ViewManager {
@@ -213,6 +214,9 @@ public class ViewManager {
JFXDialog dialog = new JFXDialog((StackPane) node, JFXDialog dialog = new JFXDialog((StackPane) node,
dialogContent.load(), dialogContent.load(),
DialogTransition.CENTER); DialogTransition.CENTER);
KeyBindingDialogController keyBindingDialogController = dialogContent
.getController();
keyBindingDialogController.setGameClient(this.gameClient);
dialog.show(); dialog.show();
Sounds.playButtonClick(); Sounds.playButtonClick();
} }
@@ -338,15 +342,6 @@ public class ViewManager {
scene.setOnKeyPressed(gameClient::keyPressed); scene.setOnKeyPressed(gameClient::keyPressed);
scene.setOnKeyReleased(gameClient::keyReleased); scene.setOnKeyReleased(gameClient::keyReleased);
// uncomment to make it full screen
// Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
// stage.setX(visualBounds.getMinX());
// stage.setY(visualBounds.getMinY());
// stage.setWidth(visualBounds.getWidth());
// stage.setHeight(visualBounds.getHeight());
// stage.setMaximized(true);
// stage.setFullScreen(true);
stage.setMinHeight(500); stage.setMinHeight(500);
stage.setMinWidth(800); stage.setMinWidth(800);
stage.setTitle("Party Parrots At Sea"); stage.setTitle("Party Parrots At Sea");
@@ -1,7 +1,6 @@
package seng302.visualiser.controllers.dialogs; package seng302.visualiser.controllers.dialogs;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialogLayout;
import com.jfoenix.controls.JFXToggleButton; import com.jfoenix.controls.JFXToggleButton;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@@ -17,16 +16,13 @@ import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import seng302.model.GameKeyBind; import seng302.model.GameKeyBind;
import seng302.model.KeyAction; import seng302.model.KeyAction;
import seng302.visualiser.GameClient;
import seng302.visualiser.controllers.ViewManager; import seng302.visualiser.controllers.ViewManager;
public class KeyBindingDialogController implements Initializable { public class KeyBindingDialogController implements Initializable {
//--------FXML BEGIN--------// //--------FXML BEGIN--------//
@FXML @FXML
private JFXDialogLayout keyBindDialog;
@FXML
private Label keyBindingDialogHeader;
@FXML
private JFXButton zoomInbtn; private JFXButton zoomInbtn;
@FXML @FXML
private JFXButton zoomOutBtn; private JFXButton zoomOutBtn;
@@ -43,7 +39,9 @@ public class KeyBindingDialogController implements Initializable {
@FXML @FXML
private JFXButton resetBtn; private JFXButton resetBtn;
@FXML @FXML
private JFXButton confirmBtn; private Label upwindLabel;
@FXML
private Label downwindLabel;
@FXML @FXML
private JFXToggleButton turningToggle; private JFXToggleButton turningToggle;
//---------FXML END---------// //---------FXML END---------//
@@ -51,6 +49,7 @@ public class KeyBindingDialogController implements Initializable {
private GameKeyBind gameKeyBind; private GameKeyBind gameKeyBind;
private List<JFXButton> buttons = new ArrayList<>(); private List<JFXButton> buttons = new ArrayList<>();
private Map<Button, KeyAction> buttonActionMap; private Map<Button, KeyAction> buttonActionMap;
private GameClient gameClient; // to send turning mode packet
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
@@ -68,6 +67,8 @@ public class KeyBindingDialogController implements Initializable {
button.setOnKeyPressed(event -> keyPressed(event, button)); button.setOnKeyPressed(event -> keyPressed(event, button));
}); });
turningToggle.setOnMouseClicked(event -> toggleTurningMode());
resetBtn.setOnMouseClicked(event -> { resetBtn.setOnMouseClicked(event -> {
gameKeyBind.setToDefault(); gameKeyBind.setToDefault();
loadKeyBind(); loadKeyBind();
@@ -81,8 +82,19 @@ public class KeyBindingDialogController implements Initializable {
buttons.forEach( buttons.forEach(
button -> button button -> button
.setText(gameKeyBind.getKeyCode(buttonActionMap.get(button)).getName())); .setText(gameKeyBind.getKeyCode(buttonActionMap.get(button)).getName()));
turningToggle.setSelected(gameKeyBind.isContinuouslyTurning());
if (gameKeyBind.isContinuouslyTurning()) {
upwindLabel.setText("ClOCKWISE TURNING");
downwindLabel.setText("ANTICLOCKWISE TURNING");
} else {
upwindLabel.setText("UPWIND");
downwindLabel.setText("DOWNWIND");
}
} }
/**
* Bind buttons with specific action in a map.
*/
private void bindButtonWithAction() { private void bindButtonWithAction() {
buttonActionMap = new HashMap<>(); buttonActionMap = new HashMap<>();
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
@@ -90,10 +102,17 @@ public class KeyBindingDialogController implements Initializable {
} }
} }
/**
* Prompt success / failure message for reassigning key action
*/
private void showSnackBar(String message) { private void showSnackBar(String message) {
ViewManager.getInstance().showSnackbar(message); ViewManager.getInstance().showSnackbar(message);
} }
/**
* When a mouse enters the button, the color and font size should change to highlight
* @param button
*/
private void mouseEnter(Button button) { private void mouseEnter(Button button) {
button.setStyle("" button.setStyle(""
+ "-fx-background-color: -fx-pp-theme-color;" + "-fx-background-color: -fx-pp-theme-color;"
@@ -101,10 +120,20 @@ public class KeyBindingDialogController implements Initializable {
+ "-fx-font-size: 15;"); + "-fx-font-size: 15;");
} }
/**
* Prompt "press key..." to inform users assign a new key bind by pressing a key
* @param button
*/
private void buttonPressed(Button button) { private void buttonPressed(Button button) {
button.setText("PRESS KEY..."); button.setText("PRESS KEY...");
} }
/**
* When mouse leaves the button, return the button to the normal state in terms of text,
* color and font size
* @param button
*/
private void mouseExit(Button button) { private void mouseExit(Button button) {
button.setText(GameKeyBind.getInstance().getKeyCode(buttonActionMap.get(button)).getName()); button.setText(GameKeyBind.getInstance().getKeyCode(buttonActionMap.get(button)).getName());
button.setStyle("" button.setStyle(""
@@ -113,6 +142,12 @@ public class KeyBindingDialogController implements Initializable {
+ "-fx-font-size: 13;"); + "-fx-font-size: 13;");
} }
/**
* When a key is pressed, check if the new binding conflicts to any existed settings, if not
* assign the selected action with the new key binding to GameKeyBind.
* @param event
* @param button
*/
private void keyPressed(KeyEvent event, Button button) { private void keyPressed(KeyEvent event, Button button) {
KeyAction buttonAction = buttonActionMap.get(button); KeyAction buttonAction = buttonActionMap.get(button);
if (gameKeyBind.bindKeyToAction(event.getCode(), buttonAction)) { if (gameKeyBind.bindKeyToAction(event.getCode(), buttonAction)) {
@@ -123,4 +158,17 @@ public class KeyBindingDialogController implements Initializable {
} }
event.consume(); event.consume();
} }
/**
* When the turning mode is toggled, update gameKeyBind and send out packet to notify the server
*/
private void toggleTurningMode() {
gameKeyBind.toggleTurningMode();
gameClient.sendToggleTurningModePacket();
loadKeyBind();
}
public void setGameClient(GameClient gameClient) {
this.gameClient = gameClient;
}
} }
@@ -59,13 +59,13 @@
<GridPane.margin> <GridPane.margin>
<Insets/> <Insets/>
</GridPane.margin></Label> </GridPane.margin></Label>
<Label text="UPWIND" GridPane.halignment="CENTER" GridPane.rowIndex="6" <Label fx:id="upwindLabel" text="UPWIND" GridPane.halignment="CENTER"
GridPane.valignment="CENTER"> GridPane.rowIndex="6" GridPane.valignment="CENTER">
<GridPane.margin> <GridPane.margin>
<Insets/> <Insets/>
</GridPane.margin></Label> </GridPane.margin></Label>
<Label text="DOWNWIND" GridPane.halignment="CENTER" GridPane.rowIndex="7" <Label fx:id="downwindLabel" text="DOWNWIND" GridPane.halignment="CENTER"
GridPane.valignment="CENTER"> GridPane.rowIndex="7" GridPane.valignment="CENTER">
<GridPane.margin> <GridPane.margin>
<Insets/> <Insets/>
</GridPane.margin></Label> </GridPane.margin></Label>