Package model_pkg

Source Code of model_pkg.GfxState

package model_pkg;

import java.awt.Image;
import display_pkg.SpriteSheet;
import def_classes.Dimensions;

/**
* A graphics state holds a sprite-sheet reference and an animation index.
* There's also a set of methods that handles the update process of a specific animation.
* @author Johansson M.
*/
public class GfxState {
 
  private  SpriteSheet  sprites;      //
  private int     animationIndex;    //
  private int     frameIndex;      //
  private int     currentTimeLeft;  //
 
  /**
   * Creates a GfxState object from a SpriteSheet and an animation index.
   * @param sprSheet The spriteSheet containing the graphics information
   * @param animIndex The row index in the spriteSheet to start at
   */
  public GfxState(SpriteSheet sprSheet, int animIndex) {
    sprites = sprSheet;
   
    animationIndex = animIndex;
    frameIndex = 0;
    currentTimeLeft = 0;
    updateCurrentTimeLeft();
  }
 
  /**
   * If the current frame has been displayed for it's whole update time,
   * the frame index is increased by one and looped if at the end of the animation.
   * The update time is measured in game ticks.
   * This should be called once a frame.
   */
  public void updateAnim() {
    currentTimeLeft -= 1;
    if (currentTimeLeft <= 0) {
      frameIndex += 1;
      if (frameIndex >= sprites.getAnimationLength(animationIndex)) {
        frameIndex = 0;
      }
      updateCurrentTimeLeft();
    }
  }
 
  /**
   * Gets the currently used image
   * @return The currently used image
   */
  public Image getImage() {
    return sprites.getImage(animationIndex, frameIndex);
  }
 
  /**
   * Gets the dimensions of the currently used image
   * @return The dimensions of the currently used image
   */
  public Dimensions getCurrentImageDimensions() {
    return new Dimensions(
        sprites.getImage(animationIndex, frameIndex).getWidth(null),
        sprites.getImage(animationIndex, frameIndex).getHeight(null));
  }
 
  /**
   * Updates the current time left for the new frame
   */
  private void updateCurrentTimeLeft() {
    /*
     * We add the new frames duration to manage
     * real-value durations.
     */
    currentTimeLeft += sprites.getSpriteDuration(animationIndex, frameIndex);
  }
 
  /**
   * Sets a new animation index
   * @param animIdx The new animation index
   */
  public void setAnimationIndex(int animIdx) {
    /*
     * It also resets the frame index to zero to avoid
     * null pointer exceptions.
     */
    if (animationIndex != animIdx) {
      if (animIdx < sprites.getNumberOfAnimations()) {
        animationIndex = animIdx;
        frameIndex = 0;
        currentTimeLeft = 0;
        updateCurrentTimeLeft();
      }
    }
  }
}
TOP

Related Classes of model_pkg.GfxState

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.