Failed attempt at separating logic and GUI but saving just in case I need it later
This commit is contained in:
@@ -29,6 +29,7 @@ import java.util.concurrent.ExecutionException;
|
|||||||
public class ShipWarfareGUI extends Player {
|
public class ShipWarfareGUI extends Player {
|
||||||
|
|
||||||
private ShipWarfareGUI ship;
|
private ShipWarfareGUI ship;
|
||||||
|
private ShipWarfareGUILogic logic;
|
||||||
private Circle cannon;
|
private Circle cannon;
|
||||||
private VBox buttonBox;
|
private VBox buttonBox;
|
||||||
private HBox fightRunBox;
|
private HBox fightRunBox;
|
||||||
@@ -36,6 +37,7 @@ public class ShipWarfareGUI extends Player {
|
|||||||
private Button runButton;
|
private Button runButton;
|
||||||
private Button continueButton;
|
private Button continueButton;
|
||||||
private VBox labelBox;
|
private VBox labelBox;
|
||||||
|
|
||||||
private Label title;
|
private Label title;
|
||||||
private Label HPLeft;
|
private Label HPLeft;
|
||||||
private Label gunsLeftOrTaken;
|
private Label gunsLeftOrTaken;
|
||||||
@@ -43,21 +45,10 @@ public class ShipWarfareGUI extends Player {
|
|||||||
private Label shipsRemaining;
|
private Label shipsRemaining;
|
||||||
private Label report;
|
private Label report;
|
||||||
|
|
||||||
private boolean winOrLose = false;
|
|
||||||
|
|
||||||
|
|
||||||
private int counter1;
|
|
||||||
private int timeCounter;
|
private int timeCounter;
|
||||||
|
|
||||||
private int numOfLittyShips = 0;
|
|
||||||
private boolean userAttacks = true;
|
|
||||||
private int startingLittyShips = 0;
|
|
||||||
private int howMuchRun = 0;
|
|
||||||
private int counter = 0;
|
|
||||||
private String pirateName = "Liu Yen";
|
|
||||||
|
|
||||||
private int missCounter;
|
|
||||||
private int hitCounter;
|
|
||||||
|
|
||||||
private int beginningX = 150;
|
private int beginningX = 150;
|
||||||
private int beginningY = 245;
|
private int beginningY = 245;
|
||||||
@@ -66,8 +57,6 @@ public class ShipWarfareGUI extends Player {
|
|||||||
private int endY = 90;
|
private int endY = 90;
|
||||||
|
|
||||||
private TranslateTransition shotsFired = new TranslateTransition();
|
private TranslateTransition shotsFired = new TranslateTransition();
|
||||||
private TranslateTransition shotsMissed = new TranslateTransition();
|
|
||||||
|
|
||||||
private TranslateTransition enemyShots = new TranslateTransition();
|
private TranslateTransition enemyShots = new TranslateTransition();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,64 +69,9 @@ public class ShipWarfareGUI extends Player {
|
|||||||
setPlayer(playerDummy);
|
setPlayer(playerDummy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of ships that attack is based on the amount of money one has on hand
|
|
||||||
*
|
|
||||||
* @return the number of ships which will attack
|
|
||||||
*/
|
|
||||||
public int numOfShips() {
|
|
||||||
|
|
||||||
int numOfShipsAttacking = 0;
|
|
||||||
Random randomValue = new Random();
|
|
||||||
|
|
||||||
if (getMoney() <= 100000) {
|
|
||||||
//Minimum one ship will attack, maximum 20
|
|
||||||
numOfShipsAttacking = randomValue.nextInt(20) + 1;
|
|
||||||
} else if (getMoney() <= 200000) {
|
|
||||||
//Minimum 30 Ships will attack, maximum 70
|
|
||||||
numOfShipsAttacking = randomValue.nextInt(40) + 31;
|
|
||||||
} else if (getMoney() <= 500000) {
|
|
||||||
//Minimum 50 ships will attack, maximum 140
|
|
||||||
numOfShipsAttacking = randomValue.nextInt(90) + 51;
|
|
||||||
} else if (getMoney() >= 1000000) {
|
|
||||||
//Minimum 100 ships will attack, maximum 300 ships
|
|
||||||
numOfShipsAttacking = randomValue.nextInt(200) + 101;
|
|
||||||
}
|
|
||||||
|
|
||||||
return numOfShipsAttacking;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* setter method that takes in an integer as an argument
|
|
||||||
*
|
|
||||||
* @param numOfLittyShips the number of ships to be used in the peasant fleet attack
|
|
||||||
*/
|
|
||||||
public void setNumOfLittyShips(int numOfLittyShips) {
|
|
||||||
counter1++;
|
|
||||||
this.numOfLittyShips = numOfLittyShips;
|
|
||||||
if (counter1 == 1) {
|
|
||||||
startingLittyShips = numOfLittyShips;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* One in two chance of running away
|
|
||||||
*
|
|
||||||
* @return true if the user is allowed to run, false if not, the "default" is false
|
|
||||||
*/
|
|
||||||
public boolean runFromShips() {
|
|
||||||
userAttacks = false;
|
|
||||||
Random randomValue = new Random();
|
|
||||||
int runSuccessChance = randomValue.nextInt(10) + 1;
|
|
||||||
if (runSuccessChance == 2) {
|
|
||||||
return true;
|
|
||||||
} else if (runSuccessChance == 1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets most of the labels invisible except for the "fight or run" label
|
* Sets most of the labels invisible except for the "fight or run" label
|
||||||
@@ -165,180 +99,27 @@ public class ShipWarfareGUI extends Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The user faces off against the litty ships and either prevails, dies, or runs away
|
|
||||||
*
|
|
||||||
* @return true if the user wins, loses, or flees, it returns false otherwise
|
|
||||||
*/
|
|
||||||
public boolean destroyLittyShipsOrEscape(Stage stage) throws Exception {
|
|
||||||
cannon.setLayoutX(beginningX);
|
|
||||||
cannon.setLayoutY(beginningY);
|
|
||||||
int calculateLoot = 0;
|
|
||||||
int chanceOfEnemyRun = 0;
|
|
||||||
hitCounter = 0;
|
|
||||||
missCounter = 0;
|
|
||||||
boolean gunFrustration = false;
|
|
||||||
|
|
||||||
|
|
||||||
runAwayOrLeft.setText("No ships ran away");
|
|
||||||
Random randomValue = new Random();
|
|
||||||
int exitValue = 0;
|
|
||||||
//Player volley
|
|
||||||
//while (exitValue == 0) {
|
|
||||||
if (getGuns() > 0) {
|
|
||||||
|
|
||||||
for (int j = 0; j < getGuns(); j++) {
|
|
||||||
if (userAttacks == true) {
|
|
||||||
|
|
||||||
int hitOrMiss = randomValue.nextInt(2) + 1;
|
|
||||||
if (hitOrMiss == 2) {
|
|
||||||
numOfLittyShips--;
|
|
||||||
if (numOfLittyShips <= 0) {
|
|
||||||
exitValue = 1;
|
|
||||||
//break;
|
|
||||||
}
|
|
||||||
hitCounter++;
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
|
||||||
missCounter++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
|
||||||
//continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (userAttacks == true) {
|
|
||||||
report.setText(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
report.setText("We don't have any guns!!!");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (numOfLittyShips <= 0) {
|
|
||||||
exitValue = 1;
|
|
||||||
//break;
|
|
||||||
}
|
|
||||||
if (getGuns() > 0) {
|
|
||||||
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
|
|
||||||
if (chanceOfEnemyRun == 2) {
|
|
||||||
howMuchRun = randomValue.nextInt(15) + 1;
|
|
||||||
if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
|
|
||||||
|
|
||||||
|
|
||||||
setNumOfLittyShips(numOfLittyShips - howMuchRun);
|
|
||||||
if (userAttacks == true) {
|
|
||||||
if (howMuchRun > 0) {
|
|
||||||
runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, getName()));
|
|
||||||
|
|
||||||
//runAwayOrLeft.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
report.setText((String.format("Escaped %d of them %s!", howMuchRun, getName())));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
shipsRemaining.setText(String.format("%d ships remaining and they look angry!", numOfLittyShips));
|
|
||||||
//Computer volley
|
|
||||||
int takeGunChance = randomValue.nextInt(4) + 1;
|
|
||||||
if (takeGunChance == 1 && getGuns() > 0) {
|
|
||||||
setGuns(getGuns() - 1);
|
|
||||||
gunFrustration = true;
|
|
||||||
} else {
|
|
||||||
if (numOfLittyShips > 0) {
|
|
||||||
int HPTaken = randomValue.nextInt(10);
|
|
||||||
setHP(getHP() - (HPTaken));
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (getHP() <= 0) {
|
|
||||||
exitValue = 2;
|
|
||||||
//break;
|
|
||||||
}
|
|
||||||
if (gunFrustration == true) {
|
|
||||||
gunsLeftOrTaken.setText(String.format("Dang it! We only have %d guns left", getGuns()));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
gunsLeftOrTaken.setText(String.format("We still have %d guns left", getGuns()));
|
|
||||||
}
|
|
||||||
|
|
||||||
HPLeft.setText(String.format("EEK, our current ship status is %d%% ", getHP()));
|
|
||||||
if (userAttacks == false) {
|
|
||||||
userAttacks = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (exitValue == 1) {
|
|
||||||
wipe();
|
|
||||||
calculateLoot = (startingLittyShips * 100) + randomValue.nextInt(startingLittyShips) * 200;
|
|
||||||
setMoney(getMoney() + calculateLoot);
|
|
||||||
report.setText(String.format("Our firm has earned $%,d in loot! ", calculateLoot));
|
|
||||||
continueButton.setVisible(true);
|
|
||||||
completeWipe();
|
|
||||||
fightButton.setVisible(false);
|
|
||||||
runButton.setVisible(false);
|
|
||||||
continueButton.setDefaultButton(true);
|
|
||||||
return true;
|
|
||||||
} else if (exitValue == 2) {
|
|
||||||
GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
|
|
||||||
gameEndGUI.initializeGameEndGUI(stage);
|
|
||||||
stage.show();
|
|
||||||
return true;
|
|
||||||
} else if (exitValue == 3) {
|
|
||||||
report.setText(String.format("We made it out at %d%% ship status!", getHP()));
|
|
||||||
|
|
||||||
continueButton.setVisible(true);
|
|
||||||
completeWipe();
|
|
||||||
fightButton.setVisible(false);
|
|
||||||
runButton.setVisible(false);
|
|
||||||
continueButton.setDefaultButton(true);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Player attacks enemy ships in an animation
|
* Player attacks enemy ships in an animation
|
||||||
*/
|
*/
|
||||||
public void playerShoots(int amountOfShots) {
|
public void playerShoots(int amountOfShots) {
|
||||||
userAttacks = true;
|
|
||||||
shotsFired.setFromX(0);
|
shotsFired.setFromX(0);
|
||||||
shotsFired.setFromY(0);
|
shotsFired.setFromY(0);
|
||||||
shotsFired.setToX(endX);
|
shotsFired.setToX(endX);
|
||||||
shotsFired.setToY(endY);
|
shotsFired.setToY(endY);
|
||||||
shots(amountOfShots, shotsFired);
|
shotsFired.setDuration(Duration.seconds(0.5));
|
||||||
}
|
|
||||||
|
|
||||||
public void playerMisses(int amountOfShots) {
|
|
||||||
userAttacks = true;
|
|
||||||
shotsMissed.setFromX(0);
|
|
||||||
shotsMissed.setFromY(0);
|
|
||||||
shotsMissed.setToX(endX + 90);
|
|
||||||
shotsMissed.setToY(endY + 90);
|
|
||||||
shots(amountOfShots, shotsMissed);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void shots(int amountOfShots, TranslateTransition shots) {
|
|
||||||
shots.setDuration(Duration.seconds(0.5));
|
|
||||||
if (getGuns() > 0) {
|
if (getGuns() > 0) {
|
||||||
shots.setCycleCount(amountOfShots);
|
shotsFired.setCycleCount(amountOfShots);
|
||||||
} else {
|
} else {
|
||||||
shots.setCycleCount(0);
|
shotsFired.setCycleCount(0);
|
||||||
shots.stop();
|
shotsFired.stop();
|
||||||
cannon.setVisible(false);
|
cannon.setVisible(false);
|
||||||
}
|
}
|
||||||
shots.setNode(cannon);
|
shotsFired.setNode(cannon);
|
||||||
shots.play();
|
shotsFired.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -379,6 +160,37 @@ public class ShipWarfareGUI extends Player {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean winOrLose( Stage stage){
|
||||||
|
if (logic.destroyLittyShipsOrEscape() == 1) {
|
||||||
|
wipe();
|
||||||
|
report.setText(logic.getReportMessage());
|
||||||
|
continueButton.setVisible(true);
|
||||||
|
completeWipe();
|
||||||
|
fightButton.setVisible(false);
|
||||||
|
runButton.setVisible(false);
|
||||||
|
continueButton.setDefaultButton(true);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
|
||||||
|
} else if (logic.destroyLittyShipsOrEscape() == 2) {
|
||||||
|
GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
|
||||||
|
gameEndGUI.initializeGameEndGUI(stage);
|
||||||
|
stage.show();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} else if (logic.destroyLittyShipsOrEscape() == 3) {
|
||||||
|
report.setText(String.format("We made it out at %d%% ship status!", getHP()));
|
||||||
|
|
||||||
|
continueButton.setVisible(true);
|
||||||
|
completeWipe();
|
||||||
|
fightButton.setVisible(false);
|
||||||
|
runButton.setVisible(false);
|
||||||
|
continueButton.setDefaultButton(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generaties ships and deploys logic for the shipwarfare
|
* Generaties ships and deploys logic for the shipwarfare
|
||||||
@@ -387,7 +199,9 @@ public class ShipWarfareGUI extends Player {
|
|||||||
* @throws Exception in case of interruptions withing the graphical interface
|
* @throws Exception in case of interruptions withing the graphical interface
|
||||||
*/
|
*/
|
||||||
public void initializeShip(Stage primaryStage) throws Exception {
|
public void initializeShip(Stage primaryStage) throws Exception {
|
||||||
setNumOfLittyShips(numOfShips());
|
cannon.setLayoutX(beginningX);
|
||||||
|
cannon.setLayoutY(beginningY);
|
||||||
|
logic.setNumOfLittyShips(logic.numOfShips());
|
||||||
|
|
||||||
Pane root = new Pane();
|
Pane root = new Pane();
|
||||||
HBox usAgainstEnemyDivisor;
|
HBox usAgainstEnemyDivisor;
|
||||||
@@ -571,7 +385,7 @@ public class ShipWarfareGUI extends Player {
|
|||||||
report.setText("Epic");
|
report.setText("Epic");
|
||||||
counter++;
|
counter++;
|
||||||
|
|
||||||
if (runFromShips() == false) {
|
if (logic.runFromShips() == false) {
|
||||||
report.setText(("Couldn't run away"));
|
report.setText(("Couldn't run away"));
|
||||||
try {
|
try {
|
||||||
winOrLose = destroyLittyShipsOrEscape(primaryStage);
|
winOrLose = destroyLittyShipsOrEscape(primaryStage);
|
||||||
@@ -630,36 +444,9 @@ public class ShipWarfareGUI extends Player {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(hitCounter!=0) {
|
playerShoots(getGuns());
|
||||||
if(winOrLose==true){
|
|
||||||
playerShoots(numOfLittyShips);
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
playerShoots(hitCounter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
playerMisses(hitCounter);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
shotsFired.setOnFinished(new EventHandler<ActionEvent>() {
|
shotsFired.setOnFinished(new EventHandler<ActionEvent>() {
|
||||||
@Override
|
|
||||||
public void handle(ActionEvent event) {
|
|
||||||
try {
|
|
||||||
|
|
||||||
playerMisses(missCounter);
|
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
shotsMissed.setOnFinished(new EventHandler<ActionEvent>() {
|
|
||||||
@Override
|
@Override
|
||||||
/**
|
/**
|
||||||
* When the user is completed their volley this information will be accessed
|
* When the user is completed their volley this information will be accessed
|
||||||
@@ -711,8 +498,3 @@ public class ShipWarfareGUI extends Player {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user