diff --git a/core/src/main/java/io/github/com/quillraven/ui/model/GameViewModel.java b/core/src/main/java/io/github/com/quillraven/ui/model/GameViewModel.java index e6b6c45..a14bcbd 100644 --- a/core/src/main/java/io/github/com/quillraven/ui/model/GameViewModel.java +++ b/core/src/main/java/io/github/com/quillraven/ui/model/GameViewModel.java @@ -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 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; + + } } diff --git a/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java b/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java index a9384b2..be88679 100644 --- a/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java +++ b/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java @@ -60,18 +60,31 @@ public class GameView extends View { } } + 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 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); + })) + ) + ); } }