diff --git a/src/commonMain/kotlin/Block.kt b/src/commonMain/kotlin/Block.kt index 74a3773..b564e25 100644 --- a/src/commonMain/kotlin/Block.kt +++ b/src/commonMain/kotlin/Block.kt @@ -3,62 +3,46 @@ import korlibs.korge.input.* import korlibs.korge.view.* import korlibs.korge.view.align.* import korlibs.math.geom.* -import korlibs.math.random.* -import kotlin.collections.random import kotlin.math.* import kotlin.properties.* +var cell11: RoundRect by Delegates.notNull() +var cell12: RoundRect by Delegates.notNull() +var cell13: RoundRect by Delegates.notNull() +var cell21: RoundRect by Delegates.notNull() +var cell22: RoundRect by Delegates.notNull() +var cell23: RoundRect by Delegates.notNull() +var cell31: RoundRect by Delegates.notNull() +var cell32: RoundRect by Delegates.notNull() +var cell33: RoundRect by Delegates.notNull() + +//var template: Container by Delegates.notNull() +//var template:MutableList by Delegates.notNull() enum class BlockType { // Single block ONE_BY_ONE, // Straight blocks (all rotations are the same) - ONE_BY_TWO, - TWO_BY_ONE, - ONE_BY_THREE, - THREE_BY_ONE, - ONE_BY_FOUR, - FOUR_BY_ONE, - ONE_BY_FIVE, - FIVE_BY_ONE, + ONE_BY_TWO, TWO_BY_ONE, ONE_BY_THREE, THREE_BY_ONE, ONE_BY_FOUR, FOUR_BY_ONE, ONE_BY_FIVE, FIVE_BY_ONE, // Square blocks (all rotations are the same) - TWO_BY_TWO, - THREE_BY_THREE, + TWO_BY_TWO, THREE_BY_THREE, // L-Shaped blocks (all rotations) - L_2X2_0, - L_2X2_90, - L_2X2_180, - L_2X2_270, + L_2X2_0, L_2X2_90, L_2X2_180, L_2X2_270, - L_2X3_0, - L_2X3_90, - L_2X3_180, - L_2X3_270, + L_2X3_0, L_2X3_90, L_2X3_180, L_2X3_270, - L_3X3_0, - L_3X3_90, - L_3X3_180, - L_3X3_270, + L_3X3_0, L_3X3_90, L_3X3_180, L_3X3_270, // T-Shaped blocks (all rotations) - T_2X3_0, - T_2X3_90, - T_2X3_180, - T_2X3_270, + T_2X3_0, T_2X3_90, T_2X3_180, T_2X3_270, // Rectangle blocks (all rotations) - TWO_BY_THREE_0, - TWO_BY_THREE_90, - THREE_BY_TWO_0, - THREE_BY_TWO_90, + TWO_BY_THREE_0, TWO_BY_THREE_90, THREE_BY_TWO_0, THREE_BY_TWO_90, // S-Shaped blocks (all rotations) - S_2X3_0, - S_2X3_90, - S_2X3_180, - S_2X3_270 + S_2X3_0, S_2X3_90, S_2X3_180, S_2X3_270 } @@ -87,31 +71,43 @@ object BlockColors { } } -fun Container.block(color: RGBA, blockType: BlockType, startPosition: StartPosition) = +fun Container.block(color: RGBA, blockType: Array>, startPosition: StartPosition) = Block(color, blockType, startPosition).addTo(this) -class Block(private var color: RGBA, blockType: BlockType, startPosition: StartPosition) : Container() { +class Block(private var color: RGBA, blockType: Array>, startPosition: StartPosition) : Container() { private var placed: Boolean = false private var master: RoundRect by Delegates.notNull() init { + val templateColor = Colors.PURPLE + + cell11 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor) + + cell12 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignLeftToRightOf(cell11) + cell13 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignLeftToRightOf(cell12) + cell21 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignTopToBottomOf(cell11) + cell22 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignLeftToRightOf(cell21) + .alignTopToBottomOf(cell12) + cell23 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignLeftToRightOf(cell22) + .alignTopToBottomOf(cell13) + cell31 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignTopToBottomOf(cell21) + cell32 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignLeftToRightOf(cell31) + .alignTopToBottomOf(cell22) + cell33 = roundRect(Size(cs, cs), RectCorners(5f), fill = templateColor).alignLeftToRightOf(cell32) + .alignTopToBottomOf(cell23) + when (startPosition) { StartPosition.LEFT -> this.position(windowWidth * 0.2, windowHeight * 0.8) StartPosition.MIDDLE -> this.position(windowWidth * 0.4, windowHeight * 0.8) StartPosition.RIGHT -> this.position(windowWidth * 0.6, windowHeight * 0.8) } - when (blockType) { - BlockType.ONE_BY_ONE -> oneByOne(this) - BlockType.TWO_BY_TWO -> twoByTwo(this) - BlockType.L_3X3_0 -> bigL(this) - else -> bigL(this) - } + + arrayTest(this, blockType) this.scale(0.5) var closeable: DraggableCloseable? = null closeable = this.draggableCloseable { it -> - //println("viewNextX: ${round(master!!.getPositionRelativeTo(first!!).x).toInt()}, viewNextY: ${round(master!!.getPositionRelativeTo(first!!).y).toInt()}") if (it.start) { println("master: $master") @@ -123,10 +119,10 @@ class Block(private var color: RGBA, blockType: BlockType, startPosition: StartP if (it.end) { this.zIndex(0) println("dragging ended: snapping!") - println("viewNextX: ${master.globalPos.x}, viewNextY: ${master.globalPos.y}") + println("viewNextX: ${this[0].globalPos.x}, viewNextY: ${this[0].globalPos.y}") val blockPosition1 = Point( - convertToCoordinateX(round(master.globalPos.x).toInt()), convertToCoordinateY( - round(master.globalPos.y).toInt() + convertToCoordinateX(round(this[0].globalPos.x).toInt()), convertToCoordinateY( + round(this[0].globalPos.y).toInt() ) ) for (field in fields) { @@ -141,7 +137,7 @@ class Block(private var color: RGBA, blockType: BlockType, startPosition: StartP if (blockPosition1 == fieldPosition && checkIfCorrectlyPlaced(this)) { println("Placed correctly snapping:") - master.globalPos = field.globalPos + this[0].globalPos = field.globalPos println("this global: ${this.globalPos}") this.forEachChild { sContainer.placedBlock( @@ -183,75 +179,51 @@ class Block(private var color: RGBA, blockType: BlockType, startPosition: StartP } } - private fun oneByOne(container: Container) { - val one = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color) - master = one - } - - private fun twoByTwo(container: Container) { - val one = container.roundRect(Size(cs, cs), RectCorners(5f), fill = Colors.RED) - master = one - container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(one) - val three = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(one) - container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(three) - .alignTopToBottomOf(one) - } - - - private fun bigL(container: Container) { - val rotation = random.get(range = 0..3) - println("Rotation: $rotation") - container.size = Size(cs * 3, cs * 3) - - - when (rotation) { - 0 -> { - // Original: vertical line + rightward tail - val one = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color) - val two = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(one) - val three = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(two) - val four = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(three) - container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(four) - master = three - } - - 1 -> { - // 90°: horizontal line + upward tail - val one = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color) - val two = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(one) - container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(two) - val four = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(two) - container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(four) - master = two - } - - 2 -> { - // 180°: vertical line + leftward tail - val one = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color) - val two = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(one) - val three = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignTopToBottomOf(two) - val four = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignRightToLeftOf(one) - container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignRightToLeftOf(four) - master = three - } - - 3 -> { - // 270°: horizontal line + downward tail - val one = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color) - val two = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(one) - val three = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignLeftToRightOf(two) - val four = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignBottomToTopOf(two) - val five = container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).alignBottomToTopOf(four) - master = one - master.color = Colors.CHOCOLATE - } + private fun arrayTest(container: Container, array: Array>) { + if (array[0][0] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell11) } - //master!!.color = Colors.RED - } + if (array[0][1] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell12) + } + if (array[0][2] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell13) + } + if (array[1][0] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell21) + } + if (array[1][1] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell22) + } + if (array[1][2] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell23) + } + if (array[2][0] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell31) + } + if (array[2][1] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell32) + } + if (array[2][2] == 1) { + container.roundRect(Size(cs, cs), RectCorners(5f), fill = color).centerOn(cell33) + } + println("c: ${container[0].globalPos}") + if (container[0] is RoundRect) master = container[0] as RoundRect + println(container[9]) + cell11.removeFromParent() + cell12.removeFromParent() + cell13.removeFromParent() + cell21.removeFromParent() + cell22.removeFromParent() + cell23.removeFromParent() + cell31.removeFromParent() + cell32.removeFromParent() + cell33.removeFromParent() + } } fun checkIfCorrectlyPlaced(wholeBlock: Block): Boolean { diff --git a/src/commonMain/kotlin/allblocks.kt b/src/commonMain/kotlin/allblocks.kt new file mode 100644 index 0000000..c892647 --- /dev/null +++ b/src/commonMain/kotlin/allblocks.kt @@ -0,0 +1,166 @@ +import kotlin.properties.* + +var allBlockTypes: List>> by Delegates.notNull() + +// === SINGLE BLOCKS === +val ONE_BY_ONE = arrayOf( + arrayOf(1, 0, 0), arrayOf(0, 0, 0), arrayOf(0, 0, 0) +) + +val ONE_BY_TWO = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 0, 0), arrayOf(0, 0, 0) +) + +val TWO_BY_ONE = arrayOf( + arrayOf(1, 1, 0), arrayOf(0, 0, 0), arrayOf(0, 0, 0) +) + +val ONE_BY_THREE = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 0, 0), arrayOf(1, 0, 0) +) + +val THREE_BY_ONE = arrayOf( + arrayOf(1, 1, 1), arrayOf(0, 0, 0), arrayOf(0, 0, 0) +) + +val ONE_BY_FOUR = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 0, 0), arrayOf(1, 0, 0) // cut off after 3 (fits 3x3) +) + +val FOUR_BY_ONE = arrayOf( + arrayOf(1, 1, 1), arrayOf(0, 0, 0), arrayOf(0, 0, 0) // cut off after 3 +) + +val ONE_BY_FIVE = ONE_BY_THREE // same logic +val FIVE_BY_ONE = THREE_BY_ONE + +val TWO_BY_TWO = arrayOf( + arrayOf(1, 1, 0), arrayOf(1, 1, 0), arrayOf(0, 0, 0) +) + +val THREE_BY_THREE = arrayOf( + arrayOf(1, 1, 1), arrayOf(1, 1, 1), arrayOf(1, 1, 1) +) + +val L_2X2_0 = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 1, 0), arrayOf(0, 0, 0) +) + +val L_2X2_90 = arrayOf( + arrayOf(1, 1, 0), arrayOf(0, 1, 0), arrayOf(0, 0, 0) +) + +val L_2X2_180 = L_2X2_90 +val L_2X2_270 = L_2X2_0 + +val L_2X3_0 = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 0, 0), arrayOf(1, 1, 0) +) + +val L_2X3_90 = arrayOf( + arrayOf(0, 0, 1), arrayOf(1, 1, 1), arrayOf(0, 0, 0) +) + +val L_2X3_180 = arrayOf( + arrayOf(1, 1, 0), arrayOf(0, 1, 0), arrayOf(0, 1, 0) +) + +val L_2X3_270 = arrayOf( + arrayOf(1, 1, 1), arrayOf(1, 0, 0), arrayOf(0, 0, 0) +) + +val L_3X3_0 = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 0, 0), arrayOf(1, 1, 1) +) + +val L_3X3_90 = arrayOf( + arrayOf(1, 1, 1), arrayOf(0, 0, 1), arrayOf(0, 0, 1) +) + +val L_3X3_180 = arrayOf( + arrayOf(1, 1, 1), arrayOf(0, 0, 1), arrayOf(0, 0, 1) +) + +val L_3X3_270 = arrayOf( + arrayOf(1, 1, 1), arrayOf(1, 0, 0), arrayOf(1, 0, 0) +) + +val T_2X3_0 = arrayOf( + arrayOf(1, 1, 1), arrayOf(0, 1, 0), arrayOf(0, 0, 0) +) + +val T_2X3_90 = arrayOf( + arrayOf(0, 1, 0), arrayOf(1, 1, 0), arrayOf(0, 1, 0) +) + +val T_2X3_180 = arrayOf( + arrayOf(0, 1, 0), arrayOf(1, 1, 1), arrayOf(0, 0, 0) +) + +val T_2X3_270 = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 1, 0), arrayOf(1, 0, 0) +) + +val TWO_BY_THREE_0 = arrayOf( + arrayOf(1, 1, 1), arrayOf(1, 1, 1), arrayOf(0, 0, 0) +) + +val TWO_BY_THREE_90 = arrayOf( + arrayOf(1, 1, 0), arrayOf(1, 1, 0), arrayOf(1, 1, 0) +) + +val THREE_BY_TWO_0 = TWO_BY_THREE_90 +val THREE_BY_TWO_90 = TWO_BY_THREE_0 + +val S_2X3_0 = arrayOf( + arrayOf(0, 1, 1), arrayOf(1, 1, 0), arrayOf(0, 0, 0) +) + +val S_2X3_90 = arrayOf( + arrayOf(1, 0, 0), arrayOf(1, 1, 0), arrayOf(0, 1, 0) +) + +val S_2X3_180 = S_2X3_0 +val S_2X3_270 = S_2X3_90 + + +// === INIT FUNCTION === +fun initBlockTypes() { + allBlockTypes = listOf( + ONE_BY_ONE, + ONE_BY_TWO, + TWO_BY_ONE, + ONE_BY_THREE, + THREE_BY_ONE, + ONE_BY_FOUR, + FOUR_BY_ONE, + ONE_BY_FIVE, + FIVE_BY_ONE, + TWO_BY_TWO, + THREE_BY_THREE, + L_2X2_0, + L_2X2_90, + L_2X2_180, + L_2X2_270, + L_2X3_0, + L_2X3_90, + L_2X3_180, + L_2X3_270, + L_3X3_0, + L_3X3_90, + L_3X3_180, + L_3X3_270, + T_2X3_0, + T_2X3_90, + T_2X3_180, + T_2X3_270, + TWO_BY_THREE_0, + TWO_BY_THREE_90, + THREE_BY_TWO_0, + THREE_BY_TWO_90, + S_2X3_0, + S_2X3_90, + S_2X3_180, + S_2X3_270 + ) +} diff --git a/src/commonMain/kotlin/main.kt b/src/commonMain/kotlin/main.kt index aa881d9..00ba6ce 100644 --- a/src/commonMain/kotlin/main.kt +++ b/src/commonMain/kotlin/main.kt @@ -30,9 +30,6 @@ val allBlocks = mutableListOf() var placedBlocks = mutableListOf() val random: Random = SecureRandom var first: Field? = null -var leftStart:RoundRect by Delegates.notNull() -val middleStart:RoundRect by Delegates.notNull() -val rightStart: RoundRect by Delegates.notNull() suspend fun main() = Korge( //windowHeight = windowHeight, windowSize = Size(windowWidth, windowHeight), @@ -64,9 +61,7 @@ suspend fun main() = Korge( //backgroundField!!.y -= 70 populateField(this) - leftStart = roundRect(Size(cs*1.5, cs*1.5), RectCorners(5f), Colors.PURPLE) - .position(windowWidth*0.2, windowHeight*0.8) - + initBlockTypes() //val testBlock = block(BlockColors.Red, BlockType.TWObyTWO, StartPosition.LEFT) createPieces(this) @@ -143,7 +138,7 @@ fun createPieces(container: Container) { } } val color = BlockColors.getRandomColor() - val c = container.block(color, BlockTypeHelper.getRandomBlockType(), location!!) + val c = container.block(color, allBlockTypes.random(), location!!) allBlocks.add(c) } @@ -164,7 +159,7 @@ fun checkForBlast() { counter++ } } - println("Row: $rowY, counter: $counter") + //println("Row: $rowY, counter: $counter") if (counter == 8) { for (block in checkedBlocks) { val occupiedField = fields.find { it.pos == block.pos } @@ -188,7 +183,7 @@ fun checkForBlast() { counter++ } } - println("Row: $columnX, counter: $counter") + //println("Row: $columnX, counter: $counter") if (counter == 8) { for (block in checkedBlocks) { val occupiedField = fields.find { it.pos == block.pos }