Archived
1
0
Fork 0

Refactor Location classes

This commit is contained in:
Moritz Ruth 2020-11-22 16:21:15 +01:00
parent 41344a8507
commit 27cf3c5a50
No known key found for this signature in database
GPG key ID: AFD57E23E753841B
6 changed files with 15 additions and 20 deletions

View file

@ -1,10 +1,10 @@
package space.blokk.net.event
import space.blokk.WorldAndLocationWithRotation
import space.blokk.event.Cancellable
import space.blokk.net.Session
import space.blokk.player.GameMode
import space.blokk.player.Player
import space.blokk.world.WorldAndLocationWithRotation
/**
* Emitted when a [Player] instance will be initialized.

View file

@ -1,10 +1,10 @@
package space.blokk.player
import kotlinx.coroutines.CoroutineScope
import space.blokk.LocationWithRotation
import space.blokk.event.EventTarget
import space.blokk.net.Session
import space.blokk.player.event.PlayerEvent
import space.blokk.world.LocationWithRotation
import space.blokk.world.World
import java.util.*

View file

@ -1,7 +1,5 @@
package space.blokk
package space.blokk.world
import space.blokk.world.VoxelLocation
import space.blokk.world.World
import kotlin.math.roundToInt
sealed class AbstractWorldAndLocation<T : AbstractLocation> {
@ -19,7 +17,7 @@ data class WorldAndLocationWithRotation(
override val location: LocationWithRotation
) : AbstractWorldAndLocation<LocationWithRotation>()
abstract class AbstractLocation {
sealed class AbstractLocation {
abstract val x: Double
abstract val y: Double
abstract val z: Double
@ -36,14 +34,6 @@ abstract class AbstractLocation {
*/
fun roundToBlock(): VoxelLocation = VoxelLocation(x.roundToInt(), y.roundToInt(), z.roundToInt())
/**
* @return A [WorldAndLocation] instance containing this location and [world].
*/
abstract infix fun inside(world: World): AbstractWorldAndLocation<*>
}
// TODO: Remove the data modifier and implement these function manually
data class Location(override val x: Double, override val y: Double, override val z: Double) : AbstractLocation() {
/**
* Returns a LocationWithRotation composed of this location, [yaw] and [pitch].
*/
@ -52,10 +42,18 @@ data class Location(override val x: Double, override val y: Double, override val
/**
* Returns a pair of [world] and this location.
*/
abstract infix fun inside(world: World): AbstractWorldAndLocation<*>
}
/**
* Represents a set of x, y and z coordinates.
*
* Whenever used as a type, [AbstractLocation] should be preferred so that [LocationWithRotation] is also a valid value.
*/
data class Location(override val x: Double, override val y: Double, override val z: Double) : AbstractLocation() {
override fun inside(world: World) = WorldAndLocation(world, this)
}
// TODO: Extend Location instead of AbstractLocation
data class LocationWithRotation(
override val x: Double,
override val y: Double,
@ -64,6 +62,4 @@ data class LocationWithRotation(
val pitch: Float
) : AbstractLocation() {
override fun inside(world: World) = WorldAndLocationWithRotation(world, this)
fun withoutRotation() = Location(x, y, z)
}

View file

@ -1,7 +1,6 @@
package space.blokk.world
import space.blokk.CoordinatePart
import space.blokk.Location
data class VoxelLocation(val x: Int, val y: Int, val z: Int) {
/**

View file

@ -1,7 +1,7 @@
package space.blokk.net.packet.play
import space.blokk.LocationWithRotation
import space.blokk.net.packet.OutgoingPacket
import space.blokk.world.LocationWithRotation
import kotlin.random.Random
/**

View file

@ -1,12 +1,12 @@
package space.blokk.player
import kotlinx.coroutines.Job
import space.blokk.LocationWithRotation
import space.blokk.event.EventBus
import space.blokk.logging.Logger
import space.blokk.net.Session
import space.blokk.player.event.PlayerEvent
import space.blokk.util.createUnconfinedSupervisorScope
import space.blokk.world.LocationWithRotation
import space.blokk.world.World
import java.util.*