Skip to content

Commit a65770d

Browse files
committed
added minecraft basic sample
1 parent 8930b39 commit a65770d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB ([email protected]).
4+
* See LICENSE for details.
5+
*/
6+
7+
package sandbox.test3d;
8+
9+
import com.almasb.fxgl.app.GameApplication;
10+
import com.almasb.fxgl.app.GameSettings;
11+
import com.almasb.fxgl.app.scene.Camera3D;
12+
import com.almasb.fxgl.entity.Entity;
13+
import com.almasb.fxgl.scene3d.Cuboid;
14+
import javafx.geometry.Point3D;
15+
import javafx.scene.input.KeyCode;
16+
import javafx.scene.input.MouseButton;
17+
import javafx.scene.paint.Color;
18+
19+
import static com.almasb.fxgl.dsl.FXGL.*;
20+
import static java.lang.Math.*;
21+
22+
/**
23+
* @author Almas Baimagambetov ([email protected])
24+
*/
25+
public class MinecraftSample extends GameApplication {
26+
27+
private Camera3D camera3D;
28+
29+
@Override
30+
protected void initSettings(GameSettings settings) {
31+
settings.setWidth(1280);
32+
settings.setHeight(720);
33+
settings.set3D(true);
34+
}
35+
36+
@Override
37+
protected void initInput() {
38+
onKey(KeyCode.W, () -> camera3D.moveForward());
39+
onKey(KeyCode.S, () -> camera3D.moveBack());
40+
onKey(KeyCode.A, () -> camera3D.moveLeft());
41+
onKey(KeyCode.D, () -> camera3D.moveRight());
42+
onKey(KeyCode.L, () -> getGameController().exit());
43+
44+
onBtnDown(MouseButton.PRIMARY, () -> {
45+
var pos = camera3D.getTransform().getPosition3D().add(camera3D.getTransform().getDirection3D().multiply(1.5));
46+
47+
spawnCube(new Point3D(round(pos.getX()), round(pos.getY()), round(pos.getZ())));
48+
});
49+
}
50+
51+
@Override
52+
protected void initGame() {
53+
camera3D = getGameScene().getCamera3D();
54+
55+
getGameScene().setBackgroundColor(Color.LIGHTBLUE.darker());
56+
getGameScene().setFPSCamera(true);
57+
getGameScene().setCursorInvisible();
58+
}
59+
60+
private Entity spawnCube(Point3D pos) {
61+
return entityBuilder()
62+
.at(pos)
63+
.view(new Cuboid(1, 1, 1))
64+
.buildAndAttach();
65+
}
66+
67+
public static void main(String[] args) {
68+
launch(args);
69+
}
70+
}

0 commit comments

Comments
 (0)