chore: use libGDX functions instead of System.currentTimeMillis

This commit is contained in:
2025-10-01 14:30:40 +07:00
parent 65880853ac
commit 57aecaf002
3 changed files with 23 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import org.vibecoders.moongazer.Game;
import org.vibecoders.moongazer.State; import org.vibecoders.moongazer.State;
import org.vibecoders.moongazer.managers.Assets; import org.vibecoders.moongazer.managers.Assets;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
@ -17,8 +18,8 @@ import com.badlogic.gdx.utils.ScreenUtils;
public class Intro extends Scene { public class Intro extends Scene {
private Texture logo; private Texture logo;
private Game game; private Game game;
private long startTime; private float totalTime = 0;
private long endTime = 0; private boolean end = false;
/** /**
* Initializes the intro scene, starts loading assets. * Initializes the intro scene, starts loading assets.
@ -26,7 +27,6 @@ public class Intro extends Scene {
public Intro(Game game) { public Intro(Game game) {
this.game = game; this.game = game;
logo = Assets.getAsset("icons/logo.png", Texture.class); logo = Assets.getAsset("icons/logo.png", Texture.class);
startTime = System.currentTimeMillis() + 500;
log.info("Starting to load all remaining assets..."); log.info("Starting to load all remaining assets...");
Assets.loadAll(); Assets.loadAll();
// Create scenes // Create scenes
@ -42,7 +42,9 @@ public class Intro extends Scene {
*/ */
@Override @Override
public void render(SpriteBatch batch) { public void render(SpriteBatch batch) {
if (System.currentTimeMillis() > endTime + 2000 && endTime != 0) { totalTime += Gdx.graphics.getDeltaTime();
log.trace("Intro total time: {}", totalTime);
if (totalTime > 5f) {
if (game.transition == null) { if (game.transition == null) {
Assets.waitUntilLoaded(); Assets.waitUntilLoaded();
log.info("All assets loaded successfully."); log.info("All assets loaded successfully.");
@ -53,12 +55,12 @@ public class Intro extends Scene {
} }
ScreenUtils.clear(Color.BLACK); ScreenUtils.clear(Color.BLACK);
// log.debug("Rendering logo at position: ({}, {})", WINDOW_WIDTH / 2 - logo.getWidth() / 4, WINDOW_HEIGHT / 2 - logo.getHeight() / 4); // log.debug("Rendering logo at position: ({}, {})", WINDOW_WIDTH / 2 - logo.getWidth() / 4, WINDOW_HEIGHT / 2 - logo.getHeight() / 4);
var currentOpacity = (float) (System.currentTimeMillis() - startTime) / 1000; var currentOpacity = totalTime;
if (currentOpacity > 1) { if (currentOpacity > 1) {
if (endTime == 0) { if (!end) {
endTime = System.currentTimeMillis() + 2000; end = true;
} }
currentOpacity = 1 - ((float) (System.currentTimeMillis() - endTime) / 1000); currentOpacity = 5f - totalTime;
} }
// Multiply with any externally applied alpha (e.g., Transition) // Multiply with any externally applied alpha (e.g., Transition)
float externalAlpha = batch.getColor().a; float externalAlpha = batch.getColor().a;

View File

@ -18,6 +18,7 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.video.VideoPlayer; import com.badlogic.gdx.video.VideoPlayer;
import com.badlogic.gdx.video.VideoPlayerCreator; import com.badlogic.gdx.video.VideoPlayerCreator;
@ -133,7 +134,7 @@ public class MainMenu extends Scene {
@Override @Override
public boolean keyDown(InputEvent event, int keycode) { public boolean keyDown(InputEvent event, int keycode) {
sKeyDown(event, keycode); sKeyDown(event, keycode);
currentKeyDown.put(keycode, System.currentTimeMillis()); currentKeyDown.put(keycode, TimeUtils.millis());
return true; return true;
} }
}); });
@ -201,9 +202,9 @@ public class MainMenu extends Scene {
for (Map.Entry<Integer, Long> entry : currentKeyDown.entrySet()) { for (Map.Entry<Integer, Long> entry : currentKeyDown.entrySet()) {
Integer keyCode = entry.getKey(); Integer keyCode = entry.getKey();
Long timeStamp = entry.getValue(); Long timeStamp = entry.getValue();
if (System.currentTimeMillis() - timeStamp > 100) { if (TimeUtils.millis() - timeStamp > 100) {
sKeyDown(null, keyCode); sKeyDown(null, keyCode);
currentKeyDown.put(keyCode, System.currentTimeMillis()); currentKeyDown.put(keyCode, TimeUtils.millis());
} }
} }
startVideoOnce(); startVideoOnce();

View File

@ -3,6 +3,7 @@ package org.vibecoders.moongazer.scenes;
import org.vibecoders.moongazer.Game; import org.vibecoders.moongazer.Game;
import org.vibecoders.moongazer.State; import org.vibecoders.moongazer.State;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
/** /**
@ -12,15 +13,16 @@ public class Transition extends Scene {
private Scene from; private Scene from;
private Scene to; private Scene to;
private State targetState; private State targetState;
private long startTime; private float totalTime = 0f;
private long duration; private long duration;
/** /**
* Creates a new transition between two scenes. * Creates a new transition between two scenes.
* @param from The scene to transition from. *
* @param to The scene to transition to. * @param from The scene to transition from.
* @param to The scene to transition to.
* @param targetState The target state of the game after the transition. * @param targetState The target state of the game after the transition.
* @param duration The duration of the transition in milliseconds. * @param duration The duration of the transition in milliseconds.
*/ */
public Transition(Game game, Scene from, Scene to, State targetState, long duration) { public Transition(Game game, Scene from, Scene to, State targetState, long duration) {
// Transition does not need to render UI elements // Transition does not need to render UI elements
@ -30,16 +32,17 @@ public class Transition extends Scene {
this.to = to; this.to = to;
this.targetState = targetState; this.targetState = targetState;
this.duration = duration; this.duration = duration;
startTime = System.currentTimeMillis();
} }
/** /**
* Renders the transition effect. * Renders the transition effect.
*
* @param batch The SpriteBatch to draw with. * @param batch The SpriteBatch to draw with.
*/ */
@Override @Override
public void render(SpriteBatch batch) { public void render(SpriteBatch batch) {
var toOpacity = ((float) (System.currentTimeMillis() - startTime)) / duration; totalTime += Gdx.graphics.getDeltaTime();
var toOpacity = totalTime / (((float) duration) / 1000);
if (toOpacity >= 0.99) { if (toOpacity >= 0.99) {
log.trace("Transition complete to state: {}", targetState); log.trace("Transition complete to state: {}", targetState);
game.state = targetState; game.state = targetState;