如何使我的JavaFX GUI等待按钮输入而不冻结?

时间:2017-02-10 02:31:56

标签: java javafx event-handling

我正在制作一个使用javafx作为其GUI的回合制格斗游戏,但是当我运行战斗菜单时它会一直冻结。当我进行战斗时,我想要查看正在播放的所有角色的列表,并且当玩家或计算机仍然具有生命角色时,循环战斗。战斗是检查玩家是否是所有角色列表中该角色的角色(并且还检查以确保如果角色是重复的,则玩家不会连续多次移动如果是,则GUI应等待用户输入,如果不是,则计算机移动。 但是,无论我尝试做什么,一旦菜单加载,GUI就会冻结。我使用单独的类来切换场景,所有菜单都在自己的类中。

这是驱动程序,主要类可以切换所有场景(请记住,这并没有完全完成,因此有些情况只是空的)

package runner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import menus.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import Misc.Character;
import Misc.Menu;
import Misc.Move;

public class Driver extends Application implements EventHandler<ActionEvent>{
    public static Stage stg;
    public Stage fightStg;
    private Menu currMenu;
    public MainMenu home;
    public SinglePlayerMenu sp;
    public MultiplayerMenu1 mpm1;
    public MultiplayerMenu2 mpm2;
    public AchievementsMenu am;
    public EditRosterMenu er;
    public ChooseOpponentMenu cp;
    public FightMenuPVC fmpvc;
    public FightMenuPVP fmpvp;

    public static void main(String[] args){
        launch(args);
    }

    @Override
    public void start(Stage stage){
        stg = stage;
        stg.setTitle("Faction Fighters");
        stg.setResizable(false);
        stg.setWidth(1000);
        stg.setHeight(750);
        home = new MainMenu(this);
        stg.setScene(home.getScene());
        currMenu = home;
        stg.show();
    }

    //Used to switch between scenes
    @Override
    public void handle(ActionEvent event){
        Button button = (Button)event.getSource();
        switch(button.getText()){
        case "Single Player":
            if(sp == null) {
                sp = new SinglePlayerMenu(this);
            }
            stg.setScene(sp.getScene());
            currMenu = sp;
            break;
        case "Multiplayer":
            if(mpm1 == null) {
                mpm1 = new MultiplayerMenu1(this);
            }
            stg.setScene(mpm1.getScene());
            currMenu = mpm1;
            break;
        case "Finish":
            if(mpm2 == null) {
                mpm2 = new MultiplayerMenu2(this);
            }
            stg.setScene(mpm2.getScene());
            currMenu = mpm2;
            break;
        case "Achievements":
            if(am == null) {
                am = new AchievementsMenu(this);
            }
            stg.setScene(am.getScene());
            currMenu = am;
            break;
        case "Edit Roster":
            if(er == null){
                er = new EditRosterMenu(this);
            }
            stg.setScene(er.getScene());
            currMenu = er;
            break;
        case "Choose Opponent":
            if(cp == null){
                cp = new ChooseOpponentMenu(this);
            }
            stg.setScene(cp.getScene());
            currMenu = cp;
            break;
        case "Fight!": //PvC fight menu 
            if(fmpvc == null){
                fmpvc = new FightMenuPVC(this);
            }
            break;
        case "FIGHT": //PvP fight menu
            if(fmpvp == null){
                fmpvp = new FightMenuPVP(this);
            }
            stg.setScene(fmpvp.getScene());
            currMenu = fmpvp;
            break;
        case "Continue":
            currMenu = currMenu.getParent();
            stg.setScene(currMenu.getScene());
            stg.show();
            break;
        case "Claim Rewards":
            break;
        case "Back to Main Menu":
            stg.setScene(home.getScene());
            currMenu = home;
            break;
        case "Back":
            currMenu = currMenu.getParent();
            stg.setScene(currMenu.getScene());
            break;
        case "Quit":
            stg.close();
            break;
        }
    }

    public Menu getCurrMenu() {
        return currMenu;
    }
}

这是我遇到问题的菜单代码:

package menus;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;

import Misc.Character;
import Misc.Menu;
import Misc.Move;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import runner.Driver;

public class FightMenuPVC implements Menu {
    public Scene sc;
    public Menu par;
    public static EventHandler<ActionEvent> handler;
    public GridPane men;
    public VBox everything;
    public ScrollPane battleLog;
    public TextArea bLog;
    public static Text pTeamLab;
    public static Text cpuLab;
    public static HBox plyrBar;
    public static HBox cpuBar;
    public static Text moveLab;
    public static TextField moveInput;
    public static Button mInput;
    public static GridPane UI;
    public static ImageView f1;
    public static ImageView f2;
    public static ImageView f3;
    public static ImageView f4;
    public static ArrayList<ImageView> f = new ArrayList<>();
    public static HBox fTeam;
    public static ImageView e1;
    public static ImageView e2;
    public static ImageView e3;
    public static ImageView e4;
    public static ArrayList<ImageView> e = new ArrayList<>();
    public static HBox eTeam;
    public static ArrayList<String> pTeam = new ArrayList<>();
    public static ArrayList<Character> playerTeam = new ArrayList<>();
    public static ArrayList<String> cTeam = new ArrayList<>();
    public static ArrayList<Character> cpuTeam = new ArrayList<>();
    public static File doc = new File("res/curTeam.txt");
    public static ArrayList<Character> roster = new ArrayList<>();
    public static ArrayList<Move> allMoves = new ArrayList<>();
    public static ArrayList<Move> burstMoves = new ArrayList<>();
    public static ArrayList<Move> hitzeldMoves = new ArrayList<>();
    public static ArrayList<Move> ignisMoves = new ArrayList<>();
    public static ArrayList<Move> erholenMoves = new ArrayList<>();
    public static ArrayList<Move> aquilusMoves = new ArrayList<>();
    public static ArrayList<Move> douseMoves = new ArrayList<>();
    public static ArrayList<Move> glacieaMoves = new ArrayList<>();
    public static ArrayList<Move> dropMoves = new ArrayList<>();
    public static ArrayList<Move> roseMoves = new ArrayList<>();
    public static ArrayList<Move> fillipaMoves = new ArrayList<>();
    public static ArrayList<Move> demetraMoves = new ArrayList<>();
    public static ArrayList<Move> floraMoves = new ArrayList<>();
    public static ArrayList<Move> raitoMoves = new ArrayList<>();
    public static ArrayList<Move> borutoMoves = new ArrayList<>();
    public static ArrayList<Move> thunderMoves = new ArrayList<>();
    public static ArrayList<Move> shockMoves = new ArrayList<>();
    public static Button forfeit;
    public static HBox moveStuff;
    public Character gettingMove;
    public static Stage finScreen;
    public static Scene finScn;
    public static GridPane fin;
    public Text winLab;
    public static Button rematch;
    public static Button cont;
    public Stage fightStg;

    public Scene getScene() {
        return sc;
    }

    public Menu getParent() {
        return par;
    }

    public FightMenuPVC(Driver d) {
        // Establishes parent
        par = d.getCurrMenu();

        handler = d;

        // Creates all of the moves
        allMoves.add(new Move("Fire Strike", 50, 90, "Fire"));
        allMoves.add(new Move("Wisp Whip", 60, 80, "Fire"));
        allMoves.add(new Move("Ignite", 0, 90, "Normal")); // 10HP DoT
        allMoves.add(new Move("Tackle", 50, 85, "Normal"));
        allMoves.add(new Move("Erupt", 90, 70, "Fire")); // takes 40% to self if it works
        allMoves.add(new Move("Heat Up", 0, 90, "Normal")); // increase self speed by 20
        allMoves.add(new Move("Body Slam", 50, 80, "Normal"));
        allMoves.add(new Move("Explosion", 70, 70, "Fire"));
        allMoves.add(new Move("Flame Shield", 0, 90, "Normal")); // buff team defense by 30%
        allMoves.add(new Move("Spirit Strike", 60, 85, "Normal"));
        allMoves.add(new Move("Fireball", 50, 90, "Fire"));
        allMoves.add(new Move("Invigorate", 0, 90, "Normal")); // heal team 20% and buff speed by 10
        allMoves.add(new Move("Water Strike", 50, 90, "Water"));
        allMoves.add(new Move("Geyser", 70, 80, "Water"));
        allMoves.add(new Move("Flash Freeze", 0, 90, "Normal")); // debuff enemy speed by 10
        allMoves.add(new Move("Power Punch", 50, 90, "Normal"));
        allMoves.add(new Move("Hydro Gun", 70, 85, "Water"));
        allMoves.add(new Move("Cool Down", 0, 90, "Normal")); // 10% heal for team
        allMoves.add(new Move("Shoulder Charge", 60, 85, "Normal"));
        allMoves.add(new Move("Ice Spike", 70, 80, "Water"));
        allMoves.add(new Move("Ice Wall", 0, 90, "Normal")); // buff team defense by 15
        allMoves.add(new Move("Icicle Shower", 55, 85, "Water"));
        allMoves.add(new Move("Health Drop", 0, 90, "Normal")); // heal 25% health of target
        allMoves.add(new Move("Healing Rain", 0, 90, "Normal")); // heal team for 20% health
        allMoves.add(new Move("Thorn Whip", 50, 90, "Plant"));
        allMoves.add(new Move("Petal Strike", 70, 80, "Plant"));
        allMoves.add(new Move("Photosynthesis", 0, 90, "Normal")); // buff team speed by 10
        allMoves.add(new Move("Thump", 50, 90, "Normal"));
        allMoves.add(new Move("Leaf Charge", 75, 80, "Plant"));
        allMoves.add(new Move("Ingrain", 50, 90, "Plant")); // heal 35 health if it works
        allMoves.add(new Move("Clobber", 60, 90, "Normal"));
        allMoves.add(new Move("Earth Power", 80, 80, "Plant"));
        allMoves.add(new Move("Earth Barrier", 0, 90, "Normal")); // buff team defense by 5
        allMoves.add(new Move("Stem Whack", 50, 90, "Plant"));
        allMoves.add(new Move("Convalesce", 0, 90, "Normal")); // heal 25% of someone
        allMoves.add(new Move("Earth's Blessing", 0, 90, "Normal")); // heal 20% and buff speed by 5 for team
        allMoves.add(new Move("Zap", 50, 90, "Electric"));
        allMoves.add(new Move("Thunder Strike", 70, 80, "Electric"));
        allMoves.add(new Move("Charge Up", 0, 90, "Normal")); // buff team speed by 5
        allMoves.add(new Move("Quick Chop", 50, 90, "Normal"));
        allMoves.add(new Move("Lightning Chop", 70, 85, "Electric"));
        allMoves.add(new Move("Reenergize", 0, 90, "Normal")); // heal self for 20%
        allMoves.add(new Move("Battle Ram", 60, 90, "Normal"));
        allMoves.add(new Move("Lightning Charge", 80, 85, "Electric"));
        allMoves.add(new Move("Thunder Storm", 0, 25, "Normal")); // instakill if it works
        allMoves.add(new Move("Strike Rune", 70, 85, "Normal"));
        allMoves.add(new Move("Health Rune", 0, 90, "Normal")); // heal 30% health of target
        allMoves.add(new Move("Revive Rune", 0, 70, "Normal")); // rez target

        // Movesets for all characters
        makeMoveSet("Fire Strike", "Wisp Whip", "Ignite", burstMoves);
        makeMoveSet("Tackle", "Erupt", "Heat Up", hitzeldMoves);
        makeMoveSet("Body Slam", "Time Bomb", "Flame Shield", ignisMoves);
        makeMoveSet("Spirit Strike", "Fireball", "Invigorate", erholenMoves);
        makeMoveSet("Water Strike", "Geyser", "Flash Freeze", aquilusMoves);
        makeMoveSet("Power Punch", "Hydro Gun", "Cool Down", douseMoves);
        makeMoveSet("Shoulder Charge", "Ice Spike", "Ice Wall", glacieaMoves);
        makeMoveSet("Icicle Shower", "Health Drop", "Healing Rain", dropMoves);
        makeMoveSet("Thorn Whip", "Petal Strike", "Photosynthesis", roseMoves);
        makeMoveSet("Thump", "Leaf Charge", "Ingain", fillipaMoves);
        makeMoveSet("Clobber", "Earth Power", "Earth Barrier", demetraMoves);
        makeMoveSet("Stem Whack", "Convalesce", "Earth's Blessing", floraMoves);
        makeMoveSet("Zap", "Thunder Strike", "Charge Up", raitoMoves);
        makeMoveSet("Quick Chop", "Lightning Chop", "Reenergize", borutoMoves);
        makeMoveSet("Battle Ram", "Lightning Charge", "Thunder Storm", thunderMoves);
        makeMoveSet("Strike Rune", "Health Rune", "Revive Rune", shockMoves);

        // Adds all of the roster
        roster.add(new Character("Burst", 1, 250, 250, 90, 45, 70, 0, "Fire", burstMoves));
        roster.add(new Character("Hitzeld", 1, 350, 350, 50, 60, 50, 0, "Fire", hitzeldMoves));
        roster.add(new Character("Ignis", 1, 450, 450, 60, 80, 35, 0, "Fire", ignisMoves));
        roster.add(new Character("Erholen", 1, 300, 300, 65, 50, 65, 0, "Fire", erholenMoves));
        roster.add(new Character("Aquilus", 1, 300, 300, 80, 60, 65, 0, "Water", aquilusMoves));
        roster.add(new Character("Douse", 1, 375, 375, 50, 60, 40, 0, "Water", douseMoves));
        roster.add(new Character("Glaciea", 1, 400, 400, 40, 90, 30, 0, "Water", glacieaMoves));
        roster.add(new Character("Drop", 1, 350, 350, 50, 70, 60, 0, "Water", dropMoves));
        roster.add(new Character("Rose", 1, 200, 200, 80, 40, 80, 0, "Plant", roseMoves));
        roster.add(new Character("Fillipa", 1, 350, 350, 50, 70, 60, 0, "Plant", fillipaMoves));
        roster.add(new Character("Demetra", 1, 500, 500, 30, 75, 45, 0, "Plant", demetraMoves));
        roster.add(new Character("Flora", 1, 300, 300, 60, 50, 70, 0, "Plant", floraMoves));
        roster.add(new Character("Raito", 1, 350, 350, 75, 30, 75, 0, "Electric", raitoMoves));
        roster.add(new Character("Boruto", 1, 400, 400, 60, 55, 55, 0, "Electric", borutoMoves));
        roster.add(new Character("Thunder", 1, 500, 500, 40, 75, 20, 0, "Electric", thunderMoves));
        roster.add(new Character("Shock", 1, 400, 400, 50, 60, 50, 0, "Electric", shockMoves));

        // Creates the Player's team
        try {
            String line = "";
            BufferedReader teamGet = new BufferedReader(new FileReader(doc));
            while ((line = teamGet.readLine()) != null) {
                pTeam.add(line);
            }
            teamGet.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Cannot open file: " + doc.getName());
        } catch (IOException ex) {
            System.out.println("Error reading file: " + doc.getName());
        }
        for (int i = 0; i < pTeam.size(); i++) {
            for (int j = 0; j < roster.size(); j++) {
                if (pTeam.get(i).equalsIgnoreCase(roster.get(j).getName())) {
                    playerTeam.add(roster.get(j));
                }
            }
        }

        // Creates the Opponent's Team
        File dct = new File("res/opponent.txt");
        try {
            String line = "";
            BufferedReader teamGet = new BufferedReader(new FileReader(dct));
            while ((line = teamGet.readLine()) != null) {
                cTeam.add(line);
            }
            teamGet.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Cannot open file: " + dct.getName());
        } catch (IOException ex) {
            System.out.println("Error reading file: " + dct.getName());
        }
        for (int i = 0; i < cTeam.size(); i++) {
            for (int j = 0; j < roster.size(); j++) {
                if (cTeam.get(i).equalsIgnoreCase(roster.get(j).getName())) {
                    cpuTeam.add(roster.get(j));
                }
            }
        }

        // Creates GUI
        // Sets up menu
        men = new GridPane();
        men.setAlignment(Pos.CENTER);
        men.setStyle("-fx-background-color: maroon");
        // Gets player's sprites
        for (String s : pTeam) {
            f.add(new ImageView(new Image("file:res/" + s + ".png")));
        }
        f1 = f.get(0);
        f1.setFitWidth(100);
        f1.setPreserveRatio(true);
        f2 = f.get(1);
        f2.setFitWidth(100);
        f2.setPreserveRatio(true);
        f3 = f.get(2);
        f3.setFitWidth(100);
        f3.setPreserveRatio(true);
        f4 = f.get(3);
        f4.setFitWidth(100);
        f4.setPreserveRatio(true);
        fTeam = new HBox(10);
        fTeam.getChildren().add(f1);
        fTeam.getChildren().add(f2);
        fTeam.getChildren().add(f3);
        fTeam.getChildren().add(f4);
        // Player bar
        pTeamLab = new Text("Player: ");
        pTeamLab.setFont(Font.font("Times New Roman", FontWeight.NORMAL, 20));
        pTeamLab.setFill(Color.GOLD);
        plyrBar = new HBox(10);
        plyrBar.getChildren().add(pTeamLab);
        plyrBar.getChildren().add(fTeam);
        // Gets enemy sprites
        for (String s : cTeam) {
            e.add(new ImageView(new Image("file:res/" + s + ".png")));
        }
        e1 = e.get(0);
        e1.setFitWidth(100);
        e1.setPreserveRatio(true);
        e2 = e.get(1);
        e2.setFitWidth(100);
        e2.setPreserveRatio(true);
        e3 = e.get(2);
        e3.setFitWidth(100);
        e3.setPreserveRatio(true);
        e4 = e.get(3);
        e4.setFitWidth(100);
        e4.setPreserveRatio(true);
        eTeam = new HBox(10);
        eTeam.getChildren().add(e1);
        eTeam.getChildren().add(e2);
        eTeam.getChildren().add(e3);
        eTeam.getChildren().add(e4);
        // Computer bar
        cpuLab = new Text("Computer: ");
        cpuLab.setFont(Font.font("Times New Roman", FontWeight.NORMAL, 20));
        cpuLab.setFill(Color.GOLD);
        cpuBar = new HBox(10);
        cpuBar.getChildren().add(cpuLab);
        cpuBar.getChildren().add(eTeam);
        // Creates the battle log
        bLog = new TextArea();
        bLog.setEditable(false);
        bLog.setPrefSize(545, 345);
        battleLog = new ScrollPane(bLog);
        battleLog.setStyle("-fx-background-color: maroon");
        battleLog.setPrefSize(550, 350);
        // Sets up UI
        UI = new GridPane();
        UI.setAlignment(Pos.CENTER);
        UI.setStyle("-fx-background-color: maroon");
        //Stuff for move input
        moveLab = new Text("Enter the move you want here: ");
        moveLab.setFill(Color.GOLD);
        moveLab.setFont(Font.font("Times New Roman", FontWeight.NORMAL, 16));
        moveInput = new TextField();
        mInput = new Button("Submit");
        mInput.setPrefSize(125, 50);
        // Grid for anything move related
        moveStuff = new HBox(20);
        moveStuff.setAlignment(Pos.CENTER);
        moveStuff.getChildren().add(moveLab);
        moveStuff.getChildren().add(moveInput);
        moveStuff.getChildren().add(mInput);
        // Add UI elements into UI
        UI.add(moveStuff, 0, 0);
        UI.add(plyrBar, 0, 1);
        UI.add(cpuBar, 0, 2);
        // Forfeit button
        forfeit = new Button("Forfeit");
        forfeit.setPrefSize(100, 50);
        forfeit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                endScreen("You Lose!");
            }
        });

        // Adds everything into VBox
        everything = new VBox(20);
        everything.setAlignment(Pos.CENTER);
        everything.getChildren().add(battleLog);
        everything.getChildren().add(UI);
        everything.getChildren().add(forfeit);

        // Adds VBox into menu pane
        men.add(everything, 0, 0);

        // Adds menu into scene
        sc = new Scene(men);

        //Shows stage
        fightStg = new Stage();
        fightStg.setResizable(false);
        fightStg.setWidth(1250);
        fightStg.setHeight(1000);

        //Fight after showing
        pvcFight();
    }

    // Fight Related Methods
    private int damage(int attack, int defense, int power) {
        int num = (attack) + ((attack) * (power / 100)) - (defense);
        return num;
    }

    private void makeMoveSet(String x, String y, String z, ArrayList<Move> moves) {
        for (Move m : allMoves) {
            if (m.getName().equals(x) || m.getName().equals(y) || m.getName().equals(z)) {
                moves.add(m);
            }
        }
    }

    private void setFightOrder(ArrayList<Character> chars) {
        boolean loop = true;
        Character temp;

        while (loop) {
            loop = false;
            for (int i = 0; i < chars.size() - 1; i++) {
                if (chars.get(i).getSpeed() < chars.get(i + 1).getSpeed()) {
                    temp = chars.get(i);
                    chars.set(i, chars.get(i + 1));
                    chars.set(i + 1, temp);
                    loop = true;
                }
            }
        }
    }

    private boolean isPlyr(Character c) {
        boolean val = false;
        for (Character cht : playerTeam) {
            if (cht.equals(c)) {
                val = true;
            }
        }
        return val;
    }

    private boolean pLive() {
        boolean val = true;
        int cnt = 0;
        for (Character c : playerTeam) {
            if (c.getHealth() <= 0) {
                cnt++;
            }
        }
        if (cnt != playerTeam.size()) {

            val = true;
        } else {
            val = false;
        }
        return val;
    }

    private boolean cpuLive() {
        boolean val = true;
        int cnt = 0;
        for (Character c : cpuTeam) {
            if (c.getHealth() <= 0) {
                cnt++;
            }
        }
        if (cnt != cpuTeam.size()) {
            val = true;
        } else {
            val = false;
        }
        return val;
    }

    private String getCharMove(Character c, int mNum) {
        ArrayList<Move> charMoves = c.getMoves();
        return charMoves.get(mNum).getName();
    }

    private boolean isWeak(Character c, String type) {
        String cType = c.getType();
        boolean weak = false;

        if (cType.equalsIgnoreCase("Fire") && type.equalsIgnoreCase("Water")) {
            weak = true;
        }
        if (cType.equalsIgnoreCase("Water") && type.equalsIgnoreCase("Electricity")) {
            weak = true;
        }
        if (cType.equalsIgnoreCase("Electricity") && type.equalsIgnoreCase("Plant")) {
            weak = true;
        }
        if (cType.equalsIgnoreCase("Plant") && type.equalsIgnoreCase("Fire")) {
            weak = true;
        }
        return weak;
    }

    private int round(double d) {
        double result = d - (int) (d);
        if (result < .5) {
            return (int) (d);
        } else {
            return (int) (d + 1);
        }
    }

    private void makeGenericMove(Character acting, Character receive, Move m) {
        int rando = 0;
        Random randy = new Random();
        double dmg = 0;

        rando = randy.nextInt(101);
        if (rando <= allMoves.get(0).getAccuracy()) {
            dmg = damage(acting.getAttack(), receive.getDefense(), allMoves.get(0).getPower());
            if (isWeak(receive, allMoves.get(0).getType())) {
                dmg = dmg * 1.5;
            }
            receive.setHealth(receive.getHealth() - round(dmg));
            bLog.appendText("\n" + receive.getName() + " lost " + round(dmg) + " health!");
        } else {
            bLog.appendText("\n" + acting.getName() + " missed!");
        }
    }

    private void doMove(Character acting, Character receive, String moveName) {
        //Performs move on person
    }

    /*
     * Make it wait for player input
     * 
     * Make player wait for input from text field http://stackoverflow.com/questions/7229788/how-to-wait-for-input-in-a-text-field
     * 
     */
    Object waiting = new Object();
    public String input;
    public void pvcFight() {
        Driver.stg.hide();
        fightStg = new Stage();
        fightStg.setTitle("Fight!");
        fightStg.setResizable(false);
        fightStg.setWidth(1000);
        fightStg.setHeight(750);

        ArrayList<Character> allChars = new ArrayList<>();
        int randoMve = 0;
        int randoChr = 0;
        Random randy = new Random();
        int i = 0;
        bLog.appendText("START FIGHT!");

        // Puts all characters in play in an array list
        for (Character c : playerTeam) {
            allChars.add(c);
        }
        for (Character c : cpuTeam) {
            allChars.add(c);
        }

        // Sets fight order
        setFightOrder(allChars);

        synchronized(waiting){
            while ((pLive() && cpuLive())){
                fightStg.setScene(sc);
                fightStg.show();
                // Checks each character
                if (isPlyr(allChars.get(i)) && allChars.get(i) != allChars.get((i + 1) % allChars.size())) { // check player's character
                    gettingMove = null;
                    bLog.appendText("\nYour " + allChars.get(i).getName() + "'s turn.");
                    bLog.appendText("\nWhat move will you perform:");
                    for(int j = 0; j < allChars.get(i).getMoves().size(); j++){ // prints out the list of moves that can be performed
                        bLog.appendText("\n\n" + allChars.get(i).getMoves().get(j).getName());
                    }
                    mInput.setOnAction(new EventHandler<ActionEvent>(){
                        @Override public void handle(ActionEvent e){
                            input = moveInput.getText();
                            synchronized(waiting){
                                waiting.notify();
                            }
                        }
                    });
                    try {
                        synchronized(waiting){
                            waiting.wait();
                        }
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else { // cpu moves
                    randoMve = randy.nextInt(3);
                    randoChr = randy.nextInt(4);
                    bLog.appendText("\nEnemy " + allChars.get(i).getName() + "'s Turn");
                    bLog.appendText("\nEnemy " + allChars.get(i).getName() + " performed " + getCharMove(allChars.get(i), randoMve));
                    doMove(allChars.get(i), playerTeam.get(randoChr), getCharMove(allChars.get(i), randoMve));
                    fightStg.setScene(sc);
                    fightStg.show();
                }
                i++;
                if (i == allChars.size()) {
                    i = 0;
                }
            } 

            // Displays screen when someone loses
            if (pLive() == false) {
                endScreen("You Lose!");
            }
            if (cpuLive() == false) {
                endScreen("You Win!");
            }
            fightStg.setScene(sc);
            fightStg.show();
        }
    }

    public void endScreen(String txt) {
        finScreen = new Stage();
        finScreen.setTitle("");
        finScreen.setResizable(false);
        finScreen.setWidth(500);
        finScreen.setHeight(325);
        fin = new GridPane();
        fin.setStyle("-fx-background-color: maroon");
        fin.setAlignment(Pos.CENTER);
        winLab = new Text(txt);
        winLab.setFont(Font.font("Times New Roman", FontWeight.NORMAL, 20));
        winLab.setFill(Color.GOLD);
        rematch = new Button("Rematch");
        rematch.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                bLog.setText(null);
                pvcFight();
            }
        });
        cont = new Button("Continue");
        cont.setOnAction(handler);
        fin.add(winLab, 0, 0);
        fin.add(rematch, 0, 1);
        fin.add(cont, 0, 2);
        finScn = new Scene(fin);
        finScreen.setScene(finScn);
        finScreen.show();
    }
}

0 个答案:

没有答案