1
+
/*
2
+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3
+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4
+
* See LICENSE for details.
5
+
*/
6
+
7
+
package intermediate.animation;
8
+
9
+
import com.almasb.fxgl.animation.Interpolators;
10
+
import com.almasb.fxgl.app.GameApplication;
11
+
import com.almasb.fxgl.app.GameSettings;
12
+
import com.almasb.fxgl.texture.AnimatedTexture;
13
+
import com.almasb.fxgl.texture.AnimationChannel;
14
+
import javafx.animation.Interpolator;
15
+
import javafx.scene.control.Button;
16
+
import javafx.scene.control.Spinner;
17
+
import javafx.scene.layout.VBox;
18
+
import javafx.scene.paint.Color;
19
+
import javafx.scene.text.Font;
20
+
import javafx.util.Duration;
21
+
22
+
import static com.almasb.fxgl.dsl.FXGL.*;
23
+
24
+
/**
25
+
* @author Almas Baimagambetov (almaslvl@gmail.com)
26
+
*/
27
+
public class AnimatedTextureSample extends GameApplication {
28
+
@Override
29
+
protected void initSettings(GameSettings settings) {
30
+
settings.setWidth(1480);
31
+
settings.setHeight(1020);
32
+
}
33
+
34
+
@Override
35
+
protected void initGame() {
36
+
getGameScene().setBackgroundColor(Color.BLACK);
37
+
38
+
var interpolators = Interpolators.values();
39
+
40
+
for (int i = 0; i < interpolators.length; i++) {
41
+
var x = i % 7;
42
+
var y = i / 7;
43
+
44
+
spawnRobot(x * 150, y * 360, interpolators[i].EASE_OUT(), interpolators[i].toString());
45
+
}
46
+
47
+
spawnRobot(0, 360 * 2, false, false);
48
+
spawnRobot(160, 360 * 2, true, false);
49
+
spawnRobot(320, 360 * 2, false, true);
50
+
spawnRobot(480, 360 * 2, true, true);
51
+
}
52
+
53
+
@Override
54
+
protected void initUI() {
55
+
var frameSpinner = new Spinner<Integer>(0, 23, 0);
56
+
frameSpinner.setPrefWidth(100);
57
+
58
+
var btn = new Button("Play from frame");
59
+
btn.setOnAction(e -> {
60
+
spawnRobotForFrame(900, 700, frameSpinner.getValue());
61
+
});
62
+
63
+
var vbox = new VBox(10, frameSpinner, btn);
64
+
65
+
addUINode(vbox, 1230, 820);
66
+
}
67
+
68
+
private void spawnRobotForFrame(double x, double y, int startFrame) {
69
+
var animChannel = new AnimationChannel(image("robot_roll.png"), 7, 275, 275, Duration.seconds(2.6), 0, 23);
70
+
var animTexture = new AnimatedTexture(animChannel);
71
+
animTexture.playFrom(startFrame);
72
+
73
+
var e = entityBuilder()
74
+
.at(x, y)
75
+
.view(animTexture)
76
+
.buildAndAttach();
77
+
78
+
animTexture.setOnCycleFinished(() -> e.removeFromWorld());
79
+
}
80
+
81
+
private void spawnRobot(double x, double y, Interpolator interpolator, String name) {
82
+
var text = addText(name, x + 120, y + 30);
83
+
text.fontProperty().unbind();
84
+
text.setFont(Font.font(18));
85
+
86
+
var animChannel = new AnimationChannel(image("robot_roll.png"), 7, 275, 275, Duration.seconds(2.6), 0, 23);
87
+
var animTexture = new AnimatedTexture(animChannel);
88
+
animTexture.setInterpolator(interpolator);
89
+
animTexture.loop();
90
+
91
+
entityBuilder()
92
+
.at(x, y)
93
+
.view(animTexture)
94
+
.buildAndAttach();
95
+
}
96
+
97
+
private void spawnRobot(double x, double y, boolean isReverse, boolean isLoop) {
98
+
var text = addText(isReverse ? "Reverse" : "Play", x + 120, y + 30);
99
+
text.fontProperty().unbind();
100
+
text.setFont(Font.font(18));
101
+
102
+
var animChannel = new AnimationChannel(image("robot_death.png"), 7, 275, 275, Duration.seconds(2.6), 0, 26);
103
+
var animTexture = new AnimatedTexture(animChannel);
104
+
105
+
if (isLoop) {
106
+
if (isReverse) {
107
+
animTexture.loopReverse();
108
+
} else {
109
+
animTexture.loop();
110
+
}
111
+
} else {
112
+
if (isReverse) {
113
+
animTexture.playReverse();
114
+
} else {
115
+
animTexture.play();
116
+
}
117
+
}
118
+
119
+
entityBuilder()
120
+
.at(x, y)
121
+
.view(animTexture)
122
+
.buildAndAttach();
123
+
}
124
+
125
+
public static void main(String[] args) {
126
+
launch(args);
127
+
}
128
+
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4