Search in sources :

Example 1 with Viewport

use of com.almasb.fxgl.scene.Viewport in project FXGL by AlmasB.

the class ViewportSample method cinematicViewportBezier.

private void cinematicViewportBezier() {
    // 1. get viewport
    Viewport viewport = getGameScene().getViewport();
    viewport.setX(-getWidth() / 2);
    viewport.setY(-getHeight() / 2);
    // 2. define "waypoints"
    Vec2[] points = { new Vec2(98, 80), new Vec2(1520, 90), new Vec2(1470, 272), new Vec2(171, 293), new Vec2(154, 485), new Vec2(1500, 483), new Vec2(1474, 683), new Vec2(144, 698), new Vec2(161, 892), new Vec2(1475, 888), new Vec2(1477, 1064), new Vec2(108, 1066) };
    BezierSpline spline = FXGLMath.closedBezierSpline(points);
    Path path = new Path();
    path.getElements().add(new MoveTo(98, 80));
    for (BezierSpline.BezierCurve c : spline.getCurves()) {
        path.getElements().add(new CubicCurveTo(c.getControl1().x, c.getControl1().y, c.getControl2().x, c.getControl2().y, c.getEnd().x, c.getEnd().y));
    }
    // if open bezier is needed
    path.getElements().remove(path.getElements().size() - 1);
    Entity camera = Entities.builder().build();
    viewport.xProperty().bind(camera.getPositionComponent().xProperty().subtract(getWidth() / 2));
    viewport.yProperty().bind(camera.getPositionComponent().yProperty().subtract(getHeight() / 2));
    Entities.animationBuilder().duration(Duration.seconds(24)).translate(camera).buildAndPlay();
}
Also used : Entity(com.almasb.fxgl.entity.Entity) BezierSpline(com.almasb.fxgl.core.math.BezierSpline) Vec2(com.almasb.fxgl.core.math.Vec2) Viewport(com.almasb.fxgl.scene.Viewport)

Example 2 with Viewport

use of com.almasb.fxgl.scene.Viewport in project FXGL by AlmasB.

the class ViewportSample method cinematicViewport.

private void cinematicViewport() {
    // 1. get viewport
    Viewport viewport = getGameScene().getViewport();
    viewport.setX(-getWidth() / 2);
    viewport.setY(-getHeight() / 2);
    // 2. define "waypoints"
    Point2D[] points = { new Point2D(98, 80), new Point2D(1520, 90), new Point2D(1470, 272), new Point2D(171, 293), new Point2D(154, 485), new Point2D(1500, 483), new Point2D(1474, 683), new Point2D(144, 698), new Point2D(161, 892), new Point2D(1475, 888), new Point2D(1477, 1064), new Point2D(108, 1066) };
    Timeline timeline = new Timeline();
    int i = 0;
    for (Point2D p : points) {
        // bind viewport property values to pre-defined points
        KeyValue kv = new KeyValue(viewport.xProperty(), p.getX() - getWidth() / 2);
        KeyValue kv2 = new KeyValue(viewport.yProperty(), p.getY() - getHeight() / 2);
        // create frame
        KeyFrame frame = new KeyFrame(Duration.seconds(2 * ++i), kv, kv2);
        timeline.getKeyFrames().add(frame);
    }
    // 3. animate
    timeline.play();
}
Also used : Timeline(javafx.animation.Timeline) KeyValue(javafx.animation.KeyValue) Point2D(javafx.geometry.Point2D) Viewport(com.almasb.fxgl.scene.Viewport) KeyFrame(javafx.animation.KeyFrame)

Aggregations

Viewport (com.almasb.fxgl.scene.Viewport)2 BezierSpline (com.almasb.fxgl.core.math.BezierSpline)1 Vec2 (com.almasb.fxgl.core.math.Vec2)1 Entity (com.almasb.fxgl.entity.Entity)1 KeyFrame (javafx.animation.KeyFrame)1 KeyValue (javafx.animation.KeyValue)1 Timeline (javafx.animation.Timeline)1 Point2D (javafx.geometry.Point2D)1