Search in sources :

Example 71 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class MapGenerationSample method coloredMap.

private void coloredMap() {
    // tile size
    int size = 1;
    int W = (getAppWidth() + 200) / size;
    int H = (getAppHeight() + 200) / size;
    Grid<HeightMapGenerator.HeightData> map = new Grid<>(HeightMapGenerator.HeightData.class, W, H, new CustomHeightMapGenerator(W, H));
    AnimatedColor water = new AnimatedColor(Color.DARKBLUE, Color.AQUA);
    AnimatedColor land = new AnimatedColor(Color.DARKGREEN, Color.LIGHTGREEN);
    AnimatedColor desert = new AnimatedColor(Color.GREENYELLOW, Color.LIGHTYELLOW);
    AnimatedColor snow = new AnimatedColor(Color.GREEN, Color.WHITESMOKE);
    WritableImage image = new WritableImage(W, H);
    map.forEach(cell -> {
        AnimatedColor anim;
        double adjustedValue;
        if (cell.getHeight() < 0.2) {
            anim = water;
            adjustedValue = map(cell.getHeight(), 0.0, 0.2, 0.0, 1.0);
        } else if (cell.getHeight() < 0.4) {
            anim = land;
            adjustedValue = map(cell.getHeight(), 0.2, 0.4, 0.0, 1.0);
        } else if (cell.getHeight() < 0.9) {
            anim = desert;
            adjustedValue = map(cell.getHeight(), 0.4, 0.9, 0.0, 1.0);
        } else {
            anim = snow;
            adjustedValue = map(cell.getHeight(), 0.9, 1.0, 0.0, 1.0);
        }
        image.getPixelWriter().setColor(cell.getX(), cell.getY(), anim.getValue(adjustedValue, interpolator.EASE_OUT()));
    });
    getGameScene().getViewport().bindToEntity(new Entity(), 0, 0);
    var t = new Texture(image);
    t.setTranslateX(-100);
    t.setTranslateY(-100);
    getGameScene().addGameView(new GameView(t, 0));
}
Also used : AnimatedColor(com.almasb.fxgl.animation.AnimatedColor) Entity(com.almasb.fxgl.entity.Entity) Grid(com.almasb.fxgl.core.collection.grid.Grid) Texture(com.almasb.fxgl.texture.Texture) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture) WritableImage(javafx.scene.image.WritableImage) HeightMapGenerator(com.almasb.fxgl.procedural.HeightMapGenerator) GameView(com.almasb.fxgl.app.scene.GameView)

Example 72 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class PathfindingSample method initGame.

@Override
protected void initGame() {
    // 2. init grid width x height
    grid = new AStarGrid(GRID_WIDTH, GRID_HEIGHT);
    pathfinder = new AStarPathfinder(grid);
    grid.forEach(c -> {
        var x = c.getX();
        var y = c.getY();
        Rectangle rect = new Rectangle(CELL_WIDTH - 2, CELL_HEIGHT - 2);
        rect.setFill(Color.WHITE);
        rect.setStroke(Color.BLACK);
        Entity tile = entityBuilder().at(x * CELL_WIDTH, y * CELL_HEIGHT).view(rect).buildAndAttach();
        rect.setOnMouseClicked(e -> {
            // if left click do search, else place a red obstacle
            if (e.getButton() == MouseButton.PRIMARY) {
                var cells = pathfinder.findPath(0, 0, x, y);
                cells.forEach(cell -> {
                    getGameWorld().getEntitiesAt(new Point2D(cell.getX() * CELL_WIDTH, cell.getY() * CELL_HEIGHT)).forEach(Entity::removeFromWorld);
                });
            } else {
                grid.get(x, y).setState(CellState.NOT_WALKABLE);
                rect.setFill(Color.RED);
            }
        });
    });
    var e = entityBuilder().viewWithBBox(new Rectangle(20, 20)).with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 100)).buildAndAttach();
    e.getComponent(CellMoveComponent.class).setPositionToCell(1, 1);
}
Also used : AStarPathfinder(com.almasb.fxgl.pathfinding.astar.AStarPathfinder) Entity(com.almasb.fxgl.entity.Entity) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) AStarGrid(com.almasb.fxgl.pathfinding.astar.AStarGrid)

Example 73 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class RopeJointSample method initInput.

@Override
protected void initInput() {
    getInput().addAction(new UserAction("LMB") {

        private double x;

        private double y;

        @Override
        protected void onActionBegin() {
            x = getInput().getMouseXWorld();
            y = getInput().getMouseYWorld();
        }

        @Override
        protected void onActionEnd() {
            var endx = getInput().getMouseXWorld();
            var endy = getInput().getMouseYWorld();
            spawnBullet(x, y, endx - x, endy - y);
        }
    }, MouseButton.PRIMARY);
    onKeyDown(KeyCode.F, () -> {
        Entity box = createPhysicsEntity();
        box.getBoundingBoxComponent().addHitBox(new HitBox("Left", BoundingShape.box(40, 40)));
        box.getBoundingBoxComponent().addHitBox(new HitBox("Right", new Point2D(40, 0), BoundingShape.box(40, 40)));
        box.getViewComponent().addChild(texture("brick.png", 40, 40).superTexture(texture("brick.png", 40, 40), HorizontalDirection.RIGHT));
        box.setRotationOrigin(new Point2D(40, 20));
        getGameWorld().addEntity(box);
    });
    onBtnDown(MouseButton.SECONDARY, () -> {
        Entity ball = createPhysicsEntity();
        ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
        ball.getViewComponent().addChild(texture("ball.png", 40, 40));
        ball.setRotationOrigin(new Point2D(20, 20));
        getGameWorld().addEntity(ball);
    });
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) Point2D(javafx.geometry.Point2D)

Example 74 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class RevoluteJointSample method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.LIGHTGRAY);
    entityBuilder().buildScreenBoundsAndAttach(50);
    // platform
    entityBuilder().at(400, 400).viewWithBBox(new Rectangle(500, 20, Color.BROWN)).with(new PhysicsComponent()).buildAndAttach();
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().density(1.1f));
    physics.setBodyType(BodyType.DYNAMIC);
    Entity block = entityBuilder().at(600, 100).viewWithBBox(new Rectangle(80, 50)).with(physics).buildAndAttach();
    PhysicsComponent physics2 = new PhysicsComponent();
    physics2.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(1.0f);
    physics2.setFixtureDef(fd);
    Entity ball1 = entityBuilder().at(600, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics2).buildAndAttach();
    PhysicsComponent physics3 = new PhysicsComponent();
    physics3.setBodyType(BodyType.DYNAMIC);
    physics3.setFixtureDef(fd);
    Entity ball2 = entityBuilder().at(700, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics3).buildAndAttach();
    physics2.getBody().setAngularDamping(1f);
    physics3.getBody().setAngularDamping(1f);
    getPhysicsWorld().addRevoluteJoint(block, ball1, new Point2D(80, 50), new Point2D(15, 15));
    getPhysicsWorld().addRevoluteJoint(block, ball2, new Point2D(0, 50), new Point2D(15, 15));
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 75 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class RevoluteJointSample method initInput.

@Override
protected void initInput() {
    getInput().addAction(new UserAction("LMB") {

        private double x;

        private double y;

        @Override
        protected void onActionBegin() {
            x = getInput().getMouseXWorld();
            y = getInput().getMouseYWorld();
        }

        @Override
        protected void onActionEnd() {
            var endx = getInput().getMouseXWorld();
            var endy = getInput().getMouseYWorld();
            spawnBullet(x, y, endx - x, endy - y);
        }
    }, MouseButton.PRIMARY);
    onKeyDown(KeyCode.F, () -> {
        Entity box = createPhysicsEntity();
        box.getBoundingBoxComponent().addHitBox(new HitBox("Left", BoundingShape.box(40, 40)));
        box.getBoundingBoxComponent().addHitBox(new HitBox("Right", new Point2D(40, 0), BoundingShape.box(40, 40)));
        box.getViewComponent().addChild(texture("brick.png", 40, 40).superTexture(texture("brick.png", 40, 40), HorizontalDirection.RIGHT));
        box.setRotationOrigin(new Point2D(40, 20));
        getGameWorld().addEntity(box);
    });
    onBtnDown(MouseButton.SECONDARY, () -> {
        Entity ball = createPhysicsEntity();
        ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
        ball.getViewComponent().addChild(texture("ball.png", 40, 40));
        ball.setRotationOrigin(new Point2D(20, 20));
        getGameWorld().addEntity(ball);
    });
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) Point2D(javafx.geometry.Point2D)

Aggregations

Entity (com.almasb.fxgl.entity.Entity)80 Rectangle (javafx.scene.shape.Rectangle)27 Point2D (javafx.geometry.Point2D)24 HitBox (com.almasb.fxgl.physics.HitBox)20 GameApplication (com.almasb.fxgl.app.GameApplication)8 EntityView (com.almasb.fxgl.entity.view.EntityView)8 UserAction (com.almasb.fxgl.input.UserAction)8 CollisionHandler (com.almasb.fxgl.physics.CollisionHandler)8 Color (javafx.scene.paint.Color)7 Input (com.almasb.fxgl.input.Input)5 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)5 Circle (javafx.scene.shape.Circle)5 Line (javafx.scene.shape.Line)5 GameSettings (com.almasb.fxgl.app.GameSettings)4 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)4 FXGL (com.almasb.fxgl.dsl.FXGL)4 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)4 PhysicsParticleComponent (com.almasb.fxgl.physics.PhysicsParticleComponent)4 ParticleGroupDef (com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef)4 Text (javafx.scene.text.Text)4