Skip to content

Commit 2cb987d

Browse files
Refac
1 parent 3ebc077 commit 2cb987d

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

pacman-app-arcade-pacman/src/main/java/de/amr/pacmanfx/arcade/pacman/rendering/ArcadePacMan_GameRenderer.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import de.amr.pacmanfx.ui._2d.GameRenderer;
1616
import de.amr.pacmanfx.uilib.assets.SpriteSheet;
1717
import javafx.scene.canvas.Canvas;
18+
import javafx.scene.image.Image;
1819
import javafx.scene.paint.Color;
1920
import javafx.scene.text.Font;
2021
import javafx.scene.text.FontWeight;
@@ -52,41 +53,42 @@ public void drawHUD(GameContext gameContext, HUD hud, Vector2f sceneSize, long t
5253
requireNonNull(hud);
5354
if (!hud.isVisible()) return;
5455

56+
GameModel game = gameContext.theGame();
5557
if (hud.isScoreVisible()) {
5658
ctx.setFont(assets().arcadeFont(scaled(8)));
5759
ctx.setFill((ARCADE_WHITE));
58-
drawScore(gameContext.theGame().score(), "SCORE", TS(1), TS(1));
59-
drawScore(gameContext.theGame().highScore(), "HIGH SCORE", TS(14), TS(1));
60+
drawScore(game.score(), "SCORE", TS(1), TS(1));
61+
drawScore(game.highScore(), "HIGH SCORE", TS(14), TS(1));
6062
}
6163

6264
if (hud.isLevelCounterVisible()) {
6365
LevelCounter levelCounter = hud.theLevelCounter();
64-
float x = sceneSize.x() - 4 * TS, y = sceneSize.y() - 2 * TS + 2;
66+
float x = sceneSize.x() - TS(4), y = sceneSize.y() - TS(2) + 2;
6567
for (byte symbol : levelCounter.symbols()) {
6668
RectShort sprite = spriteSheet.spriteSeq(SpriteID.BONUS_SYMBOLS)[symbol];
6769
drawSpriteScaled(sprite, x, y);
68-
x -= TS * 2;
70+
x -= TS(2);
6971
}
7072
}
7173

7274
if (hud.isLivesCounterVisible()) {
7375
LivesCounter livesCounter = hud.theLivesCounter();
74-
float x = 2 * TS, y = sceneSize.y() - 2 * TS;
76+
float x = TS(2), y = sceneSize.y() - TS(2);
7577
RectShort sprite = spriteSheet.sprite(SpriteID.LIVES_COUNTER_SYMBOL);
7678
for (int i = 0; i < livesCounter.visibleLifeCount(); ++i) {
77-
drawSpriteScaled(sprite, x + TS * (2 * i), y);
79+
drawSpriteScaled(sprite, x + TS(2 * i), y);
7880
}
79-
if (gameContext.theGame().lifeCount() > livesCounter.maxLivesDisplayed()) {
81+
if (game.lifeCount() > livesCounter.maxLivesDisplayed()) {
8082
// show text indicating that more lives are available than symbols displayed (cheating may cause this)
8183
Font font = Font.font("Serif", FontWeight.BOLD, scaled(8));
82-
fillTextAtScaledPosition("%d".formatted(gameContext.theGame().lifeCount()), ARCADE_YELLOW, font,
83-
x - 14, y + TS);
84+
fillTextAtScaledPosition("%d".formatted(game.lifeCount()), ARCADE_YELLOW, font, x - 14, y + TS);
8485
}
8586
}
8687

8788
if (hud.isCreditVisible()) {
8889
String text = "CREDIT %2d".formatted(gameContext.theCoinMechanism().numCoins());
89-
fillTextAtScaledPosition(text, ARCADE_WHITE, assets().arcadeFont(scaled(8)), 2 * TS, sceneSize.y());
90+
Font font = assets.arcadeFont(scaled(8));
91+
fillTextAtScaledPosition(text, ARCADE_WHITE, font, TS(2), sceneSize.y());
9092
}
9193
}
9294

@@ -110,14 +112,14 @@ public void drawLevel(
110112
ctx.save();
111113
ctx.scale(scaling(), scaling());
112114
if (mazeHighlighted) {
113-
String assetNamespace = ArcadePacMan_UIConfig.ASSET_NAMESPACE;
114-
ctx.drawImage(assets.image(assetNamespace + ".flashing_maze"), 0, GameLevel.EMPTY_ROWS_OVER_MAZE * TS);
115+
Image flashingMaze = assets.image(ArcadePacMan_UIConfig.ASSET_NAMESPACE + ".flashing_maze");
116+
ctx.drawImage(flashingMaze, 0, GameLevel.EMPTY_ROWS_OVER_MAZE * TS);
115117
}
116118
else if (level.uneatenFoodCount() == 0) {
117-
drawSprite(spriteSheet.sprite(SpriteID.MAP_EMPTY), 0, GameLevel.EMPTY_ROWS_OVER_MAZE * TS);
119+
drawSprite(spriteSheet.sprite(SpriteID.MAP_EMPTY), 0, TS(GameLevel.EMPTY_ROWS_OVER_MAZE));
118120
}
119121
else {
120-
drawSprite(spriteSheet.sprite(SpriteID.MAP_FULL), 0, GameLevel.EMPTY_ROWS_OVER_MAZE * TS);
122+
drawSprite(spriteSheet.sprite(SpriteID.MAP_FULL), 0, TS(GameLevel.EMPTY_ROWS_OVER_MAZE));
121123
ctx.setFill(backgroundColor);
122124
level.worldMap().tiles()
123125
.filter(not(level::isEnergizerPosition))

pacman-ui-lib/src/main/java/de/amr/pacmanfx/uilib/assets/AssetStorage.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +56,36 @@ public String translated(String keyOrPattern, Object... args) {
5656
}
5757

5858
/**
59-
* Generic getter for asset value. The value is cast (without check) to the expected type (font, image etc.).
60-
* <p>Usage:
61-
* <p><code>Image image = assets.asset("key_for_image");</code>
59+
* Generic getter for asset value. The value is cast to the expected type (font, image etc.).
60+
* <p>Example:
61+
* <p><code>Image image = assets.valueOfType("key_for_image", Image.class);</code>
6262
*
6363
* @param <T> expected asset type
6464
* @param key asset key
6565
* @return stored value cast to expected type
66+
* @throws ClassCastException if specified type does not match asset value type
6667
*/
67-
@SuppressWarnings("unchecked")
68-
public <T> T get(String key) {
69-
T value = (T) assetMap.get(key);
68+
public <T> T asset(String key, Class<T> assetClass) {
69+
Object value = assetMap.get(key);
7070
if (value == null) {
71-
Logger.error("Asset not found, key={}", key);
71+
Logger.error("No asset value for key '{}' exists", key);
72+
return null;
7273
}
73-
return value;
74-
}
75-
76-
public <T> T valueOfType(String key, Class<T> type) {
77-
Object value = assetMap.get(key);
78-
if (type.isInstance(value)) {
79-
return type.cast(value);
74+
if (assetClass.isInstance(value)) {
75+
return assetClass.cast(value);
8076
}
81-
throw new ClassCastException("Asset value for key '%s' not of type %s".formatted(
82-
key, type.getSimpleName()
83-
));
77+
throw new ClassCastException("Asset for key '%s' is not of type %s but %s".formatted(
78+
key, assetClass.getSimpleName(), value.getClass().getSimpleName()));
8479
}
8580

86-
public Background background(String key) { return valueOfType(key, Background.class); }
81+
public Background background(String key) { return asset(key, Background.class); }
8782

88-
public Color color(String key) { return valueOfType(key, Color.class); }
83+
public Color color(String key) { return asset(key, Color.class); }
8984

90-
public Font font(String key) { return valueOfType(key, Font.class); }
85+
public Font font(String key) { return asset(key, Font.class); }
9186

9287
public Font font(String key, double size) { return Font.font(font(key).getFamily(), size); }
9388

94-
public Image image(String key) { return valueOfType(key, Image.class); }
89+
public Image image(String key) { return asset(key, Image.class); }
9590

9691
}

pacman-ui/src/main/java/de/amr/pacmanfx/ui/api/GameUI_Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ default void storeLocalAssetValue(AssetStorage assets, String localKey, Object v
6565
* @param <T> expected asset value type
6666
*/
6767
default <T> T localAssetValue(String localKey, Class<T> type) {
68-
return theUI().assets().valueOfType(globalAssetKey(localKey), type);
68+
return theUI().assets().asset(globalAssetKey(localKey), type);
6969
}
7070

7171
default Color localAssetColor(String localKey) {

0 commit comments

Comments
 (0)