|
| 1 | +package io.bluestaggo.voxelthing.gui; |
| 2 | + |
| 3 | +import io.bluestaggo.voxelthing.Game; |
| 4 | +import io.bluestaggo.voxelthing.assets.Texture; |
| 5 | +import io.bluestaggo.voxelthing.math.MathUtil; |
| 6 | +import io.bluestaggo.voxelthing.renderer.MainRenderer; |
| 7 | +import io.bluestaggo.voxelthing.renderer.draw.Quad; |
| 8 | +import io.bluestaggo.voxelthing.world.BlockRaycast; |
| 9 | +import io.bluestaggo.voxelthing.world.Direction; |
| 10 | +import io.bluestaggo.voxelthing.world.block.Block; |
| 11 | +import org.joml.Vector2i; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.lwjgl.glfw.GLFW.GLFW_KEY_1; |
| 17 | +import static org.lwjgl.glfw.GLFW.GLFW_KEY_9; |
| 18 | + |
| 19 | +public class IngameGui extends GuiScreen { |
| 20 | + private static final List<Block> blocks = new ArrayList<>(); |
| 21 | + |
| 22 | + private int heldIndex = 0; |
| 23 | + private int[] prevHoverProgress = new int[9]; |
| 24 | + private int[] hoverProgress = new int[9]; |
| 25 | + |
| 26 | + static { |
| 27 | + for (short i = 0; i < 9; i++) { |
| 28 | + Block block = Block.fromId(i); |
| 29 | + if (block != null) { |
| 30 | + blocks.add(block); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public IngameGui(Game game) { |
| 36 | + super(game); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void onKeyPressed(int key) { |
| 41 | + if (key >= GLFW_KEY_1 && key <= GLFW_KEY_9) { |
| 42 | + heldIndex = key - GLFW_KEY_1; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void tick() { |
| 48 | + for (int i = 0; i < 9; i++) { |
| 49 | + int hover = hoverProgress[i]; |
| 50 | + prevHoverProgress[i] = hover; |
| 51 | + |
| 52 | + if (i == heldIndex) { |
| 53 | + hover++; |
| 54 | + } else { |
| 55 | + hover--; |
| 56 | + } |
| 57 | + |
| 58 | + hover = MathUtil.clamp(hover, 0, Game.TICKS_PER_SECOND / 4); |
| 59 | + hoverProgress[i] = hover; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void draw() { |
| 65 | + MainRenderer r = game.renderer; |
| 66 | + |
| 67 | + Texture hotbarTexture = r.textures.getTexture("/assets/gui/hotbar.png"); |
| 68 | + Texture blocksTexture = r.textures.getTexture("/assets/blocks.png"); |
| 69 | + int slotWidth = hotbarTexture.width / 2; |
| 70 | + int slotHeight = hotbarTexture.height; |
| 71 | + |
| 72 | + float startX = (r.screen.getWidth() - slotWidth * 9) / 2.0f; |
| 73 | + float startY = r.screen.getHeight() - slotHeight - 5; |
| 74 | + |
| 75 | + var hotbarQuad = new Quad() |
| 76 | + .at(startX, startY) |
| 77 | + .size(slotWidth, slotHeight) |
| 78 | + .withTexture(hotbarTexture) |
| 79 | + .withUV(0.0f, 0.0f, 0.5f, 1.0f); |
| 80 | + var blockQuad = new Quad() |
| 81 | + .at(startX + (slotWidth - 16) / 2.0f, startY + (slotHeight - 16) / 2.0f) |
| 82 | + .size(16, 16) |
| 83 | + .withTexture(blocksTexture); |
| 84 | + |
| 85 | + for (int i = 0; i < 9; i++) { |
| 86 | + Block block = i < blocks.size() ? blocks.get(i) : null; |
| 87 | + |
| 88 | + float hover = MathUtil.lerp(prevHoverProgress[i], hoverProgress[i], (float) game.getPartialTick()) / (Game.TICKS_PER_SECOND / 4.0f); |
| 89 | + hover = MathUtil.squareOut(hover); |
| 90 | + hover *= slotHeight / 4.0f; |
| 91 | + |
| 92 | + hotbarQuad.offset(0, -hover); |
| 93 | + blockQuad.offset(0, -hover); |
| 94 | + |
| 95 | + float slotOffset = i == heldIndex ? 0.5f : 0.0f; |
| 96 | + r.draw2D.drawQuad(hotbarQuad.withUV(slotOffset, 0.0f, 0.5f + slotOffset, 1.0f)); |
| 97 | + |
| 98 | + if (block != null) { |
| 99 | + Vector2i texture = block.getTexture().get(Direction.NORTH, null, 0, 0, 0); |
| 100 | + |
| 101 | + float minU = blocksTexture.uCoord(texture.x * 16); |
| 102 | + float minV = blocksTexture.vCoord(texture.y * 16); |
| 103 | + float maxU = minU + blocksTexture.uCoord(16); |
| 104 | + float maxV = minV + blocksTexture.vCoord(16); |
| 105 | + |
| 106 | + r.draw2D.drawQuad(blockQuad.withUV(minU, minV, maxU, maxV)); |
| 107 | + } |
| 108 | + |
| 109 | + hotbarQuad.offset(slotWidth, hover); |
| 110 | + blockQuad.offset(slotWidth, hover); |
| 111 | + } |
| 112 | + |
| 113 | + Texture crosshairTexture = r.textures.getTexture("/assets/gui/crosshair.png"); |
| 114 | + r.draw2D.drawQuad(Quad.shared() |
| 115 | + .at((r.screen.getWidth() - crosshairTexture.width) / 2.0f, (r.screen.getHeight() - crosshairTexture.height) / 2.0f) |
| 116 | + .withTexture(crosshairTexture)); |
| 117 | + } |
| 118 | + |
| 119 | + private Block getPlacedBlock() { |
| 120 | + if (heldIndex < 0 || heldIndex >= blocks.size()) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + return blocks.get(heldIndex); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected void onMouseClicked(int button) { |
| 128 | + BlockRaycast raycast = game.getBlockRaycast(); |
| 129 | + if (raycast.blockHit()) { |
| 130 | + int x = raycast.getHitX(); |
| 131 | + int y = raycast.getHitY(); |
| 132 | + int z = raycast.getHitZ(); |
| 133 | + Direction face = raycast.getHitFace(); |
| 134 | + |
| 135 | + if (button == 0) { |
| 136 | + game.world.setBlockId(x, y, z, (short) 0); |
| 137 | + } else if (button == 1) { |
| 138 | + Block placedBlock = getPlacedBlock(); |
| 139 | + if (placedBlock != null) { |
| 140 | + game.world.setBlock(x + face.X, y + face.Y, z + face.Z, placedBlock); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments