package com.Aeon.common.sprite; import java.awt.Graphics; import java.awt.Image; import java.util.HashMap; public final class Sprite implements Cloneable { public static enum LoopMethod { NONE, ONCE, FORWARD, REVERSE; } private LoopMethod loopMethod = LoopMethod.FORWARD; private boolean loopDir = true; private final HashMap animations = new HashMap(); private final Frame[] frames; private String currentAnim; private int currentFrame; private int[] currentFrameSet; private final int assetID; private int cycle = 0; private int endActionTime; private boolean runEndAction; private Runnable endAction; /** * Initializes a new sprite. * @param assetID The asset ID. * @param frames The frame offsets data. */ public Sprite(final int assetID, final Frame[] frames){ this.assetID = assetID; this.frames = frames; } /** * Adds a new animation. * @param name The animation name. * @param set The frame order set. */ public void addNewAnimation(final String name, final int[] set){ animations.put(name, set); setAnimation(name); } /** * Sets the current frame. */ public void setCurrentFrame(final int i){ currentFrame = i; } /** * Draws the sprite. * @param x The x. * @param y The y. * @param g The graphics object. */ public void draw(final int x, final int y, final Graphics g){ final int imgNum = currentFrameSet[currentFrame]; final int xOffset = frames[imgNum].getOffsetX(); final int yOffset = frames[imgNum].getOffsetY(); g.drawImage(frames[imgNum].getImage(), x+xOffset, y+yOffset, null); if ((this.runEndAction) && (this.endActionTime-- <= 0)) { this.runEndAction = false; this.endAction.run(); this.endAction = null; } if(loopMethod == LoopMethod.NONE) return; if(!((cycle++ % 4) == 0)) return; if(currentFrame == currentFrameSet.length - 1){ if(loopMethod == LoopMethod.FORWARD){ currentFrame = 0; } else if(loopMethod == LoopMethod.ONCE){ loopMethod = LoopMethod.NONE; if (this.endAction != null) this.runEndAction = true; } else { loopDir = false; currentFrame--; } } else { if(loopMethod == LoopMethod.FORWARD || loopMethod == LoopMethod.ONCE){ currentFrame++; } else { if(currentFrame == 0){ loopDir = true; } if(loopDir){ currentFrame++; } if(!loopDir){ currentFrame--; } } } } public void drawStill(int x, int y, Graphics graphics){ final int imgNum = currentFrameSet[currentFrame]; final Image img = frames[imgNum].getImage(); final int w = img.getWidth(null); final int h = img.getHeight(null); final float n = 32f/((w>h)? w:h); final int nw = (int)(n*w); final int nh = (int)(n*h); graphics.drawImage(frames[imgNum].getImage(), x+(20-(nw/2)), y+(20-(nh/2)), nw, nh, null); } public void drawStill(int x, int y, int size, Graphics graphics) { int imgNum = this.currentFrameSet[this.currentFrame]; Image img = this.frames[imgNum].getImage(); int w = img.getWidth(null); int h = img.getHeight(null); float n = (size - 8) / (w > h ? w : h); int nw = (int) (n * w); int nh = (int) (n * h); graphics.drawImage(this.frames[imgNum].getImage(), x + (size / 2 - nw / 2), y + (size / 2 - nh / 2), nw, nh, null); } /** * Gets the image object of the specified frame. * @param i The frame number. * @return The image of the frame number. */ public Image getFrameImage(final int i){ return frames[currentFrameSet[i]].getImage(); } /** * Sets the animation. * @param name The animation name. */ public void setAnimation(final String name){ if(animations.containsKey(name)){ currentAnim = name; currentFrameSet = animations.get(currentAnim); currentFrame = 0; } } /** * Sets the loop method. * @param loopMethod The method to loop by. */ public void setLoopMethod(final LoopMethod loopMethod){ this.loopMethod = loopMethod; } /** * Gets the current animation. */ public String getCurrentAnim(){ return currentAnim; } /** * Gets the current frame. */ public int getCurrentFrame(){ return currentFrame; } /** * {@inheritDoc} */ @Override public Object clone(){ try { return super.clone(); } catch(final Exception e){ return null; } } /** * Gets the asset ID. */ public int getAssetID(){ return assetID; } public Frame[] getFrames(){ return frames; } public void setEndAction(Runnable endAction) { this.endAction = endAction; } public void setEndActionTime(int endActionTime) { this.endActionTime = endActionTime; } }