summaryrefslogtreewikicommitdiff
path: root/core/src/game/Border.kt
blob: f535681f613f17a10d537bc7430ae67a475577cc (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cat.freya.khs.game

import cat.freya.khs.config.WorldBorderConfig
import cat.freya.khs.world.World

class Border(val game: Game) {

    enum class State {
        WAITING,
        WARNED,
        SHRINKING,
    }

    @Volatile var timer: ULong = 0UL

    @Volatile var state: State = State.WAITING

    @Volatile private var enabled: Boolean = false

    private val border: World.Border?
        get() = game.map?.gameWorld?.border

    private val borderConfig: WorldBorderConfig?
        get() = game.map?.config?.worldBorder

    val expired: Boolean
        get() = border?.size?.let { it <= 100.0 } != true

    fun reset() {
        enabled = false
        state = State.WAITING

        val border = border ?: return
        val borderConfig = borderConfig ?: return

        val x = borderConfig.pos?.x ?: return
        val z = borderConfig.pos?.z ?: return
        val size = borderConfig.size ?: return

        border.move(x, z, size, 0UL)
    }

    fun update() {
        if (borderConfig?.enabled != true) return

        if (timer != 0UL) {
            timer--
            return
        }

        if (state == State.WARNED) {
            // start the world border movement!
            var amount = borderConfig?.move ?: return
            val currentSize = border?.size?.toULong() ?: return

            if (amount >= currentSize) return

            if (amount - 100UL <= currentSize) amount = 100UL

            timer = 30UL
            state = State.SHRINKING

            border?.move(amount, timer)
            game.broadcast(game.plugin.locale.worldBorder.shrinking)
            return
        }

        if (state == State.SHRINKING) {
            timer = borderConfig?.delay ?: return
            state = State.WAITING
            return
        }

        game.broadcast(game.plugin.locale.prefix.border + game.plugin.locale.worldBorder.warn)
        timer = 30UL
        state = State.WARNED
    }
}