Sprite.java

From Polish Civil, 3 Years ago, written in Java, viewed 58 times.
URL https://paste.bugabuse.net/view/44662640 Embed
Download Paste or View Raw
  1. package com.Aeon.common.sprite;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.util.HashMap;
  6.  
  7. public final class Sprite implements Cloneable {
  8.        
  9.         public static enum LoopMethod {
  10.                 NONE,
  11.                 ONCE,
  12.                 FORWARD,
  13.                 REVERSE;
  14.         }
  15.        
  16.         private LoopMethod loopMethod = LoopMethod.FORWARD;
  17.         private boolean loopDir = true;
  18.         private final HashMap<String, int[]> animations = new HashMap<String, int[]>();
  19.         private final Frame[] frames;
  20.         private String currentAnim;
  21.         private int currentFrame;
  22.         private int[] currentFrameSet;
  23.         private final int assetID;
  24.         private int cycle = 0;
  25.         private int endActionTime;
  26.         private boolean runEndAction;
  27.         private Runnable endAction;
  28.        
  29.         /**
  30.          * Initializes a new sprite.
  31.          * @param assetID The asset ID.
  32.          * @param frames The frame offsets data.
  33.          */    
  34.         public Sprite(final int assetID, final Frame[] frames){
  35.                 this.assetID = assetID;
  36.                 this.frames = frames;
  37.         }
  38.        
  39.         /**
  40.          * Adds a new animation.
  41.          * @param name The animation name.
  42.          * @param set The frame order set.
  43.          */
  44.         public void addNewAnimation(final String name, final int[] set){
  45.                 animations.put(name, set);
  46.                 setAnimation(name);
  47.         }
  48.  
  49.         /**
  50.          * Sets the current frame.
  51.          */
  52.         public void setCurrentFrame(final int i){
  53.                 currentFrame = i;
  54.         }
  55.  
  56.         /**
  57.          * Draws the sprite.
  58.          * @param x The x.
  59.          * @param y The y.
  60.          * @param g The graphics object.
  61.          */
  62.         public void draw(final int x, final int y, final Graphics g){
  63.                
  64.                 final int imgNum = currentFrameSet[currentFrame];
  65.  
  66.                 final int xOffset = frames[imgNum].getOffsetX();
  67.                 final int yOffset = frames[imgNum].getOffsetY();
  68.  
  69.                 g.drawImage(frames[imgNum].getImage(), x+xOffset, y+yOffset, null);
  70.                 if ((this.runEndAction) && (this.endActionTime-- <= 0)) {
  71.                         this.runEndAction = false;
  72.                         this.endAction.run();
  73.                         this.endAction = null;
  74.                 }
  75.                 if(loopMethod == LoopMethod.NONE) return;
  76.                 if(!((cycle++ % 4) == 0)) return;
  77.  
  78.                 if(currentFrame == currentFrameSet.length - 1){
  79.                         if(loopMethod == LoopMethod.FORWARD){
  80.                                 currentFrame = 0;
  81.                         } else if(loopMethod == LoopMethod.ONCE){
  82.                                 loopMethod = LoopMethod.NONE;
  83.                                 if (this.endAction != null)
  84.                                         this.runEndAction = true;
  85.                         } else {
  86.                                 loopDir = false;
  87.                                 currentFrame--;
  88.                         }
  89.                 } else {
  90.                         if(loopMethod == LoopMethod.FORWARD || loopMethod == LoopMethod.ONCE){
  91.                                 currentFrame++;
  92.                         } else {
  93.                                 if(currentFrame == 0){
  94.                                         loopDir = true;
  95.                                 }
  96.                                 if(loopDir){
  97.                                         currentFrame++;
  98.                                 }
  99.                                 if(!loopDir){
  100.                                         currentFrame--;
  101.                                 }
  102.                         }
  103.                 }
  104.         }
  105.        
  106.         public void drawStill(int x, int y, Graphics graphics){
  107.                 final int imgNum = currentFrameSet[currentFrame];
  108.                 final Image img = frames[imgNum].getImage();
  109.                 final int w = img.getWidth(null);
  110.                 final int h = img.getHeight(null);
  111.                 final float n = 32f/((w>h)? w:h);
  112.                 final int nw = (int)(n*w);
  113.                 final int nh = (int)(n*h);
  114.                
  115.                 graphics.drawImage(frames[imgNum].getImage(), x+(20-(nw/2)), y+(20-(nh/2)), nw, nh, null);     
  116.         }
  117.        
  118.         public void drawStill(int x, int y, int size, Graphics graphics) {
  119.                 int imgNum = this.currentFrameSet[this.currentFrame];
  120.                 Image img = this.frames[imgNum].getImage();
  121.                 int w = img.getWidth(null);
  122.                 int h = img.getHeight(null);
  123.                 float n = (size - 8) / (w > h ? w : h);
  124.                 int nw = (int) (n * w);
  125.                 int nh = (int) (n * h);
  126.  
  127.                 graphics.drawImage(this.frames[imgNum].getImage(), x
  128.                                 + (size / 2 - nw / 2), y + (size / 2 - nh / 2), nw, nh, null);
  129.         }
  130.  
  131.         /**
  132.          * Gets the image object of the specified frame.
  133.          * @param i The frame number.
  134.          * @return The image of the frame number.
  135.          */
  136.         public Image getFrameImage(final int i){
  137.                 return frames[currentFrameSet[i]].getImage();
  138.         }
  139.  
  140.         /**
  141.          * Sets the animation.
  142.          * @param name The animation name.
  143.          */
  144.         public void setAnimation(final String name){
  145.                 if(animations.containsKey(name)){
  146.                         currentAnim = name;
  147.                         currentFrameSet = animations.get(currentAnim);
  148.                         currentFrame = 0;
  149.                 }
  150.         }
  151.        
  152.         /**
  153.          * Sets the loop method.
  154.          * @param loopMethod The method to loop by.
  155.          */
  156.         public void setLoopMethod(final LoopMethod loopMethod){
  157.                 this.loopMethod = loopMethod;
  158.         }
  159.  
  160.         /**
  161.          * Gets the current animation.
  162.          */
  163.         public String getCurrentAnim(){
  164.                 return currentAnim;
  165.         }
  166.  
  167.         /**
  168.          * Gets the current frame.
  169.          */
  170.         public int getCurrentFrame(){
  171.                 return currentFrame;
  172.         }
  173.  
  174.         /**
  175.          * {@inheritDoc}
  176.          */
  177.         @Override
  178.         public Object clone(){
  179.                 try {
  180.                         return super.clone();
  181.                 } catch(final Exception e){
  182.                         return null;
  183.                 }
  184.         }
  185.  
  186.         /**
  187.          * Gets the asset ID.
  188.          */
  189.         public int getAssetID(){
  190.                 return assetID;
  191.         }
  192.  
  193.         public Frame[] getFrames(){
  194.                 return frames;
  195.         }
  196.         public void setEndAction(Runnable endAction) {
  197.                 this.endAction = endAction;
  198.         }
  199.         public void setEndActionTime(int endActionTime) {
  200.                 this.endActionTime = endActionTime;
  201.         }
  202.  
  203. }

Reply to "Sprite.java"

Here you can reply to the paste above