inital commit

This commit is contained in:
nora 2020-12-11 19:49:03 +01:00
parent 5b9846011c
commit 0d5397dbe3
35 changed files with 955 additions and 0 deletions

View file

@ -0,0 +1,123 @@
package core;
import objects.DebugPos;
import objects.ships.BattleShip;
import objects.GameObject;
import objects.world.Grid;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
* The main object that controls everything
*/
public class Master extends JPanel {
/**
* All GameObjects that exist
*/
private final ArrayList<GameObject> objects;
/**
* Stores all GameObjects that were created during a frame
*/
private final ArrayList<GameObject> objectBuffer;
private boolean mousePressed = false;
/**
* Create a new master object
*/
public Master() {
objects = new ArrayList<>();
objectBuffer = new ArrayList<>();
objects.add(new Grid());
BattleShip battleShip = new BattleShip(Color.DARK_GRAY);
BattleShip bs = new BattleShip(70, 10, 5, 80, Color.GREEN);
/*for (int i = 0; i < 10; i++) {
bs.addTurret(new Turret(bs, 25, 10 * i + 1, 50, i % 5));
}*/
objects.add(bs);
objects.add(battleShip);
}
/**
* The mein drawing method, handles everything about drawing
* @param g
*/
private void doDrawing(Graphics g) {
//TODO w/h fields
int w, h;
if (getWidth() * 9 > getHeight() * 16) {
h = getHeight();
w = h / 9 * 16;
} else {
w = getWidth();
h = w / 16 * 9;
}
Graphics2D g2d = (Graphics2D) g.create();
objects.forEach(o -> o.draw(g2d, w, this));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
/**
* Debug a position, creates a green dot at the position
* @param pos
*/
public void debugPos(Vector2D pos){
create(new DebugPos(pos, new Vector2D(10, 10)));
}
/**
* Call this method when the user pressed the left mouse button
*/
public void mousePressed() {
mousePressed = true;
}
/**
* This method is the entry method for each frame. It handles everything about the frame
*/
public void refresh() {
objects.addAll(objectBuffer);
objectBuffer.clear();
objects.forEach(t -> t.update(this));
mousePressed = false;
repaint();
}
/**
* Get the current location of the mouse relative to the frame
* @return The location of the mouse, already normalized
*/
public Point getMouseLocation() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
return p;
}
public boolean isMousePressed() {
return mousePressed;
}
/**
* This method has to be called for every newly created GameObject
* @param obj
*/
public void create(GameObject obj) {
objectBuffer.add(obj);
}
}

View file

@ -0,0 +1,158 @@
package core;
/**
* A 2-dimensional Vector that can be used to store position or velocity
*/
public class Vector2D {
public static final int CLOCKWISE = -1;
public static final int COUNTERCLOCKWISE = 1;
/**
* The x and y values of the vector
*/
public double x, y;
/**
* Create a new Vector2D object
*
* @param x the x value
* @param y the y value
*/
public Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Create a new empty Vector2D object
*/
public Vector2D() {
x = 0;
y = 0;
}
/**
* Add another Vector to this vector, modifies this object
*
* @param a The other Vector2D
* @return this after the addition
*/
public Vector2D add(Vector2D a) {
x += a.x;
y += a.y;
return this;
}
/**
* Mulitply this vector with a simple scalar, modifies this object
*
* @param a The scalar
* @return this after the multiplication
*/
public Vector2D multiply(double a) {
x *= a;
y *= a;
return this;
}
/**
* Get the magnitude of the vector
*
* @return The magnitude of this vector
*/
public double magnitude() {
return Math.sqrt(x * x + y * y);
}
/**
* Get the normalized value of this vector, with magnitude 1, does not modify this object
*
* @return The normalized value
*/
public Vector2D normalized() {
double mag = magnitude();
if (mag == 0) {
return new Vector2D();
} else {
return new Vector2D(x / mag, y / mag);
}
}
/**
* Get the negative value of this vector, does not modify this object
*
* @return The negative value
*/
public Vector2D negative() {
return new Vector2D(-x, -y);
}
/**
* Add two Vectors
* @param a Vector a
* @param b Vector b
* @return The result
*/
public static Vector2D add(Vector2D a, Vector2D b) {
return new Vector2D(a.x + b.x, a.y + b.y);
}
/**
* Subtract two Vectors
* @param a Vector a
* @param b Vector b
* @return The result
*/
public static Vector2D subtract(Vector2D a, Vector2D b) {
return new Vector2D(a.x - b.x, a.y - b.y);
}
/**
* Rotate a point around another point
* @param center The center of the rotation
* @param value The point to be rotated, absolut to the center
* @param rotation The rotation angle in radians
* @param rotationDirection The direction, -1 for clockwise, 1 for counterclockwise
* @return The rotated point
*/
public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation, int rotationDirection){
Vector2D dif = Vector2D.subtract(value, center);
double rotatedX = Math.cos(rotation * rotationDirection) * dif.x;
double rotatedY = Math.sin(rotation * rotationDirection) * dif.x;
return new Vector2D(rotatedX + center.x, rotatedY + center.y);
}
public Vector2D copy(){
return new Vector2D(x, y);
}
@Override
public String toString() {
return "Vector2D{" +
"x=" + x +
", y=" + y +
'}';
}
/**
* Overrides the equals method. True when the x and y values match
*
* @param o The other Vector
* @return True if both x and values are the same
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vector2D vector2D = (Vector2D) o;
if (Double.compare(vector2D.x, x) != 0) return false;
return Double.compare(vector2D.y, y) == 0;
}
}