Android Open Source - LucyTheMoocher Background






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.graphics;
/*from ww w  . ja  v a 2  s.c  o m*/
import com.lucythemoocher.R;
import com.lucythemoocher.Globals.Globals;
import com.lucythemoocher.util.MathUtil;

/**
 * Background of the level\n
 * Randomly generated with elementary blocks.\n
 * The sizes of images (in the grids) must be multiples.
 * Can be rendered by the Camera
 * @see Camera#drawBackground(Background)
 *
 */
public class Background implements Drawable {

  private Grid backgroundUp_;
  private Grid backgroundDown_;
  private Grid backgroundMiddle_;
  private int[][] mapping_;
  private float pxH_;
  private float pxW_;
  private int nbBoxH_;
  private int nbBoxW_;
  
  /**
   * Constructor
   * @todo 
   */
  public Background() {
    backgroundUp_ = new Grid(R.drawable.background_up);
    backgroundDown_ = new Grid(R.drawable.background_down);
    backgroundMiddle_ = new Grid(R.drawable.background_middle);
    pxH_ = Globals.getInstance().getGame().getMap().pxH()*Camera.BACKGROUNDSPEED + 2*Globals.getInstance().getCamera().h();
    pxW_ = Globals.getInstance().getGame().getMap().pxW()*Camera.BACKGROUNDSPEED + 2*Globals.getInstance().getCamera().w();
    nbBoxH_ = (int) (pxH_/backgroundDown_.boxH());
    nbBoxW_ = (int) (pxW_/backgroundDown_.boxW());
    mapping_ = new int[nbBoxH_][nbBoxW_];
    for (int i=0; i<nbBoxH_; i++) {
      for (int j=0; j<nbBoxW_; j++) {
        if ( i == pxH_/2 ) {
          mapping_[i][j] = MathUtil.uniform(0, backgroundMiddle_.getSize()-1);
        } else if ( i < pxH_/2 ) {
          mapping_[i][j] = MathUtil.uniform(0, backgroundUp_.getSize()-1);
        } else {
          mapping_[i][j] = MathUtil.uniform(0, backgroundDown_.getSize()-1);
        }
      }
    }
  }
  
  /**
   * Draw the Background properly in the Camera
   * (according to the Camera's position)
   * @see Camera
   */
  public void draw() {
    Globals.getInstance().getCamera().drawBackground(this);
  }
  
  /**
   * Action asked by the camera
   * @param x position of the screen in function of the background
   * @param y position of the screen in function of the background
   * @see Camera
   */
  void draw(float x, float y) {
    int i = (int) (y/backgroundUp_.boxW());
    int j = (int) (x/backgroundUp_.boxH());
    int h = (int) (Globals.getInstance().getCamera().h()/backgroundUp_.boxH())+2;
    int w = (int) (Globals.getInstance().getCamera().w()/backgroundUp_.boxW())+2;
    float offsetX = x-j*backgroundUp_.boxH();
    float offsetY = y-i*backgroundUp_.boxW();
    for ( int ii=i; ii<i+h && ii<nbBoxH_ ; ii++) {
      if ( ii == nbBoxH_/2 ) {
        if ( j%(backgroundMiddle_.boxW()/backgroundUp_.boxW()) != 0 ) {
          Globals.getInstance().getCamera().drawBackground(
              getImage(ii,
                  (int) (j-j%(backgroundMiddle_.boxW()/backgroundUp_.boxW()))),
                  (-j%(backgroundMiddle_.boxW()/backgroundUp_.boxW()))*backgroundUp_.boxW()-offsetX, 
                  (ii-i)*backgroundUp_.boxH()-offsetY);
        }
      }
      for ( int jj=j; jj<j+w && jj<nbBoxW_ ; jj++ ) {
        if ( ii == nbBoxH_/2 ) {
          if ( jj%(backgroundMiddle_.boxW()/backgroundUp_.boxW()) == 0 ) {
            Globals.getInstance().getCamera().drawBackground(
                getImage(ii,jj), (jj-j)*backgroundUp_.boxW()-offsetX, (ii-i)*backgroundUp_.boxH()-offsetY);
          }
        } else {
          Globals.getInstance().getCamera().drawBackground(
              getImage(ii,jj), (jj-j)*backgroundUp_.boxW()-offsetX, (ii-i)*backgroundUp_.boxH()-offsetY);
        }
      }
    }
  }
  
  private Image getImage(int i, int j) {
    if ( i == nbBoxH_/2 ) {
      return backgroundMiddle_.getImage(mapping_[i][j]);
    } else if ( i < nbBoxH_/2 ) {
      return backgroundUp_.getImage(mapping_[i][j]);
    } else {
      return backgroundDown_.getImage(mapping_[i][j]);
    }
  }
}




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