Commit 04f7d650 authored by Linus K.'s avatar Linus K.
Browse files

Add teleport animation

parent 6df64277
......@@ -3,10 +3,11 @@ package me.linuscde.mcplugins.teleportastic.utils
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.listeners.FightListener
import me.linuscde.mcplugins.teleportastic.listeners.FightListener.Companion.isInFight
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.*
import org.bukkit.entity.Player
import kotlin.math.abs
import kotlin.math.cos
import kotlin.math.sin
var Player.lastLocation: Location?
get() = Plugin.instance.config.getLocation("${this.uniqueId}.last_location")
......@@ -56,8 +57,8 @@ fun getDefaultTeleportFailHandler(player: Player): () -> Unit = {
data class TeleportingPlayer(val player: Player, val startLocation: Location,
val targetLocation: Location, var ticksPassed: Long,
val onSuccess: () -> Unit, val onFail: () -> Unit) {
val targetLocation: Location, var ticksPassed: Long,
val onSuccess: () -> Unit, val onFail: () -> Unit) {
init {
TELEPORTING_PLAYERS.add(this)
......@@ -66,11 +67,39 @@ data class TeleportingPlayer(val player: Player, val startLocation: Location,
companion object {
private val TELEPORTING_PLAYERS = arrayListOf<TeleportingPlayer>()
private fun displayOneWarpRing(location: Location) {
val radius = 0.65f
val particles = 10
for (i in 0 until particles) {
val angle: Double = 6.283185307179586 * i / particles
val x: Double = cos(angle) * radius
val z: Double = sin(angle) * radius
location.add(x, 0.0, z)
location.world!!.spawnParticle(Particle.FIREWORKS_SPARK, location, 0, .0, .0, .0)
location.subtract(x, 0.0, z)
}
}
private fun displayWarpEffect(location: Location, ringCount: Int = 12) {
for(step in 1..ringCount) {
val yOffset = 2.0 * (step * 1.0 / ringCount)
val radius = 0.65f
val particles = 10
for (i in 0 until particles) {
val angle: Double = 6.283185307179586 * i / particles
val x: Double = cos(angle) * radius
val z: Double = sin(angle) * radius
location.add(x, yOffset, z)
location.world!!.spawnParticle(Particle.FIREWORKS_SPARK, location, 0, .0, .0, .0)
location.subtract(x, yOffset, z)
}
}
}
fun initializeScheduler(plugin: org.bukkit.plugin.Plugin) {
val tickDelta = 2L
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, {
for(tp in TELEPORTING_PLAYERS.toTypedArray()) {
if(!tp.player.isOnline) {
for (tp in TELEPORTING_PLAYERS.toTypedArray()) {
if (!tp.player.isOnline) {
TELEPORTING_PLAYERS.remove(tp)
continue
}
......@@ -87,19 +116,29 @@ data class TeleportingPlayer(val player: Player, val startLocation: Location,
}
val lastDurationInSecs = tp.ticksPassed / 20
tp.ticksPassed += tickDelta
tp.ticksPassed += tickDelta // Add ticks
val currentDurationInSecs = tp.ticksPassed / 20
// Show seconds
val teleportTicks = tp.player.teleportDurationTicks
if((tp.ticksPassed == tickDelta || lastDurationInSecs != currentDurationInSecs) && tp.ticksPassed < teleportTicks) {
if ((tp.ticksPassed == tickDelta || lastDurationInSecs != currentDurationInSecs) && tp.ticksPassed < teleportTicks) {
val remainSecs = teleportTicks / 20 - currentDurationInSecs
tp.player.sendTitle("", "§6Teleporting in §e$remainSecs second${if(remainSecs != 1L) "s" else " "}", 0, 20, 0)
tp.player.sendTitle("", "§6Teleporting in §e$remainSecs second${if (remainSecs != 1L) "s" else " "}", 0, 20, 0)
}
if(teleportTicks > 0)
displayOneWarpRing(tp.startLocation.add(.0, 2.0 * (tp.ticksPassed * 1.0 / teleportTicks), .0))
// Teleport player
if(tp.ticksPassed >= teleportTicks) {
if (tp.ticksPassed >= teleportTicks) {
tp.player.sendTitle("", " ", 0, 1, 0)
if (tp.player.teleport(tp.targetLocation)) {
if(tp.startLocation.world != tp.targetLocation.world || tp.startLocation.distance(tp.targetLocation) > 15.0) {
if (teleportTicks == 0L)
displayWarpEffect(tp.startLocation)
displayWarpEffect(tp.targetLocation)
}
tp.onSuccess()
} else {
tp.onFail()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment