summaryrefslogtreewikicommitdiff
path: root/core/src/world/Position.kt
blob: daea3ab9f77f4b62bcce7bc852dead69438ba644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cat.freya.khs.world

import cat.freya.khs.config.LegacyPosition
import cat.freya.khs.player.Player
import kotlin.math.pow
import kotlin.math.sqrt

data class Position(var x: Double = 0.0, var y: Double = 0.0, var z: Double = 0.0) {

    /// Create a new position of self + offset
    fun move(offset: Position): Position {
        return Position(this.x + offset.x, this.y + offset.y, this.z + offset.z)
    }

    /// Translate self by offset
    fun moveSelf(offset: Position) {
        this.x += offset.x
        this.y += offset.y
        this.z += offset.z
    }

    /// Create a new position of self.x + offset
    fun moveX(offset: Double): Position {
        return this.move(Position(offset, 0.0, 0.0))
    }

    /// Create a new position of self.y + offset
    fun moveY(offset: Double): Position {
        return this.move(Position(0.0, offset, 0.0))
    }

    /// Create a new position of self.z + offset
    fun moveZ(offset: Double): Position {
        return this.move(Position(0.0, 0.0, offset))
    }

    fun distance(other: Position): Double {
        val dx = this.x - other.x
        val dy = this.y - other.y
        val dz = this.z - other.z
        val distanceSquared = dx.pow(2) + dy.pow(2) + dz.pow(2)
        return sqrt(distanceSquared)
    }

    fun teleport(player: Player) {
        player.teleport(this)
    }

    fun withWorld(worldName: String): Location {
        return Location(this.x, this.y, this.z, worldName)
    }

    fun toLegacy(): LegacyPosition = LegacyPosition(x, y, z, null)
}