-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedSprite.java
More file actions
50 lines (41 loc) · 1.27 KB
/
Copy pathAnimatedSprite.java
File metadata and controls
50 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.awt.Graphics2D;
import javax.swing.JPanel;
/**
* Animated sprite using sprite sheet or multiple frames.
* Provides frame-based animation support.
*/
public class AnimatedSprite extends Sprite {
private Animation animation;
private int screenX;
private int screenY;
private boolean active;
public AnimatedSprite(JPanel p, int xPos, int yPos, int w, int h) {
super(p, xPos, yPos, w, h);
screenX = xPos;
screenY = yPos;
animation = new Animation();
active = true;
}
public void setAnimation(Animation anim) {
animation = anim;
animation.start();
}
public void update() {
if (animation != null && active) {
animation.update();
}
}
public void updateScreenPosition(int cameraX, int cameraY) {
screenX = x - cameraX;
screenY = y - cameraY;
}
@Override
public void draw(Graphics2D g2) {
if (!active) return;
if (animation != null && animation.getImage() != null) {
g2.drawImage(animation.getImage(), screenX, screenY, width, height, null);
} else if (image != null) {
g2.drawImage(image, screenX, screenY, width, height, null);
}
}
}