mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 22:15:01 +01:00
coordinates change
This commit is contained in:
parent
843d30eb47
commit
47ec2337a4
9 changed files with 108 additions and 38 deletions
|
|
@ -4,6 +4,8 @@ import core.Master;
|
|||
import core.Vector2D;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* The GameObject class is the superclass of every GameObject that can be displayed on screen. It has the 2
|
||||
|
|
@ -19,6 +21,8 @@ public abstract class GameObject {
|
|||
|
||||
protected Vector2D velocity;
|
||||
|
||||
protected Color mainColor;
|
||||
|
||||
public GameObject(double x, double y, double xSize, double ySize) {
|
||||
this(new Vector2D(x, y), new Vector2D(xSize, ySize));
|
||||
}
|
||||
|
|
@ -27,37 +31,104 @@ public abstract class GameObject {
|
|||
this.position = position;
|
||||
this.size = size;
|
||||
this.velocity = new Vector2D();
|
||||
mainColor = Color.BLACK;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>The draw method is called every frame after the {@link #update(Master)} by the master object on each object. Everything
|
||||
* about drawing should be handled here in this method.</p>
|
||||
* <p>No general calculations should be made in this method. The game should be able to work just
|
||||
* fine even without this method being called.</p>
|
||||
* <p>This function is <i>NOT</i> intended to be called manually.</p>
|
||||
* @param g2d The {@code Graphics2D} object given by the master
|
||||
* @param w The width of the screen
|
||||
* @param master The master object itself
|
||||
*/
|
||||
public abstract void draw(Graphics2D g2d, int w, Master master);
|
||||
|
||||
/**
|
||||
* <p>The update method is called every frame before the {@link #draw(Graphics2D, int, Master)} method by the master object on each object. Everything
|
||||
* that is needed for the game to work should be here in this method.</p>
|
||||
* <p>No drawing should be made in this method. The {@code debug} method can be called on the master.</p>
|
||||
* <p>This function is <i>NOT</i> intended to be called manually.</p>
|
||||
* @param master The master object itself
|
||||
*/
|
||||
public abstract void update(Master master);
|
||||
|
||||
/**
|
||||
* A simple method to move the object to a Vector2D. This method should be called instead of doing it manually.
|
||||
* @param target The target position
|
||||
*/
|
||||
public void moveTo(Vector2D target){
|
||||
this.position = target;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method draws a rectangle at the current position and size
|
||||
* @param g2d The Graphics2D object provided by the master
|
||||
* @param w The width of the screen
|
||||
*/
|
||||
public void drawRect(Graphics2D g2d, int w){
|
||||
this.w = w;
|
||||
h = (int) (this.w / Master.SCREEN_RATIO);
|
||||
int xAbs = (int) getWorldCoords(position.x, true);
|
||||
int yAbs = (int) getWorldCoords(position.y, false);
|
||||
int sizeXAbs = (int) getWorldCoordsSize(size.x, true);
|
||||
int sizeYAbs = (int) getWorldCoordsSize(size.y, false);
|
||||
|
||||
g2d.setPaint(mainColor);
|
||||
g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method draws a rounded rectangle at the current position and size
|
||||
* @param g2d The Graphics2D object provided by the master
|
||||
* @param w The width of the screen
|
||||
* @param arcW The arc width of the rectangle
|
||||
* @param arcH The arc height of the rectangle
|
||||
*/
|
||||
public void drawRoundRect(Graphics2D g2d, int w, int arcW, int arcH){
|
||||
this.w = w;
|
||||
h = (int) (w / Master.SCREEN_RATIO);
|
||||
int xAbs = (int) getWorldCoords(position.x, true);
|
||||
int yAbs = (int) getWorldCoords(position.y, false);
|
||||
int sizeXAbs = (int) getWorldCoordsSize(size.x, true);
|
||||
int sizeYAbs = (int) getWorldCoordsSize(size.y, false);
|
||||
|
||||
g2d.setPaint(mainColor);
|
||||
g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, arcW, arcH);
|
||||
}
|
||||
|
||||
public double getWorldCoords(double value, boolean isX){
|
||||
if (isX){
|
||||
return (value / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * w);
|
||||
} else {
|
||||
return (value / Master.SCREEN_Y_COORDINATES * h);
|
||||
}
|
||||
}
|
||||
|
||||
public double getWorldCoordsSize(double value, boolean isX){
|
||||
if (isX){
|
||||
return (value / Master.SCREEN_Y_COORDINATES * w);
|
||||
} else {
|
||||
return (value / Master.SCREEN_Y_COORDINATES * h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public double getMapCoords(double value, boolean isX){
|
||||
if (isX){
|
||||
return (position.x + value / 100 * size.x);
|
||||
return (position.x + value / 100d * size.x);
|
||||
} else {
|
||||
return (position.y + value / 100 * size.y);
|
||||
return (position.y + value / 100d * size.y);
|
||||
}
|
||||
}
|
||||
|
||||
public double getMapCoordsSize(double value, boolean useX){
|
||||
if (useX){
|
||||
return (value / 100 * size.x);
|
||||
return (value / 100d * size.x);
|
||||
} else {
|
||||
return (value / 100 * size.y);
|
||||
}
|
||||
}
|
||||
|
||||
public double getWorldCoords(double value, boolean isX){
|
||||
if (isX){
|
||||
return (value / 100 * w);
|
||||
} else {
|
||||
return (value / 100 * h);
|
||||
return (value / 100d * size.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -66,6 +137,6 @@ public abstract class GameObject {
|
|||
}
|
||||
|
||||
public int getWorldCoordsFromLocalSize(double value, boolean useX){
|
||||
return (int) getWorldCoords(getMapCoordsSize(value, useX), useX);
|
||||
return (int) getWorldCoordsSize(getMapCoordsSize(value, useX), useX);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue