示例#1
0
 @Override
 public void update(float elapsedTime, boolean[][] keyboardstate) {
   // All the main battle logic will go here
   if (turn == 0) { // If it's the MainCharacter turn we do something
     if (StartGameState.character.isParalyzed()) {
       if (drawParalyzed) {
         StartGameState.character.paralyzeCount -= 1;
         turn = 1;
         try {
           Thread.sleep(2000);
         } catch (InterruptedException ex) {
         }
         drawParalyzed = false;
       } else {
         drawParalyzed = true;
       }
     } else {
       if (turnState == 0) {
         if (keyboardstate[KeyEvent.VK_ENTER][1]) {
           if (actionScroller.getCountX() == 1 && actionScroller.getCountY() == 1) {
             turnState = 1;
             System.out.println(turnState);
           } else if (actionScroller.getCountX() == 1 && actionScroller.getCountY() == 2) {
             turnState = 2;
             System.out.println(turnState);
           } else if (actionScroller.getCountX() == 2 && actionScroller.getCountY() == 1) {
             turnState = 3;
             System.out.println(turnState);
           } else if (actionScroller.getCountX() == 2 && actionScroller.getCountY() == 2) {
             turnState = 4;
             System.out.println(turnState);
           }
         } else {
           if (!keyboardstate[KeyEvent.VK_S][0]
               && !keyboardstate[KeyEvent.VK_W][0]
               && !keyboardstate[KeyEvent.VK_A][0]
               && !keyboardstate[KeyEvent.VK_D][0]) { // Handle the absence of scrolling
             // Reset the scroller action variables
             actionScroller.scrollDirection = -1;
             actionScroller.scrollTimer = 0;
           } else {
             if (keyboardstate[KeyEvent.VK_S][0] && keyboardstate[KeyEvent.VK_W][0]) {
               actionScroller.scrollDirection = -1;
               actionScroller.scrollTimer = 0;
             } else if (keyboardstate[KeyEvent.VK_S][1]) {
               if (actionScroller.scrollDirection
                   == 0) { // If the scroller was already scrolling down...
                 actionScroller.scrollTimer +=
                     elapsedTime; // We add the elapsed time to the scroll timer...
                 if (actionScroller.scrollTimer
                     > actionScroller
                         .TIMER_MAX) { // And determine if we've waited to long enough ...
                   actionScroller.scrollTimer -=
                       actionScroller
                           .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                   actionScroller.scrollDown(); // And finally we scroll down.
                 }
               } else { // If the scroller was not already scrolling down...
                 actionScroller.scrollDown(); // We scroll down...
                 actionScroller.scrollDirection = 0; // Set the scroll direction to down...
                 actionScroller.scrollTimer = 0; // And reset the scrollTimer.
               }
             } else if (keyboardstate[KeyEvent.VK_W][1]) {
               if (actionScroller.scrollDirection
                   == 3) { // If the scroller was already scrolling up...
                 actionScroller.scrollTimer +=
                     elapsedTime; // We add the elapsed time to the scroll timer...
                 if (actionScroller.scrollTimer
                     > actionScroller
                         .TIMER_MAX) { // And determine if we've waited to long enough ...
                   actionScroller.scrollTimer -=
                       actionScroller
                           .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                   actionScroller.scrollUp(); // And finally we scroll up.
                 }
               } else { // If the scroller was not already scrolling up...
                 actionScroller.scrollUp(); // We scroll up...
                 actionScroller.scrollDirection = 3; // Set the scroll direction to up...
                 actionScroller.scrollTimer = 0; // And reset the scrollTimer.
               }
             }
             if (keyboardstate[KeyEvent.VK_A][0] && keyboardstate[KeyEvent.VK_D][0]) {
               actionScroller.scrollDirection = -1;
               actionScroller.scrollTimer = 0;
             } else if (keyboardstate[KeyEvent.VK_A][1]) {
               if (actionScroller.scrollDirection
                   == 1) { // If the scroller was already scrolling down...
                 actionScroller.scrollTimer +=
                     elapsedTime; // We add the elapsed time to the scroll timer...
                 if (actionScroller.scrollTimer
                     > actionScroller
                         .TIMER_MAX) { // And determine if we've waited to long enough ...
                   actionScroller.scrollTimer -=
                       actionScroller
                           .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                   actionScroller.scrollLeft(); // And finally we scroll down.
                 }
               } else { // If the scroller was not already scrolling down...
                 actionScroller.scrollLeft(); // We scroll down...
                 actionScroller.scrollDirection = 1; // Set the scroll direction to down...
                 actionScroller.scrollTimer = 0; // And reset the scrollTimer.
               }
             } else if (keyboardstate[KeyEvent.VK_D][1]) {
               if (actionScroller.scrollDirection
                   == 2) { // If the scroller was already scrolling up...
                 actionScroller.scrollTimer +=
                     elapsedTime; // We add the elapsed time to the scroll timer...
                 if (actionScroller.scrollTimer
                     > actionScroller
                         .TIMER_MAX) { // And determine if we've waited to long enough ...
                   actionScroller.scrollTimer -=
                       actionScroller
                           .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                   actionScroller.scrollRight(); // And finally we scroll up.
                 }
               } else { // If the scroller was not already scrolling up...
                 actionScroller.scrollRight(); // We scroll up...
                 actionScroller.scrollDirection = 2; // Set the scroll direction to up...
                 actionScroller.scrollTimer = 0; // And reset the scrollTimer.
               }
             }
           }
         }
       } else if (turnState == 1) {
         if (drawNoMana) {
           try {
             Thread.sleep(2000);
           } catch (InterruptedException ex) {
           }
           drawNoMana = false;
         } else {
           if (keyboardstate[KeyEvent.VK_BACK_SPACE][1]) {
             turnState = 0;
           } else if (keyboardstate[KeyEvent.VK_ENTER][1]) {
             playedCard = hand.getIndexed(cardScroller.getCountX() - 1);
             if (StartGameState.character.mana < playedCard.getManaCost()) {
               drawNoMana = true;
             } else {
               turnState = 5;
               hand.remove(hand.getIndexedKey(cardScroller.getCountX() - 1));
               cardScroller.setMenuCountX(hand.size());
               cardScroller.setCountX(cardScroller.getCountX() - 1);
               if (cardScroller.getCountX() <= 0) {
                 cardScroller.setCountX(1);
               }
               if (hand.size() > 0) {
                 cardScroller.setMenuSpacingX(cardWidth / hand.size());
               }
             }
           } else {
             if (!keyboardstate[KeyEvent.VK_S][0]
                 && !keyboardstate[KeyEvent.VK_W][0]
                 && !keyboardstate[KeyEvent.VK_A][0]
                 && !keyboardstate[KeyEvent.VK_D][0]) { // Handle the absence of scrolling
               // Reset the scroller action variables
               cardScroller.scrollDirection = -1;
               cardScroller.scrollTimer = 0;
             } else {
               if (keyboardstate[KeyEvent.VK_S][0] && keyboardstate[KeyEvent.VK_W][0]) {
                 cardScroller.scrollDirection = -1;
                 cardScroller.scrollTimer = 0;
               } else if (keyboardstate[KeyEvent.VK_S][1]) {
                 if (cardScroller.scrollDirection
                     == 0) { // If the scroller was already scrolling down...
                   cardScroller.scrollTimer +=
                       elapsedTime; // We add the elapsed time to the scroll timer...
                   if (cardScroller.scrollTimer
                       > cardScroller
                           .TIMER_MAX) { // And determine if we've waited to long enough ...
                     cardScroller.scrollTimer -=
                         cardScroller
                             .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                     cardScroller.scrollDown(); // And finally we scroll down.
                   }
                 } else { // If the scroller was not already scrolling down...
                   cardScroller.scrollDown(); // We scroll down...
                   cardScroller.scrollDirection = 0; // Set the scroll direction to down...
                   cardScroller.scrollTimer = 0; // And reset the scrollTimer.
                 }
               } else if (keyboardstate[KeyEvent.VK_W][1]) {
                 if (cardScroller.scrollDirection
                     == 3) { // If the scroller was already scrolling up...
                   cardScroller.scrollTimer +=
                       elapsedTime; // We add the elapsed time to the scroll timer...
                   if (cardScroller.scrollTimer
                       > cardScroller
                           .TIMER_MAX) { // And determine if we've waited to long enough ...
                     cardScroller.scrollTimer -=
                         cardScroller
                             .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                     cardScroller.scrollUp(); // And finally we scroll up.
                   }
                 } else { // If the scroller was not already scrolling up...
                   cardScroller.scrollUp(); // We scroll up...
                   cardScroller.scrollDirection = 3; // Set the scroll direction to up...
                   cardScroller.scrollTimer = 0; // And reset the scrollTimer.
                 }
               }
               if (keyboardstate[KeyEvent.VK_A][0] && keyboardstate[KeyEvent.VK_D][0]) {
                 cardScroller.scrollDirection = -1;
                 cardScroller.scrollTimer = 0;
               } else if (keyboardstate[KeyEvent.VK_A][1]) {
                 if (cardScroller.scrollDirection
                     == 1) { // If the scroller was already scrolling down...
                   cardScroller.scrollTimer +=
                       elapsedTime; // We add the elapsed time to the scroll timer...
                   if (cardScroller.scrollTimer
                       > cardScroller
                           .TIMER_MAX) { // And determine if we've waited to long enough ...
                     cardScroller.scrollTimer -=
                         cardScroller
                             .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                     cardScroller.scrollLeft(); // And finally we scroll down.
                   }
                 } else { // If the scroller was not already scrolling down...
                   cardScroller.scrollLeft(); // We scroll down...
                   cardScroller.scrollDirection = 1; // Set the scroll direction to down...
                   cardScroller.scrollTimer = 0; // And reset the scrollTimer.
                 }
               } else if (keyboardstate[KeyEvent.VK_D][1]) {
                 if (cardScroller.scrollDirection
                     == 2) { // If the scroller was already scrolling up...
                   cardScroller.scrollTimer +=
                       elapsedTime; // We add the elapsed time to the scroll timer...
                   if (cardScroller.scrollTimer
                       > cardScroller
                           .TIMER_MAX) { // And determine if we've waited to long enough ...
                     cardScroller.scrollTimer -=
                         cardScroller
                             .TIMER_MAX; // To justify a scroll. We reset the scrollTimer...
                     cardScroller.scrollRight(); // And finally we scroll up.
                   }
                 } else { // If the scroller was not already scrolling up...
                   cardScroller.scrollRight(); // We scroll up...
                   cardScroller.scrollDirection = 2; // Set the scroll direction to up...
                   cardScroller.scrollTimer = 0; // And reset the scrollTimer.
                 }
               }
             }
           }
         }
       } else if (turnState == 2) {
         if (shouldWait) {
           if (itemMenu.isActive()) {
             itemMenu.update(elapsedTime, keyboardstate);
             if (keyboardstate[KeyEvent.VK_ENTER][1]) {
               itemMenu.setActive(false);
               drawItemUsed = true;
             }
           } else {
             shouldWait = false;
             drawItemUsed = false;
             try {
               // Waits a little on the enemy turn, this is just for testing and will be removed
               // when AI is written
               Thread.sleep(1500);
             } catch (InterruptedException ex) {
             }
             turnState = 0;
             turn = 1;
           }
         } else {
           itemMenu.setActive(true);
           if (keyboardstate[KeyEvent.VK_BACK_SPACE][1]) {
             turnState = 0;
             itemMenu.setActive(false);
           } else {
             itemMenu.update(elapsedTime, keyboardstate);
             if (keyboardstate[KeyEvent.VK_ENTER][1]) {
               if (itemMenu.state == 1 && itemMenu.error == false) {
                 shouldWait = true;
               }
             }
           }
         }
       } else if (turnState == 3) {
         if (shouldWait) {
           try {
             // Waits a little on the enemy turn, this is just for testing and will be removed when
             // AI is written
             Thread.sleep(2000);
           } catch (InterruptedException ex) {
           }
           drawNoCards = false;
           shouldWait = false;
           turnState = 0;
         } else {
           if (keyboardstate[KeyEvent.VK_BACK_SPACE][1]) {
             turnState = 0;
           } else if (keyboardstate[KeyEvent.VK_ENTER][1]) {
             if (deckCount >= MainCharacter.deck.size()) {
               drawNoCards = true;
               shouldWait = true;
             } else {
               hand.put(
                   hand.getIndexedKey(hand.size() - 1) + 1, MainCharacter.deck.get(deckCount));
               cardScroller.setMenuCountX(hand.size());
               cardScroller.setMenuSpacingX(cardWidth / hand.size());
               deckCount++;
               turn = 1;
               turnState = 0;
             }
           }
         }
       } else if (turnState == 4) {
         if (keyboardstate[KeyEvent.VK_BACK_SPACE][1]) {
           turnState = 0;
         }
       } else if (turnState == 5) {
         try {
           if (drawMoveApplication) {
             Thread.sleep(1800);
             if (enemy.health > 0) {
               turn = 1;
               turnState = 0;
             } else {
               turn = 2;
             }
             drawMoveApplication = false;
           } else {
             Thread.sleep(1800);
             effectMessage = playedCard.applyEffect(StartGameState.character, enemy);
             drawMoveApplication = true;
           }
         } catch (InterruptedException ex) {
         }
       }
     }
   } else if (turn
       == 1) { // If it's the Enemy's turn we can't do anything, and the AI decides what to do
     try {
       if (enemy.isParalyzed()) {
         if (drawParalyzed) {
           enemy.paralyzeCount -= 1;
           turn = 0;
           try {
             Thread.sleep(2000);
           } catch (InterruptedException ex) {
           }
           drawParalyzed = false;
         } else {
           drawParalyzed = true;
         }
       } else {
         // Waits a little on the enemy turn, this is just for testing and will be removed when AI
         // is written
         if (enemyMoveChosen) {
           Thread.sleep(1800);
           if (drawMoveApplication) {
             turn = 0;
             enemyMoveChosen = false;
             drawMoveApplication = false;
           } else {
             drawMoveApplication = true;
           }
         } else {
           if (enemy.getClass().equals(Creature.class)) {
             Creature creature = (Creature) enemy;
             int moveCount = creature.battleMoves.length;
             Random rand = new Random();
             double r = rand.nextDouble();
             int i = 1;
             while (r > (double) i / moveCount) {
               i++;
             }
             lastEnemyMove = MoveCache.getMove(creature.battleMoves[i - 1]);
             effectMessage = lastEnemyMove.applyEffect(creature, StartGameState.character);
             enemyMoveChosen = true;
           }
           Thread.sleep(1500);
         }
       }
     } catch (InterruptedException ex) {
     }
   } else if (turn == 2) {
     try {
       // Waits a little when the battle is won
       Thread.sleep(2000);
     } catch (InterruptedException ex) {
     }
     endEvent();
   } else if (turn == 3) {
     try {
       // Waits a little when the battle is lost
       Thread.sleep(2000);
     } catch (InterruptedException ex) {
     }
     endEvent();
   }
 }