use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.
the class UnoSample method initGame.
@Override
protected void initGame() {
centerCardPosition = new Point2D(getAppWidth() / 2 - 100 / 2, getAppHeight() / 2 - 150 / 2);
getGameWorld().addEntityFactory(new UnoFactory());
spawn("Background");
player = new Hand("Player");
enemy = new Hand("AI");
var currentCard = spawnCard(deck.drawCard());
currentCard.setPosition(centerCardPosition);
set("currentCard", currentCard);
nextHand = player;
cardsLayout.put(player, new Rectangle2D(0, getAppHeight() - 150, getAppWidth(), 150));
cardsLayout.put(enemy, new Rectangle2D(0, 0 - 150, getAppWidth(), 150));
cardsLayout.forEach((hand, bounds) -> {
hand.setChangeCallback(() -> {
hand.cardsProperty().sort(Comparator.comparingInt(e -> {
Card card = e.getObject("card");
return card.getSuit().ordinal() * 1000 + card.getRank().ordinal();
}));
evaluateAndLayout(hand);
});
if (hand == player) {
getWorldProperties().<Entity>addListener("currentCard", (prev, now) -> {
evaluateAndLayout(hand);
});
}
});
for (int i = 0; i < NUM_BASE_CARDS; i++) {
player.addCard(spawnCard(deck.drawCard()));
enemy.addCard(spawnCard(deck.drawCard()));
}
}
use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.
the class DevMenuSample method initInput.
@Override
protected void initInput() {
onKeyDown(KeyCode.F, () -> {
System.out.println(getop("pos").hashCode());
Vec2 v = geto("pos");
v.set(FXGLMath.randomPoint(new Rectangle2D(0, 0, getAppWidth(), getAppHeight())));
set("pos", v);
System.out.println(geto("pos").toString());
});
}
use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.
the class BenchmarkSample method initGame.
@Override
protected void initGame() {
frames = 0;
components = new Component[NUM_OBJECTS];
textures = new Texture[NUM_OBJECTS];
getGameWorld().addEntityFactory(new BenchmarkFactory());
for (int i = 0; i < NUM_OBJECTS; i++) {
if (IS_JAVAFX) {
var t = texture("ball.png", 100, 100);
textures[i] = t;
addUINode(t, random(100, 500), random(100, 500));
} else {
var e = spawn("ball");
e.setUpdateEnabled(false);
// var c = new ProjectileComponent(new Point2D(random(0.0, 1.0), random(0.0, 1.0)), FXGLMath.map(i, 0, 2000, 10, 60));
var c = new RandomMoveComponent(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()), random(10, 400));
components[i] = c;
ComponentHelper.setEntity(c, e);
}
}
}
use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.
the class CircleNNFactory method newCircle.
@Spawns("circle")
public Entity newCircle(SpawnData data) {
var circleComponent = new CircleComponent();
var color = FXGLMath.randomColor().brighter();
var view = new Circle(32, 32, 30, null);
view.setStrokeWidth(2);
view.setStroke(color);
var hp = new HealthIntComponent(49);
var viewHP = new Circle(32, 32, 30);
viewHP.radiusProperty().bind(hp.valueProperty().divide(49.0).multiply(30));
var viewRank = getUIFactoryService().newText("", Color.MIDNIGHTBLUE, 12.0);
viewRank.setTranslateX(32);
viewRank.setTranslateY(32);
var e = entityBuilder(data).type(CIRCLE).bbox(new HitBox(BoundingShape.box(64, 64))).view(view).view(viewHP).collidable().with("isShielded", false).with("rank", 1).with(new TimeComponent()).with(new EffectComponent()).with(hp).with(new RandomMoveComponent(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()), 300)).with(circleComponent).with(new BlockCollisionComponent()).build();
viewHP.fillProperty().bind(Bindings.when(e.getProperties().booleanProperty("isShielded")).then(Color.WHITE).otherwise(Color.color(0.5, 0.78, 0.2, 0.5).brighter()));
viewRank.textProperty().bind(e.getProperties().intProperty("rank").asString());
return e;
}
use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.
the class PolynomialFunctionSample method initGame.
@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.BLACK);
spawnPoints = new ArrayList<>();
dest = new HashMap<>();
for (int y = 0; y < getAppHeight(); y += 10) {
spawnPoints.add(new Point2D(0, y));
spawnPoints.add(new Point2D(getAppWidth(), y));
}
for (int x = 0; x < getAppWidth(); x += 10) {
spawnPoints.add(new Point2D(x, 0));
spawnPoints.add(new Point2D(x, getAppHeight()));
}
points = IntStream.rangeClosed(0, getAppWidth()).filter(i -> i % 2 == 0).map(x -> x - getAppWidth() / 2).mapToDouble(x -> x / PIXELS_PER_UNIT).mapToObj(x -> new Point2D(x * PIXELS_PER_UNIT + getAppWidth() / 2.0, getAppHeight() - (func.apply(x) * PIXELS_PER_UNIT + getAppHeight() / 2.0))).filter(p -> new Rectangle2D(0, 0, getAppWidth(), getAppHeight()).contains(p)).collect(Collectors.toList());
emitter = ParticleEmitters.newExplosionEmitter(300);
emitter.setBlendMode(BlendMode.ADD);
emitter.setMaxEmissions(Integer.MAX_VALUE);
emitter.setNumParticles(750);
emitter.setEmissionRate(0.02);
emitter.setSize(1, 24);
emitter.setSpawnPointFunction(i -> FXGLMath.random(spawnPoints).get());
emitter.setScaleFunction(i -> FXGLMath.randomPoint2D().multiply(0.01));
emitter.setExpireFunction(i -> Duration.seconds(random(0.55, 2.5)));
emitter.setAccelerationFunction(() -> Point2D.ZERO);
// emitter.setVelocityFunction(i -> Point2D.ZERO);
emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(1, 45)));
emitter.setSourceImage(texture("particles/circle_05.png", 32, 32).multiplyColor(Color.ORANGERED));
emitter.setAllowParticleRotation(true);
emitter.setControl(p -> {
var x = p.position.x;
var y = p.position.y;
var point = dest.getOrDefault(p, FXGLMath.random(points).get());
dest.put(p, point);
var v = point.subtract(x, y).normalize().multiply(0.016 * 1500);
p.velocity.x = p.velocity.x * 0.8f + (float) v.getX() * 0.2f;
p.velocity.y = p.velocity.y * 0.8f + (float) v.getY() * 0.2f;
});
var e = entityBuilder().with(new ParticleComponent(emitter)).buildAndAttach();
// e.xProperty().bind(getInput().mouseXWorldProperty().subtract(10));
// e.yProperty().bind(getInput().mouseYWorldProperty().subtract(10));
var lineX = new Line(0, getAppHeight() / 2.0, getAppWidth(), getAppHeight() / 2.0);
lineX.setStrokeType(StrokeType.OUTSIDE);
lineX.setStrokeWidth(3);
lineX.setStroke(Color.WHITE);
var lineY = new Line(getAppWidth() / 2.0, 0, getAppWidth() / 2.0, getAppHeight());
lineY.setStrokeType(StrokeType.OUTSIDE);
lineY.setStrokeWidth(3);
lineY.setStroke(Color.WHITE);
// addUINode(lineX);
// addUINode(lineY);
}
Aggregations