From ab639a0125c50f4063d94510fe2d1382ac3786d4 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 27 Jul 2017 19:24:12 -0400 Subject: [PATCH] v0.6.1: added a method to painter to allow ellipse shaped fills --- .../levels/CavesBossLevel.java | 47 +++++++------------ .../levels/painters/Painter.java | 41 ++++++++++++++++ 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/CavesBossLevel.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/CavesBossLevel.java index 4e4e6e560..e32f33b76 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/CavesBossLevel.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/CavesBossLevel.java @@ -45,6 +45,7 @@ import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import com.watabou.utils.PathFinder; import com.watabou.utils.Random; +import com.watabou.utils.Rect; public class CavesBossLevel extends Level { @@ -58,8 +59,8 @@ public class CavesBossLevel extends Level { private static final int WIDTH = 32; private static final int HEIGHT = 32; - private static final int ROOM_LEFT = WIDTH / 2 - 2; - private static final int ROOM_RIGHT = WIDTH / 2 + 2; + private static final int ROOM_LEFT = WIDTH / 2 - 3; + private static final int ROOM_RIGHT = WIDTH / 2 + 1; private static final int ROOM_TOP = HEIGHT / 2 - 2; private static final int ROOM_BOTTOM = HEIGHT / 2 + 2; @@ -100,34 +101,20 @@ public class CavesBossLevel extends Level { @Override protected boolean build() { - setSize(32, 32); - - int topMost = Integer.MAX_VALUE; - - for (int i=0; i < 8; i++) { - int left, right, top, bottom; - if (Random.Int( 2 ) == 0) { - left = Random.Int( 1, ROOM_LEFT - 3 ); - right = ROOM_RIGHT + 3; - } else { - left = ROOM_LEFT - 3; - right = Random.Int( ROOM_RIGHT + 3, width() - 1 ); - } - if (Random.Int( 2 ) == 0) { - top = Random.Int( 2, ROOM_TOP - 3 ); - bottom = ROOM_BOTTOM + 3; - } else { - top = ROOM_LEFT - 3; - bottom = Random.Int( ROOM_TOP + 3, height() - 1 ); - } - - Painter.fill( this, left, top, right - left + 1, bottom - top + 1, Terrain.EMPTY ); - - if (top < topMost) { - topMost = top; - exit = Random.Int( left, right ) + (top - 1) * width(); - } - } + setSize(WIDTH, HEIGHT); + + Rect space = new Rect(); + + space.set( + Random.IntRange(2, 2 + (int)(width*0.2f)), + Random.IntRange(2, 2 + (int)(height*0.2f)), + Random.IntRange((int)(width * 0.8f - 2), width-2 ), + Random.IntRange((int)(height * 0.8f - 2), height-2 ) + ); + + Painter.fillEllipse( this, space, Terrain.EMPTY ); + + exit = space.left + space.width()/2 + (space.top - 1) * width(); map[exit] = Terrain.LOCKED_EXIT; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/Painter.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/Painter.java index ed7b79aa6..795671bfc 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/Painter.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/Painter.java @@ -96,6 +96,47 @@ public abstract class Painter { set(level, Math.round(x), Math.round(y), value); } } + + public static void fillEllipse(Level level, Rect rect, int value ) { + fillEllipse( level, rect.left, rect.top, rect.width(), rect.height(), value ); + } + + public static void fillEllipse(Level level, Rect rect, int m, int value ) { + fillEllipse( level, rect.left + m, rect.top + m, rect.width() - m*2, rect.height() - m*2, value ); + } + + public static void fillEllipse(Level level, int x, int y, int w, int h, int value){ + + //radii + double radH = h/2f; + double radW = w/2f; + + //fills each row of the ellipse from top to bottom + for( int i = 0; i < h; i++){ + + //y coordinate of the row for determining ellipsis width + //always want to test the middle of a tile, hence the 0.5 shift + double rowY = -radH + 0.5 + i; + + //equation is derived from ellipsis formula: y^2/radH^2 + x^2/radW^2 = 1 + //solves for x and then doubles to get the width + double rowW = 2.0 * Math.sqrt((radW * radW) * (1.0 - (rowY*rowY) / (radH * radH))); + + //need to round to nearest even or odd number, depending on width + if ( w % 2 == 0 ){ + rowW = Math.round(rowW / 2.0)*2.0; + + } else { + rowW = Math.floor(rowW / 2.0)*2.0; + rowW++; + } + + int cell = x + (w - (int)rowW)/2 + ((y + i) * level.width()); + Arrays.fill( level.map, cell, cell + (int)rowW, value ); + + } + + } public static Point drawInside( Level level, Room room, Point from, int n, int value ) {