-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animated.pde
112 lines (97 loc) · 2.58 KB
/
Animated.pde
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
class Animated extends Sprite {
PImage[] currentImages;
PImage[] standIdle;
PImage[] standLeft;
PImage[] standRight;
PImage[] moveLeft;
PImage[] moveRight;
PImage[] shootRight;
PImage[] shootLeft;
PImage[] dieLeft;
PImage[] dieRight;
PImage[] crouchLeft;
PImage[] crouchRight;
int direction, lastDirection;
int index;
int frame;
final int aniSpeed = 7;
public static final int IDLE = 0;
public static final int RUN = 1;
public static final int ATTACK = 2;
public static final int SHOOTUP = 3;
public static final int CROUCH = 4;
public static final int DIE = 5;
public Animated(String filename, float scale) {
super(filename, scale);
direction = IDLE;
index = 0;
frame = 0;
}
public void updateAni() {
frame++;
if (frame % aniSpeed == 0) {
selectDirection();
selectCurrentImages();
advanceToNextImage();
}
}
public void selectDirection() {
if (changeX > 0) {
direction = RIGHT_FACING;
lastDirection = RIGHT_FACING;
} else if (changeX < 0) {
direction = LEFT_FACING;
lastDirection = LEFT_FACING;
} else
direction = IDLE;
}
public void selectCurrentImages() {
if (direction == RIGHT_FACING) {
currentImages = moveRight;
} else if (direction == LEFT_FACING) {
currentImages = moveLeft;
} else
currentImages = standIdle;
}
public void advanceToNextImage() {
index++;
if (index >= currentImages.length) {
index = 0;
}
if (keyDown && (currentImages == crouchLeft || currentImages == crouchRight) && index == 0) {
} else
img = currentImages[index];
}
public int getAniFrame() {
int aniFrame = (frameCount / aniSpeed) % getSpriteAmount(state);
return aniFrame;
}
public int getSpriteAmount(int playerAction) {
switch (playerAction) {
case 0: //idle
case 1: //atttack
return 5;
case 2: //run //jump
case 4:
return 3; //falling
case 3:
return 4;
case 5: //die
return 6;
default:
return 1;
}
}
public void drawSprites(PImage[][] sheet, float x, float y, float w, float h, int state, int aniFrame) {
if (direction == LEFT_FACING) { //if the character swaps directions
pushMatrix();
translate(x, height - y - h); //move him by one character
scale(-1, 1); //inverse the sprite
image(sheet[state][aniFrame], 0, 0); //redraw
popMatrix();
} else if (direction == RIGHT_FACING) {
//img = sheet[state][aniFrame];
image(sheet[state][aniFrame], x, height - y - h);
}
}
}