Search in sources :

Example 1 with Bundle

use of com.almasb.fxgl.core.serialization.Bundle in project FXGL by AlmasB.

the class BoundingBoxComponentTest method testSerialization.

@Test
public void testSerialization() {
    bbox.addHitBox(new HitBox("BOX", BoundingShape.box(30, 40)));
    // write
    Bundle bundle = new Bundle("BBOXTest");
    bbox.write(bundle);
    Entity e2 = new Entity();
    // read
    BoundingBoxComponent bbox2 = e2.getBoundingBoxComponent();
    bbox2.read(bundle);
    assertThat(bbox2.getWidth(), is(30.0));
    assertThat(bbox2.getHeight(), is(40.0));
    List<HitBox> boxes = bbox2.hitBoxesProperty();
    assertThat(boxes.size(), is(1));
    assertThat(boxes.get(0).getName(), is("BOX"));
    assertThat(boxes.get(0).getWidth(), is(30.0));
    assertThat(boxes.get(0).getHeight(), is(40.0));
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) BoundingBoxComponent(com.almasb.fxgl.entity.components.BoundingBoxComponent) Bundle(com.almasb.fxgl.core.serialization.Bundle) Test(org.junit.jupiter.api.Test)

Example 2 with Bundle

use of com.almasb.fxgl.core.serialization.Bundle in project FXGL by AlmasB.

the class CircleNNApp method initGame.

@Override
protected void initGame() {
    getGameWorld().addEntityFactory(new CircleNNFactory());
    getGameScene().setBackgroundColor(Color.BLACK);
    spawn("block", 200, 200);
    spawn("block", getAppWidth() - 200 - 64, 200);
    spawn("block", 200, getAppHeight() - 200 - 64);
    spawn("block", getAppWidth() - 200 - 64, getAppHeight() - 200 - 64);
    var spawnPoints = List.of(new Point2D(100, 100), new Point2D(getAppWidth() - 100 - 64, 100), new Point2D(getAppWidth() / 2.0 - 32, 100), new Point2D(100, getAppHeight() / 2.0 - 32), new Point2D(getAppWidth() - 100 - 64, getAppHeight() / 2.0 - 32), new Point2D(getAppWidth() / 2.0 - 32, getAppHeight() / 2.0 - 32), new Point2D(100, getAppHeight() - 100 - 64), new Point2D(getAppWidth() - 100 - 64, getAppHeight() - 100 - 64), new Point2D(getAppWidth() / 2.0 - 32, getAppHeight() - 100 - 64));
    spawn("circle", 500.0, 600.0);
    spawn("circle", 500.0, 600.0);
    // spawnPoints.forEach(point -> {
    // for (int i = 0; i < 11; i++) {
    // spawn("circle", point);
    // }
    // });
    player = getGameWorld().getRandom(CIRCLE).get();
    player.removeComponent(RandomMoveComponent.class);
    player.removeComponent(BlockCollisionComponent.class);
    player.addComponent(new KeepOnScreenComponent());
    player.addComponent(new PlayerComponent());
    getGameWorld().getEntitiesByType(CIRCLE).stream().filter(e -> e != player).forEach(e -> {
        e.getComponent(RandomMoveComponent.class).pause();
        e.getComponent(BlockCollisionComponent.class).pause();
        Bundle bundle = getFileSystemService().<Bundle>readDataTask("editor_json/input/input0.dat").run();
        var input = new InputCapture();
        input.read(bundle);
        e.getComponent(CircleComponent.class).getInput().applyCapture(input);
    });
    place = getGameWorld().getEntitiesByType(CIRCLE).size();
    run(() -> {
        var powerupType = PowerupType.SHIELD;
        // var powerupType = FXGLMath.random(PowerupType.values()).get();
        spawn("powerup", new SpawnData(FXGLMath.randomPoint(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()))).put("powerupType", powerupType));
    }, Duration.seconds(3));
    capture = getInput().startCapture();
}
Also used : KeyCode(javafx.scene.input.KeyCode) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) MouseButton(javafx.scene.input.MouseButton) Bundle(com.almasb.fxgl.core.serialization.Bundle) RandomMoveComponent(com.almasb.fxgl.dsl.components.RandomMoveComponent) KeepOnScreenComponent(com.almasb.fxgl.dsl.components.KeepOnScreenComponent) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) InputCapture(com.almasb.fxgl.input.InputCapture) CircleNNType(sandbox.circlegame.CircleNNType) Text(javafx.scene.text.Text) Duration(javafx.util.Duration) List(java.util.List) Interpolators(com.almasb.fxgl.animation.Interpolators) GameSettings(com.almasb.fxgl.app.GameSettings) Point2D(javafx.geometry.Point2D) SpawnData(com.almasb.fxgl.entity.SpawnData) GameApplication(com.almasb.fxgl.app.GameApplication) Entity(com.almasb.fxgl.entity.Entity) FXGL(com.almasb.fxgl.dsl.FXGL) RandomMoveComponent(com.almasb.fxgl.dsl.components.RandomMoveComponent) KeepOnScreenComponent(com.almasb.fxgl.dsl.components.KeepOnScreenComponent) Point2D(javafx.geometry.Point2D) Bundle(com.almasb.fxgl.core.serialization.Bundle) Rectangle2D(javafx.geometry.Rectangle2D) InputCapture(com.almasb.fxgl.input.InputCapture) SpawnData(com.almasb.fxgl.entity.SpawnData)

Example 3 with Bundle

use of com.almasb.fxgl.core.serialization.Bundle in project FXGL by AlmasB.

the class UDPSample method main.

public static void main(String[] args) throws Exception {
    Logger.configure(new LoggerConfig());
    Logger.addOutput(new ConsoleOutput(), LoggerLevel.DEBUG);
    var server = new NetService().newUDPServer(55555);
    server.setOnConnected(connection -> {
        connection.addMessageHandler((conn, message) -> {
            System.out.println("Server got: " + message);
            if (message.getName().equals("Bye!!!")) {
                System.out.println("Stopping now");
                server.stop();
            }
        });
    });
    var client = new NetService().newUDPClient("localhost", 55555);
    client.setOnConnected(connection -> {
        connection.addMessageHandler((conn, message) -> {
            System.out.println("Client got: " + message);
            conn.send(new Bundle("Bye!!!"));
            client.disconnect();
        });
    });
    new Thread(() -> {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Client connecting");
        client.connectTask().run();
    }).start();
    new Thread(() -> {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Broadcasting hello");
        server.broadcast(new Bundle("HELLO!"));
    }).start();
    server.startTask().run();
}
Also used : ConsoleOutput(com.almasb.fxgl.logging.ConsoleOutput) Bundle(com.almasb.fxgl.core.serialization.Bundle) NetService(com.almasb.fxgl.net.NetService) LoggerConfig(com.almasb.fxgl.logging.LoggerConfig)

Example 4 with Bundle

use of com.almasb.fxgl.core.serialization.Bundle in project FXGL by AlmasB.

the class CircleNNApp method onCircleDied.

public void onCircleDied() {
    getGameScene().getViewport().shakeTranslational(5);
    place--;
    textPlace.setText(place + "");
    animationBuilder().interpolator(Interpolators.EXPONENTIAL.EASE_OUT()).duration(Duration.seconds(0.11)).autoReverse(true).repeat(2).scale(textPlace).from(new Point2D(1, 1)).to(new Point2D(3, 3)).buildAndPlay();
    if (place == 1) {
        getInput().stopCapture();
        var bundle = new Bundle("input");
        capture.write(bundle);
        getFileSystemService().writeDataTask(bundle, "editor_json/input/input0.dat").run();
        showMessage("You Win!", () -> getGameController().startNewGame());
    }
}
Also used : Point2D(javafx.geometry.Point2D) Bundle(com.almasb.fxgl.core.serialization.Bundle)

Aggregations

Bundle (com.almasb.fxgl.core.serialization.Bundle)4 Entity (com.almasb.fxgl.entity.Entity)2 Point2D (javafx.geometry.Point2D)2 Interpolators (com.almasb.fxgl.animation.Interpolators)1 GameApplication (com.almasb.fxgl.app.GameApplication)1 GameSettings (com.almasb.fxgl.app.GameSettings)1 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)1 FXGL (com.almasb.fxgl.dsl.FXGL)1 KeepOnScreenComponent (com.almasb.fxgl.dsl.components.KeepOnScreenComponent)1 RandomMoveComponent (com.almasb.fxgl.dsl.components.RandomMoveComponent)1 SpawnData (com.almasb.fxgl.entity.SpawnData)1 BoundingBoxComponent (com.almasb.fxgl.entity.components.BoundingBoxComponent)1 InputCapture (com.almasb.fxgl.input.InputCapture)1 ConsoleOutput (com.almasb.fxgl.logging.ConsoleOutput)1 LoggerConfig (com.almasb.fxgl.logging.LoggerConfig)1 NetService (com.almasb.fxgl.net.NetService)1 HitBox (com.almasb.fxgl.physics.HitBox)1 List (java.util.List)1 Rectangle2D (javafx.geometry.Rectangle2D)1 KeyCode (javafx.scene.input.KeyCode)1