diff --git a/src/main/java/core/general/Main.java b/src/main/java/core/general/Main.java index 7ee4cf3..bce09bc 100644 --- a/src/main/java/core/general/Main.java +++ b/src/main/java/core/general/Main.java @@ -1,6 +1,6 @@ package core.general; -import objects.Init; +import custom.Init; import javax.swing.*; import java.awt.*; diff --git a/src/main/java/core/general/Master.java b/src/main/java/core/general/Master.java index fe63874..6855e7d 100644 --- a/src/main/java/core/general/Master.java +++ b/src/main/java/core/general/Master.java @@ -8,7 +8,7 @@ import core.physics.Collision; import core.objects.base.DebugPos; import core.objects.core.GameObject; import core.rendering.Drawable; -import objects.Init; +import custom.Init; import javax.swing.*; import java.awt.*; diff --git a/src/main/java/core/math/Vector2D.java b/src/main/java/core/math/Vector2D.java index 2aa5844..a0f05b9 100644 --- a/src/main/java/core/math/Vector2D.java +++ b/src/main/java/core/math/Vector2D.java @@ -43,6 +43,7 @@ public class Vector2D { return new Vector2D(point.x, point.y); } + /** * Add another Vector to this vector, modifies this object * @@ -141,6 +142,16 @@ public class Vector2D { return new Vector2D(a.x / b, a.y / b); } + /** + * Divide a Vector by a Vector + * @param a Vector a + * @param b Vector b + * @return The result + */ + public static Vector2D divide(Vector2D a, Vector2D b) { + return new Vector2D(a.x / b.x, a.y / b.y); + } + /** * Rotate a point around another point *

diff --git a/src/main/java/core/objects/base/Camera.java b/src/main/java/core/objects/base/Camera.java new file mode 100644 index 0000000..a363292 --- /dev/null +++ b/src/main/java/core/objects/base/Camera.java @@ -0,0 +1,23 @@ +package core.objects.base; + +import core.general.Input; +import core.math.Vector2D; +import core.objects.core.GameObject; +import core.rendering.renderer.EmptyRenderer; + +import java.awt.event.KeyEvent; + +/** + * The base Camera that shifts all objects + */ +public class Camera extends GameObject { + public Camera(Vector2D position, Vector2D size) { + super(position, size); + setRenderer(new EmptyRenderer(this)); + this.doesDespawn = false; + } + + @Override + protected void update() { + } +} diff --git a/src/main/java/core/objects/base/Empty.java b/src/main/java/core/objects/base/Empty.java index 5ade288..27f8c10 100644 --- a/src/main/java/core/objects/base/Empty.java +++ b/src/main/java/core/objects/base/Empty.java @@ -2,11 +2,13 @@ package core.objects.base; import core.math.Vector2D; import core.objects.core.GameObject; +import core.rendering.renderer.EmptyRenderer; public class Empty extends GameObject { public Empty(Vector2D position) { super(position, new Vector2D(0, 0)); + setRenderer(new EmptyRenderer(this)); } @Override diff --git a/src/main/java/core/physics/hitboxes/RectHitBox.java b/src/main/java/core/physics/hitboxes/RectHitBox.java index 6edcb9d..cde91b6 100644 --- a/src/main/java/core/physics/hitboxes/RectHitBox.java +++ b/src/main/java/core/physics/hitboxes/RectHitBox.java @@ -1,9 +1,8 @@ package core.physics.hitboxes; import core.math.Coordinates; -import core.general.Master; import core.math.Vector2D; -import objects.Init; +import custom.Init; import java.awt.*; diff --git a/src/main/java/core/rendering/RenderEngine.java b/src/main/java/core/rendering/RenderEngine.java index 4d6115e..1e41ee2 100644 --- a/src/main/java/core/rendering/RenderEngine.java +++ b/src/main/java/core/rendering/RenderEngine.java @@ -3,6 +3,7 @@ package core.rendering; import core.general.Master; import core.math.Coordinates; import core.math.Vector2D; +import core.objects.base.Camera; import javax.swing.*; import java.awt.*; @@ -16,6 +17,8 @@ public class RenderEngine extends JPanel { private final Master master = Master.getMaster(); + private Camera currentCamera; + /** * The current width and height of the game area */ @@ -110,11 +113,32 @@ public class RenderEngine extends JPanel { layerManager.flush(); } + public Camera getCurrentCamera() { + return currentCamera; + } + + public void setCurrentCamera(Camera currentCamera) { + this.currentCamera = currentCamera; + } + + //-------------------------------------- + + public Vector2D shiftPoint(Vector2D point){ + Vector2D newPosShifted = Vector2D.subtract(point, currentCamera.getPosition()); + return Vector2D.divide(newPosShifted, currentCamera.getSize()); + } + + public Vector2D scaleSize(Vector2D size){ + return Vector2D.divide(size, currentCamera.getSize()); + } + + + //Drawing methods--------------------------------------------------------------------------------------------------------------- public void fillRect(Vector2D position, Vector2D size, Color color, double rotation){ - Vector2D abs = Coordinates.getWorldCoordinates(position); - Vector2D sizeAbs = Coordinates.getWorldCoordinates(size); + Vector2D abs = Coordinates.getWorldCoordinates(shiftPoint(position)); + Vector2D sizeAbs = Coordinates.getWorldCoordinates(scaleSize(size)); Vector2D centerAbs = new Vector2D((abs.x + sizeAbs.x / 2), (abs.y + sizeAbs.y / 2)); g2d.setPaint(color); @@ -126,8 +150,8 @@ public class RenderEngine extends JPanel { } public void fillRoundRect(Vector2D position, Vector2D size, Vector2D arcFactors, Color color, double rotation){ - Vector2D abs = Coordinates.getWorldCoordinates(position); - Vector2D sizeAbs = Coordinates.getWorldCoordinates(size); + Vector2D abs = Coordinates.getWorldCoordinates(shiftPoint(position)); + Vector2D sizeAbs = Coordinates.getWorldCoordinates(scaleSize(size)); Vector2D centerAbs = new Vector2D((abs.x + sizeAbs.x / 2), (abs.y + sizeAbs.y / 2)); g2d.setPaint(color); @@ -140,8 +164,8 @@ public class RenderEngine extends JPanel { } public void fillOval(Vector2D position, Vector2D size, Color color, double rotation){ - Vector2D abs = Coordinates.getWorldCoordinates(position); - Vector2D sizeAbs = Coordinates.getWorldCoordinates(size); + Vector2D abs = Coordinates.getWorldCoordinates(shiftPoint(position)); + Vector2D sizeAbs = Coordinates.getWorldCoordinates(scaleSize(size)); Vector2D centerAbs = new Vector2D((abs.x + sizeAbs.x / 2), (abs.y + sizeAbs.y / 2)); g2d.setPaint(color); diff --git a/src/main/java/core/rendering/renderer/EmptyRenderer.java b/src/main/java/core/rendering/renderer/EmptyRenderer.java new file mode 100644 index 0000000..b9fd586 --- /dev/null +++ b/src/main/java/core/rendering/renderer/EmptyRenderer.java @@ -0,0 +1,24 @@ +package core.rendering.renderer; + +import core.objects.core.GameObject; + +import java.awt.*; + +/** + * A filler {@code Renderer} that does nothing + */ +public class EmptyRenderer extends Renderer{ + + /** + * The superconstructor for every {@code Renderer} that sets the color and the {@code GameObject} the renderer is assigned to + * + * @param object The {@code GameObject} the {@code Renderer} os assigned to + */ + public EmptyRenderer(GameObject object) { + super(null, object); + } + + @Override + public void draw(Graphics2D g2d) { + } +} diff --git a/src/main/java/objects/Init.java b/src/main/java/custom/Init.java similarity index 77% rename from src/main/java/objects/Init.java rename to src/main/java/custom/Init.java index 5d47c88..8009668 100644 --- a/src/main/java/objects/Init.java +++ b/src/main/java/custom/Init.java @@ -1,13 +1,15 @@ -package objects; +package custom; import core.general.Master; import core.math.Vector2D; +import core.objects.base.Camera; import core.objects.core.GameObject; -import objects.ships.BattleShip; -import objects.ships.Submarine; -import objects.ships.Turret; -import objects.world.Grid; -import objects.world.Wall; +import custom.objects.MoveCamera; +import custom.objects.ships.BattleShip; +import custom.objects.ships.Submarine; +import custom.objects.ships.Turret; +import custom.objects.world.Grid; +import custom.objects.world.Wall; import java.awt.*; @@ -33,6 +35,8 @@ public class Init { //INIT GOES HERE + Master.getMaster().getRenderEngine().setCurrentCamera(create(new MoveCamera(Vector2D.zero(), new Vector2D(1, 1)))); + create(new Grid()); BattleShip battleShip = create(new BattleShip()); diff --git a/src/main/java/custom/objects/MoveCamera.java b/src/main/java/custom/objects/MoveCamera.java new file mode 100644 index 0000000..3ba1b4a --- /dev/null +++ b/src/main/java/custom/objects/MoveCamera.java @@ -0,0 +1,34 @@ +package custom.objects; + +import core.general.Input; +import core.math.Vector2D; +import core.objects.base.Camera; + +import java.awt.event.KeyEvent; + +public class MoveCamera extends Camera { + public MoveCamera(Vector2D position, Vector2D size) { + super(position, size); + } + + @Override + protected void update() { + if(Input.keyPressed(KeyEvent.VK_RIGHT)){ + position.x += 1; + } else if(Input.keyPressed(KeyEvent.VK_LEFT)){ + position.x -= 1; + } + if(Input.keyPressed(KeyEvent.VK_UP)){ + position.y -= 1; + } else if(Input.keyPressed(KeyEvent.VK_DOWN)){ + position.y += 1; + } + + if(Input.keyPressed(KeyEvent.VK_PAGE_UP)){ + size.multiply(0.9); + } + if(Input.keyPressed(KeyEvent.VK_PAGE_DOWN)){ + size.multiply(1.1); + } + } +} diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/custom/objects/ships/BattleShip.java similarity index 95% rename from src/main/java/objects/ships/BattleShip.java rename to src/main/java/custom/objects/ships/BattleShip.java index 3969b41..36b5277 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/custom/objects/ships/BattleShip.java @@ -1,9 +1,8 @@ -package objects.ships; +package custom.objects.ships; import core.general.Input; import core.math.Vector2D; import core.objects.core.GameObject; -import core.rendering.renderer.RectRenderer; import core.rendering.renderer.RoundRectRenderer; import java.awt.*; diff --git a/src/main/java/objects/ships/Shell.java b/src/main/java/custom/objects/ships/Shell.java similarity index 96% rename from src/main/java/objects/ships/Shell.java rename to src/main/java/custom/objects/ships/Shell.java index 73879f0..7891cef 100644 --- a/src/main/java/objects/ships/Shell.java +++ b/src/main/java/custom/objects/ships/Shell.java @@ -1,4 +1,4 @@ -package objects.ships; +package custom.objects.ships; import core.math.Vector2D; import core.objects.core.CollGameObject; diff --git a/src/main/java/objects/ships/Submarine.java b/src/main/java/custom/objects/ships/Submarine.java similarity index 96% rename from src/main/java/objects/ships/Submarine.java rename to src/main/java/custom/objects/ships/Submarine.java index b1d9cf1..52b7378 100644 --- a/src/main/java/objects/ships/Submarine.java +++ b/src/main/java/custom/objects/ships/Submarine.java @@ -1,4 +1,4 @@ -package objects.ships; +package custom.objects.ships; import core.math.Coordinates; import core.math.Vector2D; diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/custom/objects/ships/Turret.java similarity index 97% rename from src/main/java/objects/ships/Turret.java rename to src/main/java/custom/objects/ships/Turret.java index 668f47d..e054172 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/custom/objects/ships/Turret.java @@ -1,4 +1,4 @@ -package objects.ships; +package custom.objects.ships; import core.general.Input; import core.math.Coordinates; @@ -19,7 +19,7 @@ public class Turret extends GameObject { private static final int SHELL_SPEED = 1; public static final double SHELL_SIZE = 2; private static final double BARREL_THICKNESS = 1.7; - private int barrelAmount = 3; + private final int barrelAmount; private final Color mainColor; @@ -78,7 +78,7 @@ public class Turret extends GameObject { @Override public void update() { - //TODO fix with everything haha + //TODO fix everything haha Point msLoc = master.getMouseLocation(); Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct diff --git a/src/main/java/objects/world/Grid.java b/src/main/java/custom/objects/world/Grid.java similarity index 97% rename from src/main/java/objects/world/Grid.java rename to src/main/java/custom/objects/world/Grid.java index 80c63f8..b543fe0 100644 --- a/src/main/java/objects/world/Grid.java +++ b/src/main/java/custom/objects/world/Grid.java @@ -1,4 +1,4 @@ -package objects.world; +package custom.objects.world; import core.math.Vector2D; import core.objects.core.GameObject; diff --git a/src/main/java/objects/world/Wall.java b/src/main/java/custom/objects/world/Wall.java similarity index 96% rename from src/main/java/objects/world/Wall.java rename to src/main/java/custom/objects/world/Wall.java index 0b717d7..a4d559a 100644 --- a/src/main/java/objects/world/Wall.java +++ b/src/main/java/custom/objects/world/Wall.java @@ -1,4 +1,4 @@ -package objects.world; +package custom.objects.world; import core.math.Vector2D; import core.physics.hitboxes.Hitbox; diff --git a/target/classes/objects/ships/BattleShip.class b/target/classes/objects/ships/BattleShip.class deleted file mode 100644 index 9dd04a0..0000000 Binary files a/target/classes/objects/ships/BattleShip.class and /dev/null differ diff --git a/target/classes/objects/ships/Shell.class b/target/classes/objects/ships/Shell.class deleted file mode 100644 index 3e994ca..0000000 Binary files a/target/classes/objects/ships/Shell.class and /dev/null differ diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class deleted file mode 100644 index a87d7be..0000000 Binary files a/target/classes/objects/ships/Turret.class and /dev/null differ diff --git a/target/classes/objects/world/Grid.class b/target/classes/objects/world/Grid.class deleted file mode 100644 index 1c7718f..0000000 Binary files a/target/classes/objects/world/Grid.class and /dev/null differ