Use Vector2*c read-only interfaces where possible
This commit is contained in:
@@ -33,7 +33,7 @@ public class FollowEntityAI implements AI {
|
||||
if (!path.isEmpty()) {
|
||||
accumulator = recalculateInterval;
|
||||
|
||||
var node = new Vector2i(path.getLast()).sub(npc.getCoordinates());
|
||||
var node = path.getLast().sub(npc.getCoordinates(), new Vector2i());
|
||||
var direction = Direction.ofVector(node);
|
||||
var movement = npc.prepareMovement(direction);
|
||||
layer.pushMovement(movement);
|
||||
|
||||
@@ -3,10 +3,10 @@ package com.bartlomiejpluta.base.api.game.camera;
|
||||
import com.bartlomiejpluta.base.api.game.window.Window;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Matrix4fc;
|
||||
|
||||
public interface Camera extends Placeable {
|
||||
Matrix4f computeViewModelMatrix(Matrix4f modelMatrix);
|
||||
Matrix4fc computeViewModelMatrix(Matrix4fc modelMatrix);
|
||||
|
||||
void render(Window window, ShaderManager shaderManager);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.game.entity;
|
||||
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import static java.lang.Math.PI;
|
||||
import static org.joml.Math.atan2;
|
||||
@@ -13,37 +14,29 @@ public enum Direction {
|
||||
|
||||
public final int x;
|
||||
public final int y;
|
||||
public final Vector2ic vector;
|
||||
|
||||
Direction(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Vector2i asVector() {
|
||||
return new Vector2i(x, y);
|
||||
this.vector = new Vector2i(x, y);
|
||||
}
|
||||
|
||||
public Direction opposite() {
|
||||
switch (this) {
|
||||
case UP:
|
||||
return DOWN;
|
||||
case RIGHT:
|
||||
return LEFT;
|
||||
case DOWN:
|
||||
return UP;
|
||||
case LEFT:
|
||||
return RIGHT;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
return switch (this) {
|
||||
case UP -> DOWN;
|
||||
case RIGHT -> LEFT;
|
||||
case DOWN -> UP;
|
||||
case LEFT -> RIGHT;
|
||||
};
|
||||
}
|
||||
|
||||
public static Direction ofVector(Vector2i vector) {
|
||||
public static Direction ofVector(Vector2ic vector) {
|
||||
// X Versor = [1, 0]
|
||||
// dot = 1 * vector.x + 0 * vector.y = vector.x
|
||||
// det = 1 * vector.y - 0 * vector.x = vector.y
|
||||
// angle = atan2(det, dot) = atan2(vector.y, vector.x)
|
||||
float angle = atan2(vector.y, vector.x);
|
||||
float angle = atan2(vector.y(), vector.x());
|
||||
|
||||
if (-PI / 4 < angle && angle < PI / 4) {
|
||||
return RIGHT;
|
||||
|
||||
@@ -4,14 +4,14 @@ import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.Renderable;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Entity extends Placeable, Renderable, Updatable {
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
Vector2i getCoordinates();
|
||||
Vector2ic getCoordinates();
|
||||
|
||||
void setCoordinates(Vector2i coordinates);
|
||||
void setCoordinates(Vector2ic coordinates);
|
||||
|
||||
void setCoordinates(int x, int y);
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.game.window.Window;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public abstract class EntityDelegate implements Entity {
|
||||
protected final Entity entity;
|
||||
@@ -22,12 +22,12 @@ public abstract class EntityDelegate implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2i getCoordinates() {
|
||||
public Vector2ic getCoordinates() {
|
||||
return entity.getCoordinates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(Vector2i coordinates) {
|
||||
public void setCoordinates(Vector2ic coordinates) {
|
||||
entity.setCoordinates(coordinates);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ public abstract class EntityDelegate implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2f getPosition() {
|
||||
public Vector2fc getPosition() {
|
||||
return entity.getPosition();
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ public abstract class EntityDelegate implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2f position) {
|
||||
public void setPosition(Vector2fc position) {
|
||||
entity.setPosition(position);
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public abstract class EntityDelegate implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePosition(Vector2f position) {
|
||||
public void movePosition(Vector2fc position) {
|
||||
entity.movePosition(position);
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ public abstract class EntityDelegate implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matrix4f getModelMatrix() {
|
||||
public Matrix4fc getModelMatrix() {
|
||||
return entity.getModelMatrix();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package com.bartlomiejpluta.base.api.game.entity;
|
||||
|
||||
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Movement {
|
||||
boolean perform();
|
||||
|
||||
Movement another();
|
||||
|
||||
Vector2i getFrom();
|
||||
Vector2ic getFrom();
|
||||
|
||||
Vector2i getTo();
|
||||
Vector2ic getTo();
|
||||
|
||||
Direction getDirection();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movement;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.game.rule.Rule;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,7 +27,7 @@ public interface ObjectLayer extends Layer {
|
||||
|
||||
PassageAbility[][] getPassageMap();
|
||||
|
||||
boolean isTileReachable(Vector2i tileCoordinates);
|
||||
boolean isTileReachable(Vector2ic tileCoordinates);
|
||||
|
||||
void pushMovement(Movement movement);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.bartlomiejpluta.base.api.game.map.layer.color.ColorLayer;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.image.ImageLayer;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.tile.TileLayer;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
public interface GameMap {
|
||||
float getWidth();
|
||||
@@ -15,7 +15,7 @@ public interface GameMap {
|
||||
|
||||
int getColumns();
|
||||
|
||||
Vector2f getSize();
|
||||
Vector2fc getSize();
|
||||
|
||||
TileLayer getTileLayer(int layerIndex);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.game.window;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
public interface Window {
|
||||
void init();
|
||||
@@ -10,7 +10,7 @@ public interface Window {
|
||||
|
||||
int getHeight();
|
||||
|
||||
Vector2f getSize();
|
||||
Vector2fc getSize();
|
||||
|
||||
boolean isResized();
|
||||
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
package com.bartlomiejpluta.base.api.internal.object;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
public interface Placeable {
|
||||
Vector2f getPosition();
|
||||
Vector2fc getPosition();
|
||||
|
||||
void setPosition(float x, float y);
|
||||
void setPosition(Vector2f position);
|
||||
|
||||
void setPosition(Vector2fc position);
|
||||
|
||||
void movePosition(float x, float y);
|
||||
void movePosition(Vector2f position);
|
||||
|
||||
void movePosition(Vector2fc position);
|
||||
|
||||
float getRotation();
|
||||
|
||||
void setRotation(float rotation);
|
||||
|
||||
void moveRotation(float rotation);
|
||||
|
||||
float getScaleX();
|
||||
@@ -28,5 +34,5 @@ public interface Placeable {
|
||||
|
||||
float euclideanDistance(Placeable other);
|
||||
|
||||
Matrix4f getModelMatrix();
|
||||
Matrix4fc getModelMatrix();
|
||||
}
|
||||
|
||||
@@ -26,15 +26,15 @@ public interface ShaderManager extends Cleanable {
|
||||
|
||||
ShaderManager setUniform(String uniformName, float value);
|
||||
|
||||
ShaderManager setUniform(String uniformName, Vector2f value);
|
||||
ShaderManager setUniform(String uniformName, Vector2fc value);
|
||||
|
||||
ShaderManager setUniform(String uniformName, Vector3f value);
|
||||
ShaderManager setUniform(String uniformName, Vector3fc value);
|
||||
|
||||
ShaderManager setUniform(String uniformName, Vector4f value);
|
||||
ShaderManager setUniform(String uniformName, Vector4fc value);
|
||||
|
||||
ShaderManager setUniform(String uniformName, Matrix3f value);
|
||||
ShaderManager setUniform(String uniformName, Matrix3fc value);
|
||||
|
||||
ShaderManager setUniform(String uniformName, Matrix4f value);
|
||||
ShaderManager setUniform(String uniformName, Matrix4fc value);
|
||||
|
||||
ShaderManager setUniform(String uniformName, Uniform uniform);
|
||||
|
||||
|
||||
@@ -18,15 +18,15 @@ public interface ShaderProgram extends Disposable {
|
||||
|
||||
void setUniform(String uniformName, float value);
|
||||
|
||||
void setUniform(String uniformName, Vector2f value);
|
||||
void setUniform(String uniformName, Vector2fc value);
|
||||
|
||||
void setUniform(String uniformName, Vector3f value);
|
||||
void setUniform(String uniformName, Vector3fc value);
|
||||
|
||||
void setUniform(String uniformName, Vector4f value);
|
||||
void setUniform(String uniformName, Vector4fc value);
|
||||
|
||||
void setUniform(String uniformName, Matrix3f value);
|
||||
void setUniform(String uniformName, Matrix3fc value);
|
||||
|
||||
void setUniform(String uniformName, Matrix4f value);
|
||||
void setUniform(String uniformName, Matrix4fc value);
|
||||
|
||||
void setUniform(String uniformName, Uniform uniform);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.api.util.pathfinder;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
@@ -37,7 +38,7 @@ public class AstarPathFinder implements PathFinder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedList<Vector2i> findPath(ObjectLayer layer, Vector2i start, Vector2i end) {
|
||||
public LinkedList<Vector2ic> findPath(ObjectLayer layer, Vector2ic start, Vector2ic end) {
|
||||
var columns = layer.getMap().getColumns();
|
||||
var rows = layer.getMap().getRows();
|
||||
|
||||
@@ -158,13 +159,13 @@ public class AstarPathFinder implements PathFinder {
|
||||
return new LinkedList<>();
|
||||
}
|
||||
|
||||
private float manhattanDistance(Vector2i a, Vector2i b) {
|
||||
return (abs(a.x - b.x) + abs(a.y - b.y));
|
||||
private float manhattanDistance(Vector2ic a, Vector2ic b) {
|
||||
return (abs(a.x() - b.x()) + abs(a.y() - b.y()));
|
||||
}
|
||||
|
||||
private LinkedList<Vector2i> recreatePath(Node node) {
|
||||
private LinkedList<Vector2ic> recreatePath(Node node) {
|
||||
var current = node;
|
||||
var list = new LinkedList<Vector2i>();
|
||||
var list = new LinkedList<Vector2ic>();
|
||||
|
||||
while (current.parent != null) {
|
||||
list.add(current.position);
|
||||
@@ -176,11 +177,11 @@ public class AstarPathFinder implements PathFinder {
|
||||
|
||||
private static class Node implements Comparable<Node> {
|
||||
public Node parent;
|
||||
public final Vector2i position;
|
||||
public final Vector2ic position;
|
||||
public float g = 0.0f;
|
||||
public float f = 0.0f;
|
||||
|
||||
public Node(Vector2i position) {
|
||||
public Node(Vector2ic position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.bartlomiejpluta.base.api.util.pathfinder;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public interface PathFinder {
|
||||
LinkedList<Vector2i> findPath(ObjectLayer layer, Vector2i start, Vector2i end);
|
||||
LinkedList<Vector2ic> findPath(ObjectLayer layer, Vector2ic start, Vector2ic end);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,13 @@ import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
import org.joml.*;
|
||||
|
||||
@Getter
|
||||
public class Material implements Renderable {
|
||||
private final Vector4f color = new Vector4f();
|
||||
private final Vector2f spritePosition = new Vector2f(0, 0);
|
||||
|
||||
@Getter
|
||||
private final Texture texture;
|
||||
|
||||
private Material(Texture texture, float red, float green, float blue, float alpha) {
|
||||
@@ -37,17 +36,21 @@ public class Material implements Renderable {
|
||||
}
|
||||
}
|
||||
|
||||
public void setColor(Vector4f color) {
|
||||
this.color.x = color.x;
|
||||
this.color.y = color.y;
|
||||
this.color.z = color.z;
|
||||
this.color.w = color.w;
|
||||
public Vector4fc getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(Vector3f color) {
|
||||
this.color.x = color.x;
|
||||
this.color.y = color.y;
|
||||
this.color.z = color.z;
|
||||
public void setColor(Vector4fc color) {
|
||||
this.color.x = color.x();
|
||||
this.color.y = color.y();
|
||||
this.color.z = color.z();
|
||||
this.color.w = color.w();
|
||||
}
|
||||
|
||||
public void setColor(Vector3fc color) {
|
||||
this.color.x = color.x();
|
||||
this.color.y = color.y();
|
||||
this.color.z = color.z();
|
||||
}
|
||||
|
||||
public void setColor(float red, float green, float blue, float alpha) {
|
||||
@@ -79,19 +82,23 @@ public class Material implements Renderable {
|
||||
this.color.w = alpha;
|
||||
}
|
||||
|
||||
public void setSpritePosition(Vector2f spritePosition) {
|
||||
public Vector2fc getSpritePosition() {
|
||||
return spritePosition;
|
||||
}
|
||||
|
||||
public void setSpritePosition(Vector2fc spritePosition) {
|
||||
if (texture != null) {
|
||||
var size = texture.getSpriteFragment();
|
||||
this.spritePosition.x = size.x * spritePosition.x;
|
||||
this.spritePosition.y = size.y * spritePosition.y;
|
||||
this.spritePosition.x = size.x() * spritePosition.x();
|
||||
this.spritePosition.y = size.y() * spritePosition.y();
|
||||
}
|
||||
}
|
||||
|
||||
public void setSpritePosition(float x, float y) {
|
||||
if (texture != null) {
|
||||
var size = texture.getSpriteFragment();
|
||||
this.spritePosition.x = size.x * x;
|
||||
this.spritePosition.y = size.y * y;
|
||||
this.spritePosition.x = size.x() * x;
|
||||
this.spritePosition.y = size.y() * y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -35,10 +36,10 @@ public class Texture implements Disposable {
|
||||
private final int columns;
|
||||
|
||||
@Getter
|
||||
private final Vector2f spriteFragment;
|
||||
private final Vector2fc spriteFragment;
|
||||
|
||||
@Getter
|
||||
private final Vector2f spriteSize;
|
||||
private final Vector2fc spriteSize;
|
||||
|
||||
|
||||
Texture(String textureFilename, ByteBuffer buffer, int rows, int columns) {
|
||||
|
||||
@@ -95,31 +95,31 @@ public class DefaultShaderManager implements ShaderManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShaderManager setUniform(String uniformName, Vector2f value) {
|
||||
public ShaderManager setUniform(String uniformName, Vector2fc value) {
|
||||
current.setUniform(uniformName, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShaderManager setUniform(String uniformName, Vector3f value) {
|
||||
public ShaderManager setUniform(String uniformName, Vector3fc value) {
|
||||
current.setUniform(uniformName, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShaderManager setUniform(String uniformName, Vector4f value) {
|
||||
public ShaderManager setUniform(String uniformName, Vector4fc value) {
|
||||
current.setUniform(uniformName, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShaderManager setUniform(String uniformName, Matrix3f value) {
|
||||
public ShaderManager setUniform(String uniformName, Matrix3fc value) {
|
||||
current.setUniform(uniformName, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShaderManager setUniform(String uniformName, Matrix4f value) {
|
||||
public ShaderManager setUniform(String uniformName, Matrix4fc value) {
|
||||
current.setUniform(uniformName, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -117,22 +117,22 @@ public class GLShaderProgram implements ShaderProgram {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniform(String uniformName, Vector2f value) {
|
||||
glUniform2f(uniforms.get(uniformName), value.x, value.y);
|
||||
public void setUniform(String uniformName, Vector2fc value) {
|
||||
glUniform2f(uniforms.get(uniformName), value.x(), value.y());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniform(String uniformName, Vector3f value) {
|
||||
glUniform3f(uniforms.get(uniformName), value.x, value.y, value.z);
|
||||
public void setUniform(String uniformName, Vector3fc value) {
|
||||
glUniform3f(uniforms.get(uniformName), value.x(), value.y(), value.z());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniform(String uniformName, Vector4f value) {
|
||||
glUniform4f(uniforms.get(uniformName), value.x, value.y, value.z, value.w);
|
||||
public void setUniform(String uniformName, Vector4fc value) {
|
||||
glUniform4f(uniforms.get(uniformName), value.x(), value.y(), value.z(), value.w());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniform(String uniformName, Matrix3f value) {
|
||||
public void setUniform(String uniformName, Matrix3fc value) {
|
||||
try (var stack = MemoryStack.stackPush()) {
|
||||
var buffer = stack.mallocFloat(3 * 3);
|
||||
value.get(buffer);
|
||||
@@ -141,7 +141,7 @@ public class GLShaderProgram implements ShaderProgram {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniform(String uniformName, Matrix4f value) {
|
||||
public void setUniform(String uniformName, Matrix4fc value) {
|
||||
try (var stack = MemoryStack.stackPush()) {
|
||||
var buffer = stack.mallocFloat(4 * 4);
|
||||
value.get(buffer);
|
||||
|
||||
@@ -7,6 +7,7 @@ import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
import org.lwjgl.glfw.GLFWErrorCallback;
|
||||
import org.lwjgl.glfw.GLFWVidMode;
|
||||
import org.lwjgl.opengl.GL;
|
||||
@@ -31,10 +32,13 @@ public class GLFWWindow implements Window {
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
private final Vector2f size;
|
||||
|
||||
public GLFWWindow(@NonNull String title, int width, int height) {
|
||||
this.title = title;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.size = new Vector2f(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,6 +77,8 @@ public class GLFWWindow implements Window {
|
||||
GLFWWindow.this.width = width;
|
||||
GLFWWindow.this.height = height;
|
||||
GLFWWindow.this.resized = true;
|
||||
GLFWWindow.this.size.x = width;
|
||||
GLFWWindow.this.size.y = height;
|
||||
});
|
||||
|
||||
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
|
||||
@@ -92,7 +98,7 @@ public class GLFWWindow implements Window {
|
||||
glfwMakeContextCurrent(windowHandle);
|
||||
|
||||
// Enable V-Sync
|
||||
// glfwSwapInterval(1);
|
||||
// glfwSwapInterval(1);
|
||||
|
||||
// Make the window visible
|
||||
glfwShowWindow(windowHandle);
|
||||
@@ -104,7 +110,7 @@ public class GLFWWindow implements Window {
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
// Set the clear color
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
clear(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -129,8 +135,8 @@ public class GLFWWindow implements Window {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2f getSize() {
|
||||
return new Vector2f(width, height);
|
||||
public Vector2fc getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public static Window create(String title, int width, int height) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class AnimatedSprite extends Sprite {
|
||||
@@ -21,7 +21,7 @@ public abstract class AnimatedSprite extends Sprite {
|
||||
|
||||
public abstract boolean shouldAnimate();
|
||||
|
||||
public abstract Vector2f[] getSpriteAnimationFramesPositions();
|
||||
public abstract Vector2fc[] getSpriteAnimationFramesPositions();
|
||||
|
||||
@Override
|
||||
public void render(Window window, Camera camera, ShaderManager shaderManager) {
|
||||
|
||||
@@ -6,13 +6,14 @@ import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
||||
import com.bartlomiejpluta.base.engine.world.object.Model;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Matrix4fc;
|
||||
|
||||
public class DefaultCamera extends Model implements Camera {
|
||||
private final Matrix4f projectionMatrix = new Matrix4f();
|
||||
private final Matrix4f viewMatrix = new Matrix4f();
|
||||
|
||||
@Override
|
||||
public Matrix4f computeViewModelMatrix(Matrix4f modelMatrix) {
|
||||
public Matrix4fc computeViewModelMatrix(Matrix4fc modelMatrix) {
|
||||
return new Matrix4f(viewMatrix).mul(modelMatrix);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.engine.world.entity.config;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import lombok.Data;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -16,13 +17,19 @@ public class EntitySpriteConfiguration {
|
||||
private int defaultSpriteColumn;
|
||||
private Map<Direction, Integer> spriteDirectionRows;
|
||||
|
||||
@Data
|
||||
public static class EntitySpriteDimensionConfiguration {
|
||||
private int rows;
|
||||
private int cols;
|
||||
private final Vector2i vector = new Vector2i();
|
||||
|
||||
public Vector2i asVector() {
|
||||
return new Vector2i(rows, cols);
|
||||
public Vector2ic asVector() {
|
||||
return vector;
|
||||
}
|
||||
|
||||
public void setRows(int rows) {
|
||||
this.vector.y = rows;
|
||||
}
|
||||
|
||||
public void setCols(int cols) {
|
||||
this.vector.x = cols;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ public class DefaultEntityManager implements EntityManager {
|
||||
private Mesh buildMesh(Material material) {
|
||||
var texture = material.getTexture();
|
||||
var dimension = configuration.getDimension().asVector();
|
||||
var spriteWidth = texture.getWidth() / (float) dimension.y;
|
||||
var spriteHeight = texture.getHeight() / (float) dimension.x;
|
||||
return meshManager.createQuad(spriteWidth, spriteHeight, spriteWidth / 2, spriteHeight*0.9f);
|
||||
var spriteWidth = texture.getWidth() / (float) dimension.x();
|
||||
var spriteHeight = texture.getHeight() / (float) dimension.y();
|
||||
return meshManager.createQuad(spriteWidth, spriteHeight, spriteWidth / 2, spriteHeight * 0.9f);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,14 +13,17 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.stream.Collectors.toUnmodifiableMap;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class DefaultEntity extends MovableSprite implements Entity {
|
||||
private final Map<Direction, Integer> spriteDirectionRows;
|
||||
private final int defaultSpriteColumn;
|
||||
private final Map<Direction, Vector2fc> defaultAnimationFrames;
|
||||
|
||||
private int animationSpeed = 100;
|
||||
|
||||
@@ -42,7 +45,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
private boolean blocking;
|
||||
|
||||
@Override
|
||||
public Vector2f[] getSpriteAnimationFramesPositions() {
|
||||
public Vector2fc[] getSpriteAnimationFramesPositions() {
|
||||
var row = spriteDirectionRows.get(faceDirection);
|
||||
var frames = material.getTexture().getRows();
|
||||
var array = new Vector2f[frames];
|
||||
@@ -56,7 +59,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
|
||||
@Override
|
||||
protected void setDefaultAnimationFrame() {
|
||||
material.setSpritePosition(new Vector2f(defaultSpriteColumn, spriteDirectionRows.get(faceDirection)));
|
||||
material.setSpritePosition(defaultAnimationFrames.get(faceDirection));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,7 +100,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
|
||||
@Override
|
||||
public Direction getDirectionTowards(Entity target) {
|
||||
return Direction.ofVector(new Vector2i(target.getCoordinates()).sub(getCoordinates()));
|
||||
return Direction.ofVector(target.getCoordinates().sub(getCoordinates(), new Vector2i()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -112,8 +115,14 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
|
||||
public DefaultEntity(Mesh mesh, Material material, EntitySpriteConfiguration configuration) {
|
||||
super(mesh, material);
|
||||
this.defaultSpriteColumn = configuration.getDefaultSpriteColumn();
|
||||
this.spriteDirectionRows = configuration.getSpriteDirectionRows();
|
||||
this.faceDirection = Direction.DOWN;
|
||||
|
||||
var defaultColumn = configuration.getDefaultSpriteColumn();
|
||||
|
||||
defaultAnimationFrames = spriteDirectionRows
|
||||
.entrySet()
|
||||
.stream()
|
||||
.collect(toUnmodifiableMap(Map.Entry::getKey, entry -> new Vector2f(defaultColumn, entry.getValue())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,6 @@ public class DefaultImage extends Sprite implements Image {
|
||||
|
||||
@Override
|
||||
public float getOpacity() {
|
||||
return material.getColor().w;
|
||||
return material.getColor().w();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ public class DefaultImageLayer implements ImageLayer {
|
||||
if (image != null) {
|
||||
if (parallax) {
|
||||
var cameraPosition = camera.getPosition();
|
||||
image.setPosition(cameraPosition.x + x, cameraPosition.y + y);
|
||||
image.setPosition(cameraPosition.x() + x, cameraPosition.y() + y);
|
||||
}
|
||||
|
||||
if (mode == ImageLayerMode.FIT_SCREEN) {
|
||||
|
||||
@@ -12,8 +12,8 @@ import com.bartlomiejpluta.base.api.game.window.Window;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
@@ -36,9 +36,9 @@ public class DefaultObjectLayer implements ObjectLayer {
|
||||
|
||||
private final int rows;
|
||||
private final int columns;
|
||||
private final Vector2f stepSize;
|
||||
private final Vector2fc stepSize;
|
||||
|
||||
public DefaultObjectLayer(@NonNull GameMap map, int rows, int columns, @NonNull Vector2f stepSize, List<Entity> entities, PassageAbility[][] passageMap) {
|
||||
public DefaultObjectLayer(@NonNull GameMap map, int rows, int columns, @NonNull Vector2fc stepSize, List<Entity> entities, PassageAbility[][] passageMap) {
|
||||
this.map = map;
|
||||
this.rows = rows;
|
||||
this.columns = columns;
|
||||
@@ -65,7 +65,7 @@ public class DefaultObjectLayer implements ObjectLayer {
|
||||
@Override
|
||||
public void addEntity(Entity entity) {
|
||||
entity.onAdd(this);
|
||||
entity.setStepSize(stepSize.x, stepSize.y);
|
||||
entity.setStepSize(stepSize.x(), stepSize.y());
|
||||
entities.add(entity);
|
||||
}
|
||||
|
||||
@@ -86,13 +86,13 @@ public class DefaultObjectLayer implements ObjectLayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTileReachable(Vector2i tileCoordinates) {
|
||||
public boolean isTileReachable(Vector2ic tileCoordinates) {
|
||||
// Is trying to go beyond the map
|
||||
if (tileCoordinates.x < 0 || tileCoordinates.y < 0 || tileCoordinates.x >= columns || tileCoordinates.y >= rows) {
|
||||
if (tileCoordinates.x() < 0 || tileCoordinates.y() < 0 || tileCoordinates.x() >= columns || tileCoordinates.y() >= rows) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (passageMap[tileCoordinates.y][tileCoordinates.x] != PassageAbility.ALLOW) {
|
||||
if (passageMap[tileCoordinates.y()][tileCoordinates.x()] != PassageAbility.ALLOW) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -159,6 +159,6 @@ public class DefaultObjectLayer implements ObjectLayer {
|
||||
}
|
||||
|
||||
private int compareObjects(Entity a, Entity b) {
|
||||
return Float.compare(a.getPosition().y, b.getPosition().y);
|
||||
return Float.compare(a.getPosition().y(), b.getPosition().y());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.bartlomiejpluta.base.engine.world.tileset.model.TileSet;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -49,7 +50,10 @@ public class DefaultGameMap implements Renderable, Updatable, GameMap {
|
||||
private final float height;
|
||||
|
||||
@Getter
|
||||
private final Vector2f stepSize;
|
||||
private final Vector2fc stepSize;
|
||||
|
||||
@Getter
|
||||
private final Vector2fc size;
|
||||
|
||||
@Getter
|
||||
private final String handler;
|
||||
@@ -59,8 +63,9 @@ public class DefaultGameMap implements Renderable, Updatable, GameMap {
|
||||
this.rows = rows;
|
||||
this.columns = columns;
|
||||
this.stepSize = new Vector2f(tileSet.getTileSet().getSpriteSize());
|
||||
this.width = columns * stepSize.x;
|
||||
this.height = rows * stepSize.y;
|
||||
this.width = columns * stepSize.x();
|
||||
this.height = rows * stepSize.y();
|
||||
this.size = new Vector2f(columns * stepSize.x(), rows * stepSize.y());
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@@ -78,11 +83,6 @@ public class DefaultGameMap implements Renderable, Updatable, GameMap {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2f getSize() {
|
||||
return new Vector2f(columns * stepSize.x, rows * stepSize.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileLayer getTileLayer(int layerIndex) {
|
||||
return (TileLayer) layers.get(layerIndex);
|
||||
|
||||
@@ -2,23 +2,25 @@ package com.bartlomiejpluta.base.engine.world.movement;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movement;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
@Data
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
public class DefaultMovement implements Movement {
|
||||
private final MovableSprite object;
|
||||
private final Direction direction;
|
||||
private final Vector2i from;
|
||||
private final Vector2i to;
|
||||
private boolean performed = false;
|
||||
private final Vector2ic from;
|
||||
private final Vector2ic to;
|
||||
|
||||
DefaultMovement(MovableSprite object, Direction direction) {
|
||||
this.object = object;
|
||||
this.direction = direction;
|
||||
|
||||
this.from = object.getCoordinates();
|
||||
this.to = direction.asVector().add(object.getCoordinates());
|
||||
this.to = direction.vector.add(object.getCoordinates(), new Vector2i());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -30,14 +32,4 @@ public class DefaultMovement implements Movement {
|
||||
public Movement another() {
|
||||
return object.prepareMovement(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2i getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2i getTo() {
|
||||
return to;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
import static java.lang.Math.max;
|
||||
@@ -21,7 +22,6 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
private int moveTime = 0;
|
||||
private Vector2f movementVector;
|
||||
|
||||
@Getter
|
||||
private final Vector2i coordinates = new Vector2i(0, 0);
|
||||
|
||||
@Getter
|
||||
@@ -29,6 +29,10 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
|
||||
protected int framesToCrossOneTile = 1;
|
||||
|
||||
public Vector2ic getCoordinates() {
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
public boolean isMoving() {
|
||||
return movement != null;
|
||||
}
|
||||
@@ -67,9 +71,11 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
return false;
|
||||
}
|
||||
|
||||
var direction = movement.getDirection().vector;
|
||||
|
||||
this.movement = movement;
|
||||
var speed = new Vector2f(coordinateStepSize).div(framesToCrossOneTile);
|
||||
movementVector = new Vector2f(movement.getDirection().x, movement.getDirection().y).mul(speed);
|
||||
movementVector = new Vector2f(speed.x * direction.x(), speed.y * direction.y());
|
||||
moveTime = framesToCrossOneTile;
|
||||
|
||||
return true;
|
||||
@@ -81,8 +87,8 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
setPosition((x + 0.5f) * coordinateStepSize.x, (y + 0.5f) * coordinateStepSize.y);
|
||||
}
|
||||
|
||||
public void setCoordinates(Vector2i coordinates) {
|
||||
setCoordinates(coordinates.x, coordinates.y);
|
||||
public void setCoordinates(Vector2ic coordinates) {
|
||||
setCoordinates(coordinates.x(), coordinates.y());
|
||||
}
|
||||
|
||||
public void setStepSize(float x, float y) {
|
||||
@@ -91,12 +97,12 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
setCoordinates(coordinates);
|
||||
}
|
||||
|
||||
public int chebyshevDistance(Vector2i coordinates) {
|
||||
return max(abs(this.coordinates.x - coordinates.x), abs(this.coordinates.y - coordinates.y));
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return max(abs(this.coordinates.x - coordinates.x()), abs(this.coordinates.y - coordinates.y()));
|
||||
}
|
||||
|
||||
public int manhattanDistance(Vector2i coordinates) {
|
||||
return abs(this.coordinates.x - coordinates.x) + abs(this.coordinates.y - coordinates.y);
|
||||
public int manhattanDistance(Vector2ic coordinates) {
|
||||
return abs(this.coordinates.x - coordinates.x()) + abs(this.coordinates.y - coordinates.y());
|
||||
}
|
||||
|
||||
public MovableSprite(Mesh mesh, Material material) {
|
||||
|
||||
@@ -5,7 +5,9 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
import static java.lang.Math.toRadians;
|
||||
|
||||
@@ -13,7 +15,6 @@ import static java.lang.Math.toRadians;
|
||||
public abstract class Model implements Placeable {
|
||||
private final Matrix4f modelMatrix = new Matrix4f();
|
||||
|
||||
@Getter
|
||||
protected final Vector2f position = new Vector2f(0, 0);
|
||||
|
||||
@Getter
|
||||
@@ -28,6 +29,11 @@ public abstract class Model implements Placeable {
|
||||
@Setter
|
||||
protected float scaleY = 1.0f;
|
||||
|
||||
@Override
|
||||
public Vector2fc getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
position.x = x;
|
||||
@@ -35,9 +41,9 @@ public abstract class Model implements Placeable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2f position) {
|
||||
this.position.x = position.x;
|
||||
this.position.y = position.y;
|
||||
public void setPosition(Vector2fc position) {
|
||||
this.position.x = position.x();
|
||||
this.position.y = position.y();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -47,9 +53,9 @@ public abstract class Model implements Placeable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePosition(Vector2f position) {
|
||||
this.position.x += position.x;
|
||||
this.position.y += position.y;
|
||||
public void movePosition(Vector2fc position) {
|
||||
this.position.x += position.x();
|
||||
this.position.y += position.y();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,7 +81,7 @@ public abstract class Model implements Placeable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matrix4f getModelMatrix() {
|
||||
public Matrix4fc getModelMatrix() {
|
||||
return modelMatrix
|
||||
.identity()
|
||||
.translate(position.x, position.y, 0)
|
||||
|
||||
@@ -44,7 +44,7 @@ public class DefaultTileSetManager implements TileSetManager {
|
||||
var source = configuration.projectFile("tilesets", asset.getSource());
|
||||
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||
var size = texture.getSpriteSize();
|
||||
var mesh = meshManager.createQuad(size.x, size.y, 0, 0);
|
||||
var mesh = meshManager.createQuad(size.x(), size.y(), 0, 0);
|
||||
tileset = new TileSet(texture, mesh);
|
||||
log.info("Loading tile set from assets to cache under the key: [{}]", uid);
|
||||
tileSets.put(uid, tileset);
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Tile extends Sprite {
|
||||
|
||||
public Tile setCoordinates(int row, int column) {
|
||||
var stepSize = material.getTexture().getSpriteSize();
|
||||
setPosition(column * stepSize.x, row * stepSize.y);
|
||||
setPosition(column * stepSize.x(), row * stepSize.y());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user