Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<TOPlayableGame> getPlayableGames(){
- String error = "";
- if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
- error = "Player privileges are required to play a game.";
- }
- Block223 block223 = Block223Application.getBlock223();
- UserRole player = Block223Application.getCurrentUserRole();
- List<TOPlayableGame> result = new ArrayList<>(); // --> creates List <TOPlayableGame>
- List<Game> games = block223.getGames();
- for(Game agame: games) {
- if(agame.isPublished()) {
- TOPlayableGame to = new TOPlayableGame(agame.getName(),-1,0);
- result.add(to);
- }
- }
- player.getPlayedGames();
- for(Game agame: games) {
- TOPlayableGame to = new TOPlayableGame(agame.getGame().getName(), agame.getId(), agame.getCurrentLevel());
- result.add(to);
- }
- return result;
- }
- public static void selectPlayableGame(String name, int id) throws InvalidInputException {
- String error = "";
- if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
- error = "Player privileges are required to play a game.";
- }
- Game agame = Game.getWithName(name);
- Block223 block223 = Block223Application.getBlock223();
- PlayedGame pgame;
- if(agame == null) {
- throw new InvalidInputException("The game does not exist.");
- }
- else if(agame != null) {
- UserRole player = Block223Application.getCurrentUserRole();
- String username = User.findUsername(player); // LOOK INTO
- if(username == null) {
- throw new InvalidInputException("The game does not exist.");
- }
- pgame = new PlayedGame(username, agame, block223);
- pgame.setPlayer((Player)player); // Player cast <----
- }else pgame = block223.findPlayableGame(id);
- Block223Application.setCurrentPlayableGame(pgame);
- }
- public static void startGame(Block223PlayModeInterface ui) {
- Game game = Block223Application.getCurrentPlayableGame();
- game.play(); // game: PlayedGame
- ui.takeInputs();
- while(game.getPlaystatus() == PlayStatus.Moving) {
- String userinputs = ui.takeInputs();
- Block223Controller.updatePaddlePosition(userinputs);
- game.move();
- if(userinputs == " ") {
- game.pause();
- }
- game.getWaitTime();
- ui.refresh();
- }
- if(game.getPlayStatus() == PlayStatus.GameOver) {
- Block223Application.setCurrentPlayableGame(null);
- }
- else if(game.getPlayer()!= null) {
- Block223 block223 = Block223Application.getBlock223();
- Block223Persistence.save(block223);
- }
- }
- public static TOCurrentlyPlayedGame getCurrentPlayableGame() {
- PlayedGame pgame = Block223Application.getCurrentPlayableGame();
- boolean paused = pgame.getPlayStatus() == PlayStatus.Ready || pgame.getPlayStatus() == PlayStatus.Paused;
- TOCurrentlyPlayedGame result = new TOCurrentlyPlayedGame(pgame.getGame().getName(), paused,
- pgame.getScore(), pgame.getLives(),pgame.getCurrentLevel(), pgame.getPlayername(),
- (int)pgame.getCurrentBallX(), (int)pgame.getCurrentBallY(), (int)pgame.getCurrentPaddleLength(), (int)pgame.getCurrentPaddleX());
- List<PlayedBlockAssignment> blocks = pgame.getBlocks();
- for(PlayedBlockAssignment pblock: blocks) {
- TOCurrentBlock to = new TOCurrentBlock(pblock.getBlock().getRed(),pblock.getBlock().getGreen(),
- pblock.getBlock().getBlue(),pblock.getBlock().getPoints(),pblock.getX(),pblock.getY(), result);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement