Last active
March 23, 2019 13:40
-
-
Save jessearmand/5efd79410fe33721de8b8e2197936b07 to your computer and use it in GitHub Desktop.
Node mutation. Testing how a class is being mutated on Kotlin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You can edit, run, and share this code. | |
* play.kotlinlang.org | |
*/ | |
class Node(name: String) { | |
var position: FloatArray = floatArrayOf(0.0f, 0.0f, 0.0f) | |
var rotation: FloatArray = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f) | |
var name: String = name | |
var children: Array<Node> = emptyArray() | |
} | |
fun main() { | |
val left = Node(name = "left") | |
val nodeA = Node(name = "A") | |
val nodeB = Node(name = "B") | |
left.children = arrayOf(nodeA, nodeB) | |
val right = Node(name = "right") | |
right.children = arrayOf(nodeA, nodeB) | |
val leftTree = arrayOf(left, right) | |
val rightTree = arrayOf(right, left) | |
val printNode = { it: Node -> | |
println("parent: ${it.name}") | |
it.children.forEach { | |
println("child is ${it.name}") | |
println("position: ${it.position[0]}, ${it.position[1]}, ${it.position[2]}") | |
println("rotation: ${it.rotation[0]}, ${it.rotation[1]}, ${it.rotation[2]}, ${it.rotation[3]}") | |
} | |
} | |
println("left tree:") | |
leftTree.forEach(printNode) | |
println("===============") | |
println("Start mutating:") | |
println("===============") | |
val transform = { it: Node -> | |
it.children.forEachIndexed { index, child -> | |
child.name = "$index" | |
child.position = floatArrayOf(1.0f, 1.0f, 1.0f + index) | |
child.rotation = floatArrayOf(1.0f, 1.0f, 1.0f, 0.7f + index) | |
} | |
} | |
println("right tree:") | |
rightTree.forEach(transform) | |
val nodeC = nodeA | |
nodeC.name = "C" | |
println("node A: ${nodeA.name}") | |
rightTree.forEach(printNode) | |
println("===============") | |
println("Result:") | |
println("===============") | |
println("left tree:") | |
leftTree.forEach(printNode) | |
println("right tree:") | |
rightTree.forEach(printNode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment