From 7705f24c1411026b516b6542b0a29f27a333f38f Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sun, 23 Jul 2017 22:08:45 -0400 Subject: [PATCH] v0.6.1: completely reimplemented tunnel rooms --- .../levels/rooms/connection/TunnelRoom.java | 156 ++++++++---------- 1 file changed, 70 insertions(+), 86 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/TunnelRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/TunnelRoom.java index b1c20e09b..78fcf24bf 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/TunnelRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/TunnelRoom.java @@ -23,103 +23,87 @@ package com.shatteredpixel.shatteredpixeldungeon.levels.rooms.connection; import com.shatteredpixel.shatteredpixeldungeon.levels.Level; import com.shatteredpixel.shatteredpixeldungeon.levels.painters.Painter; +import com.watabou.utils.GameMath; import com.watabou.utils.Point; +import com.watabou.utils.PointF; import com.watabou.utils.Random; +import com.watabou.utils.Rect; //tunnels along the rooms center, with straight lines public class TunnelRoom extends ConnectionRoom { - + public void paint(Level level) { - + int floor = level.tunnelTile(); - - Point c = center(); - - if (width() > height() || (width() == height() && Random.Int( 2 ) == 0)) { - - int from = right - 1; - int to = left + 1; - - for (Door door : connected.values()) { - - int step = door.y < c.y ? +1 : -1; - - if (door.x == left) { - - from = left + 1; - for (int i=door.y; i != c.y; i += step) { - Painter.set( level, from, i, floor ); - } - - } else if (door.x == right) { - - to = right - 1; - for (int i=door.y; i != c.y; i += step) { - Painter.set( level, to, i, floor ); - } - - } else { - if (door.x < from) { - from = door.x; - } - if (door.x > to) { - to = door.x; - } - - for (int i=door.y+step; i != c.y; i += step) { - Painter.set( level, door.x, i, floor ); - } - } - } - - for (int i=from; i <= to; i++) { - Painter.set( level, i, c.y, floor ); - } - - } else { - - int from = bottom - 1; - int to = top + 1; - - for (Door door : connected.values()) { - - int step = door.x < c.x ? +1 : -1; - - if (door.y == top) { - - from = top + 1; - for (int i=door.x; i != c.x; i += step) { - Painter.set( level, i, from, floor ); - } - - } else if (door.y == bottom) { - - to = bottom - 1; - for (int i=door.x; i != c.x; i += step) { - Painter.set( level, i, to, floor ); - } - - } else { - if (door.y < from) { - from = door.y; - } - if (door.y > to) { - to = door.y; - } - - for (int i=door.x+step; i != c.x; i += step) { - Painter.set( level, i, door.y, floor ); - } - } - } - - for (int i=from; i <= to; i++) { - Painter.set( level, c.x, i, floor ); + + Rect c = getConnectionSpace(); + + for (Door door : connected.values()) { + + Point start; + Point mid; + Point end; + + start = new Point(door); + if (start.x == left) start.x++; + else if (start.y == top) start.y++; + else if (start.x == right) start.x--; + else if (start.y == bottom) start.y--; + + int rightShift; + int downShift; + + if (start.x < c.left) rightShift = c.left - start.x; + else if (start.x > c.right) rightShift = c.right - start.x; + else rightShift = 0; + + if (start.y < c.top) downShift = c.top - start.y; + else if (start.y > c.bottom) downShift = c.bottom - start.y; + else downShift = 0; + + //always goes inward first + if (door.x == left || door.x == right){ + mid = new Point(start.x + rightShift, start.y); + end = new Point(mid.x, mid.y + downShift); + + } else { + mid = new Point(start.x, start.y + downShift); + end = new Point(mid.x + rightShift, mid.y); + } + + Painter.drawLine( level, start, mid, floor ); + Painter.drawLine( level, mid, end, floor ); } - + for (Door door : connected.values()) { door.set( Door.Type.TUNNEL ); } } + + //returns the space which all doors must connect to (usually 1 cell, but can be more) + //Note that, like rooms, this space is inclusive to its right and bottom sides + protected Rect getConnectionSpace(){ + Point c = getDoorCenter(); + + return new Rect(c.x, c.y, c.x, c.y); + } + + //returns a point equidistant from all doors this room has + protected final Point getDoorCenter(){ + PointF doorCenter = new PointF(0, 0); + + for (Door door : connected.values()) { + doorCenter.x += door.x; + doorCenter.y += door.y; + } + + Point c = new Point((int)doorCenter.x / connected.size(), (int)doorCenter.y / connected.size()); + if (Random.Float() < doorCenter.x % 1) c.x++; + if (Random.Float() < doorCenter.y % 1) c.y++; + c.x = (int)GameMath.gate(left+1, c.x, right-1); + c.y = (int)GameMath.gate(top+1, c.y, bottom-1); + + return c; + } }