-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.java
More file actions
109 lines (89 loc) · 2.72 KB
/
Platform.java
File metadata and controls
109 lines (89 loc) · 2.72 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
package doodlejump;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
public class Platform {
private Rectangle platform;
private PlatformType type;
private Pane doodlePane;
private double deltaX;
private double minX;
private double maxX;
public Platform(BorderPane root, Pane doodlePane, double x, double y, PlatformType type) {
this.doodlePane = doodlePane;
this.type = type;
this.platform = new Rectangle(x, y, Constants.PLATFORM_WIDTH, Constants.PLATFORM_HEIGHT);
setColorBasedOnType();
doodlePane.getChildren().add(this.platform);
if (type == PlatformType.MOVING) {
this.deltaX = Constants.PLATFORM_MOVING_SPEED;
this.minX = 0;
this.maxX = Constants.SCENE_WIDTH - Constants.PLATFORM_WIDTH;
}
}
private void setColorBasedOnType() {
switch (type) {
case STANDARD:
platform.setFill(Color.GRAY);
break;
case DISAPPEARING:
platform.setFill(Color.RED);
break;
case EXTRA_BOUNCY:
platform.setFill(Color.GREEN);
break;
case MOVING:
platform.setFill(Color.BLUE);
break;
}
}
public PlatformType getType() {
return type;
}
public double getX() {
return this.platform.getX();
}
public double getY() {
return this.platform.getY();
}
public void setY(double y) {
this.platform.setY(y);
}
public double getWidth() {
return this.platform.getWidth();
}
public double getHeight() {
return this.platform.getHeight();
}
public enum PlatformType {
STANDARD,
DISAPPEARING,
EXTRA_BOUNCY,
MOVING
}
public void update() {
if (this.type == PlatformType.MOVING) {
double newX = this.platform.getX() + this.deltaX;
if (newX > maxX || newX < minX) {
this.deltaX = -this.deltaX;
newX = Math.min(Math.max(newX, minX), maxX);
}
this.platform.setX(newX);
}
}
public void hide() {
doodlePane.getChildren().remove(this.platform);
}
public static PlatformType getRandomPlatformType() {
PlatformType[] types = PlatformType.values();
int randomIndex = (int) (Math.random() * types.length);
return types[randomIndex];
}
public void removeFromPane() {
this.doodlePane.getChildren().remove(this.platform);
}
public Rectangle getShape() {
return this.platform;
}
}