Skip to content
Snippets Groups Projects
Select Git revision
  • 88206905dbe0158bb5507a9ee46c6a3cf397e8e1
  • main default protected
  • 39-retour-utilisateur-sur-le-compteur
3 results

Main.java

Blame
  • Main.java 5.06 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");
    
            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();
    
            // arrivée de l'ennemi
            level.ennemyShowing();
    
            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();
    
            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();
                        Display.clearDialogBox();
                        System.out.println('\n');
                        System.out.println("                            Congrats!       "+'\n');
                        System.out.println("      you won your first fight! + \n \t let's see what you can do with your second ennemy...   Good luck! \n\t your currrent enemy level is "+level.getEnnemy().getLevel());
                        
                        System.out.println(" \n this time your enemy health is "+ (int) level.getEnnemy().getCurrentHealth()+ "\n ... ");
                        
                    }
    
                }
                
            }
                in.close();
                Save.saveObject("res/gamedata", gamedata);
            }
        } 
    }