添加了新的 UI 资源:logo.png、uiskin.atlas 和 uiskin.json,以改进界面设计。 移除了过时的 FighterController 和 GameCore 类,以精简代码库。 引入了新的角色类:FighterList 和 Reimu,增加了角色选择选项。 实现了新的桌面屏幕:CharacterSelectScreen(角色选择屏幕)、GameScreen(游戏屏幕)、MainMenuScreen(主菜单屏幕)和 StartScreen(开始屏幕),以改善用户导航。 通过新的 ConnectClient、ConnectServer 和 NetworkManager 类建立了网络功能。 更新了工具类:ClearScreen、Font 和 SimpleFormatter,以提升功能。 创建了新的 ButtonActions 类来处理按钮交互。
143 lines
5.5 KiB
Java
143 lines
5.5 KiB
Java
package uno.mloluyu.desktop;
|
|
|
|
import com.badlogic.gdx.Gdx;
|
|
import com.badlogic.gdx.ScreenAdapter;
|
|
import com.badlogic.gdx.graphics.Color;
|
|
import com.badlogic.gdx.graphics.GL20;
|
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|
|
|
import uno.mloluyu.network.ConnectClient;
|
|
import uno.mloluyu.network.ConnectServer;
|
|
import static uno.mloluyu.util.Font.loadChineseFont;
|
|
import uno.mloluyu.util.ClearScreen;
|
|
|
|
public class NetworkSettingsScreen extends ScreenAdapter {
|
|
private final MainGame game;
|
|
private SpriteBatch batch;
|
|
private BitmapFont font;
|
|
private ShapeRenderer shapeRenderer;
|
|
|
|
private static final int BUTTON_WIDTH = 400;
|
|
private static final int BUTTON_HEIGHT = 80;
|
|
private static final int BUTTON_X = 760;
|
|
private static final int CREATE_ROOM_Y = 500;
|
|
private static final int JOIN_ROOM_Y = 380;
|
|
private static final int EXIT_Y = 260; // 退出按钮位置
|
|
|
|
public NetworkSettingsScreen(MainGame game) {
|
|
this.game = game;
|
|
}
|
|
|
|
@Override
|
|
public void show() {
|
|
batch = new SpriteBatch();
|
|
shapeRenderer = new ShapeRenderer();
|
|
font = loadChineseFont();
|
|
font.setColor(Color.WHITE);
|
|
font.getData().setScale(2f);
|
|
}
|
|
|
|
@Override
|
|
public void render(float delta) {
|
|
new ClearScreen();
|
|
|
|
int mouseX = Gdx.input.getX();
|
|
int mouseY = Gdx.graphics.getHeight() - Gdx.input.getY();
|
|
|
|
renderButtons(mouseX, mouseY);
|
|
renderTexts();
|
|
|
|
handleInput(mouseX, mouseY);
|
|
}
|
|
|
|
private void renderButtons(int mouseX, int mouseY) {
|
|
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
|
|
drawButton(CREATE_ROOM_Y, mouseX, mouseY);
|
|
drawButton(JOIN_ROOM_Y, mouseX, mouseY);
|
|
drawButton(EXIT_Y, mouseX, mouseY); // 新增退出按钮
|
|
shapeRenderer.end();
|
|
}
|
|
|
|
private void renderTexts() {
|
|
batch.begin();
|
|
font.draw(batch, "联机设置", BUTTON_X + 100, 650);
|
|
drawButtonText(CREATE_ROOM_Y, "创建房间");
|
|
drawButtonText(JOIN_ROOM_Y, "加入房间");
|
|
drawButtonText(EXIT_Y, "返回"); // 新增退出按钮文字
|
|
batch.end();
|
|
}
|
|
|
|
private void handleInput(int mouseX, int mouseY) {
|
|
if (Gdx.input.justTouched()) {
|
|
if (isHovered(mouseX, mouseY, BUTTON_X, CREATE_ROOM_Y)) {
|
|
Gdx.app.log("Network", "创建房间按钮被点击!");
|
|
new Thread(new ConnectServer(11455)).start();
|
|
|
|
// 添加服务器启动提示信息
|
|
Gdx.app.log("Network", "服务器已启动,等待玩家连接...");
|
|
System.out.println("服务器已启动,等待玩家连接...");
|
|
|
|
// 创建联机模式标识并传递到角色选择界面
|
|
CharacterSelectScreen characterSelectScreen = new CharacterSelectScreen(game);
|
|
characterSelectScreen.setMultiplayerMode(true); // 设置为联机模式
|
|
game.setScreen(characterSelectScreen);
|
|
|
|
} else if (isHovered(mouseX, mouseY, BUTTON_X, JOIN_ROOM_Y)) {
|
|
Gdx.app.log("Network", "加入房间按钮被点击!");
|
|
// 使用LibGDX的输入对话框避免AWT线程问题
|
|
Gdx.input.getTextInput(new com.badlogic.gdx.Input.TextInputListener() {
|
|
@Override
|
|
public void input(String ip) {
|
|
if (ip != null && !ip.trim().isEmpty()) {
|
|
new Thread(() -> new ConnectClient(ip.trim(), 11455)).start();
|
|
Gdx.app.log("Network", "正在连接到服务器 " + ip.trim() + "...");
|
|
System.out.println("正在连接到服务器 " + ip.trim() + "...");
|
|
|
|
// 使用postRunnable确保在主线程中执行屏幕切换
|
|
Gdx.app.postRunnable(() -> {
|
|
CharacterSelectScreen characterSelectScreen = new CharacterSelectScreen(game);
|
|
characterSelectScreen.setMultiplayerMode(true);
|
|
game.setScreen(characterSelectScreen);
|
|
});
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void canceled() {
|
|
Gdx.app.log("Network", "用户取消输入 IP");
|
|
}
|
|
}, "请输入服务器 IP 地址", "", "加入房间");
|
|
|
|
} else if (isHovered(mouseX, mouseY, BUTTON_X, EXIT_Y)) {
|
|
Gdx.app.log("Network", "退出按钮被点击!");
|
|
game.setScreen(new MainMenuScreen(game)); // 或者 Gdx.app.exit(); 直接退出游戏
|
|
}
|
|
}
|
|
}
|
|
|
|
private void drawButton(int y, int mouseX, int mouseY) {
|
|
boolean hovered = isHovered(mouseX, mouseY, BUTTON_X, y);
|
|
shapeRenderer.setColor(hovered ? Color.LIGHT_GRAY : Color.DARK_GRAY);
|
|
shapeRenderer.rect(BUTTON_X, y, BUTTON_WIDTH, BUTTON_HEIGHT);
|
|
}
|
|
|
|
private void drawButtonText(int y, String text) {
|
|
float textX = BUTTON_X + BUTTON_WIDTH / 2f - font.getScaleX() * text.length() * 10;
|
|
float textY = y + BUTTON_HEIGHT / 2f + 20;
|
|
font.draw(batch, text, textX, textY);
|
|
}
|
|
|
|
private boolean isHovered(int x, int y, int bx, int by) {
|
|
return x >= bx && x <= bx + BUTTON_WIDTH && y >= by && y <= by + BUTTON_HEIGHT;
|
|
}
|
|
|
|
@Override
|
|
public void dispose() {
|
|
batch.dispose();
|
|
font.dispose();
|
|
shapeRenderer.dispose();
|
|
}
|
|
|
|
} |