use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class MapGenerationSample method coloredMap.
private void coloredMap() {
// tile size
int size = 1;
int W = (getAppWidth() + 200) / size;
int H = (getAppHeight() + 200) / size;
Grid<HeightMapGenerator.HeightData> map = new Grid<>(HeightMapGenerator.HeightData.class, W, H, new CustomHeightMapGenerator(W, H));
AnimatedColor water = new AnimatedColor(Color.DARKBLUE, Color.AQUA);
AnimatedColor land = new AnimatedColor(Color.DARKGREEN, Color.LIGHTGREEN);
AnimatedColor desert = new AnimatedColor(Color.GREENYELLOW, Color.LIGHTYELLOW);
AnimatedColor snow = new AnimatedColor(Color.GREEN, Color.WHITESMOKE);
WritableImage image = new WritableImage(W, H);
map.forEach(cell -> {
AnimatedColor anim;
double adjustedValue;
if (cell.getHeight() < 0.2) {
anim = water;
adjustedValue = map(cell.getHeight(), 0.0, 0.2, 0.0, 1.0);
} else if (cell.getHeight() < 0.4) {
anim = land;
adjustedValue = map(cell.getHeight(), 0.2, 0.4, 0.0, 1.0);
} else if (cell.getHeight() < 0.9) {
anim = desert;
adjustedValue = map(cell.getHeight(), 0.4, 0.9, 0.0, 1.0);
} else {
anim = snow;
adjustedValue = map(cell.getHeight(), 0.9, 1.0, 0.0, 1.0);
}
image.getPixelWriter().setColor(cell.getX(), cell.getY(), anim.getValue(adjustedValue, interpolator.EASE_OUT()));
});
getGameScene().getViewport().bindToEntity(new Entity(), 0, 0);
var t = new Texture(image);
t.setTranslateX(-100);
t.setTranslateY(-100);
getGameScene().addGameView(new GameView(t, 0));
}
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(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).buildAndAttach();
rect.setOnMouseClicked(e -> {
// if left click do search, else place a red obstacle
if (e.getButton() == MouseButton.PRIMARY) {
var cells = pathfinder.findPath(0, 0, x, y);
cells.forEach(cell -> {
getGameWorld().getEntitiesAt(new Point2D(cell.getX() * CELL_WIDTH, cell.getY() * CELL_HEIGHT)).forEach(Entity::removeFromWorld);
});
} else {
grid.get(x, y).setState(CellState.NOT_WALKABLE);
rect.setFill(Color.RED);
}
});
});
var e = entityBuilder().viewWithBBox(new Rectangle(20, 20)).with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 100)).buildAndAttach();
e.getComponent(CellMoveComponent.class).setPositionToCell(1, 1);
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class RopeJointSample method initInput.
@Override
protected void initInput() {
getInput().addAction(new UserAction("LMB") {
private double x;
private double y;
@Override
protected void onActionBegin() {
x = getInput().getMouseXWorld();
y = getInput().getMouseYWorld();
}
@Override
protected void onActionEnd() {
var endx = getInput().getMouseXWorld();
var endy = getInput().getMouseYWorld();
spawnBullet(x, y, endx - x, endy - y);
}
}, MouseButton.PRIMARY);
onKeyDown(KeyCode.F, () -> {
Entity box = createPhysicsEntity();
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().addChild(texture("brick.png", 40, 40).superTexture(texture("brick.png", 40, 40), HorizontalDirection.RIGHT));
box.setRotationOrigin(new Point2D(40, 20));
getGameWorld().addEntity(box);
});
onBtnDown(MouseButton.SECONDARY, () -> {
Entity ball = createPhysicsEntity();
ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
ball.getViewComponent().addChild(texture("ball.png", 40, 40));
ball.setRotationOrigin(new Point2D(20, 20));
getGameWorld().addEntity(ball);
});
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class RevoluteJointSample method initGame.
@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.LIGHTGRAY);
entityBuilder().buildScreenBoundsAndAttach(50);
// platform
entityBuilder().at(400, 400).viewWithBBox(new Rectangle(500, 20, Color.BROWN)).with(new PhysicsComponent()).buildAndAttach();
PhysicsComponent physics = new PhysicsComponent();
physics.setFixtureDef(new FixtureDef().density(1.1f));
physics.setBodyType(BodyType.DYNAMIC);
Entity block = entityBuilder().at(600, 100).viewWithBBox(new Rectangle(80, 50)).with(physics).buildAndAttach();
PhysicsComponent physics2 = new PhysicsComponent();
physics2.setBodyType(BodyType.DYNAMIC);
FixtureDef fd = new FixtureDef();
fd.setDensity(1.0f);
physics2.setFixtureDef(fd);
Entity ball1 = entityBuilder().at(600, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics2).buildAndAttach();
PhysicsComponent physics3 = new PhysicsComponent();
physics3.setBodyType(BodyType.DYNAMIC);
physics3.setFixtureDef(fd);
Entity ball2 = entityBuilder().at(700, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics3).buildAndAttach();
physics2.getBody().setAngularDamping(1f);
physics3.getBody().setAngularDamping(1f);
getPhysicsWorld().addRevoluteJoint(block, ball1, new Point2D(80, 50), new Point2D(15, 15));
getPhysicsWorld().addRevoluteJoint(block, ball2, new Point2D(0, 50), new Point2D(15, 15));
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class RevoluteJointSample method initInput.
@Override
protected void initInput() {
getInput().addAction(new UserAction("LMB") {
private double x;
private double y;
@Override
protected void onActionBegin() {
x = getInput().getMouseXWorld();
y = getInput().getMouseYWorld();
}
@Override
protected void onActionEnd() {
var endx = getInput().getMouseXWorld();
var endy = getInput().getMouseYWorld();
spawnBullet(x, y, endx - x, endy - y);
}
}, MouseButton.PRIMARY);
onKeyDown(KeyCode.F, () -> {
Entity box = createPhysicsEntity();
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().addChild(texture("brick.png", 40, 40).superTexture(texture("brick.png", 40, 40), HorizontalDirection.RIGHT));
box.setRotationOrigin(new Point2D(40, 20));
getGameWorld().addEntity(box);
});
onBtnDown(MouseButton.SECONDARY, () -> {
Entity ball = createPhysicsEntity();
ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
ball.getViewComponent().addChild(texture("ball.png", 40, 40));
ball.setRotationOrigin(new Point2D(20, 20));
getGameWorld().addEntity(ball);
});
}
Aggregations