Android Open Source - block-composer Static Level Store






From Project

Back to project page block-composer.

License

The source code is released under:

MIT License

If you think the Android project block-composer 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 bwr.blockcomposer.gamedata;
/*  w ww  .ja  v a  2 s  .  com*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import bwr.blockcomposer.GameResources;

import android.content.Context;
import android.util.SparseArray;

public class StaticLevelStore implements LevelStore {
  private final LevelMetadata[] levelData;
  private final SparseArray<Level> levels = new SparseArray<Level>();
  private final SparseArray<GameState> gameStates = new SparseArray<GameState>();
  
  private int currentLevel = 0;
  private int nextLevel, prevLevel;
  
  private final GameResources gameResources;
  
  public StaticLevelStore(GameResources gameResources, LevelMetadata[] levelData) {
    this.gameResources = gameResources;
    this.levelData = levelData;
    loadLevels();
    updatePrevAndNext();
  }
  
  private void loadLevels() {
    for(LevelMetadata lm : levelData) {
      try {
        Level level = loadLevel(lm.resourceId);
        
        level.version = lm.version; // TODO: Level Version should be inside the level file
        
        level.setAuthor(lm.author); // TODO: Level Author should be inside the level file
        
        level.setName(lm.name);
        
        levels.append(lm.resourceId, level);
        
        GameState state = null;
        
        if(lm.stateFilename != null) {
          try {
            FileInputStream fin = gameResources.getContext().openFileInput(lm.stateFilename);
            
            DataInputStream in = new DataInputStream(new BufferedInputStream(fin));
            
            state = new GameState(level, in);
            fin.close();
  
  //          if(state.getVersion() != l.version) {
  //            Log.i("BlockComposer", "Unable to restore level '" + l.name + "' due to mismatch in version.");
  //            deleteFile(l.stateFilename);
  //          } else {
  //            m.restoreState(state);
  //          }
            
          } catch (FileNotFoundException e) {
            state = new GameState(level);
          }
        } else {
          state = new GameState(level);
        }
        
        state.exitText = lm.exitText;
        state.entryText = lm.entryText;
        
        gameStates.append(lm.resourceId, state);
        
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  
  private Level loadLevel(int resourceId) throws IOException {
    InputStream is = gameResources.getContext().getResources().openRawResource(resourceId);
    Level result = Level.loadFromFile(is, gameResources);
    is.close();
    return result;
  }

  public GameState getCurrent() {
    return gameStates.get(levelData[currentLevel].resourceId);
  }

  public GameState getPrevious() {
    return gameStates.get(levelData[prevLevel].resourceId);
  }

  public GameState getNext() {
    return gameStates.get(levelData[nextLevel].resourceId);
  }
  
  public File getCurrentFile() {
    return levelData[currentLevel].levelFile;
  }
  
  private void updatePrevAndNext() {
    nextLevel = (currentLevel + 1) % levelData.length;
    prevLevel = currentLevel - 1;
    if(prevLevel < 0) prevLevel = levelData.length-1;
  }

  public void moveForward() {
    currentLevel++;
    currentLevel %= levelData.length;
    updatePrevAndNext();
  }

  public void moveBack() {
    currentLevel--;
    if(currentLevel < 0) currentLevel = levelData.length-1;
    updatePrevAndNext();
  }

  public boolean isStoreEmpty() {
    return levelData.length == 0;
  }

  public boolean hasOnlyOneLevel() {
    return levelData.length == 1;
  }

  public void reset() {
    // nothing
  }

  private void saveLevelState(Level level, String stateFilename, GameState state) throws IOException {
    final Context context = gameResources.getContext();
    
    final FileOutputStream fout = context.openFileOutput(stateFilename, Context.MODE_PRIVATE);
    final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fout));

    state.setVersion(level.version);
    state.writeTo(out);
          
    out.flush();
    fout.close();
  }
  
  public void saveLevelStates() {
    for(LevelMetadata levelMetadata : levelData) {
      if(levelMetadata.stateFilename == null) continue;
      
      GameState state = gameStates.get(levelMetadata.resourceId);
      
      if(state != null && state.isDirty()) {
        Level level = state.getLevel();
        
        if(level != null) {
          try {
            saveLevelState(level, levelMetadata.stateFilename, state);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        
      }
      
    }
  }
}




Java Source Code List

bwr.blockcomposer.AnimationFinishedListener.java
bwr.blockcomposer.BlockComposerActivity.java
bwr.blockcomposer.BlockComposerRenderer.java
bwr.blockcomposer.BlockComposer.java
bwr.blockcomposer.Camera.java
bwr.blockcomposer.GameResources.java
bwr.blockcomposer.OnUpdateAction.java
bwr.blockcomposer.gamedata.Block.java
bwr.blockcomposer.gamedata.GameEntity.java
bwr.blockcomposer.gamedata.GameState.java
bwr.blockcomposer.gamedata.LevelDimensions.java
bwr.blockcomposer.gamedata.LevelMask.java
bwr.blockcomposer.gamedata.LevelMetadata.java
bwr.blockcomposer.gamedata.LevelStore.java
bwr.blockcomposer.gamedata.Level.java
bwr.blockcomposer.gamedata.Move.java
bwr.blockcomposer.gamedata.StaticLevelStore.java
bwr.blockcomposer.gamedata.UserLevelStore.java
bwr.blockcomposer.misc.GLUtility.java
bwr.blockcomposer.misc.Timer.java
bwr.blockcomposer.models.AxisRenderer.java
bwr.blockcomposer.models.Model.java
bwr.blockcomposer.models.SelectBoxRenderer.java
bwr.blockcomposer.modes.EditMode.java
bwr.blockcomposer.modes.GameDialogMode.java
bwr.blockcomposer.modes.GameMode.java
bwr.blockcomposer.modes.LevelImportMode.java
bwr.blockcomposer.modes.LevelSelectMode.java
bwr.blockcomposer.modes.LogoDisplayMode.java
bwr.blockcomposer.modes.ModeController.java
bwr.blockcomposer.modes.Mode.java
bwr.blockcomposer.modes.OverlayMode.java
bwr.blockcomposer.types.FlipRotateFloat.java
bwr.blockcomposer.types.FloatValueVector3.java
bwr.blockcomposer.types.FloatValue.java
bwr.blockcomposer.types.IntVector.java
bwr.blockcomposer.types.RotatingFloat.java
bwr.blockcomposer.ui.TextRenderer.java
bwr.blockcomposer.ui.UIElementPicture.java
bwr.blockcomposer.ui.UIElementText.java
bwr.blockcomposer.ui.UIElement.java