Search in sources :

Example 36 with Entity

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

the class JointSample method initInput.

@Override
protected void initInput() {
    Input input = getInput();
    input.addAction(new UserAction("Spawn Box") {

        @Override
        protected void onActionBegin() {
            Entity box = createPhysicsEntity();
            // 3. set hit box (-es) to specify bounding shape
            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().setView(new Rectangle(80, 40, Color.BLUE));
            getGameWorld().addEntity(box);
        }
    }, MouseButton.PRIMARY);
    input.addAction(new UserAction("Spawn Ball") {

        @Override
        protected void onActionBegin() {
            Entity ball = createPhysicsEntity();
            // 3. set hit box to specify bounding shape
            ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
            ball.getViewComponent().setView(new Circle(20, Color.RED));
            getGameWorld().addEntity(ball);
        }
    }, MouseButton.SECONDARY);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Entity(com.almasb.fxgl.entity.Entity) Circle(javafx.scene.shape.Circle) Input(com.almasb.fxgl.input.Input) HitBox(com.almasb.fxgl.physics.HitBox) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle)

Example 37 with Entity

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

the class AchievementsSample method initGame.

@Override
protected void initGame() {
    playerControl = new PlayerControl();
    Entity player = Entities.builder().at(100, 100).viewFromNode(new Rectangle(40, 40)).with(playerControl).buildAndAttach(getGameWorld());
    getGameState().doubleProperty("playerX").bind(player.getPositionComponent().xProperty());
}
Also used : Entity(com.almasb.fxgl.entity.Entity) Rectangle(javafx.scene.shape.Rectangle) PlayerControl(common.PlayerControl)

Example 38 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(20, 15);
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 20; j++) {
            final int x = j;
            final int y = i;
            Entity tile = new Entity();
            tile.getPositionComponent().setValue(j * 40, i * 40);
            Rectangle graphics = new Rectangle(38, 38);
            graphics.setFill(Color.WHITE);
            graphics.setStroke(Color.BLACK);
            // add on click listener
            graphics.setOnMouseClicked(e -> {
                // if left click do search, else place a red obstacle
                if (e.getButton() == MouseButton.PRIMARY) {
                    List<AStarNode> nodes = grid.getPath(0, 0, x, y);
                    nodes.forEach(n -> {
                        getGameWorld().getEntitiesAt(new Point2D(n.getX() * 40, n.getY() * 40)).forEach(Entity::removeFromWorld);
                    });
                } else {
                    grid.setNodeState(x, y, NodeState.NOT_WALKABLE);
                    graphics.setFill(Color.RED);
                }
            });
            tile.getViewComponent().setView(graphics);
            getGameWorld().addEntity(tile);
        }
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity) AStarNode(com.almasb.fxgl.ai.pathfinding.AStarNode) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) AStarGrid(com.almasb.fxgl.ai.pathfinding.AStarGrid)

Example 39 with Entity

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

the class RemoveSelectedEntitesBug method initGame.

@Override
protected void initGame() {
    Entity e1 = Entities.builder().at(100, 100).viewFromNode(new ColoredTexture(40, 30, Color.BLUE)).with(new SelectableComponent(true)).buildAndAttach(getGameWorld());
    Entity e2 = Entities.builder().at(200, 100).viewFromNode(new Rectangle(40, 40, Color.RED)).with(new SelectableComponent(true)).buildAndAttach(getGameWorld());
    Cursor cursor = getGameScene().getRoot().getCursor();
    // this solves the issue, I think when mouse is clicked / released the game cursor is overridden
    // e1.getView().setCursor(cursor);
    // e2.getView().setCursor(cursor);
    getGameWorld().selectedEntityProperty().addListener((o, oldEntity, newEntity) -> {
        if (newEntity != null)
            getGameWorld().removeEntity(newEntity);
    });
}
Also used : Entity(com.almasb.fxgl.entity.Entity) Rectangle(javafx.scene.shape.Rectangle) Cursor(javafx.scene.Cursor) ColoredTexture(com.almasb.fxgl.texture.ColoredTexture) SelectableComponent(com.almasb.fxgl.entity.component.SelectableComponent)

Example 40 with Entity

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

the class ScriptSample method initPhysics.

@Override
protected void initPhysics() {
    getPhysicsWorld().addCollisionHandler(new CollisionHandler(EntityType.PC, EntityType.NPC) {

        @Override
        protected void onCollisionBegin(Entity pc, Entity npc) {
            getGameplay().getCutsceneManager().startCutscene("cutscene.js");
        }
    });
    getPhysicsWorld().addCollisionHandler(new CollisionHandler(EntityType.PC, EntityType.COIN) {

        @Override
        protected void onCollisionBegin(Entity pc, Entity coin) {
            getGameState().increment("coins", +1);
            coin.removeFromWorld();
        }
    });
}
Also used : Entity(com.almasb.fxgl.entity.Entity) CollisionHandler(com.almasb.fxgl.physics.CollisionHandler)

Aggregations

Entity (com.almasb.fxgl.entity.Entity)55 Rectangle (javafx.scene.shape.Rectangle)21 Point2D (javafx.geometry.Point2D)15 HitBox (com.almasb.fxgl.physics.HitBox)12 EntityView (com.almasb.fxgl.entity.view.EntityView)8 UserAction (com.almasb.fxgl.input.UserAction)6 CollisionHandler (com.almasb.fxgl.physics.CollisionHandler)6 Input (com.almasb.fxgl.input.Input)5 GameApplication (com.almasb.fxgl.app.GameApplication)4 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)4 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)4 PhysicsParticleComponent (com.almasb.fxgl.physics.PhysicsParticleComponent)4 ParticleGroupDef (com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef)4 GameSettings (com.almasb.fxgl.settings.GameSettings)4 ParticleControl (com.almasb.fxgl.effect.ParticleControl)3 ParticleEmitter (com.almasb.fxgl.effect.ParticleEmitter)3 Entities (com.almasb.fxgl.entity.Entities)3 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)3 Color (javafx.scene.paint.Color)3 Circle (javafx.scene.shape.Circle)3