diff --git a/build.gradle b/build.gradle index 6eca3aaa..6fca6cc6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ group 'de.ellpeck.rockbottom' -version '0.5.0' +version '0.6.0' apply plugin: 'java' +apply plugin: 'java-library' apply plugin: 'idea' project.ext.lwjglVersion = "3.2.3" -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +java.toolchain.languageVersion = JavaLanguageVersion.of(17) group = "de.ellpeck.rockbottom" archivesBaseName = "RockBottomApi" @@ -22,36 +22,36 @@ repositories { dependencies { // LWJGL - compile "org.lwjgl:lwjgl:$lwjglVersion" - compile "org.lwjgl:lwjgl-glfw:$lwjglVersion" - compile "org.lwjgl:lwjgl-openal:$lwjglVersion" - compile "org.lwjgl:lwjgl-opengl:$lwjglVersion" - compile "org.lwjgl:lwjgl-stb:$lwjglVersion" - compile "org.joml:joml:1.9.8" + api "org.lwjgl:lwjgl:$lwjglVersion" + api "org.lwjgl:lwjgl-glfw:$lwjglVersion" + api "org.lwjgl:lwjgl-openal:$lwjglVersion" + api "org.lwjgl:lwjgl-opengl:$lwjglVersion" + api "org.lwjgl:lwjgl-stb:$lwjglVersion" + api "org.joml:joml:1.9.8" // LWJGL Natives - compile "org.lwjgl:lwjgl:$lwjglVersion:natives-windows" - compile "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-windows" - compile "org.lwjgl:lwjgl-openal:$lwjglVersion:natives-windows" - compile "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-windows" - compile "org.lwjgl:lwjgl-stb:$lwjglVersion:natives-windows" - compile "org.lwjgl:lwjgl:$lwjglVersion:natives-linux" - compile "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-linux" - compile "org.lwjgl:lwjgl-openal:$lwjglVersion:natives-linux" - compile "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-linux" - compile "org.lwjgl:lwjgl-stb:$lwjglVersion:natives-linux" - compile "org.lwjgl:lwjgl:$lwjglVersion:natives-macos" - compile "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-macos" - compile "org.lwjgl:lwjgl-openal:$lwjglVersion:natives-macos" - compile "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-macos" - compile "org.lwjgl:lwjgl-stb:$lwjglVersion:natives-macos" + api "org.lwjgl:lwjgl:$lwjglVersion:natives-windows" + api "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-windows" + api "org.lwjgl:lwjgl-openal:$lwjglVersion:natives-windows" + api "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-windows" + api "org.lwjgl:lwjgl-stb:$lwjglVersion:natives-windows" + api "org.lwjgl:lwjgl:$lwjglVersion:natives-linux" + api "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-linux" + api "org.lwjgl:lwjgl-openal:$lwjglVersion:natives-linux" + api "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-linux" + api "org.lwjgl:lwjgl-stb:$lwjglVersion:natives-linux" + api "org.lwjgl:lwjgl:$lwjglVersion:natives-macos" + api "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-macos" + api "org.lwjgl:lwjgl-openal:$lwjglVersion:natives-macos" + api "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-macos" + api "org.lwjgl:lwjgl-stb:$lwjglVersion:natives-macos" // Netty - compile group: 'io.netty', name: 'netty-all', version: '4.1.59.Final' + api group: 'io.netty', name: 'netty-all', version: '4.1.59.Final' // Google Commons - compile group: 'com.google.guava', name: 'guava', version: '30.1-jre' - compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6' + api group: 'com.google.guava', name: 'guava', version: '30.1-jre' + api group: 'com.google.code.gson', name: 'gson', version: '2.9.0' } task sourcesJar(type: Jar) { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7a19b8b5..5699c425 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip diff --git a/src/main/java/de/ellpeck/rockbottom/api/Constants.java b/src/main/java/de/ellpeck/rockbottom/api/Constants.java index e14b6504..e7aa55e1 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/Constants.java +++ b/src/main/java/de/ellpeck/rockbottom/api/Constants.java @@ -41,7 +41,7 @@ public final class Constants { /** - * The seperator for any resources, game or mod based. + * The separator for any resources, game or mod based. * * @see ResourceName */ @@ -130,4 +130,24 @@ public final class Constants { * This is a link to the website of the game */ public static final String WEBSITE_LINK = "https://rockbottom.ellpeck.de"; + + /** + * This is a link to the Twitter page for the game + */ + public static final String TWITTER_LINK = "https://twitter.com/RockBottom_Game"; + + /** + * This is a link to the itch.io page for the game + */ + public static final String ITCHIO_LINK = "https://rockbottomgame.itch.io"; + + /** + * This is a link to the github page for the game + */ + public static final String GITHUB_LINK = "https://github.com/RockBottomGame"; + + /** + * This is a link to the official discord channel for the game + */ + public static final String DISCORD_LINK = "https://discord.gg/vYm8TVxMtc"; } diff --git a/src/main/java/de/ellpeck/rockbottom/api/GameContent.java b/src/main/java/de/ellpeck/rockbottom/api/GameContent.java index 2213d23c..62d04757 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/GameContent.java +++ b/src/main/java/de/ellpeck/rockbottom/api/GameContent.java @@ -135,6 +135,7 @@ public static class Items { public static final Item MUSH = getItem("mush").setMaxAmount(20); public static final Item WOOD_BOOMERANG = getItem("wood_boomerang"); public static final Item SIMPLE_HOE = getItem("simple_hoe"); + public static final Item PLANT_ROPE = getItem("plant_rope"); } /* diff --git a/src/main/java/de/ellpeck/rockbottom/api/IApiHandler.java b/src/main/java/de/ellpeck/rockbottom/api/IApiHandler.java index 4d7461e1..14371aa4 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/IApiHandler.java +++ b/src/main/java/de/ellpeck/rockbottom/api/IApiHandler.java @@ -56,7 +56,6 @@ import java.util.Map; import java.util.Random; import java.util.function.Consumer; -import java.util.function.Function; import java.util.logging.Logger; /** diff --git a/src/main/java/de/ellpeck/rockbottom/api/IGameInstance.java b/src/main/java/de/ellpeck/rockbottom/api/IGameInstance.java index 90cd1d96..5bc5b1c2 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/IGameInstance.java +++ b/src/main/java/de/ellpeck/rockbottom/api/IGameInstance.java @@ -52,7 +52,7 @@ /** * The game instance that can be used to interact with various game-related - * values and acces things like {@link IRenderer} and {@link IAssetManager}. + * values and access things like {@link IRenderer} and {@link IAssetManager}. * Notice that this instance represents both the server and the client, however * some methods that are not implemented on the dedicated server (like graphics * and rendering methods) will throw an {@link UnsupportedOperationException}. diff --git a/src/main/java/de/ellpeck/rockbottom/api/IRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/IRenderer.java index bc336fe2..06d90502 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/IRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/IRenderer.java @@ -31,6 +31,7 @@ import de.ellpeck.rockbottom.api.render.engine.*; import de.ellpeck.rockbottom.api.util.ApiInternal; import de.ellpeck.rockbottom.api.util.Util; +import de.ellpeck.rockbottom.api.util.math.MatrixStack; import org.lwjgl.opengl.GL15; import java.nio.FloatBuffer; @@ -156,6 +157,10 @@ public interface IRenderer extends IDisposable { @ApiInternal void flush(); + void pushMatrix(); + + void popMatrix(); + /** * Rotates this renderer's rendering context by the given angle. This will * only affect any vertices that are added to this renderer after calling @@ -165,25 +170,6 @@ public interface IRenderer extends IDisposable { */ void rotate(float angle); - /** - * Sets the center of rotation for this renderer. This will only affect any - * vertices that are added to this renerer after calling this method. - * - * @param x The rotation center's x - * @param y The rotation center's y - */ - void setRotationCenter(float x, float y); - - /** - * @return This renderer's rotation center's x - */ - float getRotationCenterX(); - - /** - * @return This renderer's rotation center's y - */ - float getRotationCenterY(); - /** * Translates this renderer's origin by the given amount. This will only * affect any vertices that are added to this renderer after calling this @@ -195,14 +181,12 @@ public interface IRenderer extends IDisposable { void translate(float x, float y); /** - * Rather than translating by a certain amount (see {@link #translate(float, - * float)}), this method sets the translation of this renderer to the given - * x and y coordinates. + * Scales the renderer in both axes by the given amount. This will only affect any + * vertices that are added to this renderer after calling this method. * - * @param x The origin x - * @param y The origin y + * @param scale The scale */ - void setTranslation(float x, float y); + void scale(float scale); /** * Scales the renderer by the given amount. This will only affect any @@ -213,84 +197,11 @@ public interface IRenderer extends IDisposable { */ void scale(float x, float y); - /** - * Rather than scaling by a given amount (see {@link #scale(float, float)}), - * this method sets the scale of the renderer directly. - * - * @param x The x scale - * @param y The y scale - */ - void setScale(float x, float y); - - /** - * Mirrors the renderer either horizontally or vertically. This will only - * affect any vertices that are added to this renderer after calling this - * method. - * - * @param hor If the renderer should be mirrored horizontally - * @param vert If the renderer should be mirrored vertically - */ - void mirror(boolean hor, boolean vert); - - /** - * Rather than mirroring the renderer (see {@link #mirror(boolean, - * boolean)}), this method sets the mirrored states of this renderer to the - * given values. - * - * @param hor If the renderer should be mirrored horizontally - * @param vert If the renderer should be mirrored vertically - */ - void setMirrored(boolean hor, boolean vert); - /** * Resets this renderer's translation, rotation, scale, mirrored states and * texture origin back to their default values. */ - void resetTransformation(); - - /** - * @return This renderer's rotation - */ - float getRotation(); - - /** - * Rather than rotating by a given angle (see {@link #rotate(float)}), this - * method merely sets the rotation of this renderer to the given rotation. - * You can use this to reset the rotation back to 0 or its original state. - * - * @param angle The angle - */ - void setRotation(float angle); - - /** - * @return This renderer's translation x - */ - float getTranslationX(); - - /** - * @return This renderer's translation y - */ - float getTranslationY(); - - /** - * @return This renderer's x scale - */ - float getScaleX(); - - /** - * @return This renderer's y scale - */ - float getScaleY(); - - /** - * @return Whether this renderer is mirrored horizontally - */ - boolean isMirroredHor(); - - /** - * @return Whether this renderer is mirrored vertically - */ - boolean isMirroredVert(); + void resetMatrix(); /** * @return The renderer's current shader program, set using {@link @@ -710,4 +621,6 @@ public interface IRenderer extends IDisposable { double getCameraX(); double getCameraY(); + + MatrixStack getMatrixStack(); } diff --git a/src/main/java/de/ellpeck/rockbottom/api/RockBottomAPI.java b/src/main/java/de/ellpeck/rockbottom/api/RockBottomAPI.java index 2227514f..8113cdd7 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/RockBottomAPI.java +++ b/src/main/java/de/ellpeck/rockbottom/api/RockBottomAPI.java @@ -45,7 +45,7 @@ public final class RockBottomAPI { /** * The current API version equal to the version in the build.gradle file. */ - public static final String VERSION = "0.5.0"; + public static final String VERSION = "0.6.0"; /** * A set of internal references to API classes that are initialized by the @@ -183,5 +183,6 @@ public static void setInternals(Internals intern) { Preconditions.checkState(internals == null, "Mod tried to modify internal handlers - This is not allowed!"); internals = intern; } + } diff --git a/src/main/java/de/ellpeck/rockbottom/api/StaticTileProps.java b/src/main/java/de/ellpeck/rockbottom/api/StaticTileProps.java index 7367c765..c0674e73 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/StaticTileProps.java +++ b/src/main/java/de/ellpeck/rockbottom/api/StaticTileProps.java @@ -142,6 +142,13 @@ public final class StaticTileProps { public static final EnumProp COVER_COLOR = new EnumProp<>("cover_color", DyeColor.WHITE, DyeColor.class); public static final EnumProp PILLOW_COLOR = new EnumProp<>("pillow_color", DyeColor.WHITE, DyeColor.class); + /** + * Determines whether the state is an ending of a tile or not. + * Used by: + * {@link GameContent.Tiles#PLANT_ROPE} + */ + public static final BoolProp IS_ENDING = new BoolProp("is_ending", false); + /** * An enum of log types that are used for the {@link #LOG_VARIANT} * property. @@ -164,5 +171,4 @@ public boolean isNatural() { return this != PLACED; } } - } \ No newline at end of file diff --git a/src/main/java/de/ellpeck/rockbottom/api/assets/IAnimation.java b/src/main/java/de/ellpeck/rockbottom/api/assets/IAnimation.java index 6694fd40..fcd7714b 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/assets/IAnimation.java +++ b/src/main/java/de/ellpeck/rockbottom/api/assets/IAnimation.java @@ -43,6 +43,10 @@ public interface IAnimation extends IAsset { void drawRow(int row, float x, float y, float width, float height, int[] light, int filter); + void drawRow(int row, float x, float y, float width, float height, int filter, boolean mirrorHor, boolean mirrorVert); + + void drawRow(int row, float x, float y, float width, float height, int[] light, int filter, boolean mirrorHor, boolean mirrorVert); + void drawRow(int row, float x, float y, float scale, int filter); void drawRow(int row, float x, float y, float scale, int[] light, int filter); @@ -53,6 +57,10 @@ public interface IAnimation extends IAsset { void drawRow(long startTimeMillis, int row, float x, float y, float width, float height, int[] light, int filter); + void drawRow(long startTimeMillis, int row, float x, float y, float width, float height, int filter, boolean mirrorHor, boolean mirrorVert); + + void drawRow(long startTimeMillis, int row, float x, float y, float width, float height, int[] light, int filter, boolean mirrorHor, boolean mirrorVert); + void drawRow(long startTimeMillis, int row, float x, float y, float scale, int filter); void drawRow(long startTimeMillis, int row, float x, float y, float scale, int[] light, int filter); diff --git a/src/main/java/de/ellpeck/rockbottom/api/assets/texture/ITexture.java b/src/main/java/de/ellpeck/rockbottom/api/assets/texture/ITexture.java index 36f61a1c..1796c014 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/assets/texture/ITexture.java +++ b/src/main/java/de/ellpeck/rockbottom/api/assets/texture/ITexture.java @@ -70,6 +70,10 @@ public interface ITexture extends IAsset { void draw(float x, float y, float width, float height, int[] light, int filter); + void draw(float x, float y, float width, float height, int filter, boolean mirrorHor, boolean mirrorVert); + + void draw(float x, float y, float width, float height, int[] light, int filter, boolean mirrorHor, boolean mirrorVert); + void draw(float x, float y, float x2, float y2, float srcX, float srcY, float srcX2, float srcY2); void draw(float x, float y, float x2, float y2, float srcX, float srcY, float srcX2, float srcY2, int[] light); diff --git a/src/main/java/de/ellpeck/rockbottom/api/content/IContentLoader.java b/src/main/java/de/ellpeck/rockbottom/api/content/IContentLoader.java index 64412de9..f0318586 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/content/IContentLoader.java +++ b/src/main/java/de/ellpeck/rockbottom/api/content/IContentLoader.java @@ -25,6 +25,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import de.ellpeck.rockbottom.api.IGameInstance; import de.ellpeck.rockbottom.api.Registries; import de.ellpeck.rockbottom.api.RockBottomAPI; @@ -60,7 +61,7 @@ default void register() { default JsonObject getRecipeObject(IGameInstance game, String path) throws IOException { InputStreamReader reader = new InputStreamReader(game.getClassLoader().getResourceAsStream(path), Charsets.UTF_8); - JsonElement recipeElement = Util.JSON_PARSER.parse(reader); + JsonElement recipeElement = JsonParser.parseReader(reader); reader.close(); return recipeElement.getAsJsonObject(); } diff --git a/src/main/java/de/ellpeck/rockbottom/api/data/settings/Settings.java b/src/main/java/de/ellpeck/rockbottom/api/data/settings/Settings.java index 1284ae6f..f900146b 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/data/settings/Settings.java +++ b/src/main/java/de/ellpeck/rockbottom/api/data/settings/Settings.java @@ -56,6 +56,7 @@ public final class Settings implements IJsonSettings { public static final Keybind KEY_TOGGLE_CAMERA_MODE = new Keybind(ResourceName.intern("camera_mode"), GLFW.GLFW_KEY_K).register(); public static final Keybind KEY_ZOOM_OUT = new Keybind(ResourceName.intern("camera_zoom_out"), GLFW.GLFW_KEY_MINUS).register(); public static final Keybind KEY_ZOOM_IN = new Keybind(ResourceName.intern("camera_zoom_in"), GLFW.GLFW_KEY_EQUAL).register(); + public static final Keybind KEY_DELETE = new Keybind(ResourceName.intern("delete"), GLFW.GLFW_KEY_DELETE).register(); public static final Keybind[] KEYS_ITEM_SELECTION = new Keybind[8]; public static final int DEFAULT_GUI_COLOR = 0xFF30704E; diff --git a/src/main/java/de/ellpeck/rockbottom/api/gui/component/ButtonComponent.java b/src/main/java/de/ellpeck/rockbottom/api/gui/component/ButtonComponent.java index 00fff578..1a732c4a 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/gui/component/ButtonComponent.java +++ b/src/main/java/de/ellpeck/rockbottom/api/gui/component/ButtonComponent.java @@ -34,15 +34,15 @@ public class ButtonComponent extends GuiComponent { private final String[] hover; - private final Supplier supplier; + private final Supplier onClicked; public boolean hasBackground = true; protected String text; - public ButtonComponent(Gui gui, int x, int y, int sizeX, int sizeY, Supplier supplier, String text, String... hover) { + public ButtonComponent(Gui gui, int x, int y, int sizeX, int sizeY, Supplier onClicked, String text, String... hover) { super(gui, x, y, sizeX, sizeY); this.text = text; this.hover = hover; - this.supplier = supplier; + this.onClicked = onClicked; } public ButtonComponent setHasBackground(boolean has) { @@ -88,7 +88,7 @@ public void renderOverlay(IGameInstance game, IAssetManager manager, IRenderer g @Override public boolean onMouseAction(IGameInstance game, int button, float x, float y) { if (Settings.KEY_GUI_ACTION_1.isKey(button) && this.isMouseOver(game)) { - if (this.onPressed(game) || (this.supplier != null && this.supplier.get())) { + if (this.onPressed(game) || (this.onClicked != null && this.onClicked.get())) { game.getAssetManager().getSound(ResourceName.intern("menu.click")).play(); return true; } diff --git a/src/main/java/de/ellpeck/rockbottom/api/gui/component/FancyButtonComponent.java b/src/main/java/de/ellpeck/rockbottom/api/gui/component/ImageButtonComponent.java similarity index 93% rename from src/main/java/de/ellpeck/rockbottom/api/gui/component/FancyButtonComponent.java rename to src/main/java/de/ellpeck/rockbottom/api/gui/component/ImageButtonComponent.java index 41e47322..9214cf16 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/gui/component/FancyButtonComponent.java +++ b/src/main/java/de/ellpeck/rockbottom/api/gui/component/ImageButtonComponent.java @@ -29,11 +29,11 @@ import java.util.function.Supplier; -public class FancyButtonComponent extends ButtonComponent { +public class ImageButtonComponent extends ButtonComponent { protected final ResourceName texture; - public FancyButtonComponent(Gui gui, int x, int y, int sizeX, int sizeY, Supplier supplier, ResourceName texture, String... hover) { + public ImageButtonComponent(Gui gui, int x, int y, int sizeX, int sizeY, Supplier supplier, ResourceName texture, String... hover) { super(gui, x, y, sizeX, sizeY, supplier, null, hover); this.texture = texture; } diff --git a/src/main/java/de/ellpeck/rockbottom/api/gui/component/FancyToggleButtonComponent.java b/src/main/java/de/ellpeck/rockbottom/api/gui/component/ImageToggleButtonComponent.java similarity index 93% rename from src/main/java/de/ellpeck/rockbottom/api/gui/component/FancyToggleButtonComponent.java rename to src/main/java/de/ellpeck/rockbottom/api/gui/component/ImageToggleButtonComponent.java index 3d335992..f5d08a48 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/gui/component/FancyToggleButtonComponent.java +++ b/src/main/java/de/ellpeck/rockbottom/api/gui/component/ImageToggleButtonComponent.java @@ -27,12 +27,12 @@ import java.util.function.Supplier; -public class FancyToggleButtonComponent extends FancyButtonComponent { +public class ImageToggleButtonComponent extends ImageButtonComponent { private final ResourceName texToggled; private boolean isToggled; - public FancyToggleButtonComponent(Gui gui, int x, int y, int sizeX, int sizeY, boolean defaultState, Supplier supplier, ResourceName texture, String... hover) { + public ImageToggleButtonComponent(Gui gui, int x, int y, int sizeX, int sizeY, boolean defaultState, Supplier supplier, ResourceName texture, String... hover) { super(gui, x, y, sizeX, sizeY, supplier, texture, hover); this.isToggled = defaultState; this.texToggled = this.texture.addSuffix("_toggled"); diff --git a/src/main/java/de/ellpeck/rockbottom/api/internal/IInternalHooks.java b/src/main/java/de/ellpeck/rockbottom/api/internal/IInternalHooks.java index fa49caa5..106787b8 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/internal/IInternalHooks.java +++ b/src/main/java/de/ellpeck/rockbottom/api/internal/IInternalHooks.java @@ -27,7 +27,6 @@ import de.ellpeck.rockbottom.api.assets.IAssetManager; import de.ellpeck.rockbottom.api.assets.font.FormattingCode; import de.ellpeck.rockbottom.api.construction.compendium.PlayerCompendiumRecipe; -import de.ellpeck.rockbottom.api.construction.compendium.SmithingRecipe; import de.ellpeck.rockbottom.api.effect.ActiveEffect; import de.ellpeck.rockbottom.api.entity.AbstractItemEntity; import de.ellpeck.rockbottom.api.entity.Entity; @@ -71,7 +70,6 @@ public interface IInternalHooks { boolean doDefaultShiftClicking(IGameInstance game, int button, ContainerGui gui, SlotComponent slot); - // TODO: Move to IApiHandler boolean placeTile(int x, int y, TileLayer layer, AbstractPlayerEntity player, ItemInstance selected, Tile tile, boolean removeItem, boolean simulate); /** diff --git a/src/main/java/de/ellpeck/rockbottom/api/inventory/CombinedInventory.java b/src/main/java/de/ellpeck/rockbottom/api/inventory/CombinedInventory.java index 2c0b5723..5ed7c9ec 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/inventory/CombinedInventory.java +++ b/src/main/java/de/ellpeck/rockbottom/api/inventory/CombinedInventory.java @@ -213,6 +213,13 @@ public void fillRandomly(Random random, List items) { } } + @Override + public void clear() { + for (IInventory inventory : this.inventories) { + inventory.clear(); + } + } + @Override public Iterator iterator() { Iterator[] its = new Iterator[this.inventories.length]; diff --git a/src/main/java/de/ellpeck/rockbottom/api/inventory/IInventory.java b/src/main/java/de/ellpeck/rockbottom/api/inventory/IInventory.java index c3b5ae06..90da1264 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/inventory/IInventory.java +++ b/src/main/java/de/ellpeck/rockbottom/api/inventory/IInventory.java @@ -69,4 +69,6 @@ default boolean containsInv(IInventory inv) { ItemInstance addExistingFirst(ItemInstance instance, boolean simulate); void fillRandomly(Random random, List items); + + void clear(); } diff --git a/src/main/java/de/ellpeck/rockbottom/api/inventory/Inventory.java b/src/main/java/de/ellpeck/rockbottom/api/inventory/Inventory.java index a2f12bda..e1a5441e 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/inventory/Inventory.java +++ b/src/main/java/de/ellpeck/rockbottom/api/inventory/Inventory.java @@ -77,4 +77,9 @@ public void load(DataSet set) { } } } + + @Override + public void clear() { + Arrays.fill(this.slots, null); + } } diff --git a/src/main/java/de/ellpeck/rockbottom/api/net/IPacketContext.java b/src/main/java/de/ellpeck/rockbottom/api/net/IPacketContext.java new file mode 100644 index 00000000..603aebe2 --- /dev/null +++ b/src/main/java/de/ellpeck/rockbottom/api/net/IPacketContext.java @@ -0,0 +1,20 @@ +package de.ellpeck.rockbottom.api.net; + +import de.ellpeck.rockbottom.api.entity.player.AbstractPlayerEntity; +import io.netty.channel.ChannelHandlerContext; + +public interface IPacketContext { + + /** + * Retrieves the channel context passed to the packet handler. + */ + ChannelHandlerContext getChannelContext(); + + /** + * @return The player on the server that has sent the packet + * + * @throws UnsupportedOperationException on the client. + */ + AbstractPlayerEntity getSender(); + +} diff --git a/src/main/java/de/ellpeck/rockbottom/api/net/NetUtil.java b/src/main/java/de/ellpeck/rockbottom/api/net/NetUtil.java index 954bd0e8..0f8d16db 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/net/NetUtil.java +++ b/src/main/java/de/ellpeck/rockbottom/api/net/NetUtil.java @@ -49,7 +49,7 @@ public static void readSetFromBuffer(AbstractDataSet set, ByteBuf buf) { } } - public static void writeStringToBuffer(String s, ByteBuf buf) { + public static void writeStringToBuffer(ByteBuf buf, String s) { buf.writeInt(s.length()); for (char c : s.toCharArray()) { @@ -67,8 +67,8 @@ public static String readStringFromBuffer(ByteBuf buf) { return new String(chars); } - public static void writeResToBuffer(ResourceName name, ByteBuf buf) { - writeStringToBuffer(name.toString(), buf); + public static void writeResToBuffer(ByteBuf buf, ResourceName name) { + writeStringToBuffer(buf, name.toString()); } public static ResourceName readResFromBuffer(ByteBuf buf) { diff --git a/src/main/java/de/ellpeck/rockbottom/api/net/packet/IPacket.java b/src/main/java/de/ellpeck/rockbottom/api/net/packet/IPacket.java index f8fb3353..98b9477e 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/net/packet/IPacket.java +++ b/src/main/java/de/ellpeck/rockbottom/api/net/packet/IPacket.java @@ -23,6 +23,7 @@ import de.ellpeck.rockbottom.api.IGameInstance; import de.ellpeck.rockbottom.api.Registries; +import de.ellpeck.rockbottom.api.net.IPacketContext; import de.ellpeck.rockbottom.api.util.reg.ResourceName; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -33,7 +34,7 @@ public interface IPacket { void fromBuffer(ByteBuf buf); - void handle(IGameInstance game, ChannelHandlerContext context); + void handle(IGameInstance game, IPacketContext context); ResourceName getName(); diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/item/DefaultItemRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/item/DefaultItemRenderer.java index 49742471..c12436f2 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/item/DefaultItemRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/item/DefaultItemRenderer.java @@ -40,8 +40,8 @@ public DefaultItemRenderer(ResourceName texture) { } @Override - public void render(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, float x, float y, float scale, int filter) { - manager.getTexture(this.texture).draw(x, y, 1F * scale, 1F * scale, filter); + public void render(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { + manager.getTexture(this.texture).draw(x, y, scale, scale, filter, mirrored, false); } @Override diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/item/IItemRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/item/IItemRenderer.java index 12e190d3..9ed253d5 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/item/IItemRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/item/IItemRenderer.java @@ -32,26 +32,19 @@ public interface IItemRenderer { - void render(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, float x, float y, float scale, int filter); + void render(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored); default void renderOnMouseCursor(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, float x, float y, float scale, int filter, boolean isInPlayerRange) { } default void renderHolding(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, AbstractPlayerEntity player, float x, float y, float rotation, float scale, int filter, boolean mirrored) { + renderer.pushMatrix(); + renderer.translate(x, y); renderer.rotate(rotation); - - if (mirrored) { - renderer.mirror(true, false); - } - - this.render(game, manager, renderer, item, instance, x, y, scale * 0.5F, filter); - - if (mirrored) { - renderer.mirror(true, false); - } - - renderer.rotate(-rotation); + renderer.scale(scale); + this.render(game, manager, renderer, item, instance, 0F, 0F, 0.5F, filter, mirrored); + renderer.popMatrix(); } default JsonElement getAdditionalTextureData(IGameInstance game, IAssetManager manager, IRenderer renderer, T item, ItemInstance instance, AbstractPlayerEntity player, String name) { diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/item/MetaItemRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/item/MetaItemRenderer.java index 77b5f2f9..d3fb5aa2 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/item/MetaItemRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/item/MetaItemRenderer.java @@ -37,13 +37,13 @@ public MetaItemRenderer(ResourceName texture) { } @Override - public void render(IGameInstance game, IAssetManager manager, IRenderer renderer, MetaItem item, ItemInstance instance, float x, float y, float scale, int filter) { + public void render(IGameInstance game, IAssetManager manager, IRenderer renderer, MetaItem item, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { int meta = instance.getMeta(); if (meta >= 0 && item.subResourceNames.size() > meta) { - manager.getTexture(item.subResourceNames.get(meta)).draw(x, y, 1F * scale, 1F * scale, filter); + manager.getTexture(item.subResourceNames.get(meta)).draw(x, y, scale, scale, filter, mirrored, false); } else { - super.render(game, manager, renderer, item, instance, x, y, scale, filter); + super.render(game, manager, renderer, item, instance, x, y, scale, filter, mirrored); } } diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/item/TileItemRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/item/TileItemRenderer.java index 1eca062c..6948b981 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/item/TileItemRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/item/TileItemRenderer.java @@ -35,12 +35,12 @@ public class TileItemRenderer implements IItemRenderer { @Override - public void render(IGameInstance game, IAssetManager manager, IRenderer g, TileItem item, ItemInstance instance, float x, float y, float scale, int filter) { + public void render(IGameInstance game, IAssetManager manager, IRenderer g, TileItem item, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { Tile tile = item.getTile(); if (tile != null) { ITileRenderer renderer = tile.getRenderer(); if (renderer != null) { - renderer.renderItem(game, manager, g, tile, instance, x, y, scale, filter); + renderer.renderItem(game, manager, g, tile, instance, x, y, scale, filter, mirrored); } } } @@ -48,7 +48,7 @@ public void render(IGameInstance game, IAssetManager manager, IRenderer g, TileI @Override public void renderOnMouseCursor(IGameInstance game, IAssetManager manager, IRenderer renderer, TileItem item, ItemInstance instance, float x, float y, float scale, int filter, boolean isInPlayerRange) { if (isInPlayerRange) { - this.render(game, manager, renderer, item, instance, x + 0.1F * scale, y, scale * 0.75F, filter); + this.render(game, manager, renderer, item, instance, x + 0.1F * scale, y, scale * 0.75F, filter, false); } } diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/item/ToolItemRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/item/ToolItemRenderer.java index ab340035..df7843c4 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/item/ToolItemRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/item/ToolItemRenderer.java @@ -37,7 +37,7 @@ public ToolItemRenderer(ResourceName texture) { @Override public void renderOnMouseCursor(IGameInstance game, IAssetManager manager, IRenderer renderer, ToolItem item, ItemInstance instance, float x, float y, float scale, int filter, boolean isInPlayerRange) { if (isInPlayerRange) { - this.render(game, manager, renderer, item, instance, x, y, scale, filter); + this.render(game, manager, renderer, item, instance, x, y, scale, filter, false); } } } diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/tile/DefaultTileRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/tile/DefaultTileRenderer.java index 0324edb8..936bcead 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/tile/DefaultTileRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/tile/DefaultTileRenderer.java @@ -44,15 +44,21 @@ public class DefaultTileRenderer implements ITileRenderer { public ResourceName texture; + public ResourceName itemTexture; public DefaultTileRenderer(ResourceName texture) { + this(texture, false); + } + + public DefaultTileRenderer(ResourceName texture, boolean hasCustomItemTexture) { this.texture = texture.addPrefix("tiles."); + this.itemTexture = hasCustomItemTexture ? this.texture.addSuffix(".item") : this.texture; } @Override public void render(IGameInstance game, IAssetManager manager, IRenderer renderer, IWorld world, T tile, TileState state, int x, int y, TileLayer layer, float renderX, float renderY, float scale, int[] light) { if (!tile.isChiseled(world, x, y, layer, state)) - manager.getTexture(this.texture).getPositionalVariation(x, y).draw(renderX, renderY, scale, scale, light); + manager.getTexture(this.getTextureForState(world, layer, x, y, state)).getPositionalVariation(x, y).draw(renderX, renderY, scale, scale, light); } @@ -66,23 +72,23 @@ else if (tile.isChiselable() && layer == TileLayer.MAIN) } @Override - public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter) { - manager.getTexture(this.texture).draw(x, y, scale, scale, filter); + public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { + manager.getTexture(this.getTextureForItem(tile, instance)).draw(x, y, scale, scale, filter); } @Override public ITexture getParticleTexture(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, TileState state) { - return manager.getTexture(this.texture); + return manager.getTexture(this.getTextureForState(null, TileLayer.MAIN, 0, 0, state)); } @Override public void renderInMainMenuBackground(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, TileState state, float x, float y, float scale) { - manager.getTexture(this.texture).getPositionalVariation((int) x, (int) y).draw(x, y, scale, scale); + manager.getTexture(this.getTextureForState(null, TileLayer.MAIN, (int) x, (int) y, state)).getPositionalVariation((int) x, (int) y).draw(x, y, scale, scale); } protected void renderChiseled(IGameInstance game, IAssetManager manager, IRenderer renderer, IWorld world, T tile, TileState state, int x, int y, TileLayer layer, float renderX, float renderY, float scale, int[] light) { - ITexture texture = manager.getTexture(this.texture).getPositionalVariation(x, y); + ITexture texture = manager.getTexture(this.getTextureForState(world, layer, x, y, state)).getPositionalVariation(x, y); int prop = state.get(StaticTileProps.CHISEL_STATE); boolean[] chiseledCorners = Util.decodeBitVector(prop, 4); @@ -95,7 +101,7 @@ protected void renderChiseled(IGameInstance game, IAssetManager manager, IRender float maxX = (float) box.getMaxX(); float maxY = (float) box.getMaxY(); - int[] newLights = getChiseledLight(light, i); + int[] newLights = this.getChiseledLight(light, i); texture.draw(renderX + minX * scale, renderY + (1 - minY) * scale, renderX + maxX * scale, renderY + (1 - maxY) * scale, minX * 12, (1 - minY) * 12, maxX * 12, (1 - maxY) * 12, newLights); this.renderChiselHighlight(game, renderer, box, x, y, renderX, renderY, scale); @@ -105,6 +111,11 @@ protected void renderChiseled(IGameInstance game, IAssetManager manager, IRender protected void renderChiselHighlight(IGameInstance game, IRenderer renderer, BoundingBox box, int x, int y, float renderX, float renderY, float scale) { AbstractPlayerEntity player = game.getPlayer(); + ItemInstance held = game.getPlayer().getInv().get(game.getPlayer().getSelectedSlot()); + if (held == null) { + return; + } + int tileX = Util.floor(renderer.getMousedTileX()); int tileY = Util.floor(renderer.getMousedTileY()); if (!player.isInRange(tileX, tileY, player.getMaxInteractionDistance(player.world, renderer.getMousedTileX(), renderer.getMousedTileY(), player))) @@ -127,9 +138,6 @@ protected void renderChiselHighlight(IGameInstance game, IRenderer renderer, Bou float minX = (float) box.getMinX(); float minY = (float) box.getMinY(); - ItemInstance held = game.getPlayer().getInv().get(game.getPlayer().getSelectedSlot()); - if (held == null) - return; if (held.getItem().hasToolProperty(held, ToolProperty.CHISEL) && box.copy().add(x, y).contains(tileMouseX, tileMouseY)) { renderer.addEmptyRect(renderX + minX * scale, renderY + (0.5f - minY) * scale, 0.5f * scale, 0.5f * scale, Colors.WHITE); } @@ -137,7 +145,15 @@ protected void renderChiselHighlight(IGameInstance game, IRenderer renderer, Bou @Override public JsonElement getAdditionalTextureData(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, AbstractPlayerEntity player, String name) { - return manager.getTexture(this.texture).getAdditionalData(name); + return manager.getTexture(this.getTextureForItem(tile, instance)).getAdditionalData(name); + } + + public ResourceName getTextureForState(IWorld world, TileLayer layer, int x, int y, TileState state) { + return this.texture; + } + + public ResourceName getTextureForItem(T tile, ItemInstance item) { + return this.itemTexture; } protected int[] getChiseledLight(int[] light, int corner) { diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/tile/ITileRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/tile/ITileRenderer.java index 92f0575a..9be0eea9 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/tile/ITileRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/tile/ITileRenderer.java @@ -38,7 +38,7 @@ public interface ITileRenderer { void render(IGameInstance game, IAssetManager manager, IRenderer renderer, IWorld world, T tile, TileState state, int x, int y, TileLayer layer, float renderX, float renderY, float scale, int[] light); - void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter); + void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored); ITexture getParticleTexture(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, TileState state); diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/tile/MetaTileRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/tile/MetaTileRenderer.java index d5b2bda6..b7641da3 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/tile/MetaTileRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/tile/MetaTileRenderer.java @@ -47,9 +47,9 @@ public void render(IGameInstance game, IAssetManager manager, IRenderer renderer } @Override - public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter) { - this.setTextureResource(tile, instance.getMeta()); - super.renderItem(game, manager, renderer, tile, instance, x, y, scale, filter); + public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { + this.setItemTextureResource(tile, instance.getMeta()); + super.renderItem(game, manager, renderer, tile, instance, x, y, scale, filter, mirrored); } @Override @@ -86,4 +86,8 @@ public JsonElement getAdditionalTextureData(IGameInstance game, IAssetManager ma private void setTextureResource(T tile, int meta) { this.texture = getTextureResource(tile, meta); } + + private void setItemTextureResource(T tile, int meta) { + this.itemTexture = getTextureResource(tile, meta); + } } diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/tile/MultiTileRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/tile/MultiTileRenderer.java index 1119f097..e9d6421c 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/tile/MultiTileRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/tile/MultiTileRenderer.java @@ -69,7 +69,7 @@ public ITexture getParticleTexture(IGameInstance game, IAssetManager manager, IR } @Override - public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter) { + public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, T tile, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { manager.getTexture(this.texItem).draw(x, y, scale, scale, filter); } diff --git a/src/main/java/de/ellpeck/rockbottom/api/render/tile/PlatformTileRenderer.java b/src/main/java/de/ellpeck/rockbottom/api/render/tile/PlatformTileRenderer.java index 3cd591b0..15f481f0 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/render/tile/PlatformTileRenderer.java +++ b/src/main/java/de/ellpeck/rockbottom/api/render/tile/PlatformTileRenderer.java @@ -79,7 +79,7 @@ public void render(IGameInstance game, IAssetManager manager, IRenderer renderer } @Override - public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, PlatformTile tile, ItemInstance instance, float x, float y, float scale, int filter) { + public void renderItem(IGameInstance game, IAssetManager manager, IRenderer renderer, PlatformTile tile, ItemInstance instance, float x, float y, float scale, int filter, boolean mirrored) { manager.getTexture(this.texture).draw(x, y + scale/2, x + scale, y + scale + scale/2, 24, 0, 36, 12, filter); } } diff --git a/src/main/java/de/ellpeck/rockbottom/api/toast/ItemToast.java b/src/main/java/de/ellpeck/rockbottom/api/toast/ItemToast.java index 5405bb55..dddd4623 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/toast/ItemToast.java +++ b/src/main/java/de/ellpeck/rockbottom/api/toast/ItemToast.java @@ -73,7 +73,7 @@ public void render(IGameInstance game, IAssetManager manager, IRenderer g, float Item item = instance.getItem(); - item.getRenderer().render(game, manager, g, item, instance, x + 1, y + 1, 12f / 12f * size, Colors.WHITE); + item.getRenderer().render(game, manager, g, item, instance, x + 1, y + 1, 12f / 12f * size, Colors.WHITE, false); } if (this.descriptions.size() != this.items.size()) id = 0; diff --git a/src/main/java/de/ellpeck/rockbottom/api/util/Util.java b/src/main/java/de/ellpeck/rockbottom/api/util/Util.java index e584a19b..51c5855f 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/util/Util.java +++ b/src/main/java/de/ellpeck/rockbottom/api/util/Util.java @@ -66,11 +66,6 @@ public final class Util { * will not be a single line. */ public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - /** - * A global instance of google gson's json parser that can be used to parse - * jsons into easily modifyable and queryable json objects. - */ - public static final JsonParser JSON_PARSER = new JsonParser(); /** * Returns the absolute distance between two points using the Pythagorean diff --git a/src/main/java/de/ellpeck/rockbottom/api/util/math/Matrix3.java b/src/main/java/de/ellpeck/rockbottom/api/util/math/Matrix3.java new file mode 100644 index 00000000..1d08d9f4 --- /dev/null +++ b/src/main/java/de/ellpeck/rockbottom/api/util/math/Matrix3.java @@ -0,0 +1,120 @@ +package de.ellpeck.rockbottom.api.util.math; + +public class Matrix3 { + + public static Matrix3 translation(float x, float y) { + return new Matrix3( + 1, 0, x, + 0, 1, y, + 0, 0, 1 + ); + } + + public static Matrix3 rotation(float radians) { + return new Matrix3( + (float) Math.cos(radians), (float) Math.sin(radians), 0, + -(float) Math.sin(radians), (float) Math.cos(radians), 0, + 0, 0, 1 + ); + } + + public static Matrix3 scale(float scaleX, float scaleY) { + return new Matrix3( + scaleX, 0, 0, + 0, scaleY, 0, + 0, 0, 1 + ); + } + + public static Matrix3 horizontalMirror() { + return new Matrix3( + -1, 0, 0, + 0, 1, 0, + 0, 0, 1 + ); + } + + public static Matrix3 verticalMirror() { + return new Matrix3( + 1, 0, 0, + 0, -1, 0, + 0, 0, 1 + ); + } + + public static Matrix3 identity() { + return new Matrix3(); + } + + private float m00, m01, m02; + private float m10, m11, m12; + private float m20, m21, m22; + + public Matrix3() { + this.m00 = 1; + this.m11 = 1; + this.m22 = 1; + } + + public Matrix3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) { + this.m00 = m00; + this.m01 = m01; + this.m02 = m02; + this.m10 = m10; + this.m11 = m11; + this.m12 = m12; + this.m20 = m20; + this.m21 = m21; + this.m22 = m22; + } + + public Matrix3(Matrix3 copy) { + this(copy.m00, copy.m01, copy.m02, copy.m10, copy.m11, copy.m12, copy.m20, copy.m21, copy.m22); + } + + public Matrix3 multiply(Matrix3 matrix) { + float m00 = this.m00 * matrix.m00 + this.m01 * matrix.m10 + this.m02 * matrix.m20; + float m01 = this.m00 * matrix.m01 + this.m01 * matrix.m11 + this.m02 * matrix.m21; + float m02 = this.m00 * matrix.m02 + this.m01 * matrix.m12 + this.m02 * matrix.m22; + + float m10 = this.m10 * matrix.m00 + this.m11 * matrix.m10 + this.m12 * matrix.m20; + float m11 = this.m10 * matrix.m01 + this.m11 * matrix.m11 + this.m12 * matrix.m21; + float m12 = this.m10 * matrix.m02 + this.m11 * matrix.m12 + this.m12 * matrix.m22; + + float m20 = this.m20 * matrix.m00 + this.m21 * matrix.m10 + this.m22 * matrix.m20; + float m21 = this.m20 * matrix.m01 + this.m21 * matrix.m11 + this.m22 * matrix.m21; + float m22 = this.m20 * matrix.m02 + this.m21 * matrix.m12 + this.m22 * matrix.m22; + + this.m00 = m00; + this.m01 = m01; + this.m02 = m02; + this.m10 = m10; + this.m11 = m11; + this.m12 = m12; + this.m20 = m20; + this.m21 = m21; + this.m22 = m22; + return this; + } + + public Vector2 apply(Vector2 point) { + return new Vector2( + this.m00 * point.x + this.m01 * point.y + this.m02, + this.m10 * point.x + this.m11 * point.y + this.m12 + ); + } + + public Matrix3 copy() { + return new Matrix3(this); + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Matrix3{"); + sb.append('\n').append(m00).append(" ").append(m01).append(" ").append(m02); + sb.append('\n').append(m10).append(" ").append(m11).append(" ").append(m12); + sb.append('\n').append(m20).append(" ").append(m21).append(" ").append(m22); + sb.append("\n}"); + return sb.toString(); + } +} diff --git a/src/main/java/de/ellpeck/rockbottom/api/util/math/MatrixStack.java b/src/main/java/de/ellpeck/rockbottom/api/util/math/MatrixStack.java new file mode 100644 index 00000000..2cbe1882 --- /dev/null +++ b/src/main/java/de/ellpeck/rockbottom/api/util/math/MatrixStack.java @@ -0,0 +1,55 @@ +package de.ellpeck.rockbottom.api.util.math; + +import com.google.common.collect.Queues; +import de.ellpeck.rockbottom.api.RockBottomAPI; + +import java.util.Deque; +import java.util.logging.Level; + +public class MatrixStack { + + private final Deque stack = Queues.newArrayDeque(); + private Matrix3 matrix = Matrix3.identity(); + + public void push() { + this.stack.push(this.matrix); + this.matrix = this.matrix.copy(); + } + + public void pop() { + if (this.stack.isEmpty()) { + RockBottomAPI.logger().log(Level.WARNING, "Attempted to pop the matrix stack while empty. Make sure the number of pops and pushes are paired correctly."); + return; + } + this.matrix = this.stack.pop(); + } + + public Matrix3 getLast() { + return this.matrix; + } + + public void translate(float x, float y) { + this.matrix.multiply(Matrix3.translation(x, y)); + } + + public void rotate(float degrees) { + this.rotateRad((float)Math.toRadians(degrees)); + } + + public void rotateRad(float radians) { + this.matrix.multiply(Matrix3.rotation(radians)); + } + + public void scale(float scale) { + this.scale(scale, scale); + } + + public void scale(float scaleX, float scaleY) { + this.matrix.multiply(Matrix3.scale(scaleX, scaleY)); + } + + public void clear() { + this.stack.clear(); + this.matrix = Matrix3.identity(); + } +} diff --git a/src/main/java/de/ellpeck/rockbottom/api/util/math/Vector2.java b/src/main/java/de/ellpeck/rockbottom/api/util/math/Vector2.java new file mode 100644 index 00000000..b9df8ebe --- /dev/null +++ b/src/main/java/de/ellpeck/rockbottom/api/util/math/Vector2.java @@ -0,0 +1,36 @@ +package de.ellpeck.rockbottom.api.util.math; + +public class Vector2 { + + public static final Vector2 ZERO = new Vector2(0); + public static final Vector2 ONE = new Vector2(1); + + public final float x; + public final float y; + + public Vector2(float value) { + this(value, value); + } + + public Vector2(float x, float y) { + this.x = x; + this.y = y; + } + + public Vector2 add(Vector2 other) { + return new Vector2(this.x + other.x, this.y + other.y); + } + + public Vector2 multiply(Vector2 scale) { + return new Vector2(this.x * scale.x, this.y * scale.y); + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Vector2{"); + sb.append(x); + sb.append(", ").append(y); + sb.append('}'); + return sb.toString(); + } +} diff --git a/src/main/java/de/ellpeck/rockbottom/api/util/reg/NameToIndexInfo.java b/src/main/java/de/ellpeck/rockbottom/api/util/reg/NameToIndexInfo.java index 12d12129..855a8d7f 100644 --- a/src/main/java/de/ellpeck/rockbottom/api/util/reg/NameToIndexInfo.java +++ b/src/main/java/de/ellpeck/rockbottom/api/util/reg/NameToIndexInfo.java @@ -81,7 +81,7 @@ public void toBuffer(ByteBuf buf) { for (Map.Entry entry : this.reg.map.entrySet()) { buf.writeInt(entry.getKey()); - NetUtil.writeStringToBuffer(entry.getValue().toString(), buf); + NetUtil.writeStringToBuffer(buf, entry.getValue().toString()); } }