我试图确定“面向玩家”的方向,例如北,南,西,东。我创建了此代码,但有时不起作用...
package me.Nightfighter001.GlobalSystem.WorldEditor;
import org.bukkit.entity.Player;
public enum Yaw {
NORTH, SOUTH, EAST, WEST;
public static Yaw getYaw(Player p) {
float yaw = p.getLocation().getYaw();
if (yaw > 135 || yaw < -135) {
return Yaw.NORTH;
} else if (yaw < -45) {
return Yaw.EAST;
} else if (yaw > 45) {
return Yaw.WEST;
} else {
return Yaw.SOUTH;
}
}
}
有谁可以帮助我吗?对不起,我的英语不好,谢谢:)
最佳答案
偏航值的范围是-360到360,因此对于大于135或小于-135的值,您的代码当前无法显示正确的基本方向。就您而言,据我所知,您可以将360度添加到任何负偏航值以缩短代码(减少检查)。然后,从0-45度和315至360度的范围是南,从45-135度是西,135-225度是北,而225-315度是东。代码看起来像这样,我测试了一段时间,它似乎与f3调试菜单/屏幕的值匹配:
public static CardinalDirection get(Player player) {
float yaw = player.getLocation().getYaw();
if (yaw < 0) {
yaw += 360;
}
if (yaw >= 315 || yaw < 45) {
return CardinalDirection.SOUTH;
} else if (yaw < 135) {
return CardinalDirection.WEST;
} else if (yaw < 225) {
return CardinalDirection.NORTH;
} else if (yaw < 315) {
return CardinalDirection.EAST;
}
return CardinalDirection.NORTH;
}