mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
inital commit
This commit is contained in:
parent
5b9846011c
commit
0d5397dbe3
35 changed files with 955 additions and 0 deletions
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
|
|
@ -0,0 +1 @@
|
|||
2DGame
|
||||
2
.idea/Java2DGame.iml
generated
Normal file
2
.idea/Java2DGame.iml
generated
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
||||
14
.idea/compiler.xml
generated
Normal file
14
.idea/compiler.xml
generated
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="Java2DGame" />
|
||||
<module name="2DGame" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/discord.xml
generated
Normal file
6
.idea/discord.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/encodings.xml
generated
Normal file
6
.idea/encodings.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
|
||||
<file url="PROJECT" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
25
.idea/jarRepositories.xml
generated
Normal file
25
.idea/jarRepositories.xml
generated
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="public-snapshots" />
|
||||
<option name="name" value="public-snapshots" />
|
||||
<option name="url" value="https://portal.paas.intraxa/nexus/content/groups/ch-public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
9
.idea/libraries/2DGame.xml
generated
Normal file
9
.idea/libraries/2DGame.xml
generated
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<component name="libraryTable">
|
||||
<library name="2DGame">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/2DGame.zip!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_14" project-jdk-name="14" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
124
.idea/uiDesigner.xml
generated
Normal file
124
.idea/uiDesigner.xml
generated
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
2
2DGame.iml
Normal file
2
2DGame.iml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
||||
BIN
2DGame.zip
BIN
2DGame.zip
Binary file not shown.
31
pom.xml
Normal file
31
pom.xml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>2DGame</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>14</maven.compiler.source>
|
||||
<maven.compiler.target>14</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.6.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.6.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
47
src/main/java/BasicEx.java
Normal file
47
src/main/java/BasicEx.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import core.Master;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
class BasicEx extends JFrame {
|
||||
|
||||
Master master;
|
||||
Timer timer;
|
||||
|
||||
public BasicEx() {
|
||||
initUI();
|
||||
timer = new Timer(1000/60, e -> {
|
||||
master.refresh();
|
||||
});
|
||||
timer.start();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
master = new Master();
|
||||
add(master);
|
||||
|
||||
setTitle("Points");
|
||||
setSize(1600, 900);
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
master.mousePressed();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
|
||||
BasicEx ex = new BasicEx();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
123
src/main/java/core/Master.java
Normal file
123
src/main/java/core/Master.java
Normal 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);
|
||||
}
|
||||
}
|
||||
158
src/main/java/core/Vector2D.java
Normal file
158
src/main/java/core/Vector2D.java
Normal 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;
|
||||
}
|
||||
}
|
||||
26
src/main/java/objects/DebugPos.java
Normal file
26
src/main/java/objects/DebugPos.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package objects;
|
||||
|
||||
import core.Master;
|
||||
import core.Vector2D;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A GameObject used for debugging
|
||||
*/
|
||||
public class DebugPos extends GameObject {
|
||||
public DebugPos(Vector2D position, Vector2D size) {
|
||||
super(position.copy(), size);
|
||||
this.velocity = new Vector2D();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, int w, Master master) {
|
||||
g2d.setPaint(Color.green);
|
||||
g2d.drawOval((int) (position.x + size.x / 2), (int) (position.y + size.y / 2), (int) size.x, (int) size.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Master master) {
|
||||
}
|
||||
}
|
||||
67
src/main/java/objects/GameObject.java
Normal file
67
src/main/java/objects/GameObject.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package objects;
|
||||
|
||||
import core.Master;
|
||||
import core.Vector2D;
|
||||
|
||||
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 {
|
||||
|
||||
protected int w;
|
||||
protected int h;
|
||||
|
||||
protected Vector2D position;
|
||||
protected Vector2D size;
|
||||
|
||||
protected Vector2D velocity;
|
||||
|
||||
public GameObject(double x, double y, double xSize, double ySize) {
|
||||
this(new Vector2D(x, y), new Vector2D(xSize, ySize));
|
||||
}
|
||||
|
||||
public GameObject(Vector2D position, Vector2D size) {
|
||||
this.position = position;
|
||||
this.size = size;
|
||||
this.velocity = new Vector2D();
|
||||
}
|
||||
|
||||
public abstract void draw(Graphics2D g2d, int w, Master master);
|
||||
public abstract void update(Master master);
|
||||
|
||||
|
||||
public double getMapCoords(double value, boolean isX){
|
||||
if (isX){
|
||||
return (position.x + value / 100 * size.x);
|
||||
} else {
|
||||
return (position.y + value / 100 * size.y);
|
||||
}
|
||||
}
|
||||
|
||||
public double getMapCoordsSize(double value, boolean useX){
|
||||
if (useX){
|
||||
return (value / 100 * 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);
|
||||
}
|
||||
}
|
||||
|
||||
public int getWorldCoordsFromLocal(double value, boolean isX){
|
||||
return (int) getWorldCoords(getMapCoords(value, isX), isX);
|
||||
}
|
||||
|
||||
public int getWorldCoordsFromLocalSize(double value, boolean useX){
|
||||
return (int) getWorldCoords(getMapCoordsSize(value, useX), useX);
|
||||
}
|
||||
}
|
||||
54
src/main/java/objects/ships/BattleShip.java
Normal file
54
src/main/java/objects/ships/BattleShip.java
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package objects.ships;
|
||||
|
||||
import core.Master;
|
||||
import objects.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A Battleship that can have several turrets
|
||||
*/
|
||||
public class BattleShip extends GameObject {
|
||||
|
||||
private Color mainColor;
|
||||
private ArrayList<Turret> turrets;
|
||||
|
||||
public BattleShip(Color mainColor) {
|
||||
super(20, 20, 5, 40);
|
||||
turrets = new ArrayList<>();
|
||||
turrets.add(new Turret(this, 25, 25, 50, 1));
|
||||
//turrets.add(new Turret(this, 25, 10, 50, 2));
|
||||
//turrets.add(new Turret(this, 25, 70, 50, 2));
|
||||
this.mainColor = mainColor;
|
||||
}
|
||||
|
||||
public BattleShip(double x, double y, double xSize, double ySize, Color mainColor) {
|
||||
super(x, y, xSize, ySize);
|
||||
turrets = new ArrayList<>();
|
||||
this.mainColor = mainColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, int w, Master master) {
|
||||
this.w = w;
|
||||
h = w/16*9;
|
||||
g2d.setPaint(mainColor);
|
||||
int xAbs = (int) getWorldCoords(position.x, true);
|
||||
int yAbs = (int) getWorldCoords(position.y, false);
|
||||
int sizeXAbs = (int) getWorldCoords(size.x, true);
|
||||
int sizeYAbs = (int) getWorldCoords(size.y, false);
|
||||
g2d.fillRoundRect(xAbs, yAbs, sizeXAbs, sizeYAbs, w/10, w/10);
|
||||
|
||||
turrets.forEach((turret -> turret.draw(g2d, w, master)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Master master) {
|
||||
turrets.forEach((turret -> turret.update(master)));
|
||||
}
|
||||
|
||||
public void addTurret(Turret turret){
|
||||
turrets.add(turret);
|
||||
}
|
||||
}
|
||||
27
src/main/java/objects/ships/Shell.java
Normal file
27
src/main/java/objects/ships/Shell.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package objects.ships;
|
||||
|
||||
import core.Master;
|
||||
import core.Vector2D;
|
||||
import objects.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Shell extends GameObject {
|
||||
|
||||
|
||||
public Shell(Vector2D position, Vector2D size, Vector2D velocity) {
|
||||
super(position, size);
|
||||
this.velocity = velocity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, int w, Master master) {
|
||||
g2d.setPaint(Color.orange);
|
||||
g2d.fillOval((int) position.x, (int) position.y, (int) size.x, (int) size.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Master master) {
|
||||
position.add(velocity);
|
||||
}
|
||||
}
|
||||
103
src/main/java/objects/ships/Turret.java
Normal file
103
src/main/java/objects/ships/Turret.java
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package objects.ships;
|
||||
|
||||
import core.Master;
|
||||
import core.Vector2D;
|
||||
import objects.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A Turret that can shoot projectiles
|
||||
*/
|
||||
public class Turret extends GameObject {
|
||||
|
||||
BattleShip battleShip;
|
||||
|
||||
private int barrelAmount = 3;
|
||||
|
||||
private Color mainColor;
|
||||
|
||||
private long lastShot = 0;
|
||||
private static int SHOT_EFFECT_TIME = 300;
|
||||
private static int SHELL_SPEED = 10;
|
||||
private double rotation;
|
||||
|
||||
public Turret(BattleShip battleShip, double x, double y, double size, int barrelAmount) {
|
||||
super(x, y, size, size);
|
||||
this.battleShip = battleShip;
|
||||
this.barrelAmount = barrelAmount;
|
||||
mainColor = Color.GRAY;
|
||||
}
|
||||
|
||||
public Turret(BattleShip battleShip) {
|
||||
super(25, 50, 50, 50);
|
||||
this.battleShip = battleShip;
|
||||
mainColor = Color.GRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, int w, Master master) {
|
||||
//TODO draw should be draw only for better everything
|
||||
h = w / 16 * 9;
|
||||
g2d.setPaint(mainColor);
|
||||
int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true);
|
||||
int yAbs = battleShip.getWorldCoordsFromLocal(position.y, false);
|
||||
int sizeAbs = battleShip.getWorldCoordsFromLocalSize(size.x, true);
|
||||
int xCenterAbs = xAbs + sizeAbs / 2;
|
||||
int yCenterAbs = yAbs + sizeAbs / 2;
|
||||
|
||||
g2d.fillOval(xAbs, yAbs, sizeAbs, sizeAbs);
|
||||
|
||||
g2d.setStroke(new BasicStroke(battleShip.getWorldCoordsFromLocalSize(10, true), BasicStroke.CAP_BUTT,
|
||||
BasicStroke.JOIN_BEVEL));
|
||||
|
||||
//BARRELS---------------------------------------
|
||||
|
||||
g2d.setPaint(Color.BLACK);
|
||||
int barrelSpacing = sizeAbs / (barrelAmount + 1);
|
||||
g2d.rotate(rotation, xCenterAbs, yCenterAbs);
|
||||
for (int i = 0; i < barrelAmount; i++) {
|
||||
int barrelX = xAbs + (i + 1) * barrelSpacing;
|
||||
int frontPosY = yAbs - sizeAbs / 2;
|
||||
g2d.drawLine(barrelX, yCenterAbs, barrelX, frontPosY);
|
||||
|
||||
if (lastShot + SHOT_EFFECT_TIME > System.currentTimeMillis()) {
|
||||
g2d.setPaint(Color.YELLOW);
|
||||
g2d.fillOval(barrelX - 5, frontPosY - 5, 10, 10);
|
||||
g2d.setPaint(Color.BLACK);
|
||||
}
|
||||
}
|
||||
g2d.rotate(-rotation, xCenterAbs, yCenterAbs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Master master) {
|
||||
|
||||
int xAbs = battleShip.getWorldCoordsFromLocal(position.x, true);
|
||||
int yAbs = battleShip.getWorldCoordsFromLocal(position.y, false);
|
||||
int sizeAbs = battleShip.getWorldCoordsFromLocalSize(size.x, true);
|
||||
int xCenterAbs = xAbs + sizeAbs / 2;
|
||||
int yCenterAbs = yAbs + sizeAbs / 2;
|
||||
|
||||
Point msLoc = master.getMouseLocation();
|
||||
rotation = -Math.atan2(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y);
|
||||
|
||||
int barrelSpacing = sizeAbs / (barrelAmount + 1);
|
||||
|
||||
for (int i = 0; i < barrelAmount; i++) {
|
||||
int barrelX = xAbs + (i + 1) * barrelSpacing;
|
||||
int frontPosY = yAbs - sizeAbs / 2;
|
||||
|
||||
if (master.isMousePressed()) {
|
||||
lastShot = System.currentTimeMillis();
|
||||
|
||||
Vector2D shellVel = new Vector2D(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y).normalized().negative().multiply(SHELL_SPEED);
|
||||
Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), new Vector2D(barrelX, frontPosY), rotation, Vector2D.CLOCKWISE);
|
||||
|
||||
master.debugPos(pos);
|
||||
|
||||
master.create(new Shell(pos, new Vector2D(10, 10), shellVel));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
src/main/java/objects/world/Grid.java
Normal file
47
src/main/java/objects/world/Grid.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package objects.world;
|
||||
|
||||
import core.Master;
|
||||
import objects.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A basic background grid
|
||||
*/
|
||||
public class Grid extends GameObject {
|
||||
|
||||
private int gridSpacing = 50;
|
||||
|
||||
public Grid() {
|
||||
super(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, int w, Master master) {
|
||||
g2d.setPaint(Color.LIGHT_GRAY);
|
||||
|
||||
this.w = w;
|
||||
h = w/16*9;
|
||||
|
||||
g2d.drawRect(0, 0, w, h);
|
||||
|
||||
int verticalLines = w / gridSpacing;
|
||||
int horizontalLines = h / gridSpacing;
|
||||
|
||||
for (int i = 0; i < horizontalLines; i++) {
|
||||
int y = i * h / horizontalLines;
|
||||
g2d.drawLine(0, y, w, y);
|
||||
}
|
||||
|
||||
for (int i = 0; i < verticalLines; i++) {
|
||||
int x = i * w / verticalLines;
|
||||
g2d.drawLine(x, 0, x, h);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Master master) {
|
||||
|
||||
}
|
||||
}
|
||||
60
src/test/java/core/Vector2DTest.java
Normal file
60
src/test/java/core/Vector2DTest.java
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package core;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class Vector2DTest {
|
||||
|
||||
|
||||
@Test
|
||||
void addOK() {
|
||||
Vector2D a = new Vector2D(1, 2);
|
||||
Vector2D b = new Vector2D(1, 2);
|
||||
Vector2D c = new Vector2D(-1 ,-1);
|
||||
Vector2D d = new Vector2D(0, 1);
|
||||
|
||||
assertEquals(new Vector2D(2, 4), Vector2D.add(a, b));
|
||||
assertEquals(new Vector2D(0, 1), Vector2D.add(a, c));
|
||||
assertEquals(new Vector2D(-1, 0), Vector2D.add(c, d));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalized() {
|
||||
Vector2D a = new Vector2D(1, 1);
|
||||
Vector2D b = new Vector2D(1, 0);
|
||||
|
||||
assertEquals(new Vector2D(1 / Math.sqrt(2), 1 / Math.sqrt(2)), a.normalized());
|
||||
assertEquals(new Vector2D(1,0), b.normalized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void subtractOK() {
|
||||
Vector2D a = new Vector2D(1, 2);
|
||||
Vector2D b = new Vector2D(1, 2);
|
||||
Vector2D c = new Vector2D(-1 ,-1);
|
||||
Vector2D d = new Vector2D(0, 1);
|
||||
|
||||
assertEquals(new Vector2D(0, 0), Vector2D.subtract(a, b));
|
||||
assertEquals(new Vector2D(2, 3), Vector2D.subtract(a, c));
|
||||
assertEquals(new Vector2D(-1, -2), Vector2D.subtract(c, d));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotateAroundTest(){
|
||||
Vector2D cent = new Vector2D(0, 0);
|
||||
Vector2D a = new Vector2D(1, 0);
|
||||
Vector2D exceptedA = new Vector2D(6.123233995736766E-17 ,-1); //floating point precision probably
|
||||
|
||||
Vector2D centB = new Vector2D(1, 1);
|
||||
Vector2D b = new Vector2D(2, 1);
|
||||
Vector2D exceptedB = new Vector2D(1 ,2);
|
||||
|
||||
|
||||
Vector2D rotated = Vector2D.rotateAround(cent, a, Math.toRadians(90), Vector2D.CLOCKWISE);
|
||||
Vector2D rotatedb = Vector2D.rotateAround(centB, b, Math.toRadians(90), Vector2D.COUNTERCLOCKWISE);
|
||||
|
||||
assertEquals(exceptedA, rotated);
|
||||
assertEquals(exceptedB, rotatedb);
|
||||
}
|
||||
}
|
||||
BIN
target/classes/BasicEx$1.class
Normal file
BIN
target/classes/BasicEx$1.class
Normal file
Binary file not shown.
BIN
target/classes/BasicEx.class
Normal file
BIN
target/classes/BasicEx.class
Normal file
Binary file not shown.
BIN
target/classes/core/Master.class
Normal file
BIN
target/classes/core/Master.class
Normal file
Binary file not shown.
BIN
target/classes/core/Vector2D.class
Normal file
BIN
target/classes/core/Vector2D.class
Normal file
Binary file not shown.
BIN
target/classes/objects/DebugPos.class
Normal file
BIN
target/classes/objects/DebugPos.class
Normal file
Binary file not shown.
BIN
target/classes/objects/GameObject.class
Normal file
BIN
target/classes/objects/GameObject.class
Normal file
Binary file not shown.
BIN
target/classes/objects/ships/BattleShip.class
Normal file
BIN
target/classes/objects/ships/BattleShip.class
Normal file
Binary file not shown.
BIN
target/classes/objects/ships/Shell.class
Normal file
BIN
target/classes/objects/ships/Shell.class
Normal file
Binary file not shown.
BIN
target/classes/objects/ships/Turret.class
Normal file
BIN
target/classes/objects/ships/Turret.class
Normal file
Binary file not shown.
BIN
target/classes/objects/world/Grid.class
Normal file
BIN
target/classes/objects/world/Grid.class
Normal file
Binary file not shown.
BIN
target/test-classes/core/Vector2DTest.class
Normal file
BIN
target/test-classes/core/Vector2DTest.class
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue