Search in sources :

Example 76 with Entity

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

the class TiledFlippingSample method initGame.

@Override
protected void initGame() {
    getGameWorld().addEntityFactory(new TileFlippingEntityFactory());
    List<Entity> entities = setLevelFromMap("tmx/flipped_tiles.tmx").getEntities();
    // Rotate all of the objects (index 0 is tile layer)
    Entity f = entities.get(1);
    Entity x = entities.get(2);
    Entity g = entities.get(3);
    Entity l = entities.get(4);
    f.rotateBy(10.0);
    x.rotateBy(20.0);
    g.rotateBy(30.0);
    l.rotateBy(40.0);
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 77 with Entity

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

the class MassUnitsRTSSample 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).with(new IDComponent("" + x + "," + y, 0)).buildAndAttach();
        rect.setOnMouseClicked(event -> {
            // if left click do search, else place a red obstacle
            if (event.getButton() == MouseButton.PRIMARY) {
                var units = getGameWorld().getEntitiesFiltered(e -> e.getPropertyOptional("type").isPresent());
                var cells = grid.getWalkableCells().stream().sorted(Comparator.comparingInt(cell -> cell.distance(grid.get(x, y)))).limit(units.size()).collect(Collectors.toList());
                Collections.shuffle(cells);
                for (int i = 0; i < units.size(); i++) {
                    var unit = units.get(i);
                    var cell = cells.get(i);
                    unit.getComponent(AStarMoveComponent.class).moveToCell(cell);
                }
            }
        });
    });
    for (int i = 0; i < 485; i++) {
        var color = FXGLMath.randomColor().darker().darker();
        var e = entityBuilder().viewWithBBox(new Circle(CELL_WIDTH / 4, CELL_WIDTH / 4, CELL_WIDTH / 4, color)).with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 300)).with(new AStarMoveComponent(grid)).with("type", "unit").buildAndAttach();
    }
}
Also used : Color(javafx.scene.paint.Color) AStarPathfinder(com.almasb.fxgl.pathfinding.astar.AStarPathfinder) MouseButton(javafx.scene.input.MouseButton) AStarMoveComponent(com.almasb.fxgl.pathfinding.astar.AStarMoveComponent) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) Rectangle(javafx.scene.shape.Rectangle) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Collectors(java.util.stream.Collectors) AStarGrid(com.almasb.fxgl.pathfinding.astar.AStarGrid) IDComponent(com.almasb.fxgl.entity.components.IDComponent) GameSettings(com.almasb.fxgl.app.GameSettings) CellState(com.almasb.fxgl.pathfinding.CellState) Circle(javafx.scene.shape.Circle) Comparator(java.util.Comparator) GameApplication(com.almasb.fxgl.app.GameApplication) Collections(java.util.Collections) Entity(com.almasb.fxgl.entity.Entity) FXGL(com.almasb.fxgl.dsl.FXGL) AStarPathfinder(com.almasb.fxgl.pathfinding.astar.AStarPathfinder) Entity(com.almasb.fxgl.entity.Entity) Circle(javafx.scene.shape.Circle) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Rectangle(javafx.scene.shape.Rectangle) IDComponent(com.almasb.fxgl.entity.components.IDComponent) AStarMoveComponent(com.almasb.fxgl.pathfinding.astar.AStarMoveComponent) AStarGrid(com.almasb.fxgl.pathfinding.astar.AStarGrid)

Example 78 with Entity

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

the class SaveSample method initGame.

private void initGame(Point2D playerPos, Point2D enemyPos) {
    playerPosition = playerPos;
    enemyPosition = enemyPos;
    player = entityBuilder().type(Type.PLAYER).at(playerPosition).view(new Rectangle(40, 40, Color.BLUE)).build();
    playerControl = new PlayerControl();
    player.addComponent(playerControl);
    enemy = new Entity();
    // enemy.getTypeComponent().setValue(Type.ENEMY);
    // enemy.getPositionComponent().setValue(enemyPosition);
    // enemy.getViewComponent().setView(new EntityView(new Rectangle(40, 40, Color.RED)));
    getGameWorld().addEntities(player, enemy);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) Rectangle(javafx.scene.shape.Rectangle)

Example 79 with Entity

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

the class BlockCollisionComponent method applyVelocityManually.

private void applyVelocityManually() {
    var vel = randomMove.getVelocity();
    var dx = Math.signum(vel.x);
    var dy = Math.signum(vel.y);
    for (int i = 0; i < (int) vel.x; i++) {
        entity.translateX(dx);
        boolean collision = false;
        for (Entity block : blocks) {
            if (block.isColliding(entity)) {
                collision = true;
                break;
            }
        }
        if (collision) {
            entity.translateX(-dx);
            break;
        }
    }
    for (int i = 0; i < (int) vel.y; i++) {
        entity.translateY(dy);
        boolean collision = false;
        for (Entity block : blocks) {
            if (block.isColliding(entity)) {
                collision = true;
                break;
            }
        }
        if (collision) {
            entity.translateY(-dy);
            break;
        }
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 80 with Entity

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

the class CircleNNApp method initPhysics.

@Override
protected void initPhysics() {
    onCollisionBegin(BLOCK, BULLET, (block, bullet) -> {
        bullet.removeFromWorld();
    });
    onCollisionBegin(CIRCLE, BULLET, (circle, bullet) -> {
        if (bullet.getObject("owner") == circle)
            return;
        var point = circle.getCenter();
        int damage = bullet.getInt("damage");
        circle.getComponent(CircleComponent.class).takeHit(damage);
        if (!circle.isActive()) {
            Entity killer = bullet.getObject("owner");
            if (killer.isActive()) {
                killer.getComponent(CircleComponent.class).onKill();
            }
            onCircleDied();
            spawn("explosion", point);
        }
        bullet.removeFromWorld();
    });
    onCollisionBegin(CIRCLE, POWERUP, (circle, powerup) -> {
        circle.getComponent(CircleComponent.class).applyPowerup(powerup.getObject("powerupType"));
        powerup.removeFromWorld();
    });
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

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