make damage label stationary and not move with game camera

This commit is contained in:
Quillraven
2025-06-24 21:35:05 +02:00
parent ee39eab8eb
commit 95e1091e16
2 changed files with 29 additions and 11 deletions

View File

@@ -15,16 +15,16 @@ public class GameViewModel extends ViewModel {
private final AudioService audioService;
private int lifePoints;
private int maxLife;
private final Vector2 tmpVec2;
private Map.Entry<Vector2, Integer> playerDamage;
private final Vector2 tmpVec2;
public GameViewModel(GdxGame game) {
super(game);
this.audioService = game.getAudioService();
this.lifePoints = 0;
this.maxLife = 0;
this.tmpVec2 = new Vector2();
this.playerDamage = null;
this.tmpVec2 = new Vector2();
}
public void setMaxLife(int maxLife) {
@@ -58,10 +58,15 @@ public class GameViewModel extends ViewModel {
}
public void playerDamage(int amount, float x, float y) {
tmpVec2.set(x, y);
game.getViewport().project(tmpVec2);
this.playerDamage = Map.entry(tmpVec2, amount);
Vector2 position = new Vector2(x, y);
this.playerDamage = Map.entry(position, amount);
this.propertyChangeSupport.firePropertyChange(PLAYER_DAMAGE, null, this.playerDamage);
}
public Vector2 toScreenCoords(Vector2 position) {
tmpVec2.set(position);
game.getViewport().project(tmpVec2);
return tmpVec2;
}
}

View File

@@ -60,18 +60,31 @@ public class GameView extends View<GameViewModel> {
}
}
private Vector2 toStageCoords(Vector2 gamePosition) {
Vector2 resultPosition = viewModel.toScreenCoords(gamePosition);
stage.getViewport().unproject(resultPosition);
resultPosition.y = stage.getViewport().getWorldHeight() - resultPosition.y;
return resultPosition;
}
/**
* Shows animated damage text at the specified position.
*/
private void showDamage(Map.Entry<Vector2, Integer> damAndPos) {
Vector2 position = damAndPos.getKey();
final Vector2 position = damAndPos.getKey();
int damage = damAndPos.getValue();
stage.getViewport().unproject(position);
position.y = stage.getViewport().getWorldHeight() - position.y;
TextraLabel textraLabel = new TypingLabel("[%75]{JUMP=2.0;0.5;0.9}{RAINBOW}" + damage, skin, "small");
textraLabel.setPosition(position.x, position.y);
stage.addActor(textraLabel);
textraLabel.addAction(Actions.sequence(Actions.delay(1.25f), Actions.removeActor()));
textraLabel.addAction(
Actions.parallel(
Actions.sequence(Actions.delay(1.25f), Actions.removeActor()),
Actions.forever(Actions.run(() -> {
Vector2 stageCoords = toStageCoords(position);
textraLabel.setPosition(stageCoords.x, stageCoords.y);
}))
)
);
}
}