chore(scene): cleanup main menu

This commit is contained in:
2025-10-01 02:45:51 +07:00
parent 967926c4d2
commit a4bece69f9
2 changed files with 37 additions and 72 deletions

View File

@ -61,12 +61,11 @@ public class Intro extends Scene {
currentOpacity = 1 - ((float) (System.currentTimeMillis() - endTime) / 1000);
}
// Multiply with any externally applied alpha (e.g., Transition)
float externalAlpha = batch.getColor().a; // alpha set by Transition before calling render
float externalAlpha = batch.getColor().a;
float finalAlpha = currentOpacity * externalAlpha;
batch.setColor(1, 1, 1, finalAlpha);
batch.draw(logo, WINDOW_WIDTH / 2 - logo.getWidth() / 4, WINDOW_HEIGHT / 2 - logo.getHeight() / 4,
logo.getWidth() / 2, logo.getHeight() / 2);
// Reset color for safety (Transition will set again anyway)
batch.setColor(1,1,1,externalAlpha);
batch.setColor(1, 1, 1, externalAlpha);
}
}

View File

@ -2,17 +2,17 @@ package org.vibecoders.moongazer.scenes;
import static org.vibecoders.moongazer.Constants.*;
import java.io.FileNotFoundException;
import org.vibecoders.moongazer.Game;
import org.vibecoders.moongazer.State;
import org.vibecoders.moongazer.managers.Assets;
import org.vibecoders.moongazer.ui.UIImageButton;
import org.vibecoders.moongazer.ui.UITextButton;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.video.VideoPlayer;
import com.badlogic.gdx.video.VideoPlayerCreator;
@ -23,11 +23,6 @@ public class MainMenu extends Scene {
private Texture titleTexture;
private float titleY, titleX, titleWidth, titleHeight;
private boolean videoPrepared = false;
private boolean firstFrameLogged = false;
private long videoStartTime;
private static final String WEBM_PATH = "videos/main_menu_background.webm";
private static final String OGV_PATH = "videos/main_menu_background.ogv";
public MainMenu(Game game) {
super(game);
@ -37,25 +32,17 @@ public class MainMenu extends Scene {
}
private void initVideo() {
videoPlayer = VideoPlayerCreator.createVideoPlayer();
videoFileHandle = Assets.getAsset("videos/main_menu_background.webm", FileHandle.class);
try {
videoPlayer = VideoPlayerCreator.createVideoPlayer();
FileHandle webm = Gdx.files.internal(WEBM_PATH);
FileHandle ogv = Gdx.files.internal(OGV_PATH);
if (webm.exists()) {
videoFileHandle = webm;
log.info("Using menu background (WebM): {}", webm.path());
} else if (ogv.exists()) {
videoFileHandle = ogv;
log.info("Using menu background (OGV): {}", ogv.path());
} else {
log.warn("No background video found (expected {} or {}).", WEBM_PATH, OGV_PATH);
}
} catch (Exception e) {
log.error("Cannot create VideoPlayer", e);
videoPlayer.load(videoFileHandle);
} catch (FileNotFoundException e) {
log.error("Failed to load video", e);
}
}
private void initUI() {
// Title
titleTexture = Assets.getAsset("textures/main_menu/title.png", Texture.class);
float targetTitleWidth = 500f;
float scale = targetTitleWidth / titleTexture.getWidth();
@ -64,12 +51,13 @@ public class MainMenu extends Scene {
titleX = (WINDOW_WIDTH - titleWidth) / 2f;
titleY = WINDOW_HEIGHT / 2f - titleHeight / 8f;
// Buttons
var font = Assets.getFont("ui", 24);
UITextButton playButton = new UITextButton("Play", font);
UITextButton loadButton = new UITextButton("Load", font);
UITextButton settingsButton = new UITextButton("Settings", font);
UITextButton exitButton = new UITextButton("Exit", font);
int buttonWidth = 300;
int buttonHeight = 80;
@ -77,16 +65,20 @@ public class MainMenu extends Scene {
loadButton.setSize(buttonWidth, buttonHeight);
settingsButton.setSize(buttonWidth, buttonHeight);
exitButton.setSize(buttonWidth, buttonHeight);
int centerX = WINDOW_WIDTH / 2 - buttonWidth / 2;
int startY = WINDOW_HEIGHT / 2 - buttonHeight / 2;
int spacing = 65;
playButton.setSize(buttonWidth, buttonHeight); playButton.setPosition(centerX, startY);
loadButton.setSize(buttonWidth, buttonHeight); loadButton.setPosition(centerX, startY - spacing);
settingsButton.setSize(buttonWidth, buttonHeight); settingsButton.setPosition(centerX, startY - spacing * 2);
exitButton.setSize(buttonWidth, buttonHeight); exitButton.setPosition(centerX, startY - spacing * 3);
playButton.setSize(buttonWidth, buttonHeight);
playButton.setPosition(centerX, startY);
loadButton.setSize(buttonWidth, buttonHeight);
loadButton.setPosition(centerX, startY - spacing);
settingsButton.setSize(buttonWidth, buttonHeight);
settingsButton.setPosition(centerX, startY - spacing * 2);
exitButton.setSize(buttonWidth, buttonHeight);
exitButton.setPosition(centerX, startY - spacing * 3);
playButton.onClick(() -> log.debug("Play clicked"));
loadButton.onClick(() -> log.debug("Load clicked"));
settingsButton.onClick(() -> {
@ -108,56 +100,30 @@ public class MainMenu extends Scene {
game.stage.addActor(root);
}
private void ensureVideoStarted() {
if (videoPlayer == null || videoPrepared) return;
if (videoFileHandle == null || !videoFileHandle.exists()) return;
if (game.transition == null && game.state != State.MAIN_MENU) return;
startVideo();
}
private void startVideo() {
try {
videoPlayer.setLooping(true);
videoPlayer.play(videoFileHandle);
videoPrepared = true;
videoStartTime = TimeUtils.millis();
} catch (Exception e) {
log.error("Failed to play video: {}", videoFileHandle.path(), e);
}
}
public void forceStartVideo() { // used by Transition for early warm-up
if (!videoPrepared && videoFileHandle != null && videoFileHandle.exists()) startVideo();
private void startVideoOnce() {
if (videoPlayer == null || videoPrepared)
return;
if (videoFileHandle == null || !videoFileHandle.exists())
return;
if (game.transition == null && game.state != State.MAIN_MENU)
return;
videoPlayer.setLooping(true);
videoPlayer.play();
videoPrepared = true;
}
@Override
public void render(SpriteBatch batch) {
ensureVideoStarted();
if (videoPlayer != null && videoPrepared) {
videoPlayer.update();
Texture videoTexture = videoPlayer.getTexture();
if (videoTexture != null) {
if (!firstFrameLogged) {
firstFrameLogged = true;
log.info("Menu video first frame in {} ms", TimeUtils.timeSinceMillis(videoStartTime));
}
batch.draw(videoTexture, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
} else {
batch.draw(Assets.getBlackTexture(), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
}
} else {
batch.draw(Assets.getBlackTexture(), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
}
startVideoOnce();
videoPlayer.update();
Texture videoTexture = videoPlayer.getTexture();
batch.draw(videoTexture, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
batch.draw(titleTexture, titleX, titleY, titleWidth, titleHeight);
}
public void updateVideo() {
if (videoPlayer != null && videoPrepared) videoPlayer.update();
}
@Override
public void dispose() {
super.dispose();
if (videoPlayer != null) videoPlayer.dispose();
videoPlayer.dispose();
}
}