Skip to content

Commit

Permalink
Simplified, use builder
Browse files Browse the repository at this point in the history
  • Loading branch information
armin-reichert committed Jan 6, 2025
1 parent f516869 commit dddcbdc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ protected void invalidated() {
private final Group mazeGroup = new Group();
private final Group house3D = new Group();
private Box floor;
private final Message3D message3D;
private final Pac3D pac3D;
private final List<Ghost3DAppearance> ghost3DAppearances;
private final Map<Vector2i, Pellet3D> pellets3D = new HashMap<>();
private final ArrayList<Energizer3D> energizers3D = new ArrayList<>();
private final LivesCounter3D livesCounter3D;
private Door3D door3D;
private Message3D message3D;

private Bonus3D bonus3D;

Expand All @@ -119,16 +119,12 @@ public GameLevel3D(GameContext context) {
livesCounter3D = createLivesCounter3D(game.canStartNewGame());
livesCounter3D.livesCountPy.bind(livesCounterPy);

message3D = new Message3D("", context.assets().font("font.arcade", 6), Color.YELLOW, Color.WHITE);
message3D.setRotation(Rotate.X_AXIS, 90);
message3D.setVisible(false);

buildWorld3D(world, coloring);

// Walls and house must be added after the guys! Otherwise, transparency is not working correctly.
getChildren().addAll(pac3D.shape3D(), pac3D.shape3D().light());
getChildren().addAll(ghost3DAppearances);
getChildren().addAll(message3D, livesCounter3D, worldGroup);
getChildren().addAll(livesCounter3D, worldGroup);

PY_3D_WALL_HEIGHT.addListener((py,ov,nv) -> obstacleBaseHeightPy.set(nv.doubleValue()));
wallOpacityPy.bind(PY_3D_WALL_OPACITY);
Expand Down Expand Up @@ -380,18 +376,28 @@ private void addFood3D(GameWorld world, Model3D pelletModel3D, Material foodMate
}

public void showAnimatedMessage(String text, double displaySeconds, double centerX, double y) {
message3D.setText(text);
message3D.setVisible(true);
getChildren().remove(message3D);
message3D = Message3D.newBuilder()
.text(text)
.font(context.assets().font("font.arcade", 6))
.borderColor(Color.WHITE)
.textColor(Color.YELLOW)
.build();
getChildren().add(message3D);

double halfHeight = 0.5 * message3D.getBoundsInLocal().getHeight();
message3D.setTranslateX(centerX - 0.5 * message3D.getFitWidth());
message3D.setTranslateY(y);
message3D.setTranslateZ(halfHeight); // just under floor

var moveUpAnimation = new TranslateTransition(Duration.seconds(1), message3D);
moveUpAnimation.setToZ(-(halfHeight + 0.5 * obstacleBaseHeightPy.get()));

var moveDownAnimation = new TranslateTransition(Duration.seconds(1), message3D);
moveDownAnimation.setDelay(Duration.seconds(displaySeconds));
moveDownAnimation.setToZ(halfHeight);
moveDownAnimation.setOnFinished(e -> message3D.setVisible(false));

new SequentialTransition(moveUpAnimation, moveDownAnimation).play();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@
*/
package de.amr.games.pacman.ui3d.level;

import javafx.geometry.Point3D;
import de.amr.games.pacman.lib.Globals;
import javafx.scene.canvas.Canvas;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import org.tinylog.Logger;

import static java.util.Objects.requireNonNull;

/**
* @author Armin Reichert
*/
public class Message3D extends ImageView {

public static Builder newBuilder() {
return new Builder();
}

public static class Builder {
private final Message3D message = new Message3D();
public Builder font(Font font) { message.font = Globals.assertNotNull(font); return this; }
public Builder borderColor(Color color) { message.borderColor = Globals.assertNotNull(color); return this; }
public Builder textColor(Color color) { message.textColor = Globals.assertNotNull(color); return this; }
public Builder text(String text) { message.text = Globals.assertNotNull(text); return this; }
public Message3D build() { message.updateImage(); return message; }
}

private static final int MARGIN = 3;
private static final int QUALITY = 3;

private Font font;
private Color borderColor;
private Color textColor;
private String text;
private boolean batchUpdate;

public Message3D(String text, Font font, Color textColor, Color borderColor) {
beginBatch();
setFont(font);
setBorderColor(borderColor);
setTextColor(textColor);
setText(text);
endBatch();
}

public Message3D() {
this("Hello, world!", Font.font("Sans", 8), Color.WHITE, Color.grayRgb(200));
private Message3D() {
setRotationAxis(Rotate.X_AXIS);
setRotate(90);
}

private void updateImage() {
if (batchUpdate) {
return;
}
double width = text.length() * font.getSize() + MARGIN;
double height = font.getSize() + MARGIN;

Expand All @@ -64,53 +64,4 @@ private void updateImage() {
setFitHeight(height);
Logger.trace("New source produced");
}

public void setRotation(Point3D axis, double angle) {
requireNonNull(axis);
setRotationAxis(axis);
setRotate(angle);
}

public void beginBatch() {
batchUpdate = true;
}

public void endBatch() {
batchUpdate = false;
updateImage();
}

public void setFont(Font font) {
requireNonNull(font);
if (!font.equals(this.font)) {
this.font = font;
updateImage();
}
}

public void setBorderColor(Color color) {
requireNonNull(color);
if (!color.equals(this.borderColor)) {
this.borderColor = color;
updateImage();
}
}

public void setTextColor(Color color) {
requireNonNull(color);
if (!color.equals(this.textColor)) {
this.textColor = color;
updateImage();
}
}

public void setText(String text) {
if (text == null) {
text = "";
}
if (!text.equals(this.text)) {
this.text = text;
updateImage();
}
}
}

0 comments on commit dddcbdc

Please sign in to comment.