created physics system and new Drawable interface

This commit is contained in:
nora 2020-12-12 14:34:15 +01:00
parent 6bc1f74016
commit 87ca37c9ca
19 changed files with 390 additions and 43 deletions

View file

@ -1,7 +1,9 @@
package objects;
import core.Drawable;
import core.Master;
import core.math.Vector2D;
import core.physics.Collidable;
import java.awt.*;
@ -9,7 +11,7 @@ import java.awt.*;
* The GameObject class is the superclass of every GameObject that can be displayed on screen. It has the 2
* {@link #update(Master)} and {@link #draw(Graphics2D, int, Master)} methods that have to be
*/
public abstract class GameObject {
public abstract class GameObject implements Drawable {
protected int w;
protected int h;
@ -32,41 +34,40 @@ public abstract class GameObject {
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){
public void moveTo(Vector2D target, Master master) {
Vector2D oldPos = position;
this.position = target;
if (this instanceof Collidable) {
if (master.doesCollide((Collidable) this)) {
this.position = oldPos;
}
}
}
/**
* 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
* @param w The width of the screen
*/
public void drawRect(Graphics2D g2d, int w){
public void drawRect(Graphics2D g2d, int w) {
this.w = w;
h = (int) (this.w / Master.SCREEN_RATIO);
int xAbs = (int) getWorldCoords(position.x, true);
@ -80,12 +81,13 @@ public abstract class GameObject {
/**
* 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 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){
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);
@ -97,16 +99,16 @@ public abstract class GameObject {
g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, arcW, arcH);
}
public double getWorldCoords(double value, boolean isX){
if (isX){
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){
public double getWorldCoordsSize(double value, boolean isX) {
if (isX) {
return (value / Master.SCREEN_Y_COORDINATES * w);
} else {
return (value / Master.SCREEN_Y_COORDINATES * h);
@ -114,27 +116,31 @@ public abstract class GameObject {
}
public double getMapCoords(double value, boolean isX){
if (isX){
public double getMapCoords(double value, boolean isX) {
if (isX) {
return (position.x + value / 100d * size.x);
} else {
return (position.y + value / 100d * size.y);
}
}
public double getMapCoordsSize(double value, boolean useX){
if (useX){
public double getMapCoordsSize(double value, boolean useX) {
if (useX) {
return (value / 100d * size.x);
} else {
return (value / 100d * size.y);
}
}
public int getWorldCoordsFromLocal(double value, boolean isX){
public int getWorldCoordsFromLocal(double value, boolean isX) {
return (int) getWorldCoords(getMapCoords(value, isX), isX);
}
public int getWorldCoordsFromLocalSize(double value, boolean useX){
public int getWorldCoordsFromLocalSize(double value, boolean useX) {
return (int) getWorldCoordsSize(getMapCoordsSize(value, useX), useX);
}
public Vector2D getCenterPosition(){
return new Vector2D(position.x + size.x, position.y + size.y);
}
}

View file

@ -2,14 +2,20 @@ package objects.ships;
import core.Master;
import core.math.Vector2D;
import core.physics.Collidable;
import core.physics.Hitbox;
import core.physics.RectHitBox;
import objects.GameObject;
import java.awt.*;
public class Submarine extends GameObject {
public class Submarine extends GameObject implements Collidable {
private RectHitBox hitbox;
public Submarine(Vector2D position, Vector2D size) {
super(position, size);
this.hitbox = new RectHitBox(position, size);
}
@Override
@ -21,6 +27,26 @@ public class Submarine extends GameObject {
@Override
public void update(Master master) {
Point mouse = master.getMouseLocation();
moveTo(new Vector2D(mouse.x, mouse.y));
moveTo(new Vector2D(mouse.x, mouse.y), master);
}
@Override
public boolean collidesWith(Collidable o) {
return hitbox.collidesWith(o.getHitbox());
}
@Override
public Hitbox getHitbox() {
return hitbox;
}
@Override
public Vector2D getCenterPos() {
return getCenterPosition();
}
@Override
public Vector2D getSize() {
return size;
}
}

View file

@ -1,14 +1,21 @@
package objects.world;
import core.Master;
import core.math.Vector2D;
import core.physics.Collidable;
import core.physics.Hitbox;
import core.physics.RectHitBox;
import objects.GameObject;
import java.awt.*;
public class Wall extends GameObject {
public class Wall extends GameObject implements Collidable {
private RectHitBox hitbox;
public Wall(double x, double y, double xSize, double ySize) {
super(x, y, xSize, ySize);
this.hitbox = new RectHitBox(new Vector2D(x, y), new Vector2D(xSize, ySize));
}
@Override
@ -19,4 +26,24 @@ public class Wall extends GameObject {
@Override
public void update(Master master) {
}
@Override
public boolean collidesWith(Collidable o) {
return this.hitbox.collidesWith(o.getHitbox());
}
@Override
public Hitbox getHitbox() {
return hitbox;
}
@Override
public Vector2D getCenterPos() {
return position;
}
@Override
public Vector2D getSize() {
return size;
}
}