Mehr javadoc & remove dice3d ordner

This commit is contained in:
joachimschmidt557 2019-03-20 18:09:32 +01:00
parent 6f8b981cea
commit 6431df6bf4
10 changed files with 104 additions and 541 deletions

View file

@ -1,85 +0,0 @@
package dice3d.main;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import dice3d.models.cuboids.Cuboid;
import dice3d.models.cuboids.Dice;
public class MainWindow extends JPanel {
World w = new World();
public MainWindow() {
addKeyListener(new KeyHandler());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
draw((Graphics2D) g);
}
public void draw(Graphics2D g) {
for(Cuboid c : w.cuboids) {
c.draw(g);
}
int yOffset = 0;
g.drawString("Press space key to roll again", 10, yOffset =+ 20);
for(int i = 0; i < w.cuboids.size(); i++) {
Cuboid c = w.cuboids.get(i);
if(c instanceof Dice) {
Dice d = (Dice) c;
g.drawString("dice[" + i + "]: moving=" + !d.notMoving() + " number_rolled=" + d.getNumberRolled(), 10, yOffset += 20);
} else {
g.drawString("cuboid[" + i + "]: moving=" + !c.notMoving(), 10, yOffset += 20);
}
}
repaint();
}
private class KeyHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if (e.getID() != KeyEvent.KEY_PRESSED) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
for(Cuboid c : w.cuboids) {
c.reset();
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainWindow view = new MainWindow();
JFrame frame = new JFrame();
frame.setTitle("Dice");
frame.getContentPane().add(view);
frame.setSize(1280, 720);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setResizable(false);
frame.setVisible(true);
view.requestFocus();
}
});
}
}

View file

@ -1,43 +0,0 @@
package dice3d.main;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import dice3d.models.cuboids.Cuboid;
import dice3d.models.cuboids.Dice;
public class World {
public static final double projectionDistance = 1000;
public ArrayList<Cuboid> cuboids;
public Cuboid floor;
public World() {
cuboids = new ArrayList<Cuboid>();
floor = new Cuboid(10, 400, 600, 700, 1000, 10);
cuboids.add(floor);
cuboids.add(new Dice(150, 100, 800, 80));
cuboids.add(new Dice(50, 120, 800, 80));
long delayInMS = 500; // start updating after 500ms
long intervalInMS = 15; // update every 15ms
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for(Cuboid c : cuboids) {
if(c == floor) {
continue;
}
c.update();
}
}
}, delayInMS, intervalInMS);
}
}

View file

@ -1,75 +0,0 @@
package dice3d.math;
public class Vector {
public double x;
public double y;
public double z;
public Vector() {
}
public Vector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vector(Vector v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public void set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void set(Vector v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public void add(Vector v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
}
public void sub(Vector v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
}
public void scale(double s) {
scale(s, s, s);
}
public void scale(double sx, double sy, double sz) {
this.x *= sx;
this.y *= sy;
this.z *= sz;
}
public double getSize() {
return Math.sqrt(x * x + y * y + z * z);
}
public void normalize() {
scale(1 / getSize());
}
@Override
public String toString() {
return "[" + x + ", " + y + ", " + z + "]";
}
public boolean equals(Vector v) {
return v.x == x && v.y == y && v.z == z;
}
}

View file

@ -1,31 +0,0 @@
package dice3d.models;
import java.awt.Graphics2D;
import dice3d.main.World;
public class Edge {
private Vertex a;
private Vertex b;
public Edge(Vertex a, Vertex b) {
this.a = a;
this.b = b;
}
public void update() {
}
public void draw(Graphics2D g) {
double ax = World.projectionDistance * a.position.x / a.position.z;
double ay = World.projectionDistance * a.position.y / a.position.z;
double bx = World.projectionDistance * b.position.x / b.position.z;
double by = World.projectionDistance * b.position.y / b.position.z;
g.drawLine((int) ax, (int) ay, (int) bx, (int) by);
}
}

View file

@ -1,40 +0,0 @@
package dice3d.models;
import java.awt.Graphics2D;
import dice3d.main.World;
import dice3d.math.Vector;
public class Vertex {
public Vector position = new Vector();
public Vector positionReset = new Vector();
public String id;
public Vertex(double x, double y, double z) {
position.set(x, y, z);
positionReset.set(position);
}
public void reset() {
position.set(positionReset);
}
public void update() {
}
public void draw(Graphics2D g) {
double px = World.projectionDistance * position.x / position.z;
double py = World.projectionDistance * position.y / position.z;
if (id == null) {
int size = 6;
g.fillOval((int) (px - (size / 2)), (int) (py - (size / 2)), size, size);
} else {
g.drawString(id, (int)px, (int)py);
}
}
}

View file

@ -1,9 +0,0 @@
package dice3d.models.cuboids;
public class Cube extends Cuboid {
public Cube(double x, double y, double z, int size) {
super(x, y, z, size, size, size);
}
}

View file

@ -1,147 +0,0 @@
package dice3d.models.cuboids;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import dice3d.math.Vector;
import dice3d.models.Edge;
import dice3d.models.Vertex;
public class Cuboid {
public ArrayList<Vertex> vertices = new ArrayList<Vertex>();
public ArrayList<Edge> edges = new ArrayList<Edge>();
public boolean collided = false;
public Cuboid(double x, double y, double z, int length, int width, int height) {
Vector corner = new Vector(x, y, z);
Vertex a = new Vertex(corner.x, corner.y, corner.z);
Vertex b = new Vertex(corner.x + length, corner.y, corner.z);
Vertex c = new Vertex(corner.x + length, corner.y + height, corner.z);
Vertex d = new Vertex(corner.x, corner.y + height, corner.z);
Vertex e = new Vertex(corner.x, corner.y, corner.z + width);
Vertex f = new Vertex(corner.x + length, corner.y, corner.z + width);
Vertex g = new Vertex(corner.x + length, corner.y + height, corner.z + width);
Vertex h = new Vertex(corner.x, corner.y + height, corner.z + width);
a.id = "A";
b.id = "B";
c.id = "C";
d.id = "D";
e.id = "E";
f.id = "F";
g.id = "G";
h.id = "H";
vertices.add(a);
vertices.add(b);
vertices.add(c);
vertices.add(d);
vertices.add(e);
vertices.add(f);
vertices.add(g);
vertices.add(h);
Edge edge01 = new Edge(a, b);
Edge edge02 = new Edge(b, c);
Edge edge03 = new Edge(c, d);
Edge edge04 = new Edge(d, a);
Edge edge05 = new Edge(e, f);
Edge edge06 = new Edge(f, g);
Edge edge07 = new Edge(g, h);
Edge edge08 = new Edge(h, e);
Edge edge09 = new Edge(a, e);
Edge edge10 = new Edge(b, f);
Edge edge11 = new Edge(c, g);
Edge edge12 = new Edge(d, h);
Edge edge13 = new Edge(a, g);
Edge edge14 = new Edge(b, h);
Edge edge15 = new Edge(c, e);
Edge edge16 = new Edge(d, f);
Edge edge17 = new Edge(a, c);
Edge edge18 = new Edge(b, g);
Edge edge19 = new Edge(f, h);
Edge edge20 = new Edge(d, e);
Edge edge21 = new Edge(d, g);
Edge edge22 = new Edge(a, f);
edges.add(edge01);
edges.add(edge02);
edges.add(edge03);
edges.add(edge04);
edges.add(edge05);
edges.add(edge06);
edges.add(edge07);
edges.add(edge08);
edges.add(edge09);
edges.add(edge10);
edges.add(edge11);
edges.add(edge12);
edges.add(edge13);
edges.add(edge14);
edges.add(edge15);
edges.add(edge16);
edges.add(edge17);
edges.add(edge18);
edges.add(edge19);
edges.add(edge20);
edges.add(edge21);
edges.add(edge22);
}
public void update() {
for (Vertex v : vertices) {
v.update();
}
for (Edge e : edges) {
e.update();
}
}
public void reset() {
for (Vertex v : vertices) {
v.reset();
}
}
public void draw(Graphics2D g) {
// draw all edges, even inner ones
g.setColor(Color.GREEN);
if(collided) {
g.setColor(Color.RED);
}
for(Edge e: edges) {
e.draw(g);
}
g.setColor(Color.BLACK);
for (Vertex point : vertices) {
point.draw(g);
}
}
public boolean notMoving() {
return false;
}
public void updateCollision(Cuboid d) {
collided = false;
}
}

View file

@ -1,111 +0,0 @@
package dice3d.models.cuboids;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import dice3d.main.World;
import dice3d.models.Vertex;
public class Dice extends Cube {
private Vertex[][] face = new Vertex[6][4];
public Dice(double x, double y, double z, int size) {
super(x,y,z, size);
face[0][0] = vertices.get(0); // a
face[0][1] = vertices.get(1); // b
face[0][2] = vertices.get(2); // c ...
face[0][3] = vertices.get(3);
face[1][0] = vertices.get(1);
face[1][1] = vertices.get(5);
face[1][2] = vertices.get(6);
face[1][3] = vertices.get(2);
face[2][0] = vertices.get(5);
face[2][1] = vertices.get(4);
face[2][2] = vertices.get(7);
face[2][3] = vertices.get(6);
face[3][0] = vertices.get(4);
face[3][1] = vertices.get(0);
face[3][2] = vertices.get(3);
face[3][3] = vertices.get(7);
face[4][0] = vertices.get(3);
face[4][1] = vertices.get(2);
face[4][2] = vertices.get(6);
face[4][3] = vertices.get(7);
face[5][0] = vertices.get(1);
face[5][1] = vertices.get(0);
face[5][2] = vertices.get(4);
face[5][3] = vertices.get(5);
}
public void draw(Graphics2D g) {
drawDice(g);
// draw all edges, even inner ones
// for(Edge e: edges) {
// e.draw(g);
// }
// for (Vertex point : vertices) {
// point.draw(g);
// }
}
public int getNumberRolled() {
return 1;
}
private void drawDice(Graphics2D g) {
for (int f = 0; f < 6; f++) {
// each face as polygon
Polygon polygon = new Polygon();
for (int v = 0; v < 4; v++) {
Vertex vertex = face[f][v];
double vx = World.projectionDistance * vertex.position.x / vertex.position.z;
double vy = World.projectionDistance * vertex.position.y / vertex.position.z;
polygon.addPoint((int) vx, (int) vy);
}
// back-face culling
double v1x = polygon.xpoints[0] - polygon.xpoints[1];
double v1y = polygon.ypoints[0] - polygon.ypoints[1];
double v2x = polygon.xpoints[2] - polygon.xpoints[1];
double v2y = polygon.ypoints[2] - polygon.ypoints[1];
double visible = v1x * v2y - v1y * v2x;
if (visible < 0) {
g.setColor(Color.WHITE);
if(collided) {
g.setColor(Color.RED);
}
g.fill(polygon);
g.setColor(Color.BLACK);
g.draw(polygon);
// draw number of each face (that is visible)
// calc center of each face/square
double xs = 0d;
double ys = 0d;
for (int i = 0; i < 4; i++) {
xs += polygon.xpoints[i];
ys += polygon.ypoints[i];
}
xs = xs / 4;
ys = ys / 4;
g.drawString(f + 1 + "", (int) xs, (int) ys);
}
}
}
}

View file

@ -24,6 +24,9 @@ public class Game {
private GameInterface gameInterface;
private AttackThread attackThread;
/**
* Initialize a new game
*/
public Game() {
this.isOver = false;
this.hasStarted = false;
@ -31,6 +34,10 @@ public class Game {
this.players = new LinkedList<>();
}
/**
* Add a player to the game
* @param p The new player
*/
public void addPlayer(Player p) {
if(players.contains(p))
throw new IllegalArgumentException("Spieler wurde bereits hinzugefügt");
@ -38,19 +45,34 @@ public class Game {
this.players.add(p);
}
/**
* Sets the goal to achieve
* @param goal The goal
*/
public void setGoal(Goal goal) {
this.goal = goal;
this.goal.setGame(this);
}
/**
* The round this game is currently in
* @return The round
*/
public int getRound() {
return round;
}
/**
* Sets the size of the map
* @param mapSize The size of the map
*/
public void setMapSize(MapSize mapSize) {
this.mapSize = mapSize;
}
/**
* Generates the map
*/
private void generateMap() {
int mapSizeMultiplier = this.mapSize.ordinal() + 1;
@ -70,6 +92,10 @@ public class Game {
this.gameMap = GameMap.generateRandomMap(width, height, 40, numRegions, continents);
}
/**
* Starts the game
* @param gameInterface The interface of the game
*/
public void start(GameInterface gameInterface) {
if(hasStarted)
@ -102,6 +128,13 @@ public class Game {
nextTurn();
}
/**
* Starts an attack
* @param source
* @param target
* @param troopCount
* @return
*/
public AttackThread startAttack(Castle source, Castle target, int troopCount) {
if(attackThread != null)
return attackThread;
@ -115,6 +148,13 @@ public class Game {
return attackThread;
}
/**
* Makes a castle attack another castle
* @param attackerCastle
* @param defenderCastle
* @param rollAttacker
* @param rollDefender
*/
public void doAttack(Castle attackerCastle, Castle defenderCastle, int[] rollAttacker, int[] rollDefender) {
Integer[] rollAttackerSorted = Arrays.stream(rollAttacker).boxed().sorted(Comparator.reverseOrder()).toArray(Integer[]::new);
@ -145,6 +185,12 @@ public class Game {
gameInterface.onUpdate();
}
/**
* Move troops from castle to castle
* @param source
* @param destination
* @param troopCount
*/
public void moveTroops(Castle source, Castle destination, int troopCount) {
if(troopCount >= source.getTroopCount() || source.getOwner() != destination.getOwner())
return;
@ -153,23 +199,46 @@ public class Game {
gameInterface.onUpdate();
}
/**
* Abort the attack
*/
public void stopAttack() {
this.attackThread = null;
this.gameInterface.onAttackStopped();
}
/**
* Rolls the dice
* @param player
* @param dices
* @param fastForward
* @return
*/
public int[] roll(Player player, int dices, boolean fastForward) {
return gameInterface.onRoll(player, dices, fastForward);
}
/**
* Are all castles chosen?
* @return
*/
private boolean allCastlesChosen() {
return gameMap.getCastles().stream().noneMatch(c -> c.getOwner() == null);
}
/**
* The attack thread
* @return
*/
public AttackThread getAttackThread() {
return this.attackThread;
}
/**
* A player chooses this castle
* @param castle
* @param player
*/
public void chooseCastle(Castle castle, Player player) {
if(castle.getOwner() != null || player.getRemainingTroops() == 0)
return;
@ -186,6 +255,12 @@ public class Game {
}
}
/**
* Adds troops to a castle
* @param player
* @param castle
* @param count
*/
public void addTroops(Player player, Castle castle, int count) {
if(count < 1 || castle.getOwner() != player)
return;
@ -195,11 +270,19 @@ public class Game {
player.removeTroops(count);
}
/**
* Gives a player more points
* @param player
* @param score
*/
public void addScore(Player player, int score) {
player.addPoints(score);
gameInterface.onAddScore(player, score);
}
/**
* This is it. The endgame.
*/
public void endGame() {
isOver = true;
Player winner = goal.getWinner();
@ -215,6 +298,9 @@ public class Game {
gameInterface.onGameOver(winner);
}
/**
* Moves on to the next turn
*/
public void nextTurn() {
if(goal.isCompleted()) {
@ -275,18 +361,34 @@ public class Game {
playerQueue.add(currentPlayer);
}
/**
* The currently active player
* @return
*/
public Player getCurrentPlayer() {
return this.currentPlayer;
}
/**
* All players
* @return
*/
public List<Player> getPlayers() {
return this.players;
}
/**
* The game map
* @return
*/
public GameMap getMap() {
return this.gameMap;
}
/**
* Is this game over yet?
* @return
*/
public boolean isOver() {
return this.isOver;
}