1
0
Fork 0
This commit is contained in:
Piegames 2018-04-22 10:44:29 +02:00
parent d737457101
commit cc36c1f48c
2 changed files with 103 additions and 100 deletions

View file

@ -1,4 +1,3 @@
import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
@ -17,8 +16,10 @@ import org.joml.Vector2i;
import javafx.util.Pair; import javafx.util.Pair;
public class LD41Map { public class LD41Map {
public static class Vertex {
static final int TUNNEL_THICKNESS = 3;
public static class Vertex {
public final Room r; public final Room r;
public float value; public float value;
@ -30,8 +31,8 @@ public class LD41Map {
parent = null; parent = null;
} }
} }
public static class Edge {
public static class Edge {
public final Vertex r1, r2; public final Vertex r1, r2;
public double dist; public double dist;
@ -42,21 +43,24 @@ public class LD41Map {
dist = r1.r.distance(r2.r); dist = r1.r.distance(r2.r);
} }
} }
public static class Room {
public static class Room {
Rectangle bounds = new Rectangle(); Rectangle bounds = new Rectangle();
// int width = 0, height = 0; // int width = 0, height = 0;
Map<Vector2i, TileType> tiles; Map<Vector2i, TileType> tiles;
// Vector2i pos = new Vector2i(); // Vector2i pos = new Vector2i();
Set<Vector2i> doors;
public float distance(Room r) { public float distance(Room r) {
// Vector2i center1 = new Vector2i(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); return Math.abs(getCenter().x - r.getCenter().x) + Math.abs(getCenter().y - r.getCenter().y);
// Vector2i center2 = new Vector2i(r.bounds.x + r.bounds.width / 2, r.bounds.y + r.bounds.height / 2); }
// return (float) center1.distance(center2);
return (float) Point.distance(bounds.x, bounds.y, r.bounds.x, r.bounds.y); public Point getCenter() {
return new Point((int) bounds.getCenterX(), (int) bounds.getCenterY());
} }
} }
public static enum TileType { public static enum TileType {
GROUND, WALL, DOOR; GROUND, WALL, DOOR;
@ -81,17 +85,9 @@ public class LD41Map {
g.translate(getWidth() / 2, getHeight() / 2); g.translate(getWidth() / 2, getHeight() / 2);
g.scale(4, 4); g.scale(4, 4);
for (Room room : graph.getKey()) { for (Room room : graph.getKey()) {
g.setColor(new Color(1f, 0f, 0f, 0.8f)); g.setColor(new Color(1f, 0f, 0f, 0.5f));
g.fill(room.bounds); g.fill(room.bounds);
} }
g.setColor(Color.BLUE);
g.setStroke(new BasicStroke(2));
for (Edge e : graph.getValue().getValue()) {
g.drawLine(e.r1.r.bounds.x, e.r1.r.bounds.y, e.r2.r.bounds.x, e.r2.r.bounds.y);
}
Room root = graph.getValue().getKey().r;
g.setColor(Color.GREEN);
g.fillRect(root.bounds.x, root.bounds.y, 2, 2);
} }
}; };
frame.add(panel); frame.add(panel);
@ -191,111 +187,122 @@ public class LD41Map {
// G list of edges // G list of edges
// rooms list of rooms // rooms list of rooms
for(Edge ed : G) for (Edge ed : G) {
{
// horizontal // horizontal
float diff1 = ed.r1.r.bounds.y - ed.r2.r.bounds.y - ed.r2.r.bounds.height + constants.tunnelThickness; float diff1 = ed.r1.r.bounds.y - ed.r2.r.bounds.y - ed.r2.r.bounds.height + TUNNEL_THICKNESS;
float diff2 = ed.r2.r.bounds.y - ed.r1.r.bounds.y - ed.r1.r.bounds.height + constants.tunnelThickness; float diff2 = ed.r2.r.bounds.y - ed.r1.r.bounds.y - ed.r1.r.bounds.height + TUNNEL_THICKNESS;
// vertical // vertical
float diff3 = ed.r1.r.bounds.x - ed.r2.r.bounds.x - ed.r2.r.bounds.width + constants.tunnelThickness; float diff3 = ed.r1.r.bounds.x - ed.r2.r.bounds.x - ed.r2.r.bounds.width + TUNNEL_THICKNESS;
float diff4 = ed.r2.r.bounds.x - ed.r1.r.bounds.x - ed.r1.r.bounds.width + constants.tunnelThickness; float diff4 = ed.r2.r.bounds.x - ed.r1.r.bounds.x - ed.r1.r.bounds.width + TUNNEL_THICKNESS;
if(diff1 < 0 && diff2 < 0) if (diff1 < 0 && diff2 < 0) {
{ addStraightHorizontal(rooms, ed);
addStraightHorizontal(rooms,ed); } else if (diff3 < 0 && diff4 < 0) {
} addStraightVertical(rooms, ed);
else if(diff3 < 0 && diff4 < 0) } else
{ addCurve(rooms, ed);
addStraightVertical(rooms,ed);
}
else
addCurve(rooms,ed);
} }
return new Pair<>(rooms, new Pair<>(root, G)); return new Pair<>(rooms, new Pair<>(root, G));
} }
public static void addStraightHorizontal(Set<Room> rooms, Edge ed) public static void addStraightHorizontal(Set<Room> rooms, Edge ed) {
{
Room tunnel = new Room(); Room tunnel = new Room();
boolean multiBitX = (ed.r1.r.bounds.x < ed.r2.r.bounds.x); int minX = Math.min(ed.r1.r.bounds.x + ed.r1.r.bounds.width, ed.r2.r.bounds.x + ed.r2.r.bounds.width);
boolean multiBitY = (ed.r1.r.bounds.y < ed.r2.r.bounds.y); int minY = Math.max(ed.r1.r.bounds.y, ed.r2.r.bounds.y);
int maxX = Math.max(ed.r1.r.bounds.x, ed.r2.r.bounds.x);
tunnel.bounds.x = (multiBitX)? ed.r1.r.bounds.x + ed.r1.r.bounds.width : ed.r2.r.bounds.x + ed.r2.r.bounds.width; int maxY = Math.min(ed.r1.r.bounds.y + ed.r1.r.bounds.height, ed.r2.r.bounds.y + ed.r2.r.bounds.height);
tunnel.bounds.y = (multiBitY)? ed.r2.r.bounds.y : ed.r1.r.bounds.y; tunnel.bounds.x = minX;
tunnel.bounds.y = (minY + maxY) / 2 - TUNNEL_THICKNESS / 2;
tunnel.bounds.width = Math.abs(ed.r1.r.bounds.x - ed.r2.r.bounds.x) - ((multiBitX)? ed.r1.r.bounds.width : ed.r2.r.bounds.width); tunnel.bounds.width = (maxX - minX);
tunnel.bounds.height = constants.tunnelThickness; tunnel.bounds.height = TUNNEL_THICKNESS;
rooms.add(tunnel); rooms.add(tunnel);
} }
public static void addStraightVertical(Set<Room> rooms, Edge ed) public static void addStraightVertical(Set<Room> rooms, Edge ed) {
{
Room tunnel = new Room(); Room tunnel = new Room();
boolean multiBitX = (ed.r1.r.bounds.x < ed.r2.r.bounds.x); int minX = Math.max(ed.r1.r.bounds.x, ed.r2.r.bounds.x);
boolean multiBitY = (ed.r1.r.bounds.y < ed.r2.r.bounds.y); int minY = Math.min(ed.r1.r.bounds.y + ed.r1.r.bounds.height, ed.r2.r.bounds.y + ed.r2.r.bounds.height);
int maxX = Math.min(ed.r1.r.bounds.x + ed.r1.r.bounds.width, ed.r2.r.bounds.x + ed.r2.r.bounds.width);
tunnel.bounds.x = (multiBitX)? ed.r2.r.bounds.x : ed.r1.r.bounds.x; int maxY = Math.max(ed.r1.r.bounds.y, ed.r2.r.bounds.y);
tunnel.bounds.y = (multiBitY)? ed.r1.r.bounds.y + ed.r1.r.bounds.height : ed.r2.r.bounds.y + ed.r2.r.bounds.height; tunnel.bounds.x = (minX + maxX) / 2 - TUNNEL_THICKNESS / 2;
tunnel.bounds.y = minY;
tunnel.bounds.width = constants.tunnelThickness; tunnel.bounds.width = TUNNEL_THICKNESS;
tunnel.bounds.height = Math.abs(ed.r1.r.bounds.y - ed.r2.r.bounds.y) - ((multiBitY)? ed.r1.r.bounds.height: ed.r2.r.bounds.height); tunnel.bounds.height = (maxY - minY);
rooms.add(tunnel); rooms.add(tunnel);
} }
public static void addCurve(Set<Room> rooms, Edge ed) public static void addCurve(Set<Room> rooms, Edge ed) {
{ Room higher = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r1.r : ed.r2.r;
Vector2i directionVec = new Vector2i(ed.r2.r.bounds.x - ed.r1.r.bounds.x, ed.r2.r.bounds.y - ed.r1.r.bounds.y); Room lower = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r2.r : ed.r1.r;
boolean bigAngle = directionVec.x < 0; Room righter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r1.r : ed.r2.r;
double angle = Math.acos((directionVec.x + directionVec.y) / (directionVec.length()) + ((bigAngle)? 180 : 0)); Room lefter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r2.r : ed.r1.r;
if(angle < 90) Rectangle r = new Rectangle(lefter.getCenter().x, lower.getCenter().y, righter.getCenter().x - lefter.getCenter().x, higher.getCenter().y - lower.getCenter().y);
{
Room tunnel = new Room();
tunnel.bounds.x = ed.r1.r.bounds.x + ed.r1.r.bounds.width - constants.tunnelThickness;
tunnel.bounds.y = ed.r2.r.bounds.y;
tunnel.bounds.width = ed.r1.r.bounds.x - ed.r2.r.bounds.x; Room vertical1 = new Room();
tunnel.bounds.height = ed.r2.r.bounds.y - ed.r1.r.bounds.y; vertical1.bounds.x = r.x - TUNNEL_THICKNESS / 2;
vertical1.bounds.y = r.y - TUNNEL_THICKNESS / 2;
vertical1.bounds.width = TUNNEL_THICKNESS;
vertical1.bounds.height = r.height + TUNNEL_THICKNESS;
// add tiles to room Room horizontal1 = new Room();
} horizontal1.bounds.x = r.x - TUNNEL_THICKNESS / 2;
else if(angle < 180) horizontal1.bounds.y = r.y - TUNNEL_THICKNESS / 2;
{ horizontal1.bounds.width = r.width + TUNNEL_THICKNESS;
Room tunnel = new Room(); horizontal1.bounds.height = TUNNEL_THICKNESS;
tunnel.bounds.x = ed.r1.r.bounds.x + ed.r1.r.bounds.width - constants.tunnelThickness;
tunnel.bounds.y = ed.r1.r.bounds.y + ed.r1.r.bounds.height;
tunnel.bounds.width = ed.r1.r.bounds.x - ed.r2.r.bounds.x; Room vertical2 = new Room();
tunnel.bounds.height = ed.r1.r.bounds.y - ed.r2.r.bounds.y; vertical2.bounds.x = r.x + r.width - TUNNEL_THICKNESS / 2;
vertical2.bounds.y = r.y - TUNNEL_THICKNESS / 2;
vertical2.bounds.width = TUNNEL_THICKNESS;
vertical2.bounds.height = r.height + TUNNEL_THICKNESS;
// add tiles to room Room horizontal2 = new Room();
} horizontal2.bounds.x = r.x - TUNNEL_THICKNESS / 2;
else if(angle < 270) horizontal2.bounds.y = r.y + r.height - TUNNEL_THICKNESS / 2;
{ horizontal2.bounds.width = r.width + TUNNEL_THICKNESS;
Room tunnel = new Room(); horizontal2.bounds.height = TUNNEL_THICKNESS;
tunnel.bounds.x = ed.r2.r.bounds.x + ed.r2.r.bounds.width;
tunnel.bounds.y = ed.r1.r.bounds.y + ed.r1.r.bounds.height;
tunnel.bounds.width = ed.r1.r.bounds.x - ed.r2.r.bounds.x + constants.tunnelThickness; boolean flip = Math.random() > 0.5;
tunnel.bounds.height = ed.r1.r.bounds.y - ed.r2.r.bounds.y + constants.tunnelThickness; boolean diffX = ed.r2.r.getCenter().x - ed.r1.r.getCenter().x > 0;
boolean diffY = ed.r2.r.getCenter().y - ed.r1.r.getCenter().y > 0;
// add tiles to room if (diffX && diffY) {
} if (flip) {
else //(angle < 360) rooms.add(vertical1);
{ rooms.add(horizontal2);
Room tunnel = new Room(); } else {
tunnel.bounds.x = ed.r2.r.bounds.x + ed.r2.r.bounds.width; rooms.add(vertical2);
tunnel.bounds.y = ed.r2.r.bounds.y - ed.r2.r.bounds.height; rooms.add(horizontal1);
}
tunnel.bounds.width = ed.r1.r.bounds.x - ed.r2.r.bounds.x + constants.tunnelThickness; } else if (diffX && !diffY) {
tunnel.bounds.height = ed.r2.r.bounds.y - ed.r2.r.bounds.height - ed.r1.r.bounds.y + constants.tunnelThickness; if (flip) {
rooms.add(vertical2);
// add tiles to room rooms.add(horizontal2);
} else {
rooms.add(vertical1);
rooms.add(horizontal1);
}
} else if (!diffX && diffY) {
if (flip) {
rooms.add(vertical1);
rooms.add(horizontal1);
} else {
rooms.add(vertical2);
rooms.add(horizontal2);
}
} else if (!diffX && !diffY) {
if (flip) {
rooms.add(vertical2);
rooms.add(horizontal1);
} else {
rooms.add(vertical1);
rooms.add(horizontal2);
}
} }
} }
} }

View file

@ -1,4 +0,0 @@
public enum constants
{;
static final int tunnelThickness = 3;
}