Add new game states
This commit is contained in:
50
src/main/kotlin/club/mcscrims/speedhg/util/DirectionUtil.kt
Normal file
50
src/main/kotlin/club/mcscrims/speedhg/util/DirectionUtil.kt
Normal file
@@ -0,0 +1,50 @@
|
||||
package club.mcscrims.speedhg.util
|
||||
|
||||
import club.mcscrims.speedhg.SpeedHG
|
||||
import net.kyori.adventure.text.Component
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.util.Vector
|
||||
import kotlin.math.atan2
|
||||
|
||||
object DirectionUtil {
|
||||
|
||||
private val plugin = SpeedHG.instance
|
||||
private val directions = arrayOf("north", "northEast", "east", "southEast", "south", "southWest", "west", "northWest")
|
||||
|
||||
fun getDirectionToPlayer(
|
||||
player: Player,
|
||||
nearestPlayer: Player
|
||||
): Component
|
||||
{
|
||||
val yaw = getDirection( player, nearestPlayer )
|
||||
var normalizedYaw = yaw % 360
|
||||
|
||||
if ( normalizedYaw < 0 )
|
||||
normalizedYaw += 360
|
||||
|
||||
val index = (( normalizedYaw + 22.5 ) / 45 ).toInt() % 8
|
||||
return plugin.chatFormatter.format( "compass.directions.${directions[ index ]}" )
|
||||
}
|
||||
|
||||
private fun getDirection(
|
||||
fromPlayer: Player,
|
||||
toPlayer: Player
|
||||
): Double
|
||||
{
|
||||
val fromLocation = fromPlayer.location
|
||||
val toLocation = toPlayer.location
|
||||
|
||||
val directionVector = Vector( toLocation.x - fromLocation.x, 0.0, toLocation.z - fromLocation.z ).normalize()
|
||||
|
||||
val angle = atan2( directionVector.x, directionVector.z )
|
||||
val yaw = Math.toDegrees( angle )
|
||||
|
||||
var adjustedYaw = yaw + 180
|
||||
|
||||
if ( adjustedYaw < 0)
|
||||
adjustedYaw += 360
|
||||
|
||||
return adjustedYaw
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user