-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
HappyNewYearApp.java
137 lines (110 loc) · 3.94 KB
/
HappyNewYearApp.java
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
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
package sandbox;
import com.almasb.fxgl.animation.Interpolators;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.texture.ImagesKt;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
import static com.almasb.fxgl.dsl.FXGL.*;
/**
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
public class HappyNewYearApp extends GameApplication {
private static final String MESSAGE = "Happy New Year\n 2023!";
private List<SnowParticle> particles;
private boolean[][] points = new boolean[1280][720];
private double t = 0.0;
@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(1280);
settings.setHeight(720);
}
@Override
protected void initInput() {
onKeyDown(KeyCode.D, () -> {
t = 0.0;
particles.forEach(p -> {
p.isCollidable = false;
if (!p.isRunning) {
animationBuilder()
.onFinished(() -> {
animationBuilder()
.duration(Duration.seconds(3))
.interpolator(Interpolators.BOUNCE.EASE_IN())
.fadeOut(p)
.buildAndPlay();
})
.duration(Duration.seconds(2 + t * 0.02))
.interpolator(Interpolators.EXPONENTIAL.EASE_IN())
.delay(Duration.seconds(t * 0.07))
.scale(p)
.to(new Point2D(3, 3))
.buildAndPlay();
t += 0.01;
}
});
});
}
@Override
protected void initGame() {
getGameScene().setCursorInvisible();
getGameScene().setBackgroundColor(Color.BLACK);
particles = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
runOnce(() -> {
var p = new SnowParticle();
p.setTranslateX(random(5, getAppWidth() - 5));
particles.add(p);
addUINode(p);
}, Duration.millis(i));
}
Text text = getUIFactoryService().newText(MESSAGE, Color.BLACK, 122);
ImagesKt.toPixels(ImagesKt.toImage(text))
.stream()
.filter(p -> !p.getColor().equals(Color.TRANSPARENT))
.forEach(p -> {
points[p.getX() + 165][p.getY() + 250] = true;
});
}
@Override
protected void onUpdate(double tpf) {
particles.forEach(p -> {
if (p.isRunning) {
p.setTranslateY(p.getTranslateY() + random(1.0, 15));
if (p.getTranslateY() >= 715) {
p.setTranslateX(random(5, getAppWidth() - 5));
p.setTranslateY(0);
}
if (p.isCollidable) {
var isCollision = points[(int) p.getTranslateX()][(int) p.getTranslateY()];
if (FXGLMath.randomBoolean(0.1) && isCollision) {
p.isRunning = false;
}
}
}
});
}
private static class SnowParticle extends Circle {
boolean isRunning = true;
boolean isCollidable = true;
SnowParticle() {
setRadius(0.5);
setFill(Color.WHITE);
}
}
public static void main(String[] args) {
launch(args);
}
}