Skip to content

Commit

Permalink
Removed inline classes to support negative tile values and fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed May 3, 2020
1 parent 5ed3f9a commit cba00b9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 49 deletions.
6 changes: 0 additions & 6 deletions engine/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`java-library`
}
Expand All @@ -10,8 +8,4 @@ dependencies {
implementation(project(":network"))
implementation("io.github.classgraph:classgraph:4.8.65")
implementation(kotlin("script-runtime"))
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
freeCompilerArgs = listOf("-XXLanguage:+InlineClasses")
}
20 changes: 8 additions & 12 deletions engine/src/main/kotlin/rs/dusk/engine/model/Chunk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ package rs.dusk.engine.model
* @author Greg Hibberd <greg@greghibberd.com>
* @since April 16, 2020
*/
inline class Chunk(val id: Int) {
data class Chunk(val x: Int, val y: Int) {

constructor(chunkX: Int, chunkY: Int) : this((chunkY and 0xfff) + ((chunkX and 0xfff) shl 12))
constructor(id: Int) : this(id shr 12, id and 0xfff)

val x: Int
get() = id shr 12
val id by lazy { (y and 0xfff) + ((x and 0xfff) shl 12) }
val region by lazy { Region(x / 8, y / 8) }
val tile by lazy { Tile(x * 8, y * 8, 0) }

val y: Int
get() = id and 0xfff

val region
get() = Region(x / 8, y / 8)

val tile
get() = Tile(x * 8, y * 8, 0)
companion object {
fun createSafe(x: Int, y: Int) = Chunk(x and 0xfff, y and 0xfff)
}
}
20 changes: 8 additions & 12 deletions engine/src/main/kotlin/rs/dusk/engine/model/Region.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ package rs.dusk.engine.model
* @author Greg Hibberd <greg@greghibberd.com>
* @since April 16, 2020
*/
inline class Region(val id: Int) {
data class Region(val x: Int, val y: Int) {

constructor(regionX: Int, regionY: Int) : this((regionY and 0xff) + ((regionX and 0xff) shl 8))
constructor(id: Int) : this(id shr 8, id and 0xff)

val x: Int
get() = id shr 8
val id by lazy { (y and 0xff) + ((x and 0xff) shl 8) }
val chunk by lazy { Chunk(x * 8, y * 8) }
val tile by lazy { Tile(x * 64, y * 64, 0) }

val y: Int
get() = id and 0xff

val chunk
get() = Chunk(x * 8, y * 8)

val tile
get() = Tile(x * 64, y * 64, 0)
companion object {
fun createSafe(x: Int, y: Int) = Region(x and 0xff, y and 0xff)
}
}
22 changes: 7 additions & 15 deletions engine/src/main/kotlin/rs/dusk/engine/model/Tile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,18 @@ package rs.dusk.engine.model
* @author Greg Hibberd <greg@greghibberd.com>
* @since March 28, 2020
*/
inline class Tile(val id: Int) {
data class Tile(val x: Int, val y: Int, val plane: Int = 0) {

constructor(x: Int, y: Int, plane: Int) : this((y and 0x3fff) + ((x and 0x3fff) shl 14) + ((plane and 0x3) shl 28))
constructor(id: Int) : this(id shr 14 and 0x3fff, id and 0x3fff, id shr 28)

val x: Int
get() = id shr 14 and 0x3fff
val id by lazy { (y and 0x3fff) + ((x and 0x3fff) shl 14) + ((plane and 0x3) shl 28) }
val chunk by lazy { Chunk(x / 8, y / 8) }
val region by lazy { Region(x / 64, y / 64) }

val y: Int
get() = id and 0x3fff

val plane: Int
get() = id shr 28

val chunk
get() = Chunk(x / 8, y / 8)
companion object {

val region
get() = Region(x / 64, y / 64)
fun createSafe(x: Int, y: Int, plane: Int = 0) = Tile(x and 0x3fff, y and 0x3fff, plane and 0x3)

companion object {
fun Tile.add(x: Int = 0, y: Int = 0, plane: Int = 0): Tile {
return Tile(this.x + x, this.y + y, this.plane + plane)
}
Expand Down
3 changes: 2 additions & 1 deletion engine/src/test/kotlin/rs/dusk/engine/event/EventBusTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.koin.test.mock.declareMock
import rs.dusk.engine.script.KoinMock
import kotlin.reflect.KClass

/**
* @author Greg Hibberd <greg@greghibberd.com>
Expand All @@ -34,7 +35,7 @@ internal class EventBusTest : KoinMock() {
// Then
verify {
bus.add<TestEvent>(any(), any())
// register(any<KClass<TestEvent>>(), any()).hint(Unit::class) FIXME
register(any<KClass<TestEvent>>(), any())
}
}

Expand Down
14 changes: 13 additions & 1 deletion engine/src/test/kotlin/rs/dusk/engine/model/ChunkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ internal class ChunkTest {
val x = chunk.x
val y = chunk.y
// Then
assertEquals(-10, x)
assertEquals(-50, y)
}

@Test
fun `Negative values safe`() {
// Given
val chunk = Chunk.createSafe(-10, -50)
// When
val x = chunk.x
val y = chunk.y
// Then
assertEquals(4086, x)
assertEquals(4046, y)
}
Expand All @@ -49,7 +61,7 @@ internal class ChunkTest {
@Test
fun `Overflow values`() {
// Given
val chunk = Chunk(4097, 4098)
val chunk = Chunk.createSafe(4097, 4098)
// When
val x = chunk.x
val y = chunk.y
Expand Down
14 changes: 13 additions & 1 deletion engine/src/test/kotlin/rs/dusk/engine/model/RegionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ internal class RegionTest {
val x = region.x
val y = region.y
// Then
assertEquals(-10, x)
assertEquals(-50, y)
}

@Test
fun `Negative values safe`() {
// Given
val region = Region.createSafe(-10, -50)
// When
val x = region.x
val y = region.y
// Then
assertEquals(246, x)
assertEquals(206, y)
}
Expand All @@ -50,7 +62,7 @@ internal class RegionTest {
@Test
fun `Overflow values`() {
// Given
val region = Region(257, 258)
val region = Region.createSafe(257, 258)
// When
val x = region.x
val y = region.y
Expand Down
16 changes: 15 additions & 1 deletion engine/src/test/kotlin/rs/dusk/engine/model/TileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ internal class TileTest {
val y = tile.y
val plane = tile.plane
// Then
assertEquals(-10, x)
assertEquals(-50, y)
assertEquals(-2, plane)
}

@Test
fun `Negative values safe`() {
// Given
val tile = Tile.createSafe(-10, -50, -2)
// When
val x = tile.x
val y = tile.y
val plane = tile.plane
// Then
assertEquals(16374, x)
assertEquals(16334, y)
assertEquals(2, plane)
Expand All @@ -56,7 +70,7 @@ internal class TileTest {
@Test
fun `Overflow values`() {
// Given
val tile = Tile(16384, 16385, 6)
val tile = Tile.createSafe(16384, 16385, 6)
// When
val x = tile.x
val y = tile.y
Expand Down

0 comments on commit cba00b9

Please sign in to comment.