Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 85 additions & 113 deletions src/commonMain/kotlin/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
FSaurenbach marked this conversation as resolved.

//var template: Container by Delegates.notNull()
//var template:MutableList<RoundRect> 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
}


Expand Down Expand Up @@ -87,31 +71,43 @@ object BlockColors {
}
}

fun Container.block(color: RGBA, blockType: BlockType, startPosition: StartPosition) =
fun Container.block(color: RGBA, blockType: Array<Array<Int>>, 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<Array<Int>>, 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")
Expand All @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -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<Array<Int>>) {

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 {
Expand Down
Loading