-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedSprite.java
More file actions
112 lines (96 loc) · 2.75 KB
/
AnimatedSprite.java
File metadata and controls
112 lines (96 loc) · 2.75 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.awt.image.BufferedImage;
/**
* Creates an animation using spites to be used by all Character
*/
public class AnimatedSprite extends Sprite implements GameObject{
//global variables
private Sprite[] sprites;
private int currentSprite = 0;
private int speed;
private int counter = 0;
private int startSprite = 0;
private int endSprite;
private boolean looped = false;
/**
* main Constructor that creates the Animation
* @param sheet the sprite Sheet
* @param pos the position the character exists on the SpriteSheet
* @param speed the speed to cycle trough
*/
public AnimatedSprite(SpriteSheet sheet, Rectangle[] pos, int speed) {
sprites = new Sprite[pos.length];
this.speed = speed;
this.endSprite = pos.length - 1;
for(int i = 0; i < pos.length; i++)sprites[i] = new Sprite(sheet, pos[i].x, pos[i].y, pos[i].w, pos[i].h);
}
/**
* Constructor that creates the Animation
* @param sheet the sprite Sheet
* @param speed the speed to cycle trough
*/
public AnimatedSprite(SpriteSheet sheet, int speed) {
sprites = sheet.getLoadedSprites();
this.speed = speed;
this.endSprite = sprites.length - 1;
}
/**
* updates the animation of the sprites
interfaced vars not used
*/
public void update(Game game, Player player, Spawn spawner){
counter++;
if(counter >= speed) {
counter = 0;
incrementSprite();
}
}
//reset animation time
public void reset(){
counter = 0;
currentSprite = startSprite;
looped = false;
}
/**
* creates an animation Range
* @param startSprite starting Pos
* @param endSprite ending Pos
*/
public void setAnimationRange(int startSprite, int endSprite){
this.startSprite = startSprite;
this.endSprite = endSprite;
reset();
}
//animate sprite in a loop
public void incrementSprite() {
currentSprite++;
if(currentSprite >= endSprite){
currentSprite = startSprite;
looped = true;
}
}
//getters and setters
public void setStatic(){
currentSprite = endSprite;
}
public boolean getLooped(){
return looped;
}
public int getHeight(){
return sprites[currentSprite].getHeight();
}
public int getWidth(){
return sprites[currentSprite].getWidth();
}
public int[] getPixels(){
return sprites[currentSprite].getPixels();
}
public int getLayer() {
return -1;
}
public Rectangle getRectangle() {
return null;
}
//unused Methods
public void render(RenderHandler renderer, int xZoom, int yZoom) {}
public boolean handleMouseClick(Rectangle mouseRectangle, Rectangle camera, int xZoom, int yZoom) { return false; }
}