Skip to content
Snippets Groups Projects
Main.java 4.67 KiB
package bitFight;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Random;

class Main {

    public static void main(String[] args) throws IOException, InterruptedException{

        Display.clearScreen();

        Enemy enemy = new Enemy("BAD PAUL", 1); 
        Player player = new Player("play");        
        Menu menu= new Menu("name");
        Level level = new Level(1, player, enemy);
        GameData gamedata = Save.loadObject("res/gamedata");

        Display display = new Display(level);

        if (gamedata != null) {
            player = gamedata.getPlayer();
            level = gamedata.getLevelReached();
        }

        try {
            menu.display();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        menu.validate();

        Display.clearScreen();
        menu.displayGameRules();
        menu.validate();

        Display.clearScreen();
        Display.setCursorPos(50,0);
        Display.printDialogBox();

        Display.goToDialogBox();

        
        Display.newPrintln(enemy.toString());
        Display.newPrintln(Terminal.RED_BACKGROUND + "ATTAQUEZ" + Terminal.RESET + " ou " + Terminal.PURPLE_BACKGROUND + "QUITTEZ" + Terminal.RESET);
        
        Display.goToUserInput();
        
        // Récupère le nom de cahque attaque dans une ArrayList
        ArrayList<String> actionNames = new ArrayList<>();
        for (Attack a : Attack.values()) actionNames.add(a.getName());
        for (Defense d : Defense.values()) actionNames.add(d.getName());
        
        BufferedReader in= new BufferedReader( new InputStreamReader(System.in));
        
        Random rand = new Random();
        
        
        Input input = new Input();
        input.newInput();
        Display.goToDialogBox();
        
        // arrivée de l'ennemi
        level.ennemyShowing();

        if (input.getInput().equals("a")) {

        while (!input.getInput().equals("q") && !enemy.isDead() && !player.isDead()) {

            //A garder au début de la boucle, mesure le temps que le joueur à pris d'écrire
            LocalDateTime timeBeforeAttack = LocalDateTime.now();
            int attackTime = 5 - (int)(5 * rand.nextDouble());

            if (input.getInput().equals("a")) {
                level.possibleActions();
                input.newInput();
                
                // Boucle tant que le joueur n'a pas taper de nom d'une attaque valide ou tape trop tard ou tape q
                while ((!actionNames.contains(input.getInput()) || !Attack.attackInTime(timeBeforeAttack, attackTime)) && !input.getInput().equals("q")) {
                    //Si le joueur est trop lent, l'ennemi fait une attaque aléatoire
                    if(!Attack.attackInTime(timeBeforeAttack, attackTime)){
                        Display.clearDialogBox();
                        Attack randomEnemyAttack = Attack.values()[(int) rand.nextDouble()*Attack.values().length];
                        level.ennemyFaster(randomEnemyAttack);
                        timeBeforeAttack = LocalDateTime.now();
                        input.newInput();
                    } 
                    
                    if (!actionNames.contains(input.getInput())){
                        Display.clearDialogBox();
                        level.invalidActionChoice();                        
                        input.newInput();
                    }
                }
                
                
                if(!player.isExhausted() && !input.getInput().equals("q") ){
                    Attack attack = Attack.valueOf(actionNames.get(actionNames.indexOf(input.getInput())).toUpperCase());
                    level.playerHasEnergy(attack);
                } else {} //TODO ajouter display trop fatigué
                
                
                //Display.clearDialogBox();
                
                Display.newPrintln(enemy.toString());
                
                //Display.clearDialogBox();

                input.newInput();

                if (enemy.isDead()){
                    level.ennemyDying();
                    player.revive();
                    enemy.revive();
                    enemy.levelUp();
                    level.levelGoingUp();
                    level.displayEndOfLevel();                    
                }

            }
            
        }
            in.close();
            Save.saveObject("res/gamedata", gamedata);
        }
    } 
}