general parenting

This commit is contained in:
nora 2020-12-25 12:14:18 +01:00
parent a332f3488c
commit e82b5fe02f
8 changed files with 121 additions and 26 deletions

View file

@ -0,0 +1,36 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
<option name="TOP_LEVEL_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="INNER_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="METHOD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
</value>
</option>
<option name="FIELD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="IGNORE_DEPRECATED" value="false" />
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
<option name="myAdditionalJavadocTags" value="Code" />
</inspection_tool>
</profile>
</component>

View file

@ -1,6 +1,8 @@
package core.math; package core.math;
import core.general.Master; import core.general.Master;
import core.objects.core.MapAnchor;
import core.objects.core.GameObject;
/** /**
* This class provides everything about the local coordinate system the game uses * This class provides everything about the local coordinate system the game uses
@ -9,6 +11,8 @@ import core.general.Master;
*/ */
public class Coordinates { public class Coordinates {
public static final GameObject MAP_ANCHOR = new MapAnchor();
/** /**
* This utility class should not be instantiated * This utility class should not be instantiated
*/ */

View file

@ -0,0 +1,21 @@
package core.objects.base;
import core.math.Vector2D;
import core.objects.core.GameObject;
import java.awt.*;
public class Empty extends GameObject {
public Empty(Vector2D position) {
super(position, new Vector2D(0, 0));
}
@Override
public void draw(Graphics2D g2d) {
}
@Override
public void update() {
}
}

View file

@ -8,7 +8,7 @@ import core.math.Vector2D;
import java.awt.*; import java.awt.*;
/** /**
* The GameObject class is the superclass of every GameObject that can be displayed on screen. It has the 2 * The GameObject class is the superclass of every {@code GameObject} that can be displayed on screen. It has the 2
* {@link #update()} and {@link #draw(Graphics2D)} methods that have to be overridden * {@link #update()} and {@link #draw(Graphics2D)} methods that have to be overridden
*/ */
public abstract class GameObject implements Drawable { public abstract class GameObject implements Drawable {
@ -39,6 +39,7 @@ public abstract class GameObject implements Drawable {
this.mainColor = Color.BLACK; this.mainColor = Color.BLACK;
this.master = Master.getMaster(); this.master = Master.getMaster();
this.layer = 0; this.layer = 0;
this.parent = Coordinates.MAP_ANCHOR;
} }
/** /**
@ -107,13 +108,14 @@ public abstract class GameObject implements Drawable {
* This method draws an oval at the current position and size with arguments * This method draws an oval at the current position and size with arguments
* *
* @param g2d The Graphics2D object provided by the master * @param g2d The Graphics2D object provided by the master
* @param arg Arguments like "center" for the object being drawn in the center
*/ */
public void drawOval(Graphics2D g2d, String arg) { public void drawOval(Graphics2D g2d, String arg) {
Vector2D abs; Vector2D abs;
if(arg.contains("center")){ if(arg.contains("center")){
abs = Coordinates.getWorldCoordinates(new Vector2D(position.x - size.x / 2, position.y - size.y / 2)); abs = Coordinates.getWorldCoordinates(getCenterPosition());
} else { } else {
abs = Coordinates.getWorldCoordinates(position); abs = Coordinates.getWorldCoordinates(position);
} }
@ -147,53 +149,55 @@ public abstract class GameObject implements Drawable {
g2d.rotate(-rotation, xCenterAbs, yCenterAbs); g2d.rotate(-rotation, xCenterAbs, yCenterAbs);
} }
/**
* Destroy this {@code GameObject}
*/
public void destroy() { public void destroy() {
master.destroy(this); master.destroy(this);
} }
/** /**
* Returns the value as map coords (only called on a parent) * Returns the value as map coords
* @param value * @param value The value relative to the parent
* @return * @return The value in global map coordinates
*/ */
public Vector2D getMapCoords(Vector2D value) { public Vector2D getMapCoords(Vector2D value) {
double x = position.x + value.x; double x = parent.position.x + value.x;
double y = position.y + value.y; double y = parent.position.y + value.y;
return new Vector2D(x, y); return parent.getMapCoords(new Vector2D(x, y));
} }
/** /**
* Returns the value as world coordinates (only called on a parent) * Returns the value as world coordinates (only called on a parent)
* @param value * @param value The value relative to the parent
* @return * @return The absolute world coordinates
*/ */
public Vector2D getWorldCoordsFromLocal(Vector2D value) { public Vector2D getWorldCoordsFromLocal(Vector2D value) {
return Coordinates.getWorldCoordinates(getMapCoords(value)); return Coordinates.getWorldCoordinates(getMapCoords(value));
} }
/** /**
* Get world coords of a value (called on itself) * Get the center position of the object
* @param value * @return The center position
* @return
*/ */
public Vector2D getWorldCoords(Vector2D value){ public Vector2D getCenterPosition(){
return parent.getWorldCoordsFromLocal(value);
}
public Vector2D getCenterPosition(Vector2D position){
return new Vector2D(position.x + size.x / 2, position.y + size.y / 2); return new Vector2D(position.x + size.x / 2, position.y + size.y / 2);
} }
public Vector2D getCenterPosition() { /**
return getCenterPosition(position); * Get the render layer of the object
} * @return The render layer
*/
public int getLayer() { public int getLayer() {
return layer; return layer;
} }
/**
* Get the rotation of the object as a Vector2D
* @return The rotation
*/
protected Vector2D getV2DRotation(){ protected Vector2D getV2DRotation(){
return Vector2D.getUnitVector(rotation); return Vector2D.getUnitVector(rotation);
} }

View file

@ -0,0 +1,30 @@
package core.objects.core;
import core.math.Vector2D;
import java.awt.*;
/**
* The {@code mapAnchor} objects is the default parent of every new {@code GameObject}.
*/
public class MapAnchor extends GameObject {
public MapAnchor() {
super(new Vector2D(0, 0), new Vector2D(0, 0));
}
@Override
public void draw(Graphics2D g2d) {
}
@Override
public void update() {
}
@Override
public Vector2D getMapCoords(Vector2D value) {
return value.copy();
}
}

View file

@ -40,7 +40,7 @@ public class Turret extends GameObject {
@Override @Override
public void draw(Graphics2D g2d) { public void draw(Graphics2D g2d) {
g2d.setPaint(mainColor); g2d.setPaint(mainColor);
Vector2D abs = parent.getWorldCoordsFromLocal(position); Vector2D abs = getWorldCoordsFromLocal(position);
int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x; int sizeAbs = (int) Coordinates.getWorldCoordinates(size).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2); int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2); int yCenterAbs = (int) (abs.y + sizeAbs / 2);
@ -77,7 +77,7 @@ public class Turret extends GameObject {
Point msLoc = master.getMouseLocation(); Point msLoc = master.getMouseLocation();
Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct Vector2D mouseRel = Coordinates.getMapCoordinatesFromWorld(Vector2D.fromPoint(msLoc)); //100 correct
Vector2D centerMap = parent.getMapCoords(getCenterPosition()); Vector2D centerMap = getMapCoords(getCenterPosition());
double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y); double targetRotation = -Math.atan2(centerMap.x - mouseRel.x, centerMap.y - mouseRel.y);
rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED); rotation = ExMath.angleLerp(rotation, targetRotation, ROTATION_SPEED);
@ -88,7 +88,7 @@ public class Turret extends GameObject {
int barrelX = (int) (position.x + (i + 1) * barrelSpacing); int barrelX = (int) (position.x + (i + 1) * barrelSpacing);
int frontPosY = (int) (position.y - size.x / 2); int frontPosY = (int) (position.y - size.x / 2);
Vector2D spawnPosNR = parent.getMapCoords(new Vector2D(barrelX, frontPosY)); Vector2D spawnPosNR = getMapCoords(new Vector2D(barrelX, frontPosY));
if (Input.isMousePressed()) { if (Input.isMousePressed()) {
lastShot = System.currentTimeMillis(); lastShot = System.currentTimeMillis();