mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
basic camera
This commit is contained in:
parent
01f76d5c51
commit
72b7c47a3e
20 changed files with 145 additions and 25 deletions
|
|
@ -1,6 +1,6 @@
|
|||
package core.general;
|
||||
|
||||
import objects.Init;
|
||||
import custom.Init;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
|
|
|||
|
|
@ -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.*;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
* <p>
|
||||
|
|
|
|||
23
src/main/java/core/objects/base/Camera.java
Normal file
23
src/main/java/core/objects/base/Camera.java
Normal file
|
|
@ -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() {
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.*;
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
24
src/main/java/core/rendering/renderer/EmptyRenderer.java
Normal file
24
src/main/java/core/rendering/renderer/EmptyRenderer.java
Normal file
|
|
@ -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) {
|
||||
}
|
||||
}
|
||||
|
|
@ -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());
|
||||
34
src/main/java/custom/objects/MoveCamera.java
Normal file
34
src/main/java/custom/objects/MoveCamera.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.*;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package objects.ships;
|
||||
package custom.objects.ships;
|
||||
|
||||
import core.math.Vector2D;
|
||||
import core.objects.core.CollGameObject;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package objects.ships;
|
||||
package custom.objects.ships;
|
||||
|
||||
import core.math.Coordinates;
|
||||
import core.math.Vector2D;
|
||||
|
|
@ -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
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package objects.world;
|
||||
package custom.objects.world;
|
||||
|
||||
import core.math.Vector2D;
|
||||
import core.objects.core.GameObject;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package objects.world;
|
||||
package custom.objects.world;
|
||||
|
||||
import core.math.Vector2D;
|
||||
import core.physics.hitboxes.Hitbox;
|
||||
Loading…
Add table
Add a link
Reference in a new issue