Android Open Source - X3n0break Level Loader






From Project

Back to project page X3n0break.

License

The source code is released under:

GNU General Public License

If you think the Android project X3n0break listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.x3n0m0rph59.breakout;
// ww w  .  jav  a2 s. c  o m
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;


public final class LevelLoader {
  public static HashMap<String, String> getGlobalMetaData() {
    final HashMap<String, String> data = new HashMap<String, String>();
    
    BufferedReader reader = null;
    
    try {      
      final FileHandle in = Gdx.files.internal("data/sets/metadata.set");      
      reader = new BufferedReader(in.reader());      

      String line;
      
      while ((line = reader.readLine()) != null) {
        boolean commentLine = false;
        int commentCharIndex = 0;
        
        charLoop:
        for (int i = 0; i < line.length(); i++) {
          char c = line.charAt(i);
          
          switch (c) {
          case '#':
            // '#' comment char
            commentLine = true;
            commentCharIndex = i;
            break charLoop;
          }
        }
        
        if (commentLine) {          
          final String[] pair = new String(line.substring(commentCharIndex + 1)).split(":");
          
          if (pair.length == 2) {
            data.put(pair[0].trim(), pair[1].trim());
          } else {
            // skip, maybe it is a user comment?
            
            //throw new RuntimeException("Malformed global metadata: " + line);
          }
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }      
    }    
    
    return data;
  }
  
  public static HashMap<String, String> getLevelSetMetaData() {
    final HashMap<String, String> data = new HashMap<String, String>();
    
    BufferedReader reader = null;
    
    try {      
      final FileHandle in = Gdx.files.internal(ResourceMapper.getPath("metadata.lvl", ResourceType.LEVEL));      
      reader = new BufferedReader(in.reader());      

      String line;
      
      while ((line = reader.readLine()) != null) {
        boolean commentLine = false;
        int commentCharIndex = 0;
        
        charLoop:
        for (int i = 0; i < line.length(); i++) {
          char c = line.charAt(i);
          
          switch (c) {
          case '#':
            // '#' comment char
            commentLine = true;
            commentCharIndex = i;
            break charLoop;
          }
        }
        
        if (commentLine) {          
          final String[] pair = new String(line.substring(commentCharIndex + 1)).split(":");
          
          if (pair.length == 2) {
            data.put(pair[0].trim(), pair[1].trim());
          } else {
            // skip, maybe it is a user comment?
            
            //throw new RuntimeException("Malformed level set metadata: " + line);
          }
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }      
    }    
    
    return data;
  }
  
  public static HashMap<String, String> getLevelMetaData(int level) {
    final HashMap<String, String> data = new HashMap<String, String>();
    
    BufferedReader reader = null;
    
    try {      
      final FileHandle in = Gdx.files.internal(ResourceMapper.getPath("level" + String.format("%02d", level + 1) + ".lvl", ResourceType.LEVEL));      
      reader = new BufferedReader(in.reader());      

      String line;
      
      while ((line = reader.readLine()) != null) {
        boolean commentLine = false;
        int commentCharIndex = 0;
        
        charLoop:
        for (int i = 0; i < line.length(); i++) {
          char c = line.charAt(i);
          
          switch (c) {
          case '#':
            // '#' comment char
            commentLine = true;
            commentCharIndex = i;
            break charLoop;
          }
        }
        
        if (commentLine) {          
          final String[] pair = new String(line.substring(commentCharIndex + 1)).split(":");
          
          if (pair.length == 2) {
            data.put(pair[0].trim(), pair[1].trim());
          } else {
            // skip, maybe it is a user comment?
            
            //throw new RuntimeException("Malformed level metadata: " + line);
          }
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }      
    }    
    
    return data;
  }

  public static List<Brick> loadLevel(int level) {
    final List<Brick> bricks = new ArrayList<Brick>();
    
    BufferedReader reader = null;
    
    try {      
      final FileHandle in = Gdx.files.internal(ResourceMapper.getPath("level" + String.format("%02d", level + 1) + ".lvl", ResourceType.LEVEL));      
      reader = new BufferedReader(in.reader());      

      String line;
      int lindex = 0, cindex = 0;            
      
      while ((line = reader.readLine()) != null) {
        Brick.Type type = null;
        EnumSet<Brick.Behavior> behavior = EnumSet.noneOf(Brick.Behavior.class);
        
        final int numberOfBricksInLine = getNumberOfBrickDefs(line);
        
        char lastBehaviourModifier = '#';
        int multiplier = 1;
        boolean commentLine = false,
            modifierChar = false;
        
        charLoop:
        for (int i = 0; i < line.length(); i++) {
          char c = line.charAt(i);

          modifierChar = false;
          
          switch (c) {
          case '#':
            // '#' comment char, ignore everything until next line
            commentLine = true;
            modifierChar = true;            
            break charLoop;
            
          case '\'':
            // ''' reset behavior to normal
            behavior = EnumSet.noneOf(Brick.Behavior.class);
            
            multiplier = 1;
            
            lastBehaviourModifier = c;
            modifierChar = true;
            break;
            
          case '<':
            // '<' move left
            if (behavior.contains(Brick.Behavior.MOVE_RIGHT))
              behavior.remove(Brick.Behavior.MOVE_RIGHT);
            
            behavior.add(Brick.Behavior.MOVE_LEFT);
            
            if (c == lastBehaviourModifier)
              multiplier *= Config.BRICK_MOVEMENT_MULTIPLIER;
            else
              multiplier = 1;
            
            lastBehaviourModifier = c;
            modifierChar = true;
            continue;
            
          case '>':
            // '>' move right
            if (behavior.contains(Brick.Behavior.MOVE_LEFT))
              behavior.remove(Brick.Behavior.MOVE_LEFT);
            
            behavior.add(Brick.Behavior.MOVE_RIGHT);
            
            if (c == lastBehaviourModifier)
              multiplier *= Config.BRICK_MOVEMENT_MULTIPLIER;
            else
              multiplier = 1;
            
            lastBehaviourModifier = c;
            modifierChar = true;
            continue;
            
          case '(':
            // '(' rotate right
            if (behavior.contains(Brick.Behavior.ROTATE_LEFT))
              behavior.remove(Brick.Behavior.ROTATE_LEFT);
            
            behavior.add(Brick.Behavior.ROTATE_RIGHT);
            
            if (c == lastBehaviourModifier)
              multiplier *= Config.BRICK_MOVEMENT_MULTIPLIER;
            else
              multiplier = 1;
            
            lastBehaviourModifier = c;
            modifierChar = true;
            continue;
            
          case ')':            
            // '(' rotate left
            if (behavior.contains(Brick.Behavior.ROTATE_RIGHT))
              behavior.remove(Brick.Behavior.ROTATE_RIGHT);
            
            behavior.add(Brick.Behavior.ROTATE_LEFT);
            
            if (c == lastBehaviourModifier)
              multiplier *= Config.BRICK_MOVEMENT_MULTIPLIER;
            else
              multiplier = 1;
            
            lastBehaviourModifier = c;
            modifierChar = true;
            continue;
          
          // Brick types
          case ' ':                        
          case 'X':
            // Whitespace and 'X' means "no brick at that position"
            cindex++;
            modifierChar = false;
            continue;
            
          case 'N':
            type = Brick.Type.NORMAL;
            modifierChar = false;
            break;
            
          case 'W':
            type = Brick.Type.WEAK;
            modifierChar = false;
            break;
            
          case 'H':
            type = Brick.Type.HARD;
            modifierChar = false;
            break;
            
          case 'S':
            type = Brick.Type.SOLID;
            modifierChar = false;
            break;
            
          case 'P':
            type = Brick.Type.POWERUP;
            modifierChar = false;
            break;
          }
          
          final float BRICK_WIDTH = Math.abs(((Config.getInstance().getClientWidth() - 
                         (2 * Config.BRICK_OFFSET_X) - 
                         (numberOfBricksInLine * Config.BRICK_SPACING_X))) / 
                          numberOfBricksInLine);
          final float BRICK_HEIGHT = Config.BRICK_HEIGHT;
          
          final float BRICK_SPEED = Config.BRICK_MOVEMENT_SPEED * multiplier;
          
          float ANGULAR_VELOCITY = 0.0f;
          if (behavior.contains(Brick.Behavior.ROTATE_LEFT))          
            ANGULAR_VELOCITY = -(Config.BRICK_ROTATION_SPEED * multiplier);
          else if (behavior.contains(Brick.Behavior.ROTATE_RIGHT))
            ANGULAR_VELOCITY = +(Config.BRICK_ROTATION_SPEED * multiplier);
          
                    
          bricks.add(new Brick(type, behavior, BRICK_SPEED, ANGULAR_VELOCITY,
                     new Point((cindex * (BRICK_WIDTH  + Config.BRICK_SPACING_X)) + Config.BRICK_OFFSET_X, 
                           (lindex * (BRICK_HEIGHT + Config.BRICK_SPACING_Y)) + Config.BRICK_OFFSET_Y), 
                       BRICK_WIDTH, BRICK_HEIGHT));
          
          if (!modifierChar)
            cindex++;      
        }
        
        if (!commentLine)
          lindex++;
        
        commentLine = false;  
        cindex = 0;
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }      
    }
    
    return bricks;
  }

  private static int getNumberOfBrickDefs(String line) {
    int cnt = 0;
    
    for (int i = 0; i < line.length(); i++) {
      char c = line.charAt(i);
      
      switch (c) {
      case ' ':
      case 'X':
        
      case 'N':
        
      case 'W':
        
      case 'H':
        
      case 'S':
        
      case 'P':
        cnt++;
        break;
      }
    }
    
    return cnt;
  }

//  private static float getNumberOfModifierChars(String line) {
//    int cnt = 0;
//    
//    for (int i = 0; i < line.length(); i++) {
//      char c = line.charAt(i);
//      
//      switch (c) {
//      case '#':
//  
//      case '\'':
//        
//      case '<':
//        
//      case '>':
//        
//      case '(':
//        
//      case ')':
//        cnt++;
//        break;
//      }
//    }
//    
//    return cnt;
//  }
}




Java Source Code List

org.x3n0m0rph59.breakout.App.java
org.x3n0m0rph59.breakout.BackgroundFactory.java
org.x3n0m0rph59.breakout.Background.java
org.x3n0m0rph59.breakout.Ball.java
org.x3n0m0rph59.breakout.BottomWall.java
org.x3n0m0rph59.breakout.Brick.java
org.x3n0m0rph59.breakout.Config.java
org.x3n0m0rph59.breakout.Destroyable.java
org.x3n0m0rph59.breakout.EffectManager.java
org.x3n0m0rph59.breakout.Effect.java
org.x3n0m0rph59.breakout.FontLoader.java
org.x3n0m0rph59.breakout.FontTuple.java
org.x3n0m0rph59.breakout.ForceFeedback.java
org.x3n0m0rph59.breakout.GameInputProcessor.java
org.x3n0m0rph59.breakout.GameObject.java
org.x3n0m0rph59.breakout.GameScreen.java
org.x3n0m0rph59.breakout.GameState.java
org.x3n0m0rph59.breakout.GrapplingHookSegment.java
org.x3n0m0rph59.breakout.GrapplingHook.java
org.x3n0m0rph59.breakout.HelpInputProcessor.java
org.x3n0m0rph59.breakout.HelpScreen.java
org.x3n0m0rph59.breakout.HighScoreManager.java
org.x3n0m0rph59.breakout.HighScoreScreen.java
org.x3n0m0rph59.breakout.HighScore.java
org.x3n0m0rph59.breakout.HighscoreInputProcessor.java
org.x3n0m0rph59.breakout.IOSLauncher.java
org.x3n0m0rph59.breakout.LevelLoader.java
org.x3n0m0rph59.breakout.LevelSetSelectorInputProcessor.java
org.x3n0m0rph59.breakout.LevelSetSelectorScreen.java
org.x3n0m0rph59.breakout.Logger.java
org.x3n0m0rph59.breakout.MenuInputProcessor.java
org.x3n0m0rph59.breakout.MenuScreen.java
org.x3n0m0rph59.breakout.MusicLoader.java
org.x3n0m0rph59.breakout.MusicStream.java
org.x3n0m0rph59.breakout.ObjectPool.java
org.x3n0m0rph59.breakout.Paddle.java
org.x3n0m0rph59.breakout.ParticleSystem.java
org.x3n0m0rph59.breakout.Particle.java
org.x3n0m0rph59.breakout.Point.java
org.x3n0m0rph59.breakout.Poolable.java
org.x3n0m0rph59.breakout.Powerup.java
org.x3n0m0rph59.breakout.Projectile.java
org.x3n0m0rph59.breakout.Renderable.java
org.x3n0m0rph59.breakout.ResourceMapperDecisionCache.java
org.x3n0m0rph59.breakout.ResourceMapper.java
org.x3n0m0rph59.breakout.ScoreBoard.java
org.x3n0m0rph59.breakout.ScreenManager.java
org.x3n0m0rph59.breakout.ScreenType.java
org.x3n0m0rph59.breakout.SettingsInputProcessor.java
org.x3n0m0rph59.breakout.SettingsScreen.java
org.x3n0m0rph59.breakout.SoundLayer.java
org.x3n0m0rph59.breakout.SoundLoader.java
org.x3n0m0rph59.breakout.SoundSprite.java
org.x3n0m0rph59.breakout.SpaceBomb.java
org.x3n0m0rph59.breakout.SpriteLoader.java
org.x3n0m0rph59.breakout.SpriteObject.java
org.x3n0m0rph59.breakout.SpriteTuple.java
org.x3n0m0rph59.breakout.Star.java
org.x3n0m0rph59.breakout.Stepable.java
org.x3n0m0rph59.breakout.TextAnimationManager.java
org.x3n0m0rph59.breakout.TextAnimation.java
org.x3n0m0rph59.breakout.Util.java
org.x3n0m0rph59.breakout.Vector.java
org.x3n0m0rph59.breakout.android.AndroidLauncher.java
org.x3n0m0rph59.breakout.client.HtmlLauncher.java
org.x3n0m0rph59.breakout.desktop.DesktopLauncher.java