Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. // ****************************
  2.     // Start/Resume/Pause
  3.     // ****************************
  4. /*public static void selectPlayableGame(String name, int id) throws InvalidInputException  {
  5. }
  6.  
  7. public static void startGame(Block223PlayModeInterface ui) throws InvalidInputException {
  8. }*/
  9.    
  10.     public static List<TOPlayableGame> getPlayableGames(){
  11.        
  12.         String error = "";
  13.         if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
  14.             error = "Player privileges are required to play a game.";
  15.         }
  16.        
  17.         Block223 block223 = Block223Application.getBlock223();
  18.         UserRole player = Block223Application.getCurrentUserRole();
  19.        
  20.         List<TOPlayableGame> result = new ArrayList<>();  //  --> creates List <TOPlayableGame>
  21.        
  22.         List<Game> games = block223.getGames();
  23.        
  24.         for(Game agame: games) {
  25.            
  26.             if(agame.isPublished()) {
  27.                 TOPlayableGame to = new TOPlayableGame(agame.getName(),-1,0);
  28.                 result.add(to);
  29.             }
  30.         }
  31.        
  32.         List<PlayedGame> playedgames = ((Player) player).getPlayedGames();
  33.        
  34.         for(PlayedGame apgame: playedgames) {
  35.             TOPlayableGame to = new TOPlayableGame(apgame.getGame().getName(), apgame.getId(), apgame.getCurrentLevel());
  36.             result.add(to);
  37.         }
  38.        
  39.         return result;
  40.     }
  41.    
  42.     public static void selectPlayableGame(String name, int id) throws InvalidInputException {
  43.        
  44.         String error = "";
  45.         if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
  46.             error = "Player privileges are required to play a game.";
  47.         }
  48.  
  49.         Game agame = Game.getWithName(name);
  50.         Block223 block223 = Block223Application.getBlock223();
  51.         PlayedGame pgame;
  52.        
  53.         if(agame == null) {
  54.             throw new InvalidInputException("The game does not exist.");
  55.         }
  56.        
  57.         else if(agame != null) {
  58.            
  59.             UserRole player = Block223Application.getCurrentUserRole();
  60.             String username = User.findUsername(player); // LOOK INTO
  61.            
  62.             if(username == null) {
  63.                 throw new InvalidInputException("The game does not exist.");
  64.             }
  65.            
  66.             pgame = new PlayedGame(username, agame, block223);
  67.             pgame.setPlayer((Player)player); // Player cast <----
  68.            
  69.            
  70.         }else pgame = block223.findPlayableGame(id);
  71.        
  72.         Block223Application.setCurrentPlayableGame(pgame);
  73.     }
  74.    
  75.     public static void startGame(Block223PlayModeInterface ui) {
  76.        
  77.         PlayedGame game = Block223Application.getCurrentPlayableGame();
  78.         game.play(); // game: PlayedGame
  79.        
  80.         ui.takeInputs();
  81.        
  82.         while(game.getPlaystatus() == PlayStatus.Moving) {
  83.             String userinputs = ui.takeInputs();
  84.            
  85.             Block223Controller.updatePaddlePosition(userinputs);
  86.            
  87.             game.move();
  88.            
  89.             if(userinputs == " ") {
  90.                 game.pause();
  91.             }
  92.             game.getWaitTime();
  93.             ui.refresh();
  94.         }
  95.        
  96.         if(game.getPlayStatus() == PlayStatus.GameOver) {
  97.             Block223Application.setCurrentPlayableGame(null);
  98.         }
  99.         else if(game.getPlayer()!= null) {
  100.             Block223 block223 = Block223Application.getBlock223();
  101.             Block223Persistence.save(block223);
  102.         }
  103.     }
  104.    
  105.     public static TOCurrentlyPlayedGame getCurrentPlayableGame() {
  106.        
  107.         PlayedGame pgame = Block223Application.getCurrentPlayableGame();
  108.        
  109.         boolean paused = pgame.getPlayStatus() == PlayStatus.Ready || pgame.getPlayStatus() == PlayStatus.Paused;
  110.        
  111.         TOCurrentlyPlayedGame result = new TOCurrentlyPlayedGame(pgame.getGame().getName(), paused,
  112.                 pgame.getScore(), pgame.getLives(),pgame.getCurrentLevel(), pgame.getPlayername(),
  113.                 (int)pgame.getCurrentBallX(), (int)pgame.getCurrentBallY(), (int)pgame.getCurrentPaddleLength(), (int)pgame.getCurrentPaddleX());
  114.        
  115.         List<PlayedBlockAssignment> blocks = pgame.getBlocks();
  116.        
  117.         for(PlayedBlockAssignment pblock: blocks) {
  118.             TOCurrentBlock to = new TOCurrentBlock(pblock.getBlock().getRed(),pblock.getBlock().getGreen(),
  119.                     pblock.getBlock().getBlue(),pblock.getBlock().getPoints(),pblock.getX(),pblock.getY(), result);
  120.         }
  121.         return result;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement