-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiddle.java
More file actions
142 lines (112 loc) · 3.8 KB
/
Copy pathFiddle.java
File metadata and controls
142 lines (112 loc) · 3.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.awt.Graphics;
import java.awt.Image;
public class Fiddle extends GameObject {
private int x;
private int y;
private int worldX = 10;
private int width;
private int height;
private Animation walkingAnimation;
private Animation jumpingAnimation;
private Image fiddle; // idle or facing left
private Image fiddleRun; // moving right
private Image jumpImage;
private int direction; // -1 = left, 1 = right, 0 = idle
private boolean jumping = false;
private double yVelocity = 0;
private final double gravity = 0.8;
private final double jumpStrength = -12;
private final int groundY = 370;
public Fiddle(int x, int y, int width, int height) {
super(x, y, width, height, null);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
loadAssets();
createAnimations();
}
private void loadAssets() {
fiddle = ImageManager.loadBufferedImage("images/fiddel_300.png");
fiddleRun = ImageManager.loadBufferedImage("images/fiddel_run.png");
jumpImage = ImageManager.loadBufferedImage("images/fiddel_jump.png");
}
private void createAnimations() {
walkingAnimation = new Animation(true);
walkingAnimation.addFrame(fiddle, 100);
walkingAnimation.addFrame(fiddleRun,200);
jumpingAnimation = new Animation(false);
jumpingAnimation.addFrame(fiddle, 100);
jumpingAnimation.addFrame(jumpImage, 100);
}
public void move(int direction) {
this.direction = direction;
x += direction * 4;
worldX += direction * 4;
if (direction != 0) {
if (!walkingAnimation.isStillActive()) {
walkingAnimation.start();
}
} else {
walkingAnimation.stop();
}
}
// Automatically follows Cindy and jumps if she's above
public void update(Cindy cindy) {
// Use worldX difference to always keep chasing
int worldDistance = cindy.getWorldX() - this.worldX;
if (worldDistance > 0) {
move(1); // move right
} else if (worldDistance < 0) {
move(-1); // move left
} else {
move(0); // stop if directly aligned
}
if (!jumping && cindy.getY() < groundY - 10 && Math.abs(worldDistance) < 150) {
jump();
}
if (jumping) {
yVelocity += gravity;
y += (int) yVelocity;
if (y >= groundY) {
y = groundY;
yVelocity = 0;
jumping = false;
}
}
walkingAnimation.update();
jumpingAnimation.update();
}
public void jump() {
if (!jumping) {
yVelocity = jumpStrength;
jumping = true;
}
}
public void draw(Graphics g, Cindy cindy) {
int fiddleScreenX = worldX - cindy.getWorldX() + cindy.getX();
Image currentImage;
if (jumping && jumpingAnimation != null && jumpingAnimation.isStillActive()) {
currentImage = jumpingAnimation.getImage();
} else if (direction != 0 && walkingAnimation != null && walkingAnimation.isStillActive()) {
currentImage = walkingAnimation.getImage();
} else {
currentImage = fiddle;
}
g.drawImage(currentImage, fiddleScreenX, y, width, height, null);
}
public int getWorldX() {
return worldX;
}
public void setWorldX(int worldX) {
this.worldX = worldX;
}
@Override
public int getX() { return x; }
@Override
public int getY() { return y; }
@Override
public int getWidth() { return width; }
@Override
public int getHeight() { return height; }
}