Parcourir la source

Add Zone::extend and Zone::getExternalBorder

smelc il y a 7 ans
Parent
commit
10b3cd6124

+ 13 - 0
squidlib-util/src/main/java/squidpony/squidgrid/mapping/Rectangle.java

@@ -413,6 +413,19 @@ public interface Rectangle extends Zone {
 			return new Impl(bottomLeft.translate(x, y), width, height);
 		}
 
+		@Override
+		public Collection<Coord> getExternalBorder() {
+			final List<Coord> result = new ArrayList<Coord>((width + height) * 2);
+			for (Direction dir : Direction.CARDINALS)
+				Utils.getBorder(this, dir, result);
+			return result;
+		}
+
+		@Override
+		public Rectangle extend() {
+			return new Rectangle.Impl(bottomLeft.translate(Direction.DOWN_LEFT), width + 2, height + 2);
+		}
+
 		@Override
 		public Iterator<Coord> iterator() {
 			/* Do not rely on getAll(), to avoid allocating the list */

+ 26 - 0
squidlib-util/src/main/java/squidpony/squidgrid/zone/Zone.java

@@ -1,9 +1,11 @@
 package squidpony.squidgrid.zone;
 
+import squidpony.squidgrid.mapping.DungeonUtility;
 import squidpony.squidmath.Coord;
 
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
@@ -125,6 +127,16 @@ public interface Zone extends Serializable, Iterable<Coord> {
 	/** @return {@code this} shifted by {@code (x,y)} */
 	Zone translate(int x, int y);
 
+	/** @return Cells adjacent to {@code this} that aren't in {@code this} */
+	Collection<Coord> getExternalBorder();
+
+	/**
+	 * @return A variant of {@code this} where cells adjacent to {@code this}
+	 *         have been added (i.e. it's {@code this} plus
+	 *         {@link #getExternalBorder()}).
+	 */
+	Zone extend();
+
     /**
      * A convenience partial implementation. Please try for all new
      * implementations of {@link Zone} to be subtypes of this class. It usually
@@ -257,6 +269,20 @@ public interface Zone extends Serializable, Iterable<Coord> {
 			return new ListZone(shifted);
 		}
 
+		@Override
+		/* Convenience implementation, feel free to override. */
+		public Collection<Coord> getExternalBorder() {
+			return DungeonUtility.border(getAll(), null);
+		}
+
+		@Override
+		/* Convenience implementation, feel free to override. */
+		public Zone extend() {
+			final List<Coord> list = new ArrayList<Coord>(getAll());
+			list.addAll(getExternalBorder());
+			return new ListZone(list);
+		}
+
 		private int smallest(boolean xOrY) {
 			if (isEmpty())
 				return -1;