Android Open Source - LucyTheMoocher Level Loader






From Project

Back to project page LucyTheMoocher.

License

The source code is released under:

MIT License

If you think the Android project LucyTheMoocher 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 com.lucythemoocher.game;
/*from ww w.  j  av a2s. c o  m*/
import java.io.InputStream;
import java.util.Scanner;


import android.util.Log;

import com.lucythemoocher.R;
import com.lucythemoocher.actors.MonstersManager;
import com.lucythemoocher.actors.PlayerCharacter;
import com.lucythemoocher.actors.Tank;
import com.lucythemoocher.actors.TargetCharacter;
import com.lucythemoocher.controls.AIController;
import com.lucythemoocher.controls.ActionController;
import com.lucythemoocher.graphics.Grid;
import com.lucythemoocher.util.Direction;
import com.lucythemoocher.util.Resources;

public class LevelLoader {
  /**
   * Constructor
   * Generate a map according to the raw file
   * It is supposed that the thickness is more than 1
   * @param mapName
   */
  public LevelLoader(int mapName) {
    InputStream inputStream = Resources.openRawRessources(mapName);
    Scanner s = new Scanner(inputStream);
    
    // temporary grid to have box dimensions
    Grid grid = new Grid(R.drawable.sets);
    
    monsters_ = new MonstersManager();
    
    //dimensions
    String cur = s.next();
    h_ = Integer.parseInt(cur) + 4;
    cur = s.next();
    w_ = Integer.parseInt(cur) + 4;
    
    //load map
    map_ = new int[h_][w_];
    //set contour
    for (int i=0; i<h_; i++) {
      map_[i][0] = 1;
      map_[i][1] = 1;
      map_[i][w_-2] = 1;
      map_[i][w_-1] = 1;
    }
    for (int j=0; j<w_; j++) {
      map_[0][j] = 1;
      map_[1][j] = 1;
      map_[h_-2][j] = 1;
      map_[h_-1][j] = 1;
    }
    //set level
    for (int i=2; i<h_-2; i++) {
      for (int j=2; j<w_-2; j++) {
        cur = s.next();
        char curChar = cur.charAt(0);
        if (Character.isDigit(curChar)) {
          map_[i][j] = Integer.parseInt(cur);
        } else {
          switch (curChar) {
          case 'L':
            character_ = new PlayerCharacter(new ActionController(), j * grid.boxW(), i * grid.boxH());
            break;
          case 'F':
            target_ = new TargetCharacter(new AIController(), j * grid.boxW(), i * grid.boxH());
            break;
          case 'T':
            monsters_.addMonster(new Tank(j * grid.boxW(), i * grid.boxH(), Direction.LEFT));
            break;
          default:
            Log.w("LevelLoader", "Bad code: "+curChar+" skipping...");
            break;
          }
        }
      }
    }
    
    //chose good pic !
    //note: it is supposed that thickness is more than 1
    for (int i=0; i<h_; i++) {
      for (int j=0; j<w_; j++) {
        if ( map_[i][j] != 0 ) {
          int pic = 0;
          if ( i==0 ) { //top
            if ( j==0 ) { //left corner
              pic = 1;
            } else if ( j==w_-1 ) { //right corner
              pic = 3;
            } else { //middle
              if ( map_[i][j-1] != 0 && map_[i][j+1] != 0 ) {
                pic = 2;
              } else if ( map_[i][j-1] != 0 ) {
                pic = 3;
              } else if ( map_[i][j+1] != 0 ) {
                pic = 1;
              }
            }
          } else if ( i==h_-1 ) { //bottom
            if ( j==0 ) { //left corner
              pic = 7;
            } else if ( j==w_-1 ) { //right corner
              pic = 9;
            } else { //middle
              if ( map_[i][j-1] != 0 && map_[i][j+1] != 0 ) {
                pic = 8;
              } else if ( map_[i][j-1] != 0 ) {
                pic = 9;
              } else if ( map_[i][j+1] != 0 ) {
                pic = 7;
              }
            }
          } else { //middle
            if ( j==0 ) { //left colomn
              if ( map_[i-1][j] != 0 && map_[i+1][j] != 0 ) {
                pic = 4;
              } else if ( map_[i-1][j] != 0 ) {
                pic = 7;
              } else if ( map_[i+1][j] != 0) {
                pic = 1;
              }
            } else if ( j==w_-1 ) { //right colomn
              if ( map_[i-1][j] != 0 && map_[i+1][j] != 0 ) {
                pic = 6;
              } else if ( map_[i-1][j] != 0 ) {
                pic = 9;
              } else if ( map_[i+1][j] != 0) {
                pic = 3;
              }
            } else { //middle
              if ( map_[i-1][j] != 0 && map_[i+1][j] != 0 &&
                  map_[i][j-1] != 0 && map_[i][j+1] != 0 ) { //if there is a hole
                if ( map_[i-1][j-1] == 0 ) {
                  pic = 18;
                } else if ( map_[i-1][j+1] == 0 ) {
                  pic = 16;
                } else if ( map_[i+1][j-1] == 0 ) {
                  pic = 12;
                } else if ( map_[i+1][j+1] == 0 ) {
                  pic = 10;
                } else {
                  pic = 5;
                }
              } else if ( map_[i-1][j] != 0 && map_[i+1][j] != 0 &&
                  map_[i][j-1] != 0 ) {
                pic = 6;
              } else if ( map_[i-1][j] != 0 && map_[i+1][j] != 0 &&
                  map_[i][j+1] != 0 ) {
                pic = 4;
              } else if ( map_[i-1][j] != 0 &&
                  map_[i][j-1] != 0 && map_[i][j+1] != 0 ) {
                pic = 8;
              } else if ( map_[i+1][j] != 0 &&
                  map_[i][j-1] != 0 && map_[i][j+1] != 0 ) {
                pic = 2;
              } else if ( map_[i-1][j] != 0 && map_[i][j-1] != 0 ) {
                pic = 9;
              } else if ( map_[i][j-1] != 0 && map_[i+1][j] != 0 ) {
                pic = 3;
              } else if ( map_[i+1][j] != 0 && map_[i][j+1] != 0 ) {
                pic = 1;
              } else if ( map_[i-1][j] != 0 && map_[i][j+1] != 0 ) {
                pic = 7;
              }
            }
          }
          map_[i][j] = pic;
        }
      }
    }
    
  }
  
  public int getH() {
    return h_;
  }
  
  public int getW() {
    return w_;
  }
  
  public int[][] getMap() {
    return map_;
  }
  
  public PlayerCharacter getCharacter() {
    return character_;
  }
  
  public TargetCharacter getTarget() {
    return target_;
  }
  
  public MonstersManager getMonsters() {
    return monsters_;
  }
  
  private int h_;
  private int w_;
  private int[][] map_;
  private PlayerCharacter character_;
  private TargetCharacter target_;
  private MonstersManager monsters_;
}




Java Source Code List

com.lucythemoocher.LucyTheMoocherActivity.java
com.lucythemoocher.FX.FXManager.java
com.lucythemoocher.FX.FX.java
com.lucythemoocher.Globals.Globals.java
com.lucythemoocher.actors.Actor.java
com.lucythemoocher.actors.ActorsManager.java
com.lucythemoocher.actors.Monster.java
com.lucythemoocher.actors.MonstersManager.java
com.lucythemoocher.actors.PlayerCharacter.java
com.lucythemoocher.actors.Projectile.java
com.lucythemoocher.actors.ProjectilesManager.java
com.lucythemoocher.actors.Tank.java
com.lucythemoocher.actors.TargetCharacter.java
com.lucythemoocher.actors.maincharacter.state.StateAttack.java
com.lucythemoocher.actors.maincharacter.state.StateFalling.java
com.lucythemoocher.actors.maincharacter.state.StateJumping.java
com.lucythemoocher.actors.maincharacter.state.StateNone.java
com.lucythemoocher.actors.maincharacter.state.StateRunning.java
com.lucythemoocher.actors.maincharacter.state.StateWallSliding.java
com.lucythemoocher.actors.maincharacter.state.StateWallWalking.java
com.lucythemoocher.actors.maincharacter.state.State.java
com.lucythemoocher.controls.AIController.java
com.lucythemoocher.controls.ActionController.java
com.lucythemoocher.controls.ButtonListener.java
com.lucythemoocher.controls.Controllable.java
com.lucythemoocher.controls.Controller.java
com.lucythemoocher.controls.GlobalController.java
com.lucythemoocher.controls.KeysListener.java
com.lucythemoocher.controls.TouchListener.java
com.lucythemoocher.events.EventNormal.java
com.lucythemoocher.events.EventSlow.java
com.lucythemoocher.events.Event.java
com.lucythemoocher.game.GameThread.java
com.lucythemoocher.game.Game.java
com.lucythemoocher.game.LevelLoader.java
com.lucythemoocher.graphics.ActorDrawer.java
com.lucythemoocher.graphics.Animation.java
com.lucythemoocher.graphics.Background.java
com.lucythemoocher.graphics.Camera.java
com.lucythemoocher.graphics.Drawable.java
com.lucythemoocher.graphics.Grid.java
com.lucythemoocher.graphics.HUD.java
com.lucythemoocher.graphics.Image.java
com.lucythemoocher.graphics.PersistentEffect.java
com.lucythemoocher.graphics.PersistentPic.java
com.lucythemoocher.graphics.PictureContainer.java
com.lucythemoocher.gui.MenuButtonListener.java
com.lucythemoocher.gui.MenuButtonTouchListener.java
com.lucythemoocher.gui.MenuButton.java
com.lucythemoocher.loops.CreditsLoop.java
com.lucythemoocher.loops.GameOverLoop.java
com.lucythemoocher.loops.InitMenuLoop.java
com.lucythemoocher.loops.LivesMenuLoop.java
com.lucythemoocher.loops.LoopGame.java
com.lucythemoocher.loops.LoopPause.java
com.lucythemoocher.loops.Loop.java
com.lucythemoocher.loops.MasterLoop.java
com.lucythemoocher.physics.Box.java
com.lucythemoocher.physics.Cinematic.java
com.lucythemoocher.physics.Map.java
com.lucythemoocher.sounds.SoundManager.java
com.lucythemoocher.sounds.SoundsState.java
com.lucythemoocher.sounds.StateLevel1.java
com.lucythemoocher.sounds.StateLevel2.java
com.lucythemoocher.sounds.StateLevel3.java
com.lucythemoocher.sounds.StateNormal.java
com.lucythemoocher.util.Direction.java
com.lucythemoocher.util.MathUtil.java
com.lucythemoocher.util.Resources.java
com.lucythemoocher.util.Timer.java