Search in sources :

Example 51 with Rectangle

use of javafx.scene.shape.Rectangle in project FXGL by AlmasB.

the class PhysicsSample2 method initGame.

@Override
protected void initGame() {
    playerControl = new PlayerControl();
    player = Entities.builder().type(Type.PLAYER).at(100, 100).bbox(new HitBox("PLAYER_BODY", BoundingShape.box(250, 40))).viewFromNode(new Rectangle(250, 40, Color.BLUE)).with(playerControl).build();
    enemy = Entities.builder().type(Type.ENEMY).at(200, 100).viewFromNodeWithBBox(new Rectangle(40, 40, Color.RED)).build();
    // 2. we need to add Collidable component and set its value to true
    // so that collision system can 'see' our entities
    player.addComponent(new CollidableComponent(true));
    enemy.addComponent(new CollidableComponent(true));
    getGameWorld().addEntities(player, enemy);
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle) PlayerControl(common.PlayerControl)

Example 52 with Rectangle

use of javafx.scene.shape.Rectangle in project FXGL by AlmasB.

the class PlatformerSample method createPlayer.

private Entity createPlayer(double x, double y, double width, double height) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setGenerateGroundSensor(true);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().at(x, y).viewFromNodeWithBBox(new Rectangle(width, height, Color.BLUE)).with(physics).buildAndAttach(getGameWorld());
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Rectangle(javafx.scene.shape.Rectangle)

Example 53 with Rectangle

use of javafx.scene.shape.Rectangle in project FXGL by AlmasB.

the class PlatformerSample method createPlatforms.

private void createPlatforms() {
    Entities.builder().at(0, 500).viewFromNode(new Rectangle(120, 100, Color.GRAY)).bbox(new HitBox("Main", BoundingShape.chain(new Point2D(0, 0), new Point2D(120, 0), new Point2D(120, 100), new Point2D(0, 100)))).with(new PhysicsComponent()).buildAndAttach(getGameWorld());
    Entities.builder().at(180, 500).viewFromNode(new Rectangle(400, 100, Color.GRAY)).bbox(new HitBox("Main", BoundingShape.chain(new Point2D(0, 0), new Point2D(400, 0), new Point2D(400, 100), new Point2D(0, 100)))).with(new PhysicsComponent()).buildAndAttach(getGameWorld());
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle)

Example 54 with Rectangle

use of javafx.scene.shape.Rectangle in project FXGL by AlmasB.

the class RealPhysicsSample 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 55 with Rectangle

use of javafx.scene.shape.Rectangle in project FXGL by AlmasB.

the class JavaScriptSample method initGame.

@Override
protected void initGame() {
    Entities.builder().at(100, 100).viewFromNode(new Rectangle(40, 40)).with(new JSControl("spin_control.js")).buildAndAttach(getGameWorld());
    Entities.builder().at(180, 100).viewFromNode(new Rectangle(40, 40)).with(new JSControl("ccw_spin_control.js")).buildAndAttach(getGameWorld());
    getAssetLoader().loadScript("sample.js").call("sample");
}
Also used : Rectangle(javafx.scene.shape.Rectangle) JSControl(com.almasb.fxgl.entity.control.JSControl)

Aggregations

Rectangle (javafx.scene.shape.Rectangle)190 Point2D (javafx.geometry.Point2D)33 Text (javafx.scene.text.Text)28 Entity (com.almasb.fxgl.entity.Entity)27 Circle (javafx.scene.shape.Circle)20 Color (javafx.scene.paint.Color)18 HitBox (com.almasb.fxgl.physics.HitBox)16 Pane (javafx.scene.layout.Pane)16 Group (javafx.scene.Group)15 Node (javafx.scene.Node)14 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)12 Scene (javafx.scene.Scene)12 EntityView (com.almasb.fxgl.entity.view.EntityView)10 Label (javafx.scene.control.Label)10 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 TextFlow (javafx.scene.text.TextFlow)8 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)7 Bounds (javafx.geometry.Bounds)7 KeyCode (javafx.scene.input.KeyCode)7