diff --git a/roguelike-starterkit/src/main/scala/roguelikestarterkit/starterkit/package.scala b/roguelike-starterkit/src/main/scala/roguelikestarterkit/starterkit/package.scala index d66e8d6f..d589ec59 100644 --- a/roguelike-starterkit/src/main/scala/roguelikestarterkit/starterkit/package.scala +++ b/roguelike-starterkit/src/main/scala/roguelikestarterkit/starterkit/package.scala @@ -1,5 +1,23 @@ package roguelikestarterkit +import indigo.shared.collections.Batch +import indigo.shared.datatypes.Point +import indigo.shared.datatypes.Rectangle + +object syntax: + + extension (r: Rectangle) + def toPoints: Batch[Point] = + Batch.fromIndexedSeq( + (0 until r.height).flatMap { y => + (0 until r.width).map { x => + Point(r.x + x, r.y + y) + } + } + ) + +end syntax + // Terminal type TerminalEmulator = terminal.TerminalEmulator diff --git a/roguelike-starterkit/src/test/scala/roguelikestarterkit/starterkit/SyntaxTests.scala b/roguelike-starterkit/src/test/scala/roguelikestarterkit/starterkit/SyntaxTests.scala new file mode 100644 index 00000000..faf6977f --- /dev/null +++ b/roguelike-starterkit/src/test/scala/roguelikestarterkit/starterkit/SyntaxTests.scala @@ -0,0 +1,52 @@ +package roguelikestarterkit.starterkit + +import indigo.* +import roguelikestarterkit.syntax.toPoints + +class SyntaxTests extends munit.FunSuite { + + test("Can convert a rectangle to a grid of points") { + val actual = Rectangle(0,0,4,3).toPoints + + val expected = + Batch( + Point(0, 0), + Point(1, 0), + Point(2, 0), + Point(3, 0), + Point(0, 1), + Point(1, 1), + Point(2, 1), + Point(3, 1), + Point(0, 2), + Point(1, 2), + Point(2, 2), + Point(3, 2), + ) + + assertEquals(actual, expected) + } + + test("Can convert a offset rectangle to a grid of points") { + val actual = Rectangle(10,20,4,3).toPoints + + val expected = + Batch( + Point(10 + 0, 20 + 0), + Point(10 + 1, 20 + 0), + Point(10 + 2, 20 + 0), + Point(10 + 3, 20 + 0), + Point(10 + 0, 20 + 1), + Point(10 + 1, 20 + 1), + Point(10 + 2, 20 + 1), + Point(10 + 3, 20 + 1), + Point(10 + 0, 20 + 2), + Point(10 + 1, 20 + 2), + Point(10 + 2, 20 + 2), + Point(10 + 3, 20 + 2), + ) + + assertEquals(actual, expected) + } + +}