Search in sources :

Example 46 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 47 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 48 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 49 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 50 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)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