From bae7f70cae0096c6b0a23a1e624a3ce8c7b079b9 Mon Sep 17 00:00:00 2001 From: minecraft8997 <35162137+minecraft8997@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:04:34 +0300 Subject: [PATCH] Crosshair --- core/src/ru/mclord/classic/Chunk.java | 2 +- core/src/ru/mclord/classic/Helper.java | 21 ++++++++++++++++ core/src/ru/mclord/classic/InGameScreen.java | 18 ++++++++++---- core/src/ru/mclord/classic/McLordClassic.java | 4 ++-- core/src/ru/mclord/classic/Pair.java | 24 +++++++++++++++++++ 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/core/src/ru/mclord/classic/Chunk.java b/core/src/ru/mclord/classic/Chunk.java index 375969a..ac6d27d 100644 --- a/core/src/ru/mclord/classic/Chunk.java +++ b/core/src/ru/mclord/classic/Chunk.java @@ -53,7 +53,7 @@ public void initGraphics() { int realX = getX(this); int realZ = getZ(this); - modelCache = new ModelCache(); + modelCache = new ModelCache(new ModelCache.Sorter(), new ModelCache.TightMeshPool()); modelCache.begin(); for (int x = realX; x < realX + CHUNK_SIZE; x++) { for (int y = 0; y < level.sizeY; y++) { diff --git a/core/src/ru/mclord/classic/Helper.java b/core/src/ru/mclord/classic/Helper.java index e9f1b38..2091cb3 100644 --- a/core/src/ru/mclord/classic/Helper.java +++ b/core/src/ru/mclord/classic/Helper.java @@ -1,6 +1,7 @@ package ru.mclord.classic; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.VertexAttributes; import com.badlogic.gdx.graphics.g3d.Material; @@ -155,6 +156,26 @@ public static float distanceSquared(Chunk chunk, float pointX, float pointZ) { return dx * dx + dz * dz; } + public static Texture generateCrosshairTexture(int screenWidth, int screenHeight) { + int size = Math.max(screenWidth / 40, screenHeight / 40); + if (size % 2 != 0) size++; + + int half = size / 2; + Pixmap pixmap = new Pixmap(size, size, Pixmap.Format.RGBA8888); + for (int i = 0; i < size; i++) { + pixmap.drawPixel(half, i, 0xFFFFFFFF); + pixmap.drawPixel(half + 1, i, 0xFFFFFFFF); + } + for (int i = 0; i < size; i++) { + pixmap.drawPixel(i, half, 0xFFFFFFFF); + pixmap.drawPixel(i, half + 1, 0xFFFFFFFF); + } + Texture result = new Texture(pixmap); + pixmap.dispose(); + + return result; + } + public static String getStacktrace(Throwable t) { StringWriter writer0 = new StringWriter(); PrintWriter writer = new PrintWriter(writer0); diff --git a/core/src/ru/mclord/classic/InGameScreen.java b/core/src/ru/mclord/classic/InGameScreen.java index 0c74806..029072f 100644 --- a/core/src/ru/mclord/classic/InGameScreen.java +++ b/core/src/ru/mclord/classic/InGameScreen.java @@ -3,6 +3,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.PerspectiveCamera; +import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g3d.Environment; @@ -14,6 +15,7 @@ public class InGameScreen implements Screen { private BitmapFont font; private SpriteBatch spriteBatch; + private Texture crosshairTexture; private ModelBatch modelBatch; private McLordFirstPersonCameraController cameraController; private Level level; @@ -57,12 +59,12 @@ public void show() { environment = null; } - Player player = McLordClassic.getPlayer(); camera = new PerspectiveCamera( fov, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); - // camera.position.set(player.spawnLocation.x, player.spawnLocation.y, player.spawnLocation.z); + // camera.position.set(player.spawnLocation.x, + // player.spawnLocation.y, player.spawnLocation.z); camera.position.set(-1.0f, -1.0f, -1.0f); - camera.near = 0.35f; // todo might be not the best value + camera.near = 0.35f; camera.far = 1000000000.0f; camera.update(); @@ -90,11 +92,18 @@ public void render(float delta) { } level.render(modelBatch, environment); modelBatch.end(); + + spriteBatch.begin(); + int size = crosshairTexture.getWidth(); + spriteBatch.draw(crosshairTexture, Gdx.graphics.getWidth() / 2.0f - + size / 2.0f, Gdx.graphics.getHeight() / 2.0f - size / 2.0f); + spriteBatch.end(); } @Override public void resize(int width, int height) { - + Helper.dispose(crosshairTexture); + crosshairTexture = Helper.generateCrosshairTexture(width, height); } @Override @@ -116,6 +125,7 @@ public void hide() { public void dispose() { Helper.dispose(font); font = null; Helper.dispose(spriteBatch); spriteBatch = null; + Helper.dispose(crosshairTexture); crosshairTexture = null; Helper.dispose(modelBatch); modelBatch = null; Helper.dispose(level); level = null; cameraController = null; diff --git a/core/src/ru/mclord/classic/McLordClassic.java b/core/src/ru/mclord/classic/McLordClassic.java index 14f670c..34fcca7 100644 --- a/core/src/ru/mclord/classic/McLordClassic.java +++ b/core/src/ru/mclord/classic/McLordClassic.java @@ -55,8 +55,8 @@ public enum GameStage { public static final boolean DEBUG = true; public static final String APP_NAME = "McLordClassic"; - public static final String VERSION = "0.1.2"; - public static final int VERSION_CODE = 2; + public static final String VERSION = "0.1.4"; + public static final int VERSION_CODE = 4; private static final McLordClassic INSTANCE = new McLordClassic(); diff --git a/core/src/ru/mclord/classic/Pair.java b/core/src/ru/mclord/classic/Pair.java index a90c61c..0ff4110 100644 --- a/core/src/ru/mclord/classic/Pair.java +++ b/core/src/ru/mclord/classic/Pair.java @@ -1,5 +1,7 @@ package ru.mclord.classic; +import java.util.Objects; + public class Pair { private final A first; private final B second; @@ -20,4 +22,26 @@ public A getFirst() { public B getSecond() { return second; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Pair pair = (Pair) o; + + return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); + } + + @Override + public int hashCode() { + return Objects.hash(first, second); + } + + @Override + public String toString() { + return "Pair{" + + "first=" + first + + ", second=" + second + + '}'; + } }