Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package pianotiles.logic;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Random;
  5. import pianotiles.logic.exceptions.InvalidInputException;
  6.  
  7. public class Game {
  8.  
  9.     private static final int TILE_WIDTH = 150, TILE_HEIGHT = 200;
  10.  
  11.     private boolean gameOver;
  12.     private static Game instance = null;
  13.     public Player player;
  14.     public int score;
  15.     public LinkedList<Tile> tiles;
  16.     public Level level;
  17.  
  18.     public static Game getInstance() {
  19.         if (instance == null) {
  20.             try {
  21.                 setNewGame("No name");
  22.             } catch (InvalidInputException e) {
  23.             }
  24.         }
  25.         return instance;
  26.     }
  27.  
  28.     private Game(Player pPlayer) {
  29.         this.player = pPlayer;
  30.         this.level = Level.Training;
  31.         this.tiles = new LinkedList<>();
  32.     }
  33.    
  34.     public void addTileOnTop(){
  35.         Tile tile = new Tile();
  36.         tile.setSize(TILE_WIDTH, TILE_HEIGHT);
  37.         int r = (new Random().nextInt(4))*150 + 100;
  38.         tile.setLocation(r, -TILE_HEIGHT);
  39.         tile.increase = Level.getLevel(score).speed;
  40.         this.tiles.add(tile);
  41.     }
  42.  
  43.     public void nukeTheGame() {
  44.         this.gameOver = false;
  45.         this.score = 0;
  46.     }
  47.  
  48.     public void setScore() {
  49.         if (!gameOver) {
  50.             score += new Random().nextInt(3)+1;
  51.            
  52.             this.level = Level.getLevel(score);
  53.            
  54.             for (Tile tile : this.tiles){
  55.                 tile.increase = this.level.speed;
  56.             }
  57.         }
  58.     }
  59.  
  60.     public void setGameOver() {
  61.         gameOver = true;
  62.     }
  63.    
  64.     public boolean isGameOver() {
  65.         return gameOver;
  66.     }
  67.    
  68.     public static void setNewGame(String playerName) throws InvalidInputException {
  69.         Player player = new Player(playerName);
  70.         Game.instance = new Game(player);
  71.     }
  72.    
  73.     public enum Level {
  74.         Training(-1, 50, 4.0f, 500),
  75.         Beginner(50, 100, 4.2f, 475),
  76.         Intermediate(100, 250, 4.5f, 450),
  77.         Advanced(250, 350, 5.5f, 425),
  78.         Expert(350, 450, 6.5f, 400),
  79.         Professional(450, 600, 7f, 375),
  80.         OLIVETO_BOSS(600, Integer.MAX_VALUE, 7.5f, 350);
  81.        
  82.         public int minRange, maxRange, sleep;
  83.         public float speed;
  84.  
  85.         private Level(int r1, int r2, float i, int sleep) {
  86.             this.minRange = r1;
  87.             this.maxRange = r2;
  88.             this.speed = i;
  89.             this.sleep = sleep;
  90.         }
  91.        
  92.         public static Level getLevel(int score){
  93.             for (Level level : Level.values()){
  94.                 if (score > level.minRange && score <= level.maxRange){
  95.                     return level;
  96.                 }
  97.             }
  98.             return null;
  99.         }
  100.        
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement