v0.6.0: implemented point connection limits for rooms

This commit is contained in:
Evan Debenham 2017-04-11 22:46:51 -04:00
parent 9a902d97bd
commit 7153104e4d
3 changed files with 36 additions and 16 deletions

View File

@ -21,7 +21,7 @@
package com.watabou.utils; package com.watabou.utils;
import java.util.HashSet; import java.util.ArrayList;
public class Rect { public class Rect {
@ -133,8 +133,8 @@ public class Rect {
return shrink( 1 ); return shrink( 1 );
} }
public HashSet<Point> getPoints() { public ArrayList<Point> getPoints() {
HashSet<Point> points = new HashSet<>(square()*2); ArrayList<Point> points = new ArrayList<>(square()*2);
for (int i = left; i <= right; i++) for (int i = left; i <= right; i++)
for (int j = top; j <= bottom; j++) for (int j = top; j <= bottom; j++)
points.add(new Point(i, j)); points.add(new Point(i, j));

View File

@ -29,6 +29,7 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.Room;
import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.standard.StandardRoom; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.standard.StandardRoom;
import com.shatteredpixel.shatteredpixeldungeon.levels.traps.Trap; import com.shatteredpixel.shatteredpixeldungeon.levels.traps.Trap;
import com.watabou.utils.PathFinder; import com.watabou.utils.PathFinder;
import com.watabou.utils.Point;
import com.watabou.utils.Random; import com.watabou.utils.Random;
import com.watabou.utils.Rect; import com.watabou.utils.Rect;
@ -113,15 +114,12 @@ public class RegularPainter extends Painter {
if (door == null) { if (door == null) {
Rect i = r.intersect( n ); Rect i = r.intersect( n );
if (i.width() == 0) { ArrayList<Point> doorSpots = new ArrayList<>();
door = new Room.Door( for (Point p : i.getPoints()){
i.left, if (r.canConnect(p) && n.canConnect(p))
Random.Int( i.top + 1, i.bottom ) ); doorSpots.add(p);
} else {
door = new Room.Door(
Random.Int( i.left + 1, i.right ),
i.top);
} }
door = new Room.Door(Random.element(doorSpots));
r.connected.put( n, door ); r.connected.put( n, door );
n.connected.put( r, door ); n.connected.put( r, door );

View File

@ -190,20 +190,38 @@ public class Room extends Rect implements Graph.Node, Bundlable {
//TODO make abstract //TODO make abstract
public int maxConnections(int direction){ return -1; } public int maxConnections(int direction){ return -1; }
//only considers point-specific limits, not direction limits
public boolean canConnect(Point p){
//point must be along exactly one edge, no corners.
return (p.x == left || p.x == right) != (p.y == top || p.y == bottom);
}
//only considers direction limits, not point-specific limits
public boolean canConnect(int direction){ public boolean canConnect(int direction){
return remConnections(direction) > 0; return remConnections(direction) > 0;
} }
//considers both direction and point limits
public boolean canConnect( Room r ){ public boolean canConnect( Room r ){
Rect i = intersect( r ); Rect i = intersect( r );
boolean foundPoint = false;
for (Point p : i.getPoints()){
if (canConnect(p) && r.canConnect(p)){
foundPoint = true;
break;
}
}
if (!foundPoint) return false;
if (i.width() == 0 && i.left == left) if (i.width() == 0 && i.left == left)
return canConnect(LEFT); return canConnect(LEFT) && r.canConnect(LEFT);
else if (i.height() == 0 && i.top == top) else if (i.height() == 0 && i.top == top)
return canConnect(TOP); return canConnect(TOP) && r.canConnect(TOP);
else if (i.width() == 0 && i.right == right) else if (i.width() == 0 && i.right == right)
return canConnect(RIGHT); return canConnect(RIGHT) && r.canConnect(RIGHT);
else if (i.height() == 0 && i.bottom == bottom) else if (i.height() == 0 && i.bottom == bottom)
return canConnect(BOTTOM); return canConnect(BOTTOM) && r.canConnect(BOTTOM);
else else
return false; return false;
} }
@ -224,7 +242,7 @@ public class Room extends Rect implements Graph.Node, Bundlable {
public boolean connect( Room room ) { public boolean connect( Room room ) {
if ((neigbours.contains(room) || addNeigbour(room)) if ((neigbours.contains(room) || addNeigbour(room))
&& !connected.containsKey( room ) && canConnect(room) && room.canConnect(this)) { && !connected.containsKey( room ) && canConnect(room)) {
connected.put( room, null ); connected.put( room, null );
room.connected.put( this, null ); room.connected.put( this, null );
return true; return true;
@ -288,6 +306,10 @@ public class Room extends Rect implements Graph.Node, Bundlable {
} }
public Type type = Type.EMPTY; public Type type = Type.EMPTY;
public Door( Point p ){
super(p);
}
public Door( int x, int y ) { public Door( int x, int y ) {
super( x, y ); super( x, y );
} }