/src/graphics/Animation.java
Java | 119 lines | 90 code | 26 blank | 3 comment | 13 complexity | 2397fdc8ae285efeea9c98b403a16996 MD5 | raw file
- package graphics;
- import game.Game;
- import java.awt.image.BufferedImage;
- import program.Program;
- 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 boolean stopped; // has animations stopped
- private BufferedImage[] frames; // frames to display
- public Animation(String value)
- {
- String[] lines = value.split("\\r?\\n");
-
- // Get host spritesheet
- Sprite s = Program.RESOURCE.getSprite(lines[0]);
-
- // Get frame delay
- frameDelay = Integer.parseInt(lines[1]);
-
- // Remainder should be <x, y> for assembling frames
- lines = lines[2].split(";");
- frames = new BufferedImage[lines.length];
-
- for (int i = 0; i < lines.length; i++)
- {
- String[] p = lines[i].split(",");
- int x = Integer.parseInt(p[0]);
- int y = Integer.parseInt(p[1]);
- frames[i] = s.getFrame(x, y);
- }
- stopped = true;
- currentFrame = 0;
- animationDirection = 1;
- }
- public void start()
- {
- if (!stopped)
- {
- return;
- }
- if (frames.length == 0)
- {
- return;
- }
- stopped = false;
- }
- public void stop()
- {
- if (frames.length == 0)
- {
- return;
- }
- stopped = true;
- }
- public void restart()
- {
- if (frames.length == 0)
- {
- return;
- }
- stopped = false;
- currentFrame = 0;
- }
- public void reset()
- {
- this.stopped = true;
- this.frameCount = 0;
- this.currentFrame = 0;
- }
- public BufferedImage getImage()
- {
- return frames[currentFrame];
- }
- public void update()
- {
- if (!stopped)
- {
- frameCount++;
- if (frameCount > frameDelay)
- {
- frameCount = 0;
- currentFrame += animationDirection;
- if (currentFrame > frames.length - 1)
- {
- currentFrame = 0;
- }
- else if (currentFrame < 0)
- {
- currentFrame = frames.length - 1;
- }
- }
- }
- }
- }