Search in sources :

Example 1 with EntityView

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

the class FXShooterApp method spawnEnemy.

private void spawnEnemy() {
    Entity enemy = new Entity();
    enemy.getTypeComponent().setValue(EntityType.ENEMY);
    enemy.getPositionComponent().setValue(getWidth(), 460);
    enemy.getViewComponent().setView(new EntityView(new Rectangle(40, 40, Color.RED)), true);
    enemy.addComponent(new CollidableComponent(true));
    enemy.addComponent(new HPComponent(5));
    enemy.addControl(new EnemyControl(new Point2D(getWidth() / 2, getHeight() / 2)));
    getGameWorld().addEntity(enemy);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) EntityView(com.almasb.fxgl.entity.view.EntityView) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle)

Example 2 with EntityView

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

the class PlayerControl method shoot.

public void shoot(Point2D direction) {
    Entity bullet = new Entity();
    bullet.getTypeComponent().setValue(FXShooterApp.EntityType.BULLET);
    bullet.getPositionComponent().setValue(getEntity().getComponent(PositionComponent.class).getValue().add(20, 20));
    bullet.getViewComponent().setView(new EntityView(new Rectangle(10, 2, Color.BLACK)), true);
    bullet.addComponent(new CollidableComponent(true));
    bullet.addControl(new OffscreenCleanControl());
    bullet.addControl(new ProjectileControl(direction, 10 * 60));
    BulletComponent bulletData = new BulletComponent();
    bulletData.setDamage(1);
    bulletData.setHp(1);
    bulletData.setSpeed(10);
    bullet.addComponent(bulletData);
    getEntity().getWorld().addEntity(bullet);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) EntityView(com.almasb.fxgl.entity.view.EntityView) PositionComponent(com.almasb.fxgl.entity.component.PositionComponent) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle) ProjectileControl(com.almasb.fxgl.entity.control.ProjectileControl) OffscreenCleanControl(com.almasb.fxgl.entity.control.OffscreenCleanControl)

Example 3 with EntityView

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

the class ViewComponent method setView.

/**
 * Set view. The generate bbox flag tells the component
 * whether it should generate bbox from the given view.
 *
 * Note: the generated bounding box is an approximation based on
 * the layout bounds of the view object.
 *
 * @param view the view
 * @param generateBoundingBox generate bbox flag
 */
public void setView(Node view, boolean generateBoundingBox) {
    EntityView entityView = view instanceof EntityView ? (EntityView) view : new EntityView(view);
    this.view.getNodes().setAll(entityView.getNodes());
    if (showBBox()) {
        this.view.addNode(debugBBox);
    }
    if (generateBoundingBox) {
        generateBBox();
    }
}
Also used : EntityView(com.almasb.fxgl.entity.view.EntityView)

Example 4 with EntityView

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

the class RaycastSample method initGame.

@Override
protected void initGame() {
    spawnWall(500, 100, 50, 50);
    spawnWall(550, 150, 50, 50);
    spawnWall(600, 200, 50, 50);
    spawnWall(600, 400, 50, 50);
    spawnWall(300, 450, 50, 50);
    spawnWall(500, 550, 50, 50);
    spawnWall(300, 300, 50, 50);
    laser.setStroke(Color.RED);
    laser.setStrokeWidth(2);
    laser.setStartY(300);
    getGameScene().addGameView(new EntityView(laser));
}
Also used : EntityView(com.almasb.fxgl.entity.view.EntityView)

Example 5 with EntityView

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

the class JointSample method initGame.

@Override
protected void initGame() {
    getGameWorld().addEntity(Entities.makeScreenBounds(50));
    PhysicsComponent physics = new PhysicsComponent();
    Entity block = Entities.builder().at(200, 200).viewFromNodeWithBBox(new Rectangle(50, 50)).with(physics).buildAndAttach(getGameWorld());
    PhysicsComponent physics2 = new PhysicsComponent();
    physics2.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(1.0f);
    physics2.setFixtureDef(fd);
    Entity ball = Entities.builder().at(200, 300).bbox(new HitBox("main", BoundingShape.circle(15))).viewFromNode(getAssetLoader().loadTexture("ball.png", 30, 30)).with(physics2).buildAndAttach(getGameWorld());
    Line line = new Line();
    line.setStartX(block.getCenter().getX());
    line.setStartY(block.getCenter().getY());
    // line.startXProperty().bind(block.getPositionComponent().xProperty());
    // line.startYProperty().bind(block.getPositionComponent().yProperty());
    line.endXProperty().bind(ball.getPositionComponent().xProperty().add(15));
    line.endYProperty().bind(ball.getPositionComponent().yProperty().add(15));
    getGameScene().addGameView(new EntityView(line));
    RevoluteJointDef rDef = new RevoluteJointDef();
    rDef.bodyA = physics.getBody();
    rDef.bodyB = physics2.getBody();
    rDef.collideConnected = false;
    rDef.localAnchorA = new Vec2(0, 0);
    rDef.localAnchorB = new Vec2(0, 5);
    rDef.enableLimit = true;
    rDef.lowerAngle = (float) (FXGLMath.toRadians(-180.0f));
    rDef.upperAngle = (float) (FXGLMath.toRadians(180.0f));
    rDef.enableMotor = true;
    rDef.motorSpeed = (float) (FXGLMath.toRadians(30));
    rDef.maxMotorTorque = 15.0f;
    joint = (RevoluteJoint) getPhysicsWorld().getJBox2DWorld().createJoint(rDef);
}
Also used : Line(javafx.scene.shape.Line) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) EntityView(com.almasb.fxgl.entity.view.EntityView) Vec2(com.almasb.fxgl.core.math.Vec2) Rectangle(javafx.scene.shape.Rectangle) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef) RevoluteJointDef(com.almasb.fxgl.physics.box2d.dynamics.joints.RevoluteJointDef)

Aggregations

EntityView (com.almasb.fxgl.entity.view.EntityView)15 Rectangle (javafx.scene.shape.Rectangle)10 Entity (com.almasb.fxgl.entity.Entity)8 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)2 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)2 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)2 GameApplication (com.almasb.fxgl.app.GameApplication)1 Vec2 (com.almasb.fxgl.core.math.Vec2)1 Control (com.almasb.fxgl.entity.Control)1 Entities (com.almasb.fxgl.entity.Entities)1 RenderLayer (com.almasb.fxgl.entity.RenderLayer)1 Required (com.almasb.fxgl.entity.component.Required)1 ViewComponent (com.almasb.fxgl.entity.component.ViewComponent)1 OffscreenCleanControl (com.almasb.fxgl.entity.control.OffscreenCleanControl)1 ProjectileControl (com.almasb.fxgl.entity.control.ProjectileControl)1 Input (com.almasb.fxgl.input.Input)1 UserAction (com.almasb.fxgl.input.UserAction)1 HitBox (com.almasb.fxgl.physics.HitBox)1 FixtureDef (com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)1 RevoluteJointDef (com.almasb.fxgl.physics.box2d.dynamics.joints.RevoluteJointDef)1