From de6e50264bb6d9195a74eac2207f6d30c7eb864c Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 17 Jul 2022 18:49:41 +0200 Subject: [PATCH] Added more team/item classes to core, and some refactoring of package structure. --- .../java/nl/andrewl/aos2_client/Client.java | 2 +- .../nl/andrewl/aos2_client/ClientWorld.java | 4 +- .../aos2_client/render/chunk/ChunkMesh.java | 4 +- .../render/chunk/ChunkMeshGenerator.java | 4 +- .../render/chunk/ChunkRenderer.java | 9 +--- .../nl/andrewl/aos_core/model/Player.java | 7 +++ .../java/nl/andrewl/aos_core/model/Team.java | 43 +++++++++++++++++++ .../aos_core/model/item/Inventory.java | 33 ++++++++++++++ .../aos_core/model/item/ItemStack.java | 27 ++++++++++++ .../andrewl/aos_core/model/item/ItemType.java | 28 ++++++++++++ .../aos_core/model/item/ItemTypes.java | 20 +++++++++ .../aos_core/model/{ => world}/Chunk.java | 2 +- .../model/{ => world}/ColorPalette.java | 2 +- .../aos_core/model/{ => world}/Hit.java | 2 +- .../aos_core/model/{ => world}/World.java | 2 +- .../aos_core/model/{ => world}/WorldIO.java | 2 +- .../aos_core/model/{ => world}/Worlds.java | 2 +- .../aos_core/net/ChunkDataMessage.java | 2 +- .../aos_core/net/ChunkHashMessage.java | 2 +- .../aos_core/net/WorldInfoMessage.java | 2 +- .../aos_core/net/udp/ChunkUpdateMessage.java | 2 +- .../nl/andrewl/aos_core/model/ChunkTest.java | 1 + .../nl/andrewl/aos_core/model/WorldTest.java | 3 ++ .../ClientCommunicationHandler.java | 3 +- .../java/nl/andrewl/aos2_server/Server.java | 8 ++-- .../nl/andrewl/aos2_server/ServerPlayer.java | 2 +- 26 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 core/src/main/java/nl/andrewl/aos_core/model/Team.java create mode 100644 core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java create mode 100644 core/src/main/java/nl/andrewl/aos_core/model/item/ItemStack.java create mode 100644 core/src/main/java/nl/andrewl/aos_core/model/item/ItemType.java create mode 100644 core/src/main/java/nl/andrewl/aos_core/model/item/ItemTypes.java rename core/src/main/java/nl/andrewl/aos_core/model/{ => world}/Chunk.java (98%) rename core/src/main/java/nl/andrewl/aos_core/model/{ => world}/ColorPalette.java (97%) rename core/src/main/java/nl/andrewl/aos_core/model/{ => world}/Hit.java (86%) rename core/src/main/java/nl/andrewl/aos_core/model/{ => world}/World.java (99%) rename core/src/main/java/nl/andrewl/aos_core/model/{ => world}/WorldIO.java (97%) rename core/src/main/java/nl/andrewl/aos_core/model/{ => world}/Worlds.java (98%) diff --git a/client/src/main/java/nl/andrewl/aos2_client/Client.java b/client/src/main/java/nl/andrewl/aos2_client/Client.java index 3815a84..e1632b7 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/Client.java +++ b/client/src/main/java/nl/andrewl/aos2_client/Client.java @@ -5,7 +5,7 @@ import nl.andrewl.aos2_client.control.PlayerInputKeyCallback; import nl.andrewl.aos2_client.control.PlayerInputMouseClickCallback; import nl.andrewl.aos2_client.control.PlayerViewCursorCallback; import nl.andrewl.aos2_client.render.GameRenderer; -import nl.andrewl.aos_core.model.ColorPalette; +import nl.andrewl.aos_core.model.world.ColorPalette; import nl.andrewl.aos_core.net.*; import nl.andrewl.aos_core.net.udp.ChunkUpdateMessage; import nl.andrewl.aos_core.net.udp.PlayerUpdateMessage; diff --git a/client/src/main/java/nl/andrewl/aos2_client/ClientWorld.java b/client/src/main/java/nl/andrewl/aos2_client/ClientWorld.java index b163577..20acd22 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/ClientWorld.java +++ b/client/src/main/java/nl/andrewl/aos2_client/ClientWorld.java @@ -2,9 +2,9 @@ package nl.andrewl.aos2_client; import nl.andrewl.aos2_client.render.chunk.ChunkMesh; import nl.andrewl.aos2_client.render.chunk.ChunkMeshGenerator; -import nl.andrewl.aos_core.model.Chunk; +import nl.andrewl.aos_core.model.world.Chunk; import nl.andrewl.aos_core.model.Player; -import nl.andrewl.aos_core.model.World; +import nl.andrewl.aos_core.model.world.World; import nl.andrewl.aos_core.net.ChunkDataMessage; import nl.andrewl.aos_core.net.PlayerJoinMessage; import nl.andrewl.aos_core.net.PlayerLeaveMessage; diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMesh.java b/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMesh.java index 5363b32..d0f3e12 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMesh.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMesh.java @@ -1,7 +1,7 @@ package nl.andrewl.aos2_client.render.chunk; -import nl.andrewl.aos_core.model.Chunk; -import nl.andrewl.aos_core.model.World; +import nl.andrewl.aos_core.model.world.Chunk; +import nl.andrewl.aos_core.model.world.World; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMeshGenerator.java b/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMeshGenerator.java index f61a911..b87e963 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMeshGenerator.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkMeshGenerator.java @@ -1,7 +1,7 @@ package nl.andrewl.aos2_client.render.chunk; -import nl.andrewl.aos_core.model.Chunk; -import nl.andrewl.aos_core.model.World; +import nl.andrewl.aos_core.model.world.Chunk; +import nl.andrewl.aos_core.model.world.World; import org.joml.Vector3f; import org.joml.Vector3i; import org.lwjgl.BufferUtils; diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkRenderer.java b/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkRenderer.java index 0b8184e..3eb9d52 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkRenderer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/chunk/ChunkRenderer.java @@ -2,16 +2,9 @@ package nl.andrewl.aos2_client.render.chunk; import nl.andrewl.aos2_client.Camera; import nl.andrewl.aos2_client.render.ShaderProgram; -import nl.andrewl.aos_core.model.Chunk; -import nl.andrewl.aos_core.model.World; -import org.joml.Matrix4f; -import org.joml.Vector3i; +import nl.andrewl.aos_core.model.world.Chunk; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; import static org.lwjgl.opengl.GL46.*; diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Player.java b/core/src/main/java/nl/andrewl/aos_core/model/Player.java index 05b9648..2b49b64 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/Player.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/Player.java @@ -42,7 +42,14 @@ public class Player { */ protected final Vector3f viewVector; + /** + * The player's name. + */ protected final String username; + + /** + * The player's unique id that it was assigned by the server. + */ protected final int id; public Player(int id, String username) { diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Team.java b/core/src/main/java/nl/andrewl/aos_core/model/Team.java new file mode 100644 index 0000000..118ba50 --- /dev/null +++ b/core/src/main/java/nl/andrewl/aos_core/model/Team.java @@ -0,0 +1,43 @@ +package nl.andrewl.aos_core.model; + +import org.joml.Vector3f; + +/** + * A team is a group of players in a world that should work together to + * achieve some goal. + */ +public class Team { + /** + * The internal id that this team is assigned. + */ + private final int id; + + /** + * The name of the team. + */ + private final String name; + + /** + * The team's color, used to identify players and structures belonging to + * this team. + */ + private final Vector3f color; + + public Team(int id, String name, Vector3f color) { + this.id = id; + this.name = name; + this.color = color; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public Vector3f getColor() { + return color; + } +} diff --git a/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java b/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java new file mode 100644 index 0000000..bd7207b --- /dev/null +++ b/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java @@ -0,0 +1,33 @@ +package nl.andrewl.aos_core.model.item; + +import java.util.List; + +/** + * Represents the contents and current state of a player's inventory. + */ +public class Inventory { + /** + * The list of item stacks in the inventory. + */ + private final List itemStacks; + + /** + * The index of the selected item stack. + */ + private int selectedIndex; + + public Inventory(List itemStacks, int selectedIndex) { + this.itemStacks = itemStacks; + this.selectedIndex = selectedIndex; + } + + public ItemStack getSelectedItemStack() { + return itemStacks.get(selectedIndex); + } + + public void setSelectedIndex(int newIndex) { + while (newIndex < 0) newIndex += itemStacks.size(); + while (newIndex > itemStacks.size() - 1) newIndex -= itemStacks.size(); + this.selectedIndex = newIndex; + } +} diff --git a/core/src/main/java/nl/andrewl/aos_core/model/item/ItemStack.java b/core/src/main/java/nl/andrewl/aos_core/model/item/ItemStack.java new file mode 100644 index 0000000..61f0f03 --- /dev/null +++ b/core/src/main/java/nl/andrewl/aos_core/model/item/ItemStack.java @@ -0,0 +1,27 @@ +package nl.andrewl.aos_core.model.item; + +/** + * Represents a stack of items in the player's inventory. This is generally + * a type of item, and the amount of it. + */ +public class ItemStack { + private final ItemType type; + private int amount; + + public ItemStack(ItemType type, int amount) { + this.type = type; + this.amount = amount; + } + + public Object getType() { + return type; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } +} diff --git a/core/src/main/java/nl/andrewl/aos_core/model/item/ItemType.java b/core/src/main/java/nl/andrewl/aos_core/model/item/ItemType.java new file mode 100644 index 0000000..613dc28 --- /dev/null +++ b/core/src/main/java/nl/andrewl/aos_core/model/item/ItemType.java @@ -0,0 +1,28 @@ +package nl.andrewl.aos_core.model.item; + +/** + * Represents a type of item that a player can have. + */ +public class ItemType { + private final int id; + private final String name; + private final int maxAmount; + + public ItemType(int id, String name, int maxAmount) { + this.id = id; + this.name = name; + this.maxAmount = maxAmount; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public int getMaxAmount() { + return maxAmount; + } +} diff --git a/core/src/main/java/nl/andrewl/aos_core/model/item/ItemTypes.java b/core/src/main/java/nl/andrewl/aos_core/model/item/ItemTypes.java new file mode 100644 index 0000000..71960b3 --- /dev/null +++ b/core/src/main/java/nl/andrewl/aos_core/model/item/ItemTypes.java @@ -0,0 +1,20 @@ +package nl.andrewl.aos_core.model.item; + +import java.util.HashMap; +import java.util.Map; + +/** + * Global constant set of registered item types. + */ +public final class ItemTypes { + public static final Map TYPES_MAP = new HashMap<>(); + + static { + registerType(new ItemType(1, "Rifle", 1)); + registerType(new ItemType(2, "Block", 100)); + } + + public static void registerType(ItemType type) { + TYPES_MAP.put(type.getId(), type); + } +} diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Chunk.java b/core/src/main/java/nl/andrewl/aos_core/model/world/Chunk.java similarity index 98% rename from core/src/main/java/nl/andrewl/aos_core/model/Chunk.java rename to core/src/main/java/nl/andrewl/aos_core/model/world/Chunk.java index df8eaf7..eeefa02 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/Chunk.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/Chunk.java @@ -1,4 +1,4 @@ -package nl.andrewl.aos_core.model; +package nl.andrewl.aos_core.model.world; import net.openhft.hashing.LongHashFunction; import org.joml.Vector3i; diff --git a/core/src/main/java/nl/andrewl/aos_core/model/ColorPalette.java b/core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java similarity index 97% rename from core/src/main/java/nl/andrewl/aos_core/model/ColorPalette.java rename to core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java index 0c1a0ff..a7c4e25 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/ColorPalette.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java @@ -1,4 +1,4 @@ -package nl.andrewl.aos_core.model; +package nl.andrewl.aos_core.model.world; import org.joml.Vector3f; diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Hit.java b/core/src/main/java/nl/andrewl/aos_core/model/world/Hit.java similarity index 86% rename from core/src/main/java/nl/andrewl/aos_core/model/Hit.java rename to core/src/main/java/nl/andrewl/aos_core/model/world/Hit.java index 5b2201a..cdbe354 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/Hit.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/Hit.java @@ -1,4 +1,4 @@ -package nl.andrewl.aos_core.model; +package nl.andrewl.aos_core.model.world; import org.joml.Vector3i; import org.joml.Vector3ic; diff --git a/core/src/main/java/nl/andrewl/aos_core/model/World.java b/core/src/main/java/nl/andrewl/aos_core/model/world/World.java similarity index 99% rename from core/src/main/java/nl/andrewl/aos_core/model/World.java rename to core/src/main/java/nl/andrewl/aos_core/model/world/World.java index 5ebb4c8..1ead2bb 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/World.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/World.java @@ -1,4 +1,4 @@ -package nl.andrewl.aos_core.model; +package nl.andrewl.aos_core.model.world; import nl.andrewl.aos_core.Directions; import nl.andrewl.aos_core.MathUtils; diff --git a/core/src/main/java/nl/andrewl/aos_core/model/WorldIO.java b/core/src/main/java/nl/andrewl/aos_core/model/world/WorldIO.java similarity index 97% rename from core/src/main/java/nl/andrewl/aos_core/model/WorldIO.java rename to core/src/main/java/nl/andrewl/aos_core/model/world/WorldIO.java index ff2d16f..a28bb53 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/WorldIO.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/WorldIO.java @@ -1,4 +1,4 @@ -package nl.andrewl.aos_core.model; +package nl.andrewl.aos_core.model.world; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Worlds.java b/core/src/main/java/nl/andrewl/aos_core/model/world/Worlds.java similarity index 98% rename from core/src/main/java/nl/andrewl/aos_core/model/Worlds.java rename to core/src/main/java/nl/andrewl/aos_core/model/world/Worlds.java index 76f80ed..28cfe73 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/Worlds.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/Worlds.java @@ -1,4 +1,4 @@ -package nl.andrewl.aos_core.model; +package nl.andrewl.aos_core.model.world; import org.joml.Vector3i; diff --git a/core/src/main/java/nl/andrewl/aos_core/net/ChunkDataMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/ChunkDataMessage.java index 6ecce41..58b7ba9 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/ChunkDataMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/ChunkDataMessage.java @@ -1,6 +1,6 @@ package nl.andrewl.aos_core.net; -import nl.andrewl.aos_core.model.Chunk; +import nl.andrewl.aos_core.model.world.Chunk; import nl.andrewl.record_net.Message; /** diff --git a/core/src/main/java/nl/andrewl/aos_core/net/ChunkHashMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/ChunkHashMessage.java index 34cae45..6620cc3 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/ChunkHashMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/ChunkHashMessage.java @@ -1,6 +1,6 @@ package nl.andrewl.aos_core.net; -import nl.andrewl.aos_core.model.Chunk; +import nl.andrewl.aos_core.model.world.Chunk; import nl.andrewl.record_net.Message; /** diff --git a/core/src/main/java/nl/andrewl/aos_core/net/WorldInfoMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/WorldInfoMessage.java index f95cbf6..aa00013 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/WorldInfoMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/WorldInfoMessage.java @@ -1,6 +1,6 @@ package nl.andrewl.aos_core.net; -import nl.andrewl.aos_core.model.World; +import nl.andrewl.aos_core.model.world.World; import nl.andrewl.record_net.Message; /** diff --git a/core/src/main/java/nl/andrewl/aos_core/net/udp/ChunkUpdateMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/udp/ChunkUpdateMessage.java index 0aa5ded..e106118 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/udp/ChunkUpdateMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/udp/ChunkUpdateMessage.java @@ -1,6 +1,6 @@ package nl.andrewl.aos_core.net.udp; -import nl.andrewl.aos_core.model.World; +import nl.andrewl.aos_core.model.world.World; import nl.andrewl.record_net.Message; import org.joml.Vector3i; diff --git a/core/src/test/java/nl/andrewl/aos_core/model/ChunkTest.java b/core/src/test/java/nl/andrewl/aos_core/model/ChunkTest.java index 48fb21f..cd34fcc 100644 --- a/core/src/test/java/nl/andrewl/aos_core/model/ChunkTest.java +++ b/core/src/test/java/nl/andrewl/aos_core/model/ChunkTest.java @@ -1,5 +1,6 @@ package nl.andrewl.aos_core.model; +import nl.andrewl.aos_core.model.world.Chunk; import org.joml.Vector3i; import org.junit.jupiter.api.Test; diff --git a/core/src/test/java/nl/andrewl/aos_core/model/WorldTest.java b/core/src/test/java/nl/andrewl/aos_core/model/WorldTest.java index b1a250a..6a310b7 100644 --- a/core/src/test/java/nl/andrewl/aos_core/model/WorldTest.java +++ b/core/src/test/java/nl/andrewl/aos_core/model/WorldTest.java @@ -1,6 +1,9 @@ package nl.andrewl.aos_core.model; import nl.andrewl.aos_core.Directions; +import nl.andrewl.aos_core.model.world.Chunk; +import nl.andrewl.aos_core.model.world.Hit; +import nl.andrewl.aos_core.model.world.World; import org.joml.Vector3f; import org.joml.Vector3i; import org.junit.jupiter.api.Test; diff --git a/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java b/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java index 5e9fce3..c67dd8a 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java +++ b/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java @@ -1,9 +1,8 @@ package nl.andrewl.aos2_server; import nl.andrewl.aos_core.Net; -import nl.andrewl.aos_core.model.Chunk; +import nl.andrewl.aos_core.model.world.Chunk; import nl.andrewl.aos_core.net.*; -import nl.andrewl.aos_core.net.udp.PlayerUpdateMessage; import nl.andrewl.record_net.Message; import nl.andrewl.record_net.util.ExtendedDataInputStream; import nl.andrewl.record_net.util.ExtendedDataOutputStream; diff --git a/server/src/main/java/nl/andrewl/aos2_server/Server.java b/server/src/main/java/nl/andrewl/aos2_server/Server.java index c53e812..2648033 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/Server.java +++ b/server/src/main/java/nl/andrewl/aos2_server/Server.java @@ -1,8 +1,8 @@ package nl.andrewl.aos2_server; -import nl.andrewl.aos_core.model.World; -import nl.andrewl.aos_core.model.WorldIO; -import nl.andrewl.aos_core.model.Worlds; +import nl.andrewl.aos_core.model.world.World; +import nl.andrewl.aos_core.model.world.WorldIO; +import nl.andrewl.aos_core.model.world.Worlds; import nl.andrewl.aos_core.net.UdpReceiver; import nl.andrewl.aos_core.net.udp.ClientInputState; import nl.andrewl.aos_core.net.udp.ClientOrientationState; @@ -36,8 +36,6 @@ public class Server implements Runnable { this.playerManager = new PlayerManager(); this.worldUpdater = new WorldUpdater(this, 20); this.world = Worlds.testingWorld(); - WorldIO.write(world, Path.of("worlds", "testingWorld")); -// this.world = WorldIO.read(Path.of("worlds", "testingWorld")); } @Override diff --git a/server/src/main/java/nl/andrewl/aos2_server/ServerPlayer.java b/server/src/main/java/nl/andrewl/aos2_server/ServerPlayer.java index 88cd76e..c246970 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/ServerPlayer.java +++ b/server/src/main/java/nl/andrewl/aos2_server/ServerPlayer.java @@ -1,7 +1,7 @@ package nl.andrewl.aos2_server; import nl.andrewl.aos_core.model.Player; -import nl.andrewl.aos_core.model.World; +import nl.andrewl.aos_core.model.world.World; import nl.andrewl.aos_core.net.udp.ChunkUpdateMessage; import nl.andrewl.aos_core.net.udp.ClientInputState; import org.joml.Math;