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);
}
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());
}
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);
}
}
}
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);
});
}
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();
}
});
}
Aggregations