Skip to content

Commit

Permalink
wip animation
Browse files Browse the repository at this point in the history
  • Loading branch information
lul11003 committed Apr 4, 2015
1 parent 1e73ebf commit 394b847
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 4 deletions.
3 changes: 2 additions & 1 deletion MerchantRPGCSE2102/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="junit-4.11.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 3 additions & 3 deletions MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.source=1.6
Binary file added MerchantRPGCSE2102/src/images/testsprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions MerchantRPGCSE2102/src/sprites/Animation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;


public class Animation {

private int frameCount; // Counts ticks for change
private int frameDelay; // frame delay 1-12 (You will have to play around with this)
private int currentFrame; // animations current frame
private int animationDirection; // animation direction (i.e counting forward or backward)
private int totalFrames; // total amount of frames for your animation

private boolean stopped; // has animations stopped

private List<Frame> frames = new ArrayList<Frame>(); // Arraylist of frames

public Animation(BufferedImage[] frames, int frameDelay) {
this.frameDelay = frameDelay;
this.stopped = true;

for (int i = 0; i < frames.length; i++) {
addFrame(frames[i], frameDelay);
}

this.frameCount = 0;
this.frameDelay = frameDelay;
this.currentFrame = 0;
this.animationDirection = 1;
this.totalFrames = this.frames.size();

}

public void start() {
if (!stopped) {
return;
}

if (frames.size() == 0) {
return;
}

stopped = false;
}

public void stop() {
if (frames.size() == 0) {
return;
}

stopped = true;
}

public void restart() {
if (frames.size() == 0) {
return;
}

stopped = false;
currentFrame = 0;
}

public void reset() {
this.stopped = true;
this.frameCount = 0;
this.currentFrame = 0;
}

private void addFrame(BufferedImage frame, int duration) {
if (duration <= 0) {
System.err.println("Invalid duration: " + duration);
throw new RuntimeException("Invalid duration: " + duration);
}

frames.add(new Frame(frame, duration));
currentFrame = 0;
}

public BufferedImage getSprite() {
return frames.get(currentFrame).getFrame();
}

public void update() {
if (!stopped) {
frameCount++;

if (frameCount > frameDelay) {
frameCount = 0;
currentFrame += animationDirection;

if (currentFrame > totalFrames - 1) {
currentFrame = 0;
}
else if (currentFrame < 0) {
currentFrame = totalFrames - 1;
}
}
}

}

}
29 changes: 29 additions & 0 deletions MerchantRPGCSE2102/src/sprites/Frame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.awt.image.BufferedImage;

public class Frame {

private BufferedImage frame;
private int duration;

public Frame(BufferedImage frame, int duration) {
this.frame = frame;
this.duration = duration;
}

public BufferedImage getFrame() {
return frame;
}

public void setFrame(BufferedImage frame) {
this.frame = frame;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}

}
28 changes: 28 additions & 0 deletions MerchantRPGCSE2102/src/sprites/PlayerSprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import view.MapUI;

public class PlayerSprite {
Expand All @@ -21,6 +23,23 @@ public class PlayerSprite {
private boolean rightBeingPressed = false;
private boolean downBeingPressed = false;
private boolean upBeingPressed = false;

// Images for each animation
private BufferedImage[] walkingLeft = {Sprite.getSprite(0, 1), Sprite.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet
private BufferedImage[] walkingRight = {Sprite.getSprite(0, 2), Sprite.getSprite(2, 1)};
private BufferedImage[] walkingUp = {Sprite.getSprite(0, 1), Sprite.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet
private BufferedImage[] walkingDown = {Sprite.getSprite(0, 2), Sprite.getSprite(2, 1)};
private BufferedImage[] standing = {Sprite.getSprite(1, 0)};

// Animation states
private Animation walkLeft = new Animation(walkingLeft, 10);
private Animation walkRight = new Animation(walkingRight, 10);
private Animation walkUp = new Animation(walkingUp, 10);
private Animation walkDown = new Animation(walkingDown, 10);
private Animation standing1 = new Animation(standing, 10);

// Actual animation
private Animation animation = standing1;

public PlayerSprite(MapUI mapui, int row, int col) {
this.x = col*WIDTH;
Expand All @@ -35,19 +54,27 @@ public PlayerSprite(MapUI mapui, int row, int col) {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
dx = -1;
animation = walkLeft;
animation.start();
leftBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
dx = 1;
animation = walkRight;
animation.start();
rightBeingPressed = true;
}

if (e.getKeyCode() == KeyEvent.VK_UP) {
dy = -1;
animation = walkUp;
animation.start();
upBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
dy = 1;
animation = walkDown;
animation.start();
downBeingPressed = true;
}
}
Expand Down Expand Up @@ -171,6 +198,7 @@ public void move() {
public void paint(Graphics2D g) {
g.setColor(color);
g.fillOval(getX(), getY(), WIDTH, WIDTH);
g.drawImage(animation.getSprite(), x, y, null);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions MerchantRPGCSE2102/src/sprites/Sprite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sprites;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Sprite {

private static BufferedImage spriteSheet;
private static final int TILE_SIZE = 32;

public static BufferedImage loadSprite(String file) {

BufferedImage sprite = null;

try {
sprite = ImageIO.read(new File("src/images/testsprite.png"));
} catch (IOException e) {
e.printStackTrace();
}

return sprite;
}

public static BufferedImage getSprite(int xGrid, int yGrid) {

if (spriteSheet == null) {
spriteSheet = loadSprite("AnimationSpriteSheet");
}

return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}

}

0 comments on commit 394b847

Please sign in to comment.