mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 22:15:01 +01:00
inital commit
This commit is contained in:
parent
5b9846011c
commit
0d5397dbe3
35 changed files with 955 additions and 0 deletions
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue