-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCindy.java
More file actions
201 lines (158 loc) · 6 KB
/
Copy pathCindy.java
File metadata and controls
201 lines (158 loc) · 6 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.awt.Graphics;
import java.awt.Image;
public class Cindy extends GameObject {
private int x, y, width, height;
private Animation walkingAnimation, jumpingAnimation, walkinglevel3Animation;
private Image footRight, footLeft, jumpImage, crouchImage;
private Image tfootR, tfootL;
private Image level2Image;
private Image level3Image;
private int score;
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 = 290;
private int level_index;
private int worldX = 400;
private int worldY = 500;
public Cindy(int x, int y, int width, int height, int level) {
super(x, y, width, height, null);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.level_index = level;
loadDefaultAssets();
createAnimations();
}
private void loadDefaultAssets() {
footRight = ImageManager.loadBufferedImage("images/cindy_level1_right.png");
footLeft = ImageManager.loadBufferedImage("images/cindy_level1_left.png");
crouchImage = ImageManager.loadBufferedImage("images/cindy_crouch.png");
jumpImage = ImageManager.loadBufferedImage("images/cindy_jump.png");
level2Image = ImageManager.loadBufferedImage("images/cindy_level_2.png");
level3Image = ImageManager.loadBufferedImage("images/cindy_level_3.png");
tfootL = ImageManager.loadBufferedImage("images/cindy3leftfoot.png");
tfootR = ImageManager.loadBufferedImage("images/cindy3rightfoot.png");
}
private void createAnimations() {
walkingAnimation = new Animation(true);
walkingAnimation.addFrame(footRight, 100);
walkingAnimation.addFrame(footLeft, 100);
jumpingAnimation = new Animation(false);
jumpingAnimation.addFrame(crouchImage, 100);
jumpingAnimation.addFrame(jumpImage, 100);
walkinglevel3Animation = new Animation(true);
walkinglevel3Animation.addFrame(tfootR, 100);
walkinglevel3Animation.addFrame(tfootL, 100);
}
public void update() {
if (level_index == 0) {
if (walkingAnimation != null) walkingAnimation.update();
if (jumpingAnimation != null) jumpingAnimation.update();
yVelocity += gravity;
y += (int) yVelocity;
if (y >= groundY) {
y = groundY;
yVelocity = 0;
jumping = false;
}
}
}
public void draw(Graphics g) {
Image currentImage;
switch (level_index) {
case 0 -> {
if (jumping && jumpingAnimation != null && jumpingAnimation.isStillActive()) {
currentImage = jumpingAnimation.getImage();
} else if (direction != 0 && walkingAnimation != null && walkingAnimation.isStillActive()) {
currentImage = walkingAnimation.getImage();
} else {
currentImage = (direction == -1) ? footLeft : footRight;
}
}
case 1 -> currentImage = level2Image;
case 2 -> {
if (direction != 0 && walkinglevel3Animation != null && walkinglevel3Animation.isStillActive()) {
currentImage = walkinglevel3Animation.getImage();
} else {
currentImage = level3Image; // Standing still image for Level 3
}
}
default -> currentImage = footRight;
}
g.drawImage(currentImage, x, y, width, height, null);
}
public void move(int direction) {
this.direction = direction;
if(direction==1){
this.x = x + 5;
}
if(direction==-1){
this.x = x - 5;
}
if (direction != 0 && walkingAnimation != null) {
if (!walkingAnimation.isStillActive()) walkingAnimation.start();
} else {
walkingAnimation.stop();
}
if(level_index == 2){
if(direction == 3){
this.y = y + 10;
}
if(direction == 1){
this.x = x +10;
this.y = y + 10;
}
if(direction == -1){
this.x = x - 10;
this.y = y + 10;
}
if (direction != 0 && walkinglevel3Animation != null) {
if (!walkinglevel3Animation.isStillActive()) walkinglevel3Animation.start();
} else {
walkinglevel3Animation.stop();
}
}
System.out.println("Cindy's X: " + worldX + " Y: " + y);
}
public void jump() {
if (isOnGround()) {
yVelocity = jumpStrength;
jumping = true;
}
}
public int getScore(){
return this.score;
}
public void setScore(int num){ //for level 3 to keep nonnegative
this.score = num;
}
public void addScore(int score) {
this.score += score; }
private boolean isOnGround() {
return y >= groundY;
}
public void land() {
jumping = false;
jumpingAnimation.stop();
}
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; }
public void setLevelIndex(int level) { this.level_index = level; }
public int getLevelIndex() { return this.level_index; }
public int getWorldX() { return this.worldX; }
public void setWorldX(int worldX) { this.worldX = worldX; }
public int getWorldY() { return this.worldY; }
public void setWorldY(int worldY) { this.worldY = worldY; }
@Override public int getX() { return this.x; }
@Override public int getY() { return this.y; }
public void moveInWorld(int direction) {
this.direction = direction;
worldX += direction * 5;
}
}