diff --git a/.gitignore b/.gitignore index 66071052..c9060bdd 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ start.bat # Ignore Gradle build output directory build/ +engine.jar \ No newline at end of file diff --git a/client/build.gradle.kts b/client/build.gradle.kts index b059a30a..fb111819 100644 --- a/client/build.gradle.kts +++ b/client/build.gradle.kts @@ -79,7 +79,7 @@ dependencies { implementation(group = "me.ryandw11", name = "ods", version = "1.0.4") implementation(group = "me.ryandw11", name = "ODSCompressionPlus", version = "1.0.1") //Engine - compileOnly (group = "org.kakara", name = "engine", version = engineVersion) + compileOnly(group = "org.kakara", name = "engine", version = engineVersion); compileOnly("io.imgui.java:binding:1.77-0.17.2") compileOnly("io.imgui.java:lwjgl3:1.77-0.17.2") //Core diff --git a/client/src/main/java/org/kakara/client/engine/item/HorizontalRotationComponent.java b/client/src/main/java/org/kakara/client/engine/item/HorizontalRotationComponent.java new file mode 100644 index 00000000..85d53118 --- /dev/null +++ b/client/src/main/java/org/kakara/client/engine/item/HorizontalRotationComponent.java @@ -0,0 +1,17 @@ +package org.kakara.client.engine.item; + +import org.kakara.engine.components.Component; +import org.kakara.engine.physics.collision.BoxCollider; + +public class HorizontalRotationComponent extends Component { + @Override + public void start() { + + } + + @Override + public void update() { + getGameItem().transform.getRotation().rotateY(0.01f); + + } +} diff --git a/client/src/main/java/org/kakara/client/engine/item/HorizontalRotationFeature.java b/client/src/main/java/org/kakara/client/engine/item/HorizontalRotationFeature.java deleted file mode 100644 index 496f70dd..00000000 --- a/client/src/main/java/org/kakara/client/engine/item/HorizontalRotationFeature.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.kakara.client.engine.item; - - -import org.kakara.engine.gameitems.GameItem; -import org.kakara.engine.gameitems.features.Feature; - -public class HorizontalRotationFeature implements Feature { - @Override - public void update(GameItem gameItem) { - gameItem.getRotation().rotateY(0.01f); - } -} diff --git a/client/src/main/java/org/kakara/client/local/game/GameEngineInventoryController.java b/client/src/main/java/org/kakara/client/local/game/GameEngineInventoryController.java index 15efb04a..a0855cb6 100644 --- a/client/src/main/java/org/kakara/client/local/game/GameEngineInventoryController.java +++ b/client/src/main/java/org/kakara/client/local/game/GameEngineInventoryController.java @@ -25,7 +25,7 @@ public void render(Inventory inventory, Texture inventoryBackground, Set elementList, Inventory inventory, Font font) { super(scene); if (!(scene instanceof MainGameScene)) throw new IllegalArgumentException("Must be a MainGameScene"); - + setTag("inventory_canvas"); this.inventoryBackground = inventoryBackground; this.elements = elementList; this.inventory = inventory; @@ -65,7 +66,7 @@ public InventoryCanvas(Scene scene, Texture inventoryBackground, Set gameBlockList; private boolean updatedHappened = true; @Nullable - private UUID renderChunkID; + private UUID voxel; private Status status = Status.UNLOADED; public ClientChunk(ChunkLocation location) { @@ -63,12 +63,12 @@ public Optional getGameBlock(Location location) { return getGameBlock(location); } - public Optional getRenderChunkID() { - return Optional.ofNullable(renderChunkID); + public Optional getVoxelID() { + return Optional.ofNullable(voxel); } - public void setRenderChunkID(UUID renderChunkID) { - this.renderChunkID = renderChunkID; + public void setVoxelID(UUID renderChunkID) { + this.voxel = renderChunkID; } @Override diff --git a/client/src/main/java/org/kakara/client/scenes/MainMenuScene.java b/client/src/main/java/org/kakara/client/scenes/MainMenuScene.java index e926ca4d..b7c0f54c 100644 --- a/client/src/main/java/org/kakara/client/scenes/MainMenuScene.java +++ b/client/src/main/java/org/kakara/client/scenes/MainMenuScene.java @@ -12,6 +12,7 @@ import org.kakara.engine.math.Vector2; import org.kakara.engine.resources.ResourceManager; import org.kakara.engine.scene.AbstractMenuScene; +import org.kakara.engine.ui.canvases.ComponentCanvas; import org.kakara.engine.ui.components.Sprite; import org.kakara.engine.ui.components.shapes.Rectangle; import org.kakara.engine.ui.components.text.Text; @@ -23,7 +24,6 @@ import org.kakara.engine.ui.events.UIHoverLeaveEvent; import org.kakara.engine.ui.font.Font; import org.kakara.engine.ui.font.TextAlign; -import org.kakara.engine.ui.items.ComponentCanvas; import org.kakara.engine.utils.RGBA; import org.kakara.engine.window.WindowIcon; @@ -171,7 +171,7 @@ private void singlePlayerClick(Rectangle playButton) { gameHandler.getSceneManager().setScene(kakaraGame.join(new LocalJoin(saveCreator.createSave(),UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5") ))); } catch (Exception ex) { - setCurserStatus(true); + setCursorStatus(true); KakaraGame.LOGGER.error("unable to start game", ex); // gameHandler.getSceneManager().setScene(); } diff --git a/client/src/main/java/org/kakara/client/scenes/canvases/ActivateableCanvas.java b/client/src/main/java/org/kakara/client/scenes/canvases/ActivateableCanvas.java index 22a55135..3a5188cb 100644 --- a/client/src/main/java/org/kakara/client/scenes/canvases/ActivateableCanvas.java +++ b/client/src/main/java/org/kakara/client/scenes/canvases/ActivateableCanvas.java @@ -1,7 +1,7 @@ package org.kakara.client.scenes.canvases; import org.kakara.engine.scene.Scene; -import org.kakara.engine.ui.items.ComponentCanvas; +import org.kakara.engine.ui.canvases.ComponentCanvas; public abstract class ActivateableCanvas extends ComponentCanvas { private boolean activated = false; diff --git a/client/src/main/java/org/kakara/client/scenes/canvases/DebugCanvas.java b/client/src/main/java/org/kakara/client/scenes/canvases/DebugCanvas.java index cc9c092d..4cb1f496 100644 --- a/client/src/main/java/org/kakara/client/scenes/canvases/DebugCanvas.java +++ b/client/src/main/java/org/kakara/client/scenes/canvases/DebugCanvas.java @@ -7,16 +7,15 @@ import org.joml.Vector3f; import org.kakara.client.scenes.maingamescene.MainGameScene; import org.kakara.engine.GameHandler; -import org.kakara.engine.gameitems.MeshGameItem; -import org.kakara.engine.physics.collision.Collidable; -import org.kakara.engine.renderobjects.RenderBlock; + import org.kakara.engine.scene.AbstractGameScene; import org.kakara.engine.scene.AbstractScene; import org.kakara.engine.scene.Scene; import org.kakara.engine.ui.UICanvas; import org.kakara.engine.ui.UserInterface; -import org.kakara.engine.ui.items.ComponentCanvas; -import org.kakara.engine.ui.items.ObjectCanvas; + +import org.kakara.engine.ui.canvases.ComponentCanvas; +import org.kakara.engine.ui.canvases.ObjectCanvas; import org.kakara.engine.utils.Time; import org.kakara.engine.weather.Fog; @@ -73,6 +72,16 @@ public void cleanup(GameHandler handler) { ImGui.destroyContext(); } + @Override + public boolean isAutoScale() { + return false; + } + + @Override + public void setAutoScale(boolean b) { + + } + private void renderFPSInfo(Scene scene){ ImGui.setNextWindowSize(300, 300, ImGuiCond.Once); ImGui.setNextWindowPos(10, 300, ImGuiCond.Once); diff --git a/client/src/main/java/org/kakara/client/scenes/canvases/DebugModeCanvas.java b/client/src/main/java/org/kakara/client/scenes/canvases/DebugModeCanvas.java index 71beacd3..9b6ac9de 100644 --- a/client/src/main/java/org/kakara/client/scenes/canvases/DebugModeCanvas.java +++ b/client/src/main/java/org/kakara/client/scenes/canvases/DebugModeCanvas.java @@ -28,7 +28,7 @@ private DebugModeCanvas(KakaraGame kakaraGame, MainGameScene scene) { super(scene); gameScene = scene; this.kakaraGame = kakaraGame; - + setTag("debugmode_canvas"); ResourceManager resourceManager = GameHandler.getInstance().getResourceManager(); Font roboto = new Font("Roboto-Regular", resourceManager.getResource("Roboto-Regular.ttf"), scene); @@ -47,12 +47,12 @@ private DebugModeCanvas(KakaraGame kakaraGame, MainGameScene scene) { engineVersion = new Text("Engine: " + GameEngine.getEngineVersion(), roboto); engineVersion.position = new Vector2(0, 75); engineVersion.setTextAlign(TextAlign.LEFT); - engineVersion.setLineWidth(300); + engineVersion.setLineWidth(400); kakaraVersion = new Text("Kakara: " + KakaraGame.getGameVersion().getProperty("version"), roboto); kakaraVersion.position = new Vector2(0, 100); kakaraVersion.setTextAlign(TextAlign.LEFT); - kakaraVersion.setLineWidth(300); + kakaraVersion.setLineWidth(400); } diff --git a/client/src/main/java/org/kakara/client/scenes/canvases/HotBarCanvas.java b/client/src/main/java/org/kakara/client/scenes/canvases/HotBarCanvas.java index 4c927d4f..804b88c7 100644 --- a/client/src/main/java/org/kakara/client/scenes/canvases/HotBarCanvas.java +++ b/client/src/main/java/org/kakara/client/scenes/canvases/HotBarCanvas.java @@ -9,27 +9,25 @@ import org.kakara.core.common.game.Item; import org.kakara.core.common.game.ItemStack; import org.kakara.core.common.resources.TextureResolution; -import org.kakara.core.server.ServerGameInstance; -import org.kakara.core.server.gui.ServerBoxedInventoryContainer; import org.kakara.engine.GameHandler; import org.kakara.engine.engine.CubeData; import org.kakara.engine.events.EventHandler; import org.kakara.engine.events.event.KeyPressEvent; import org.kakara.engine.gameitems.mesh.AtlasMesh; import org.kakara.engine.gameitems.mesh.Mesh; -import org.kakara.engine.renderobjects.RenderTexture; -import org.kakara.engine.renderobjects.TextureAtlas; -import org.kakara.engine.renderobjects.renderlayouts.BlockLayout; import org.kakara.engine.scene.Scene; import org.kakara.engine.ui.UserInterface; +import org.kakara.engine.ui.canvases.ComponentCanvas; +import org.kakara.engine.ui.canvases.ObjectCanvas; import org.kakara.engine.ui.components.Panel; import org.kakara.engine.ui.components.shapes.Rectangle; import org.kakara.engine.ui.components.text.Text; import org.kakara.engine.ui.font.Font; -import org.kakara.engine.ui.items.ComponentCanvas; -import org.kakara.engine.ui.items.ObjectCanvas; import org.kakara.engine.ui.objectcanvas.UIObject; import org.kakara.engine.utils.RGBA; +import org.kakara.engine.voxels.TextureAtlas; +import org.kakara.engine.voxels.VoxelTexture; +import org.kakara.engine.voxels.layouts.BlockLayout; import org.kakara.game.items.blocks.AirBlock; import org.kakara.game.resources.GameResourceManager; @@ -60,6 +58,7 @@ public HotBarCanvas(Scene scene, TextureAtlas atlas, RenderResourceManager rende this.renderTextureCache = renderTextureCache; mainPanel = new Panel(); + mainPanel.setAllowOverflow(true); for (int i = 0; i < 5; i++) { Rectangle rect = new Rectangle(); @@ -73,10 +72,13 @@ public HotBarCanvas(Scene scene, TextureAtlas atlas, RenderResourceManager rende rects[0].setColor(selected); numberCanvas = new ComponentCanvas(scene); - + numberCanvas.setAutoScale(false); + numberCanvas.setTag("hot_bar_number_canvas"); add(mainPanel); this.roboto = roboto; renderItems(); + setAutoScale(false); + setTag("hotbar_canvas"); } public void update() { @@ -122,6 +124,8 @@ public void renderItems() { try { if (objectCanvas == null) { objectCanvas = new ObjectCanvas(scene); + objectCanvas.setAutoScale(false); + objectCanvas.setTag("hotbar_object_canvas"); } for (int i = 0; i < 5; i++) { ItemStack itemStack = contentInventory.getHotBarContents()[i]; @@ -130,7 +134,7 @@ public void renderItems() { if (item instanceof AirBlock) continue; UIObject uiObject; if (item instanceof Block) { - RenderTexture txt = getTexture(itemStack); + VoxelTexture txt = getTexture(itemStack); AtlasMesh mesh = new AtlasMesh(txt, atlas, new BlockLayout(), CubeData.vertex, CubeData.normal, CubeData.indices); uiObject = new UIObject(mesh); objectCanvas.add(uiObject); @@ -158,11 +162,18 @@ public void renderItems() { } } - private RenderTexture getTexture(ItemStack is) throws ExecutionException { - return renderTextureCache.get(GameResourceManager.correctPath(Kakara.getGameInstance().getResourceManager().getTexture(is.getItem().getTexture(), TextureResolution._16, is.getItem().getMod()).getLocalPath())); + private VoxelTexture getTexture(ItemStack is) throws ExecutionException { + return renderTextureCache.get(GameResourceManager.correctPath( + Kakara.getGameInstance().getResourceManager().getTexture(is.getItem().getTexture(), + TextureResolution._16, is.getItem().getMod()).getLocalPath())); } - public RenderTexture getCurrentItem() { + /** + * Get the Voxel Texture for the current item. + * + * @return The voxel texture. + */ + public VoxelTexture getCurrentItem() { if (contentInventory.getHotBarContents()[selectedIndex] instanceof AirBlock) { return null; } diff --git a/client/src/main/java/org/kakara/client/scenes/canvases/PauseMenuCanvas.java b/client/src/main/java/org/kakara/client/scenes/canvases/PauseMenuCanvas.java index 552f017e..4415dd72 100644 --- a/client/src/main/java/org/kakara/client/scenes/canvases/PauseMenuCanvas.java +++ b/client/src/main/java/org/kakara/client/scenes/canvases/PauseMenuCanvas.java @@ -29,7 +29,7 @@ private PauseMenuCanvas(Scene scene) { super(scene); this.scene = scene; Font roboto = new Font("Roboto", GameHandler.getInstance().getResourceManager().getResource("Roboto-Regular.ttf"), scene); - + setTag("pausemenu_canvas"); resume = new Rectangle(new Vector2(0, 270), new Vector2(300, 60), new RGBA(255, 255, 255, 0.5f)); resume.addConstraint(new HorizontalCenterConstraint()); @@ -90,14 +90,14 @@ public static PauseMenuCanvas getInstance(KakaraGame kakaraGame, Scene scene) { void add() { add(quit); add(resume); - scene.setCurserStatus(true); + scene.setCursorStatus(true); } @Override void remove() { removeComponent(quit); removeComponent(resume); - scene.setCurserStatus(false); + scene.setCursorStatus(false); } diff --git a/client/src/main/java/org/kakara/client/scenes/maingamescene/MainGameScene.java b/client/src/main/java/org/kakara/client/scenes/maingamescene/MainGameScene.java index bfd67548..5982e8c6 100644 --- a/client/src/main/java/org/kakara/client/scenes/maingamescene/MainGameScene.java +++ b/client/src/main/java/org/kakara/client/scenes/maingamescene/MainGameScene.java @@ -5,7 +5,7 @@ import org.kakara.client.ClientServerController; import org.kakara.client.KakaraGame; import org.kakara.client.MoreUtils; -import org.kakara.client.engine.item.HorizontalRotationFeature; +import org.kakara.client.engine.item.HorizontalRotationComponent; import org.kakara.client.local.game.DroppedItem; import org.kakara.client.local.game.IntegratedServer; import org.kakara.client.local.game.player.ClientPlayer; @@ -33,40 +33,42 @@ import org.kakara.engine.events.EventHandler; import org.kakara.engine.events.event.KeyPressEvent; import org.kakara.engine.events.event.MouseClickEvent; +import org.kakara.engine.gameitems.GameItem; import org.kakara.engine.gameitems.Material; -import org.kakara.engine.gameitems.MeshGameItem; import org.kakara.engine.gameitems.SkyBox; import org.kakara.engine.gameitems.Texture; import org.kakara.engine.gameitems.mesh.AtlasMesh; import org.kakara.engine.gameitems.mesh.Mesh; -import org.kakara.engine.input.MouseClickType; +import org.kakara.engine.input.mouse.MouseClickType; import org.kakara.engine.math.Vector2; import org.kakara.engine.math.Vector3; -import org.kakara.engine.models.TextureCache; import org.kakara.engine.physics.collision.BoxCollider; -import org.kakara.engine.physics.collision.Collidable; -import org.kakara.engine.renderobjects.RenderBlock; -import org.kakara.engine.renderobjects.RenderChunk; -import org.kakara.engine.renderobjects.RenderTexture; -import org.kakara.engine.renderobjects.TextureAtlas; -import org.kakara.engine.renderobjects.mesh.MeshType; -import org.kakara.engine.renderobjects.renderlayouts.BlockLayout; +import org.kakara.engine.physics.collision.ColliderComponent; + +import org.kakara.engine.physics.collision.PhysicsComponent; +import org.kakara.engine.physics.collision.VoxelCollider; +import org.kakara.engine.resources.Resource; import org.kakara.engine.resources.ResourceManager; import org.kakara.engine.scene.AbstractGameScene; +import org.kakara.engine.ui.canvases.ComponentCanvas; import org.kakara.engine.ui.components.shapes.Rectangle; import org.kakara.engine.ui.constraints.HorizontalCenterConstraint; import org.kakara.engine.ui.constraints.VerticalCenterConstraint; import org.kakara.engine.ui.font.Font; -import org.kakara.engine.ui.items.ComponentCanvas; import org.kakara.engine.utils.RGBA; import org.kakara.engine.utils.Time; +import org.kakara.engine.voxels.TextureAtlas; +import org.kakara.engine.voxels.Voxel; +import org.kakara.engine.voxels.VoxelChunk; +import org.kakara.engine.voxels.VoxelTexture; +import org.kakara.engine.voxels.layouts.BlockLayout; +import org.kakara.engine.voxels.mesh.MeshType; import org.kakara.game.GameUtils; import org.kakara.game.Server; import org.kakara.game.items.blocks.AirBlock; import org.kakara.game.resources.GameResourceManager; import java.io.File; -import java.net.MalformedURLException; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; import java.util.stream.Collectors; @@ -84,13 +86,13 @@ public class MainGameScene extends AbstractGameScene { protected ChatComponent chatComponent; protected BreakingBlock breakingBlock = null; protected HotBarCanvas hotBarCanvas; - protected MeshGameItem blockSelector; - protected RenderTexture breakingTexture; + protected GameItem blockSelector; + protected VoxelTexture breakingTexture; private boolean hasRun = false; public MainGameScene(GameHandler gameHandler, Client client, KakaraGame kakaraGame) { super(gameHandler); - setCurserStatus(false); + setCursorStatus(false); this.client = client; this.kakaraGame = kakaraGame; @@ -116,19 +118,19 @@ public void loadGraphics(GameHandler handler) { getUserInterface().addItem(PauseMenuCanvas.getInstance(kakaraGame, this)); var resourceManager = gameHandler.getResourceManager(); - List textures = new ArrayList<>(); + List textures = new ArrayList<>(); for (org.kakara.core.common.resources.Texture resource : Objects.requireNonNull(Kakara.getGameInstance()).getResourceManager().getAllTextures()) { //Ignore Textures that shouldn't be added to Texture Atlas if (resource.getProperties().contains("NO_TEXTURE_ATLAS")) { continue; } - RenderTexture txt1 = new RenderTexture(resourceManager.getResource(resource.get().getLocalPath())); + VoxelTexture txt1 = new VoxelTexture(resourceManager.getResource(resource.get().getLocalPath())); textures.add(txt1); } - textures.add(new RenderTexture(resourceManager.getResource("block_select.png"))); - breakingTexture = new RenderTexture(resourceManager.getResource("breaking/breaking.png")); + textures.add(new VoxelTexture(resourceManager.getResource("block_select.png"))); + breakingTexture = new VoxelTexture(resourceManager.getResource("breaking/breaking.png")); textures.add(breakingTexture); File file = new File(Kakara.getGameInstance().getWorkingDirectory(), "tmp"); if (!file.exists() && !file.mkdir()) { @@ -148,6 +150,8 @@ public void loadGraphics(GameHandler handler) { hotBarCanvas.show(); ComponentCanvas main = new ComponentCanvas(this); + main.setTag("main_canvas"); + main.setAutoScale(false); chatComponent = new ChatComponent(roboto, false, this); chatComponent.setPosition(0, 170); chatComponent.addUActionEvent(ChatSendEvent.class, (ChatSendEvent) message -> { @@ -156,14 +160,16 @@ public void loadGraphics(GameHandler handler) { // } getController().messageSend(message.getBytes()); }); - chatComponent.addUActionEvent(ChatFocusEvent.class, (ChatFocusEvent) () -> setCurserStatus(true)); - chatComponent.addUActionEvent(ChatBlurEvent.class, (ChatBlurEvent) () -> setCurserStatus(false)); + chatComponent.addUActionEvent(ChatFocusEvent.class, (ChatFocusEvent) () -> setCursorStatus(true)); + chatComponent.addUActionEvent(ChatBlurEvent.class, (ChatBlurEvent) () -> setCursorStatus(false)); main.add(chatComponent); Rectangle indicator = new Rectangle(new Vector2(0, 0), new Vector2(5, 5), new RGBA(0, 255, 0, 1)); + indicator.setTag("main_canvas_indicator"); + main.add(indicator); + indicator.setParentCanvas(main); indicator.addConstraint(new HorizontalCenterConstraint()); indicator.addConstraint(new VerticalCenterConstraint()); - main.add(indicator); add(main); add(hotBarCanvas); @@ -176,18 +182,17 @@ public void loadGraphics(GameHandler handler) { Mesh m = new Mesh(CubeData.vertex, CubeData.texture, CubeData.normal, CubeData.indices); m.setMaterial(new Material(new Texture(resourceManager.getResource("block_select.png"), this))); m.setWireframe(true); - this.blockSelector = new MeshGameItem(m); - this.blockSelector.setScale(1.01f); + this.blockSelector = new GameItem(m); + this.blockSelector.transform.setScale(1.01f); add(this.blockSelector); } private Texture loadSkyBoxTexture() { - try { - return TextureCache.getInstance(gameHandler.getResourceManager()).getTexture("skybox/daytime2.png", this); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - return null; + System.out.println("gameHandler.getResourceManager().getResource(\"skybox.obj\").getURL().toString() = " + gameHandler.getResourceManager().getResource("skybox.obj").getURL().toString()); + Resource resource = gameHandler.getResourceManager().getResource("skybox/daytime2.png"); + System.out.println("resource.getURL().toString() = " + resource.getURL().toString()); + System.out.println("resource.getClass().getName() = " + resource.getClass().getName()); + return new Texture(resource, this); } @@ -224,21 +229,26 @@ private void blockBreakHandler() { ClientPlayer player = (ClientPlayer) getServer().getPlayerEntity(); if (player.getGameItemID().isEmpty()) return; if (kakaraGame.getGameHandler().getMouseInput().isLeftButtonPressed() && !chatComponent.isFocused()) { - Collidable col = this.selectGameItems(20, player.getGameItemID().get()); - if (col instanceof RenderBlock) { - RenderBlock rb = (RenderBlock) col; - RenderChunk parentChunk = rb.getParentChunk(); - Location location = new Location(player.getLocation().getNullableWorld(), parentChunk.getPosition().x + rb.getPosition().x, parentChunk.getPosition().y + rb.getPosition().y, parentChunk.getPosition().z + rb.getPosition().z); + if (player.getGameItemID().isEmpty()) { + KakaraGame.LOGGER.warn("Player is not initialized."); + return; + } + ColliderComponent col = this.selectGameItems(20, player.getGameItemID().get()); + if (col instanceof VoxelCollider) { + VoxelCollider voxelCollider = (VoxelCollider) col; + Voxel rb = voxelCollider.getVoxel(); + VoxelChunk parentChunk = rb.getParentChunk(); + Location location = new Location(player.getLocation().getNullableWorld(), parentChunk.transform.getPosition().x + rb.getPosition().x, parentChunk.transform.getPosition().y + rb.getPosition().y, parentChunk.transform.getPosition().z + rb.getPosition().z); if (breakingBlock == null || !breakingBlock.getGbLocation().equals(location)) { if (breakingBlock != null) { - Optional chunk = getChunkHandler().getRenderChunkList().stream().filter(renderChunk -> renderChunk.getPosition().equals(breakingBlock.getChunkLocation())).findFirst(); - chunk.ifPresent(renderChunk -> { - RenderBlock block = renderChunk.getOctChunk()[(int) breakingBlock.getBlockLocation().x][(int) breakingBlock.getBlockLocation().y][(int) breakingBlock.getBlockLocation().z]; + Optional chunk = getChunkHandler().getVoxelChunkList().stream().filter(VoxelChunk -> VoxelChunk.transform.getPosition().equals(breakingBlock.getChunkLocation())).findFirst(); + chunk.ifPresent(voxelChunk -> { + Voxel block = voxelChunk.getVoxelArray()[(int) breakingBlock.getBlockLocation().x][(int) breakingBlock.getBlockLocation().y][(int) breakingBlock.getBlockLocation().z]; block.setOverlay(null); - renderChunk.regenerateOverlayTextures(getTextureAtlas()); + voxelChunk.regenerateOverlayTextures(getTextureAtlas()); }); } - breakingBlock = new BreakingBlock(location, parentChunk.getPosition(), rb.getPosition()); + breakingBlock = new BreakingBlock(location, parentChunk.transform.getPosition(), rb.getPosition()); rb.setOverlay(breakingTexture); parentChunk.regenerateOverlayTextures(getTextureAtlas()); } else { @@ -249,12 +259,11 @@ private void blockBreakHandler() { double breakPerFrame = GameUtils.getBreakingTime(blockAt.get(), null, player); if (breakingBlock.breakBlock(breakPerFrame * Time.getDeltaTime())) { //If the block was cancelled. By the server it will require a re-render. This might be changed in the future. - parentChunk.removeBlock(rb); + parentChunk.removeVoxel(rb); parentChunk.regenerateChunk(getTextureAtlas(), MeshType.SYNC); //Remove old breakingBlock breakingBlock = null; //call the blockBreak - System.out.println("BREAKING BLOCK"); ((ClientServerController) getServer().getServerController()).blockBreak(location); } }); @@ -271,19 +280,20 @@ public void renderDroppedItems() { for (DroppedItem droppedItem : ((ClientWorld) getServer().getPlayerEntity().getLocation().getNullableWorld()).getDroppedItems()) { if (droppedItem.getGameID() == null) { AtlasMesh mesh = new AtlasMesh(getTexture(droppedItem.getItemStack()), getTextureAtlas(), new BlockLayout(), CubeData.vertex, CubeData.normal, CubeData.indices); - MeshGameItem droppedBlock = new MeshGameItem(mesh); - BoxCollider collider = new BoxCollider(new Vector3(0f, 0f, 0f), new Vector3(0.8f, 0.9f, 0.8f)); + GameItem droppedBlock = new GameItem(mesh); + BoxCollider collider = droppedBlock.addComponent(BoxCollider.class); + PhysicsComponent physicsComponent = droppedBlock.addComponent(PhysicsComponent.class); + physicsComponent.setVelocityY(-9.18f); + physicsComponent.setResolve(true); + droppedBlock.addComponent(HorizontalRotationComponent.class); collider.setPredicate(collidable -> { - if (collidable instanceof RenderBlock) return false; - return !(collidable instanceof RenderChunk); + if (collidable instanceof VoxelCollider) return false; + return true; }); - droppedBlock.setCollider(collider); - droppedBlock.setVelocityY(-9.18f); - droppedBlock.setScale(0.3f); - droppedBlock.setPosition((float) droppedItem.getLocation().getX(), (float) droppedItem.getLocation().getY(), (float) droppedItem.getLocation().getZ()); + droppedBlock.transform.setScale(0.3f); + droppedBlock.transform.setPosition((float) droppedItem.getLocation().getX(), (float) droppedItem.getLocation().getY(), (float) droppedItem.getLocation().getZ()); droppedBlock.setTag("pickupable"); droppedBlock.getData().add(droppedItem.getItemStack().getItem().getControllerKey()); - droppedBlock.addFeature(new HorizontalRotationFeature()); droppedItem.setGameID(droppedBlock.getUUID()); add(droppedBlock); } @@ -306,12 +316,13 @@ public void onMousePress(MouseClickEvent evt) { UUID playerID = ((ClientPlayer) getServer().getPlayerEntity()).getGameItemID().orElseThrow(() -> new IllegalStateException("No Player Entity Found")); if (evt.getMouseClickType() == MouseClickType.RIGHT_CLICK && !chatComponent.isFocused()) { - Collidable col = this.selectGameItems(20, playerID); - if (col instanceof RenderBlock) { - RenderBlock rb = (RenderBlock) col; - RenderChunk parentChunk = rb.getParentChunk(); + ColliderComponent col = this.selectGameItems(20, playerID); + if (col instanceof VoxelCollider) { + VoxelCollider voxelCollider = (VoxelCollider) col; + Voxel rb = voxelCollider.getVoxel(); + VoxelChunk parentChunk = rb.getParentChunk(); - Vector3 absoluteBlockPos = rb.getPosition().add(parentChunk.getPosition()); + Vector3 absoluteBlockPos = rb.getPosition().add(parentChunk.transform.getPosition()); final Vector3 closestValue = ObjectPickingUtils.closestValue(absoluteBlockPos, getCamera()); ChunkLocation chunkLoc = GameUtils.getChunkLocation(new Location(getServer().getPlayerEntity().getLocation().getNullableWorld(), closestValue.x, closestValue.y, closestValue.z)); @@ -319,22 +330,22 @@ public void onMousePress(MouseClickEvent evt) { Chunk chunk = (Objects.requireNonNull(getServer().getPlayerEntity().getLocation().getNullableWorld())).getChunkAt(chunkLoc); if (chunk.getStatus() != Status.LOADED) return; ClientChunk cc = (ClientChunk) chunk; - List rcc = getChunkHandler().getRenderChunkList().stream().filter((rc) -> { - if (cc.getRenderChunkID().isPresent()) { - return rc.getId() == cc.getRenderChunkID().get(); + List rcc = getChunkHandler().getVoxelChunkList().stream().filter((rc) -> { + if (cc.getVoxelID().isPresent()) { + return rc.getId() == cc.getVoxelID().get(); } return false; }).collect(Collectors.toList()); - RenderChunk desiredChunk = rcc.get(0); - Vector3 newBlockLoc = closestValue.subtract(desiredChunk.getPosition()); - if (desiredChunk.getOctChunk()[(int) newBlockLoc.x][(int) newBlockLoc.y][(int) newBlockLoc.z] == null) { + VoxelChunk desiredChunk = rcc.get(0); + Vector3 newBlockLoc = closestValue.subtract(desiredChunk.transform.getPosition()); + if (desiredChunk.getVoxelArray()[(int) newBlockLoc.x][(int) newBlockLoc.y][(int) newBlockLoc.z] == null) { //IGNORE Airs if (hotBarCanvas.getCurrentItemStack().getItem() instanceof AirBlock) return; - RenderBlock rbs = new RenderBlock(new BlockLayout(), + Voxel rbs = new Voxel(new BlockLayout(), renderResourceManager.get(GameResourceManager.correctPath(Objects.requireNonNull(Kakara.getGameInstance()).getResourceManager().getTexture(hotBarCanvas.getCurrentItemStack().getItem().getTexture(), TextureResolution._16, hotBarCanvas.getCurrentItemStack().getItem().getMod()).getLocalPath())), newBlockLoc); - desiredChunk.addBlock(rbs); + desiredChunk.addVoxel(rbs); desiredChunk.regenerateChunk(getTextureAtlas(), MeshType.SYNC); - getController().blockPlace(MoreUtils.vector3ToLocation(newBlockLoc.add(desiredChunk.getPosition()), chunkLoc.getNullableWorld()), hotBarCanvas.getCurrentItemStack()); + getController().blockPlace(MoreUtils.vector3ToLocation(newBlockLoc.add(desiredChunk.transform.getPosition()), chunkLoc.getNullableWorld()), hotBarCanvas.getCurrentItemStack()); //TODO HotBar render should be done by the Inventory //hotBarCanvas.renderItems(); } @@ -361,8 +372,8 @@ public void gameSceneUpdate() { if (loadedChunk.getStatus() != Status.LOADED) continue; ClientChunk clientChunk = (ClientChunk) loadedChunk; if (!GameUtils.isLocationInsideCurrentLocationRadius(GameUtils.getChunkLocation(getServer().getPlayerEntity().getLocation()), loadedChunk.getLocation(), IntegratedServer.RADIUS)) { - if (clientChunk.getRenderChunkID().isPresent()) - getChunkHandler().removeChunk(clientChunk.getRenderChunkID().get()); + if (clientChunk.getVoxelID().isPresent()) + getChunkHandler().removeChunk(clientChunk.getVoxelID().get()); // System.out.println("Chunk Unloaded."); // Maybe: TODO The operation should be done below instead of in the ChunkCleaner @@ -404,25 +415,28 @@ public void gameSceneUpdate() { playerLocation.getNullableWorld().getChunkAt(nextLocation); } - if (clientChunk.getRenderChunkID().isEmpty() || clientChunk.isUpdatedHappened()) { - if (clientChunk.getRenderChunkID().isPresent()) - getChunkHandler().removeChunk(clientChunk.getRenderChunkID().get()); + if (clientChunk.getVoxelID().isEmpty() || clientChunk.isUpdatedHappened()) { + if (clientChunk.getVoxelID().isPresent()) + getChunkHandler().removeChunk(clientChunk.getVoxelID().get()); if (GameUtils.isLocationInsideCurrentLocationRadius(GameUtils.getChunkLocation(getServer().getPlayerEntity().getLocation()), loadedChunk.getLocation(), IntegratedServer.RADIUS * 16)) { ChunkLocation cb = loadedChunk.getLocation(); - RenderChunk rc = new RenderChunk(new ArrayList<>(), getTextureAtlas()); - rc.setPosition(cb.getX(), cb.getY(), cb.getZ()); - + VoxelChunk rc = new VoxelChunk(new ArrayList<>(), getTextureAtlas()); + rc.transform.setPosition(cb.getX(), cb.getY(), cb.getZ()); + boolean nonAirFound = false; for (GameBlock gb : loadedChunk.getGameBlocks()) { if (gb.getItemStack().getItem().getId() == 0) continue; + if (!nonAirFound) nonAirFound = true; Vector3 vector3 = MoreUtils.locationToVector3(gb.getLocation()); vector3 = vector3.subtract(cb.getX(), cb.getY(), cb.getZ()); - RenderBlock rb = new RenderBlock(new BlockLayout(), + Voxel rb = new Voxel(new BlockLayout(), renderResourceManager.get ((Kakara.getGameInstance().getResourceManager().getTexture(gb.getItemStack().getItem().getTexture(), TextureResolution._16, gb.getItemStack().getItem().getMod()).getLocalPath())), vector3); - rc.addBlock(rb); + rc.addVoxel(rb); } clientChunk.setUpdatedHappened(false); + + if (!nonAirFound) continue; if (!hasRun) { getServer().getExecutorService().submit(() -> { rc.regenerateChunk(getTextureAtlas(), MeshType.MULTITHREAD); @@ -432,7 +446,7 @@ public void gameSceneUpdate() { rc.regenerateChunk(getTextureAtlas(), MeshType.MULTITHREAD); } getChunkHandler().addChunk(rc); - clientChunk.setRenderChunkID(rc.getId()); + clientChunk.setVoxelID(rc.getId()); } } @@ -473,7 +487,7 @@ private ClientServerController getController() { return (ClientServerController) getServer().getServerController(); } - private RenderTexture getTexture(ItemStack is) { + private VoxelTexture getTexture(ItemStack is) { return renderResourceManager.get(GameResourceManager.correctPath(Kakara.getGameInstance().getResourceManager().getTexture(is.getItem().getTexture(), TextureResolution._16, is.getItem().getMod()).getLocalPath())); } } diff --git a/client/src/main/java/org/kakara/client/scenes/maingamescene/PlayerMovement.java b/client/src/main/java/org/kakara/client/scenes/maingamescene/PlayerMovement.java index 4d0efc60..a595aa1f 100644 --- a/client/src/main/java/org/kakara/client/scenes/maingamescene/PlayerMovement.java +++ b/client/src/main/java/org/kakara/client/scenes/maingamescene/PlayerMovement.java @@ -6,12 +6,15 @@ import org.kakara.core.common.world.Location; import org.kakara.engine.Camera; import org.kakara.engine.gameitems.GameItem; -import org.kakara.engine.gameitems.MeshGameItem; -import org.kakara.engine.input.KeyInput; -import org.kakara.engine.input.MouseInput; + +import org.kakara.engine.input.key.KeyInput; +import org.kakara.engine.input.mouse.MouseInput; import org.kakara.engine.math.Vector3; -import org.kakara.engine.physics.collision.Collidable; -import org.kakara.engine.renderobjects.RenderBlock; +import org.kakara.engine.physics.collision.ColliderComponent; +import org.kakara.engine.physics.collision.PhysicsComponent; +import org.kakara.engine.physics.collision.VoxelCollider; +import org.kakara.engine.voxels.Voxel; +import org.kakara.engine.voxels.VoxelTexture; import java.util.Optional; import java.util.UUID; @@ -22,7 +25,8 @@ public class PlayerMovement { boolean playerInJump = false; float lastYPos = 0; private final MainGameScene mainGameScene; - private MeshGameItem item; + private GameItem item; + private PhysicsComponent physicsComponent; public PlayerMovement(MainGameScene mainGameScene) { this.mainGameScene = mainGameScene; @@ -42,47 +46,48 @@ protected void playerMovement() { if (gameItemID.isEmpty()) return; Optional itemByID = mainGameScene.sceneUtils.getItemByID(gameItemID.get()); if (itemByID.isEmpty()) return; - item = (MeshGameItem) itemByID.get(); + item = itemByID.get(); + physicsComponent = item.getComponent(PhysicsComponent.class); } - item.setVelocityX(0); - item.setVelocityZ(0); + physicsComponent.setVelocityX(0); + physicsComponent.setVelocityZ(0); Camera gameCamera = mainGameScene.getCamera(); KeyInput ki = mainGameScene.kakaraGame.getGameHandler().getKeyInput(); if (ki.isKeyPressed(GLFW_KEY_W)) { - item.setVelocityByCamera(new Vector3(0, item.getVelocity().y, -7), gameCamera); + physicsComponent.setVelocityByCamera(new Vector3(0, physicsComponent.getVelocity().y, -7), gameCamera); } if (ki.isKeyPressed(GLFW_KEY_S)) { - item.setVelocityByCamera(new Vector3(0, item.getVelocity().y, 7), gameCamera); + physicsComponent.setVelocityByCamera(new Vector3(0, physicsComponent.getVelocity().y, 7), gameCamera); } if (ki.isKeyPressed(GLFW_KEY_A)) { - item.setVelocityByCamera(new Vector3(-7, item.getVelocity().y, 0), gameCamera); + physicsComponent.setVelocityByCamera(new Vector3(-7, physicsComponent.getVelocity().y, 0), gameCamera); } if (ki.isKeyPressed(GLFW_KEY_D)) { - item.setVelocityByCamera(new Vector3(7, item.getVelocity().y, 0), gameCamera); + physicsComponent.setVelocityByCamera(new Vector3(7, physicsComponent.getVelocity().y, 0), gameCamera); } if (ki.isKeyPressed(GLFW_KEY_LEFT_SHIFT)) { - item.movePositionByCamera(0, -0.3f, 0, gameCamera); + item.transform.movePositionByCamera(0, -0.3f, 0, gameCamera); } if (ki.isKeyPressed(GLFW_KEY_SPACE) && !playerInJump) { playerInJump = true; - lastYPos = item.getPosition().y; - item.setVelocityY(4); + lastYPos = item.transform.getPosition().y; + physicsComponent.setVelocityY(4); } if (playerInJump) { - item.movePositionByCamera(0, 0.3F, 0, gameCamera); - if (item.getPosition().y > lastYPos + 3) { + physicsComponent.getTransform().movePositionByCamera(0, 0.3F, 0, gameCamera); + if (physicsComponent.getTransform().getPosition().y > lastYPos + 3) { playerInJump = false; - item.setVelocityY(-9.18f); + physicsComponent.setVelocityY(-9.18f); } } if (ki.isKeyPressed(GLFW_KEY_G)) - item.setVelocityY(-9.18f); + physicsComponent.setVelocityY(-9.18f); MouseInput mi = mainGameScene.kakaraGame.getGameHandler().getMouseInput(); Location location; - location = new Location(player.getLocation().getNullableWorld(), item.getPosition().x, item.getPosition().y, item.getPosition().z, player.getLocation().getPitch(), player.getLocation().getYaw()); + location = new Location(player.getLocation().getNullableWorld(), item.transform.getPosition().x, item.transform.getPosition().y, item.transform.getPosition().z, player.getLocation().getPitch(), player.getLocation().getYaw()); location = location.add(new Location(0, 0, 0, (float) mi.getDeltaPosition().y(), (float) mi.getDeltaPosition().x())); ((ClientServerController) mainGameScene.getServer().getServerController()).playerMove(location); //TODO change ordering. In this current method on a server will result in large amounts of lag. @@ -91,14 +96,16 @@ protected void playerMovement() { mainGameScene.getCamera().setRotation(l.getPitch(), l.getYaw(), 0); // Handle the block selector. try { - Collidable objectFound = mainGameScene.selectGameItems(20, gameItemID.get()); + ColliderComponent objectFound = mainGameScene.selectGameItems(20, gameItemID.get()); if (mainGameScene.blockSelector != null) { - if (objectFound instanceof RenderBlock) { - RenderBlock block = (RenderBlock) objectFound; + + if (objectFound instanceof VoxelCollider) { + VoxelCollider objectFound1 = (VoxelCollider) objectFound; + Voxel block = objectFound1.getVoxel(); // This does not mutate the Vector3. - mainGameScene.blockSelector.setPosition(block.getPosition().add(block.getParentChunk().getPosition())); + mainGameScene.blockSelector.transform.setPosition(block.getPosition().add(block.getParentChunk().getTransform().getPosition())); } else { - mainGameScene.blockSelector.setPosition((float) l.getX(), -10, (float) l.getZ()); + mainGameScene.blockSelector.transform.setPosition((float) l.getX(), -10, (float) l.getZ()); } } } catch (NullPointerException ignored) { diff --git a/client/src/main/java/org/kakara/client/scenes/maingamescene/RenderResourceManager.java b/client/src/main/java/org/kakara/client/scenes/maingamescene/RenderResourceManager.java index cc0d703e..86f61dcc 100644 --- a/client/src/main/java/org/kakara/client/scenes/maingamescene/RenderResourceManager.java +++ b/client/src/main/java/org/kakara/client/scenes/maingamescene/RenderResourceManager.java @@ -2,32 +2,30 @@ import com.google.common.cache.CacheLoader; import org.kakara.client.scenes.CachingHashMap; -import org.kakara.engine.renderobjects.RenderTexture; +import org.kakara.engine.voxels.VoxelTexture; import org.kakara.game.resources.GameResourceManager; import java.util.Map; public class RenderResourceManager { private MainGameScene gameScene; - private Map renderTextureCache; + private Map renderTextureCache; public RenderResourceManager(MainGameScene gameScene) { this.gameScene = gameScene; - renderTextureCache = new CachingHashMap<>(new CacheLoader() { + renderTextureCache = new CachingHashMap<>(new CacheLoader<>() { @Override - public RenderTexture load(String s) throws Exception { + public VoxelTexture load(String s) throws Exception { return getResource(GameResourceManager.correctPath(s)); } }); } - private RenderTexture getResource(String texture) { - return gameScene.getTextureAtlas().getTextures().stream().filter(renderTexture -> GameResourceManager.correctPath(renderTexture.getResource().getOriginalPath()).equals(GameResourceManager.correctPath(texture))).findFirst().orElseThrow(() -> { - return new RuntimeException("Unable to find " + GameResourceManager.correctPath(texture)); - }); + private VoxelTexture getResource(String texture) { + return gameScene.getTextureAtlas().getTextures().stream().filter(renderTexture -> GameResourceManager.correctPath(renderTexture.getResource().getOriginalPath()).equals(GameResourceManager.correctPath(texture))).findFirst().orElseThrow(() -> new RuntimeException("Unable to find " + GameResourceManager.correctPath(texture))); } - public RenderTexture get(String localPath) { + public VoxelTexture get(String localPath) { return renderTextureCache.get(localPath); } } diff --git a/client/src/main/java/org/kakara/client/scenes/maingamescene/SceneUtils.java b/client/src/main/java/org/kakara/client/scenes/maingamescene/SceneUtils.java index fa076a4e..e43c240b 100644 --- a/client/src/main/java/org/kakara/client/scenes/maingamescene/SceneUtils.java +++ b/client/src/main/java/org/kakara/client/scenes/maingamescene/SceneUtils.java @@ -1,11 +1,13 @@ package org.kakara.client.scenes.maingamescene; +import org.kakara.core.common.Kakara; +import org.kakara.engine.components.MeshRenderer; import org.kakara.engine.gameitems.GameItem; -import org.kakara.engine.gameitems.MeshGameItem; import org.kakara.engine.gameitems.mesh.Mesh; import org.kakara.engine.math.Vector3; import org.kakara.engine.models.StaticModelLoader; import org.kakara.engine.physics.collision.BoxCollider; +import org.kakara.engine.physics.collision.PhysicsComponent; import java.util.Optional; import java.util.UUID; @@ -28,21 +30,23 @@ protected UUID createPlayerObject() { } catch (Exception e) { e.printStackTrace(); } - MeshGameItem object = new MeshGameItem(mainPlayer); - object.setVisible(false); - object.setPosition((float) gameScene.getServer().getPlayerEntity().getLocation().getX(), (float) gameScene.getServer().getPlayerEntity().getLocation().getY(), (float) gameScene.getServer().getPlayerEntity().getLocation().getZ()); - BoxCollider boxCollider = new BoxCollider(new Vector3(0, 0, 0), new Vector3(0.99f, 1.99f, 0.99f)); + GameItem object = new GameItem(mainPlayer); + object.getComponent(MeshRenderer.class).setVisible(false); + object.transform.setPosition((float) gameScene.getServer().getPlayerEntity().getLocation().getX(), (float) gameScene.getServer().getPlayerEntity().getLocation().getY(), (float) gameScene.getServer().getPlayerEntity().getLocation().getZ()); + PhysicsComponent physicsComponent = object.addComponent(PhysicsComponent.class); + BoxCollider boxCollider = object.addComponent(BoxCollider.class); + boxCollider.setPoint1(new Vector3(0, 0, 0)); + boxCollider.setPoint2(new Vector3(0.99f, 1.99f, 0.99f)); boxCollider.setPredicate(collidable -> { - if (collidable instanceof MeshGameItem) { - if (((MeshGameItem) collidable).getTag().equals("pickupable")) { - return true; - } + if (collidable.getGameItem() == null) return false; //How? + if (collidable.getGameItem().getTag() == null) return false; + if (collidable.getGameItem().getTag().equals("pickupable")) { + return true; } - return false; }); - object.setCollider(boxCollider); gameScene.add(object); + Kakara.LOGGER.debug("Player created with the UUID of "+ object.getUUID().toString()); return object.getUUID(); } } diff --git a/client/src/main/java/org/kakara/client/scenes/uicomponenets/ChatComponent.java b/client/src/main/java/org/kakara/client/scenes/uicomponenets/ChatComponent.java index af2b8b74..c7c00490 100644 --- a/client/src/main/java/org/kakara/client/scenes/uicomponenets/ChatComponent.java +++ b/client/src/main/java/org/kakara/client/scenes/uicomponenets/ChatComponent.java @@ -7,10 +7,12 @@ import org.kakara.engine.events.EventHandler; import org.kakara.engine.events.event.CharacterPressEvent; import org.kakara.engine.events.event.KeyPressEvent; +import org.kakara.engine.input.Input; +import org.kakara.engine.input.key.KeyCode; import org.kakara.engine.math.Vector2; import org.kakara.engine.scene.Scene; import org.kakara.engine.ui.UserInterface; -import org.kakara.engine.ui.components.GeneralComponent; +import org.kakara.engine.ui.components.GeneralUIComponent; import org.kakara.engine.ui.components.Panel; import org.kakara.engine.ui.components.shapes.Rectangle; import org.kakara.engine.ui.components.text.BoundedColoredText; @@ -25,12 +27,14 @@ import java.util.List; import java.util.ListIterator; -import static org.lwjgl.glfw.GLFW.*; +/** + * This handles the primary chat for the Game. + */ +public class ChatComponent extends GeneralUIComponent { -public class ChatComponent extends GeneralComponent { - - final float xSize = 700; - String tempStorage = ""; + private final float xSize = 700; + // Temporary stores what the user has typed when they push the up or down arrows. + private String tempStorage = ""; private final Font font; private boolean alwaysShowHistory; private final Text textAreaText; @@ -38,8 +42,11 @@ public class ChatComponent extends GeneralComponent { private String actualText; private final Panel historyPanel; private final Rectangle historyRectangle; + // This boolean is set to true when the chat is first opened so that the Key system does not + // put the letter T in the chat box. private boolean wait; private final List history; + // The list of sent history. (This is use for the up and down arrow functionality). private final List sentHistory = new ArrayList<>(); private int historyIndex = -1; private float timer = 0; @@ -54,13 +61,13 @@ public ChatComponent(Font fontToUse, boolean alwaysShowHistory, Scene scene) { this.setScale(xSize, 550); - Panel textArea = new Panel(); - textArea.setScale(xSize, 50); - textArea.add(new Rectangle(new Vector2(0, 0), new Vector2(500, 50), new RGBA(140, 140, 140, 0.4f))); + Rectangle textArea = new Rectangle(new Vector2(0, 0), new Vector2(500, 50), new RGBA(140, 140, 140, 0.4f)); + textArea.setVisible(true); Panel histroyArea = new Panel(); histroyArea.setScale(xSize, 500); histroyArea.setPosition(0, 0); + histroyArea.setAllowOverflow(true); this.historyRectangle = new Rectangle(new Vector2(0, 0), new Vector2(xSize, 500), new RGBA(168, 168, 168, 0.6f)); this.add(historyRectangle); textArea.setPosition(0, 500); @@ -83,6 +90,7 @@ public ChatComponent(Font fontToUse, boolean alwaysShowHistory, Scene scene) { scene.getEventManager().registerHandler(this); + setTag("chatcomponent"); } /** @@ -117,12 +125,22 @@ public boolean isAlwaysShown() { return alwaysShowHistory; } + /** + * Set if the history should always be shown. + * + * @param alwaysShown If the history should always be shown. + */ public void setAlwaysShown(boolean alwaysShown) { this.alwaysShowHistory = alwaysShown; if (alwaysShown) swapHistory(true); else if (!focus) swapHistory(false); } + /** + * Swap the history between shown and hidden. + * + * @param value If the history should be shown or hidden. + */ private void swapHistory(boolean value) { historyPanel.setVisible(value); historyRectangle.setVisible(value); @@ -130,18 +148,22 @@ private void swapHistory(boolean value) { @EventHandler public void onCharacterPress(CharacterPressEvent evt) { + // If T was just pressed or the chat is not in focus. if (!focus || wait) return; + // If the text is too long. if (actualText.length() > 100) return; + // Enter the text into the text box. byte[] ch = {(byte) evt.getCodePoint()}; String toadd = new String(ch, StandardCharsets.UTF_8).toLowerCase(); - if (GameHandler.getInstance().getKeyInput().isKeyPressed(GLFW_KEY_LEFT_SHIFT) || GameHandler.getInstance().getKeyInput().isKeyPressed(GLFW_KEY_RIGHT_SHIFT)) + if (Input.isKeyDown(KeyCode.LEFT_SHIFT) || Input.isKeyDown(KeyCode.RIGHT_SHIFT)) toadd = toadd.toUpperCase(); actualText += toadd; } @EventHandler public void onKeyPress(KeyPressEvent evt) { - if (evt.isKeyPressed(GLFW_KEY_T) && !focus) { + // Open the chat menu. + if (evt.isKeyPressed(KeyCode.T) && !focus) { focus = true; wait = true; textAreaText.setText("_"); @@ -151,19 +173,21 @@ public void onKeyPress(KeyPressEvent evt) { historyIndex = -1; return; } - if (evt.isKeyPressed(GLFW_KEY_LEFT_SHIFT) || evt.isKeyPressed(GLFW_KEY_RIGHT_SHIFT)) return; + if (evt.isKeyPressed(KeyCode.LEFT_SHIFT) || evt.isKeyPressed(KeyCode.RIGHT_SHIFT)) return; if (!focus) return; GameHandler handler = GameHandler.getInstance(); - if (handler.getKeyInput().isKeyPressed(GLFW_KEY_LEFT_CONTROL) && handler.getKeyInput().isKeyPressed(GLFW_KEY_V)) { - actualText += GameHandler.getInstance().getClipboard().getClipboard(); + // Allow the copying and pasting of data. + if (Input.isKeyDown(KeyCode.LEFT_CONTROL) && Input.isKeyDown(KeyCode.V)) { + actualText += handler.getClipboard().getClipboard(); if (actualText.length() > 100) actualText = actualText.substring(0, 100); } //TODO add text copying and highlighting. - if (handler.getKeyInput().isKeyPressed(GLFW_KEY_UP)) { + // Show previous history. + if (Input.isKeyDown(KeyCode.UP_ARROW)) { if (historyIndex + 1 < sentHistory.size()) { if (historyIndex == -1) tempStorage = actualText; @@ -171,7 +195,8 @@ public void onKeyPress(KeyPressEvent evt) { actualText = sentHistory.get(sentHistory.size() - 1 - historyIndex); } } - if (handler.getKeyInput().isKeyPressed(GLFW_KEY_DOWN)) { + // go back down history. + if (Input.isKeyDown(KeyCode.DOWN_ARROW)) { if (historyIndex - 1 > -2) { historyIndex--; if (historyIndex == -1) { @@ -182,14 +207,16 @@ public void onKeyPress(KeyPressEvent evt) { } } - if (evt.isKeyPressed(GLFW_KEY_ENTER)) { + // Send the text. + if (evt.isKeyPressed(KeyCode.ENTER)) { String t = actualText; //history.add(actualText); - //sentHistory.add(actualText); + sentHistory.add(actualText); actualText = ""; triggerEvent(ChatSendEvent.class, t); } - if (evt.isKeyPressed(GLFW_KEY_ESCAPE)) { + // Close the menu. + if (evt.isKeyPressed(KeyCode.ESCAPE)) { focus = false; actualText = ""; textAreaText.setText("Press T to type..."); @@ -208,7 +235,7 @@ public void init(UserInterface hud, GameHandler gameHandler) { @Override public void render(Vector2 relative, UserInterface hud, GameHandler handler) { - pollRender(relative, hud, handler); + super.render(relative, hud, handler); timer += Time.getDeltaTime(); if (wait) wait = false; @@ -228,10 +255,11 @@ public void render(Vector2 relative, UserInterface hud, GameHandler handler) { historyPanel.clearChildren(); List stringToRender = history.size() > 19 ? history.subList(history.size() - 19, history.size()) : new ArrayList<>(history); ListIterator li = stringToRender.listIterator(Math.min(history.size(), 19)); -// 19; + // Display the text in ascending order. float prevPos = 500; while (li.hasPrevious()) { BoundedColoredText ex = new BoundedColoredText(li.previous(), font); + ex.setParentCanvas(getParentCanvas()); ex.setMaximumBound(new Vector2(350, 55)); ex.init(hud, handler); ex.calculateLineNumbers(hud, handler); @@ -242,7 +270,7 @@ public void render(Vector2 relative, UserInterface hud, GameHandler handler) { ex.setSize(23); historyPanel.add(ex); } - if (handler.getKeyInput().isKeyPressed(GLFW_KEY_BACKSPACE) && actualText.length() > 0 && System.currentTimeMillis() - backspaceLag > 100) { + if (Input.isKeyDown(KeyCode.BACKSPACE) && actualText.length() > 0 && System.currentTimeMillis() - backspaceLag > 100) { backspaceLag = System.currentTimeMillis(); actualText = actualText.substring(0, actualText.length() - 1); } diff --git a/client/src/main/java/org/kakara/client/utils/IntegratedTime.java b/client/src/main/java/org/kakara/client/utils/IntegratedTime.java index cef7256d..4d5280aa 100644 --- a/client/src/main/java/org/kakara/client/utils/IntegratedTime.java +++ b/client/src/main/java/org/kakara/client/utils/IntegratedTime.java @@ -1,7 +1,7 @@ package org.kakara.client.utils; /** - * This class handles the time for the {@link org.kakara.client.game.IntegratedServer} class. + * This class handles the time for the {@link org.kakara.client.local.game.IntegratedServer} class. */ public class IntegratedTime { private static float deltaTime; diff --git a/game/build.gradle.kts b/game/build.gradle.kts index 7d73248a..c7243b20 100644 --- a/game/build.gradle.kts +++ b/game/build.gradle.kts @@ -44,7 +44,7 @@ dependencies { implementation(group = "me.ryandw11", name = "ods", version = "1.0.4") implementation(group = "me.ryandw11", name = "ODSCompressionPlus", version = "1.0.1") //Engine - compileOnly(group = "org.kakara", name = "engine", version = "1.0-SNAPSHOT") + compileOnly(group = "org.kakara", name = "engine", version = "1.0-SNAPSHOT"); //Core implementation("org.kakara.core:common:1.0-SNAPSHOT")