mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 22:15:01 +01:00
idk
This commit is contained in:
parent
02aa5538f5
commit
81855c6a6d
11 changed files with 33 additions and 30 deletions
|
|
@ -7,12 +7,12 @@ import core.general.Master;
|
|||
* <p>In this system, the screen is always 100 high and 100 * the screen ration wide.
|
||||
* This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing</p>
|
||||
*/
|
||||
public class Coords {
|
||||
public class Coordinates {
|
||||
|
||||
/**
|
||||
* This utility class should not be instantiated
|
||||
*/
|
||||
private Coords() {
|
||||
private Coordinates() {
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -27,7 +27,7 @@ public class Coords {
|
|||
* @param value A point in map coordinates
|
||||
* @return The point in world coordinates
|
||||
*/
|
||||
public static Vector2D getWorldCoords(Vector2D value) {
|
||||
public static Vector2D getWorldCoordinates(Vector2D value) {
|
||||
double x = (value.x / Master.SCREEN_Y_COORDINATES / Master.SCREEN_RATIO * master.getW());
|
||||
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
|
||||
return new Vector2D(x, y);
|
||||
|
|
@ -39,7 +39,7 @@ public class Coords {
|
|||
* @param value The Vector2D position in Java 2D coordinates
|
||||
* @return The Vector2D position in map coordinates
|
||||
*/
|
||||
public static Vector2D getMapCoordsFromWorld(Vector2D value) {
|
||||
public static Vector2D getMapCoordinatesFromWorld(Vector2D value) {
|
||||
double x = (value.x / master.getW()) * Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO;
|
||||
double y = (value.y / master.getH()) * Master.SCREEN_Y_COORDINATES;
|
||||
return new Vector2D(x, y);
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package core.objects.base;
|
||||
|
||||
import core.math.Coords;
|
||||
import core.math.Vector2D;
|
||||
import core.objects.core.GameObject;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package core.objects.core;
|
||||
|
||||
import core.math.Coords;
|
||||
import core.math.Coordinates;
|
||||
import core.general.Drawable;
|
||||
import core.general.Master;
|
||||
import core.math.Vector2D;
|
||||
|
|
@ -43,7 +43,7 @@ public abstract class GameObject implements Drawable {
|
|||
* Gets called at the start of the update method
|
||||
*/
|
||||
public void startUpdate(){
|
||||
if(doesDespawn && Coords.outOfBounds(position, size)){
|
||||
if(doesDespawn && Coordinates.outOfBounds(position, size)){
|
||||
destroy();
|
||||
}
|
||||
update();
|
||||
|
|
@ -72,8 +72,8 @@ public abstract class GameObject implements Drawable {
|
|||
* @param g2d The Graphics2D object provided by the master
|
||||
*/
|
||||
public void drawRect(Graphics2D g2d) {
|
||||
Vector2D abs = Coords.getWorldCoords(position);
|
||||
Vector2D sizeAbs = Coords.getWorldCoords(size);
|
||||
Vector2D abs = Coordinates.getWorldCoordinates(position);
|
||||
Vector2D sizeAbs = Coordinates.getWorldCoordinates(size);
|
||||
|
||||
g2d.setPaint(mainColor);
|
||||
g2d.fillRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
|
||||
|
|
@ -85,8 +85,8 @@ public abstract class GameObject implements Drawable {
|
|||
* @param g2d The Graphics2D object provided by the master
|
||||
*/
|
||||
public void fillOval(Graphics2D g2d) {
|
||||
Vector2D abs = Coords.getWorldCoords(position);
|
||||
Vector2D sizeAbs = Coords.getWorldCoords(size);
|
||||
Vector2D abs = Coordinates.getWorldCoordinates(position);
|
||||
Vector2D sizeAbs = Coordinates.getWorldCoordinates(size);
|
||||
|
||||
g2d.setPaint(mainColor);
|
||||
g2d.fillOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
|
||||
|
|
@ -111,12 +111,12 @@ public abstract class GameObject implements Drawable {
|
|||
Vector2D abs;
|
||||
|
||||
if(arg.contains("center")){
|
||||
abs = Coords.getWorldCoords(new Vector2D(position.x - size.x / 2, position.y - size.y / 2));
|
||||
abs = Coordinates.getWorldCoordinates(new Vector2D(position.x - size.x / 2, position.y - size.y / 2));
|
||||
} else {
|
||||
abs = Coords.getWorldCoords(position);
|
||||
abs = Coordinates.getWorldCoordinates(position);
|
||||
}
|
||||
|
||||
Vector2D sizeAbs = Coords.getWorldCoords(size);
|
||||
Vector2D sizeAbs = Coordinates.getWorldCoordinates(size);
|
||||
|
||||
g2d.setPaint(mainColor);
|
||||
g2d.drawOval((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
|
||||
|
|
@ -131,8 +131,8 @@ public abstract class GameObject implements Drawable {
|
|||
*/
|
||||
public void drawRoundRect(Graphics2D g2d, int arcW, int arcH) {
|
||||
|
||||
Vector2D abs = Coords.getWorldCoords(position);
|
||||
Vector2D sizeAbs = Coords.getWorldCoords(size);
|
||||
Vector2D abs = Coordinates.getWorldCoordinates(position);
|
||||
Vector2D sizeAbs = Coordinates.getWorldCoordinates(size);
|
||||
|
||||
int xCenterAbs = (int) (abs.x + sizeAbs.x / 2);
|
||||
int yCenterAbs = (int) (abs.y + sizeAbs.y / 2);
|
||||
|
|
@ -158,7 +158,7 @@ public abstract class GameObject implements Drawable {
|
|||
}
|
||||
|
||||
public Vector2D getWorldCoordsFromLocal(Vector2D value) {
|
||||
return Coords.getWorldCoords(getMapCoords(value));
|
||||
return Coordinates.getWorldCoordinates(getMapCoords(value));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package core.physics.hitboxes;
|
||||
|
||||
import core.math.Coords;
|
||||
import core.math.Coordinates;
|
||||
import core.general.Master;
|
||||
import core.math.Vector2D;
|
||||
import objects.Init;
|
||||
|
|
@ -10,6 +10,7 @@ import java.awt.*;
|
|||
/**
|
||||
* A rectangular hitbox
|
||||
*/
|
||||
//TODO hitbox position not calculated correctlyA
|
||||
public class RectHitBox extends Hitbox {
|
||||
|
||||
/**
|
||||
|
|
@ -176,8 +177,8 @@ public class RectHitBox extends Hitbox {
|
|||
@Override
|
||||
public void draw(Graphics2D g2d) {
|
||||
|
||||
Vector2D abs = Coords.getWorldCoords(x1);
|
||||
Vector2D sizeAbs = Coords.getWorldCoords(Vector2D.subtract(y2, x1));
|
||||
Vector2D abs = Coordinates.getWorldCoordinates(x1);
|
||||
Vector2D sizeAbs = Coordinates.getWorldCoordinates(Vector2D.subtract(y2, x1));
|
||||
|
||||
g2d.setPaint(Color.MAGENTA);
|
||||
g2d.drawRect((int) abs.x, (int) abs.y, (int) sizeAbs.x, (int) sizeAbs.y);
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import java.awt.*;
|
|||
*/
|
||||
public class Init {
|
||||
|
||||
public static final boolean DEBUG_MODE = false;
|
||||
public static final boolean DEBUG_MODE = true;
|
||||
|
||||
/**
|
||||
* Create a new GameObject
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ package objects.ships;
|
|||
|
||||
import core.general.Input;
|
||||
import core.math.Vector2D;
|
||||
import core.objects.core.CollGameObject;
|
||||
import core.objects.core.GameObject;
|
||||
import core.physics.hitboxes.RectHitBox;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
|
@ -11,7 +13,7 @@ import java.util.ArrayList;
|
|||
/**
|
||||
* A Battleship that can have several turrets
|
||||
*/
|
||||
public class BattleShip extends GameObject {
|
||||
public class BattleShip extends CollGameObject {
|
||||
|
||||
public static final double SPPED = 1;
|
||||
private static final double TURN_RATE = 0.05;
|
||||
|
|
@ -29,7 +31,7 @@ public class BattleShip extends GameObject {
|
|||
}
|
||||
|
||||
public BattleShip(double x, double y, double xSize, double ySize, Color mainColor) {
|
||||
super(x, y, xSize, ySize);
|
||||
super(x, y, xSize, ySize, new RectHitBox(new Vector2D(x, y), new Vector2D(xSize, ySize)));
|
||||
turrets = new ArrayList<>();
|
||||
this.mainColor = mainColor;
|
||||
this.doesDespawn = false;
|
||||
|
|
@ -46,7 +48,8 @@ public class BattleShip extends GameObject {
|
|||
public void update() {
|
||||
|
||||
if (playerControlled) {
|
||||
position.add(getV2DRotation().multiply(Input.getVerticalAxis() * SPPED));
|
||||
|
||||
moveTo(Vector2D.add(position, getV2DRotation().multiply(Input.getVerticalAxis() * SPPED)));
|
||||
|
||||
rotation += Input.getHorizontalAxis() * TURN_RATE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package objects.ships;
|
||||
|
||||
import core.math.Coords;
|
||||
import core.math.Coordinates;
|
||||
import core.math.Vector2D;
|
||||
import core.physics.hitboxes.RectHitBox;
|
||||
import core.objects.core.CollGameObject;
|
||||
|
|
@ -24,7 +24,7 @@ public class Submarine extends CollGameObject {
|
|||
@Override
|
||||
public void update() {
|
||||
Point mouse = master.getMouseLocation();
|
||||
Vector2D relPos = Coords.getMapCoordsFromWorld(new Vector2D(mouse.x, mouse.y));
|
||||
Vector2D relPos = Coordinates.getMapCoordinatesFromWorld(new Vector2D(mouse.x, mouse.y));
|
||||
Vector2D centerRelPos = new Vector2D(relPos.x - size.x/2, relPos.y - size.y/2);
|
||||
moveTo(centerRelPos);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package objects.ships;
|
||||
|
||||
import core.general.Input;
|
||||
import core.math.Coords;
|
||||
import core.math.Coordinates;
|
||||
import core.math.ExMath;
|
||||
import core.math.Vector2D;
|
||||
import core.objects.core.GameObject;
|
||||
|
|
@ -44,13 +44,13 @@ public class Turret extends GameObject {
|
|||
public void draw(Graphics2D g2d) {
|
||||
g2d.setPaint(mainColor);
|
||||
Vector2D abs = battleShip.getWorldCoordsFromLocal(position);
|
||||
int sizeAbs = (int) Coords.getWorldCoords(size).x;
|
||||
int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x;
|
||||
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
|
||||
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
|
||||
|
||||
g2d.fillOval((int) abs.x, (int) abs.y, sizeAbs, sizeAbs);
|
||||
|
||||
g2d.setStroke(new BasicStroke((int) Coords.getWorldCoords(new Vector2D(size.x / barrelAmount / BARREL_THICKNESS, 0)).x, BasicStroke.CAP_BUTT,
|
||||
g2d.setStroke(new BasicStroke((int) Coordinates.getWorldCoordinates(new Vector2D(size.x / barrelAmount / BARREL_THICKNESS, 0)).x, BasicStroke.CAP_BUTT,
|
||||
BasicStroke.JOIN_BEVEL));
|
||||
|
||||
//BARRELS---------------------------------------
|
||||
|
|
@ -79,7 +79,7 @@ public class Turret extends GameObject {
|
|||
public void update() {
|
||||
|
||||
Point msLoc = master.getMouseLocation();
|
||||
Vector2D mouseRel = Coords.getMapCoordsFromWorld(Vector2D.fromPoint(msLoc)); //100 correct
|
||||
Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct
|
||||
Vector2D centerMap = battleShip.getMapCoords(getCenterPosition());
|
||||
double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue