Android Open Source - LucyTheMoocher Animation






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 .java 2  s . c om*/
import com.lucythemoocher.Globals.Globals;

public class Animation {
  private Grid grid_;
  private int tab_[];
  private float period_;
  private int currentFrame_; // we print grid[tab_[currentFrame_]]
  private float timeOnLastDraw_; 
  private float offsetCurrentFrame_; // will be added to currentFrame_ when >= 1
  private boolean cycleEnded_ = false;

  private static final float DEFAULT_PERIOD = 100;

  public Animation() {
    grid_ = null;
    offsetCurrentFrame_ = 0.0f;
  }

  /**
   * Constructor
   * @param resource Resource index
   * @see Grid
   */
  public Animation(int resource) {
    this();
    initialize(resource);
  }

  /**
   * Constructor
   * @param resource Resource index
   * @param allImages : set or not all the images 
   * @see Grid
   */
  public Animation(int resource, boolean allImages) {
    this(resource);
    if ( allImages ) {
      int t[] = new int[grid_.getSize()];
      for ( int i=0; i<t.length; i++ ) {
        t[i] = i;
      }
      setAnimation(t, DEFAULT_PERIOD);
    }
  }

  /**
   * Initialize the animation 
   * @param resource Resource index
   * @see Grid
   */  
  public void initialize(int resource) {
    grid_ = new Grid(resource);
    int t[] = {0};
    setAnimation(t, 1);
    timeOnLastDraw_ = Globals.getInstance().getGame().getTime();
  }

  /**
   * 
   * @param tab Indices of the pictures the the animation grid
   * @param period Animation's period in ms
   */
  public void setAnimation(int tab[], float period) {
    tab_ = new int[tab.length];
    for (int i=0; i<tab.length; i++) {
      tab_[i] = tab[i];
    }
    period_ = period;
    currentFrame_ = 0;
  }

  /**
   * Getter
   * @return Pictures' height in pixels
   */
  public float getH() {
    return grid_.getImage(0).h();
  }

  /**
   * Getter
   * @return Pictures' width in pixels
   */
  public float getW() {
    return grid_.getImage(0).w();
  }

  /**
   * Draw the animation at the position x, y
   * @param x
   * @param y 
   */
  public void draw(float x, float y) {
    Globals.getInstance().getCamera().drawImage(x, y, getCurrentImage());
  }

  /**
   * Getter
   * @return The current image
   */
  public Image getCurrentImage() {
    return grid_.getImage(tab_[currentFrame_]);
  }

  /**
   * Update the animation, must be called at least once a frame
   */
  public void update() {
    offsetCurrentFrame_ += (Globals.getInstance().getGame().getTime() - timeOnLastDraw_) / period_;
    currentFrame_ += (int) (offsetCurrentFrame_);
    if ( currentFrame_ == tab_.length ) {
      cycleEnded_ = true;
    } else {
      cycleEnded_ = false;
    }
    currentFrame_ %= tab_.length;
    offsetCurrentFrame_ -= Math.floor((double)(offsetCurrentFrame_));
    timeOnLastDraw_ = Globals.getInstance().getGame().getTime();
  }

  /**
   * True when the first image of the animation will be displayed in the coming cycle.
   * @return if the this is the end of the cycle of the animation
   */
  public boolean cycleEnded() {
    return cycleEnded_;
  }

}




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