Răsfoiți Sursa

Upload files to 'core/src/main/java/me/lethunderhawk/screen'

Jan 1 lună în urmă
părinte
comite
edb1c4a20a

+ 105 - 0
core/src/main/java/me/lethunderhawk/screen/GameScreen.java

@@ -0,0 +1,105 @@
+package me.lethunderhawk.screen;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.GL20;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+import me.lethunderhawk.Main;
+import me.lethunderhawk.entity.Player;
+import me.lethunderhawk.network.GameClient;
+import me.lethunderhawk.network.Packets;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+public class GameScreen implements Screen {
+
+    private final ShapeRenderer shapeRenderer;
+    private final GameClient client;
+
+    public GameScreen(Main game, String ip) {
+
+        shapeRenderer = new ShapeRenderer();
+
+        client = new GameClient();
+
+        try {
+            client.connect(ip);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void render(float delta) {
+
+        update(delta);
+
+        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+
+        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
+        Player player = client.getPlayer();
+        shapeRenderer.rect(player.x, player.y, 50, 50);
+
+        for(Map.Entry<UUID, Player> entry : client.getOtherPlayers().entrySet()) {
+            Player otherPlayer = entry.getValue();
+            shapeRenderer.rect(otherPlayer.x, otherPlayer.y, 50, 50);
+        }
+
+        shapeRenderer.end();
+    }
+
+    private void update(float delta) {
+
+        float speed = 300f;
+
+        boolean moved = false;
+        Player currentPlayer = client.getPlayer();
+
+        if(Gdx.input.isKeyPressed(Input.Keys.W)) {
+            currentPlayer.y += speed * delta;
+            moved = true;
+        }
+
+        if(Gdx.input.isKeyPressed(Input.Keys.S)) {
+            currentPlayer.y -= speed * delta;
+            moved = true;
+        }
+
+        if(Gdx.input.isKeyPressed(Input.Keys.A)) {
+            currentPlayer.x -= speed * delta;
+            moved = true;
+        }
+
+        if(Gdx.input.isKeyPressed(Input.Keys.D)) {
+            currentPlayer.x += speed * delta;
+            moved = true;
+        }
+
+        if(moved) {
+
+            Packets.PlayerPositionPacket packet =
+                new Packets.PlayerPositionPacket(currentPlayer, currentPlayer.x, currentPlayer.y);
+
+            client.send(packet);
+        }
+    }
+
+
+    @Override
+    public void show() {}
+    @Override
+    public void resize(int width, int height) {}
+    @Override
+    public void pause() {}
+    @Override
+    public void resume() {}
+    @Override
+    public void hide() {}
+    @Override
+    public void dispose() {}
+}

+ 117 - 0
core/src/main/java/me/lethunderhawk/screen/MenuScreen.java

@@ -0,0 +1,117 @@
+package me.lethunderhawk.screen;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.assets.AssetManager;
+import com.badlogic.gdx.graphics.GL20;
+import com.badlogic.gdx.scenes.scene2d.Stage;
+import com.badlogic.gdx.scenes.scene2d.ui.Label;
+import com.badlogic.gdx.scenes.scene2d.ui.Skin;
+import com.badlogic.gdx.scenes.scene2d.ui.Table;
+import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
+import com.badlogic.gdx.scenes.scene2d.ui.TextField;
+import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
+import com.badlogic.gdx.utils.viewport.ScreenViewport;
+import com.badlogic.gdx.InputMultiplexer;
+import com.badlogic.gdx.scenes.scene2d.InputEvent;
+import me.lethunderhawk.Main;
+
+public class MenuScreen implements Screen {
+
+    private final Main game;
+    private Stage stage;
+    private Skin skin;
+    private TextField ipField;
+    private final AssetManager assets;
+
+    public MenuScreen(Main game) {
+        this.game = game;
+        this.assets = new AssetManager();
+    }
+
+    @Override
+    public void show() {
+        // Setup stage and viewport
+        stage = new Stage(new ScreenViewport());
+
+        // Load skin via AssetManager (skin file path relative to assets/)
+        // Ensure ui/uiskin.json references the atlas/image with correct relative paths.
+        try {
+            assets.load("ui/uiskin.json", Skin.class);
+            assets.finishLoading(); // blocking load; change if async desired
+            skin = assets.get("ui/uiskin.json", Skin.class);
+        } catch (Exception e) {
+            Gdx.app.error("MenuScreen", "Failed to load skin: ui/uiskin.json", e);
+            // Fallback: create an empty skin so UI code doesn't NPE (will look ugly)
+            skin = new Skin();
+        }
+
+        // Build UI table
+        Table table = new Table();
+        table.setFillParent(true);
+        table.center();
+        stage.addActor(table);
+
+        // Label
+        Label label = new Label("Enter Server IP:", skin);
+
+        // IP Input Field (use style from skin)
+        ipField = new TextField("localhost", skin);
+
+        // Start Button
+        TextButton startButton = new TextButton("Start Game", skin);
+        startButton.addListener(new ClickListener() {
+            @Override
+            public void clicked(InputEvent event, float x, float y) {
+                String ip = ipField.getText();
+                if (ip == null || ip.trim().isEmpty()) {
+                    ip = "localhost";
+                }
+                game.setScreen(new GameScreen(game, ip));
+            }
+        });
+
+        // Layout components
+        table.add(label).padBottom(10).row();
+        table.add(ipField).width(300).padBottom(20).row();
+        table.add(startButton).width(200);
+
+        // Set input processor
+        Gdx.input.setInputProcessor(stage);
+    }
+
+    @Override
+    public void render(float delta) {
+        Gdx.gl.glClearColor(0.12f, 0.12f, 0.12f, 1f); // dark background
+        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+
+        stage.act(Math.min(delta, 1/30f));
+        stage.draw();
+    }
+
+    @Override
+    public void resize(int width, int height) {
+        if (stage != null) {
+            stage.getViewport().update(width, height, true);
+        }
+    }
+
+    @Override
+    public void pause() {}
+    @Override
+    public void resume() {}
+    @Override
+    public void hide() {
+        // clear input when hidden
+        if (Gdx.input.getInputProcessor() == stage) {
+            Gdx.input.setInputProcessor(null);
+        }
+    }
+
+    @Override
+    public void dispose() {
+        if (stage != null) stage.dispose();
+        if (skin != null) skin.dispose();
+        if (assets != null) assets.dispose();
+    }
+}