-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate MinecraftComposeGui mono file into multiple files
- Loading branch information
Showing
3 changed files
with
110 additions
and
95 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
silk-compose/src/main/kotlin/net/silkmc/silk/compose/GuiChunk.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.silkmc.silk.compose | ||
|
||
import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket | ||
import net.minecraft.world.level.saveddata.maps.MapItemSavedData | ||
import net.silkmc.silk.compose.internal.MapIdGenerator | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
internal class GuiChunk( | ||
val mapId: Int = MapIdGenerator.nextId(), | ||
private val colors: ByteArray = ByteArray(128 * 128), | ||
) { | ||
private var dirty = false | ||
private var startX = 0 | ||
private var startY = 0 | ||
private var endX = 0 | ||
private var endY = 0 | ||
|
||
fun setColor(x: Int, y: Int, colorId: Byte) { | ||
val previousColorId = colors[x + y * 128] | ||
|
||
if (previousColorId != colorId) { | ||
colors[x + y * 128] = colorId | ||
|
||
if (dirty) { | ||
startX = min(startX, x); startY = min(startY, y) | ||
endX = max(endX, x); endY = max(endY, y) | ||
} else { | ||
dirty = true | ||
startX = x; startY = y | ||
endX = x; endY = y | ||
} | ||
} | ||
} | ||
|
||
fun createPacket(): ClientboundMapItemDataPacket? { | ||
if (!dirty) return null | ||
dirty = false | ||
|
||
val width = endX + 1 - startX | ||
val height = endY + 1 - startY | ||
val packetColors = ByteArray(width * height) | ||
|
||
for (x in 0 until width) { | ||
for (y in 0 until height) { | ||
packetColors[x + y * width] = colors[startX + x + (startY + y) * 128] | ||
} | ||
} | ||
|
||
val updateData = MapItemSavedData.MapPatch(startX, startY, width, height, packetColors) | ||
return ClientboundMapItemDataPacket(mapId, 0, false, null, updateData) | ||
} | ||
|
||
fun createClearPacket(): ClientboundMapItemDataPacket { | ||
val updateData = MapItemSavedData.MapPatch(0, 0, 128, 128, ByteArray(128 * 128)) | ||
return ClientboundMapItemDataPacket(mapId, 0, false, null, updateData) | ||
} | ||
} |
This file contains 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
47 changes: 47 additions & 0 deletions
47
silk-compose/src/main/kotlin/net/silkmc/silk/compose/util/MathUtil.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package net.silkmc.silk.compose.util | ||
|
||
import net.minecraft.core.BlockPos | ||
import net.minecraft.core.Direction | ||
import net.minecraft.core.Vec3i | ||
import net.minecraft.world.phys.Vec3 | ||
import org.jetbrains.kotlinx.multik.api.linalg.dot | ||
import org.jetbrains.kotlinx.multik.api.mk | ||
import org.jetbrains.kotlinx.multik.api.ndarray | ||
import org.jetbrains.kotlinx.multik.ndarray.data.D1Array | ||
import org.jetbrains.kotlinx.multik.ndarray.data.get | ||
import org.jetbrains.kotlinx.multik.ndarray.operations.minus | ||
import org.jetbrains.kotlinx.multik.ndarray.operations.times | ||
|
||
internal object MathUtil { | ||
|
||
fun Vec3.toMkArray() = mk.ndarray(doubleArrayOf(x, y, z)) | ||
fun Vec3i.toMkArray() = mk.ndarray(doubleArrayOf(x.toDouble(), y.toDouble(), z.toDouble())) | ||
|
||
// taken from http://rosettacode.org/wiki/Find_the_intersection_of_a_line_with_a_plane#Kotlin | ||
fun rayPlaneIntersection( | ||
rayPoint: D1Array<Double>, | ||
rayVector: D1Array<Double>, | ||
planePoint: D1Array<Double>, | ||
planeNormal: D1Array<Double>, | ||
): D1Array<Double> { | ||
val diff = rayPoint - planePoint | ||
val prod1 = diff dot planeNormal | ||
val prod2 = rayVector dot planeNormal | ||
val prod3 = prod1 / prod2 | ||
return rayPoint - rayVector * prod3 | ||
} | ||
|
||
fun BlockPos.withoutAxis(axis: Direction.Axis) = when (axis) { | ||
Direction.Axis.X -> z.toDouble() to y.toDouble() | ||
Direction.Axis.Z -> x.toDouble() to y.toDouble() | ||
// just for the compiler | ||
Direction.Axis.Y -> x.toDouble() to z.toDouble() | ||
} | ||
|
||
fun D1Array<Double>.withoutAxis(axis: Direction.Axis) = when (axis) { | ||
Direction.Axis.X -> this[2] to this[1] | ||
Direction.Axis.Z -> this[0] to this[1] | ||
// just for the compiler | ||
Direction.Axis.Y -> this[0] to this[2] | ||
} | ||
} |