What1slove / RefactoringLab

Вторая лабораторная по рефакторингу
0 stars 0 forks source link

Длинный метод + условная сложность (Board) #5

Open What1slove opened 4 years ago

What1slove commented 4 years ago

Метод actionPerformed около 140 строчек Почему плохо: 1 Тяжело читается и понимается 2 Тяжело модифицируется Также этот метод имеет сильное ветвение условных как в ширину (большое количество elsif) так и в длину (до 5-го уровня вложенности) Почему плохо: 1 Тяжело читается и понимается 2 Тяжело модифицируется 3 С каждым новым изменением бизнес-логики дерево условий будет только увеличиваться Код: `public void actionPerformed(ActionEvent e) {

    if (pause)
        return; // if we're on pause, don't do anything.

    // if player died, they don't get to move crawler or superzap
    if (!isPlayerDead()){
        crawler.move(crawlerzoffset);
        if (superzapperTicksLeft == 0 && crawler.isSuperzapping()) {
            superzapperTicksLeft = SUPERZAPPER_TICKS;
        }
    }

    if (clearboard) { 
        // if we're clearing the board, updating reduces to the boardclear animations
        if (crawlerSpiked) {
            dptLeft -=1;
            if (dptLeft <= 0)
                if (lives > 0)
                    replayLevel();
                else
                    crawlerSpiked = false; // damage has been done; other case will now handle game over.
        }
        else if (levelcleared)
        {   // player passed level.  
            // pull board out towards screen until player leaves far end of board.
            boardpov += SPEED_LEV_ADVANCE;
            if (crawlerzoffset < LEVEL_DEPTH)
                crawlerzoffset+=SPEED_LEV_ADVANCE; 

            if (boardpov > LEVEL_DEPTH * 5/4)
            {
                levelnum++;
                initLevel();
                boardpov = -LEVEL_DEPTH * 2;
                levelprep=true;
            }
        }
        else if (lives > 0)
        {   // player died but not out of lives.
            // pause, then suck crawler down and restart level
            dptLeft -=1;
            if (dptLeft <= 0) {
                crawlerzoffset += SPEED_LEV_ADVANCE*2;
                if (crawlerzoffset > LEVEL_DEPTH)
                    replayLevel();
            }
        }
        else
        { // player died and game is over.  advance everything along z away from player.
            if (boardpov > -LEVEL_DEPTH *5)
                boardpov -= GAME_OVER_BOARDSPEED;
            else
                gameover=true;
        }
    }

    if (lives > 0)
    {
        if (levelprep)
        {   // just cleared a level and we're prepping the new one
            // advance POV onto board, and then allow normal play
            boardpov += SPEED_LEV_ADVANCE*3/2;
            if (boardpov >= 0)
            {
                boardpov = 0;
                levelprep = false;
            }
        }

        // update player missile positions
        ArrayList<Missile> ms = crawler.getMissiles();
        for (int i = 0; i < ms.size(); i++) {
            Missile m = (Missile) ms.get(i);
            if (m.isVisible()) 
                m.move(LEVEL_DEPTH);
            else
                ms.remove(i);
        }

        if (!isPlayerDead())
        {   // if the player is alive, the exes and spikes can move and shoot
            for (int i = 0; i < exes.size(); i++) {
                Ex ex = (Ex) exes.get(i);
                if (ex.isVisible()) 
                {
                    ex.move(B_WIDTH, crawler.getColumn());
                    if (ex.getZ() <= 0) {
                        if (ex.isPod()) {
                            // we're at the top of the board; split the pod
                            exes.add(ex.spawn());
                            ex.setPod(false);
                        }
                    }
                    if ((ex.getZ() < LEVEL_DEPTH) 
                            && (r.nextInt(10000) < levelinfo.getExFireBPS()))
                    { // this ex fires a missile
                        enemymissiles.add(new Missile(ex.getColumn(), ex.getZ(), false));
                        SoundManager.get().play(Sound.ENEMYFIRE);
                    }
                }
                else 
                    exes.remove(i);
            }

            for (Spike s : spikes) {
                if (s.isVisible()) {
                    if (s.isSpinnerVisible()) {
                        s.move();
                        if ((s.getSpinnerZ() < LEVEL_DEPTH) 
                                && (r.nextInt(10000) < levelinfo.getExFireBPS()/4))
                        { // with 1/4 the frequency of an ex, this spinner fires a missile
                            enemymissiles.add(new Missile(s.getColumn(), s.getSpinnerZ(), false));
                            SoundManager.get().play(Sound.ENEMYFIRE);
                        }
                    }
                }
            }
        }

        // update ex missiles
        for (int i = 0; i < enemymissiles.size(); i++) {
            Missile exm = (Missile) enemymissiles.get(i);
            if (exm.isVisible()) 
                exm.move(LEVEL_DEPTH);
            else {
                enemymissiles.remove(i);
            }
        }

        if (!isPlayerDead())
            checkCollisions();

        // did player clear level?
        if (exes.size() <= 0 && !crawlerSpiked && !levelcleared)
        {
            levelcleared = true;
            clearboard = true;
            SoundManager.get().play(Sound.LEVELCLEAR);
        }
    }
    repaint();  
}`